Description
This test ensures that $onECImplicitLoad/$offECImplicitLoad behaves correctly with Embedded Code Python Contributor: Clemens Westphal, February 2022
Small Model of Type : GAMS
Category : GAMS Test library
Main file : embpy12.gms
$title 'Test implicit/explicit loading in Embedded Code' (EMBPY12,SEQ=896)
$onText
This test ensures that $onECImplicitLoad/$offECImplicitLoad behaves correctly
with Embedded Code Python
Contributor: Clemens Westphal, February 2022
$offText
$log --- Using Python library %sysEnv.GMSPYTHONLIB%
set s1(*), s2(*);
* Load sets implicitly
$onECImplicitLoad
$onEmbeddedCode Python:
gams.set('s1', ['i1', 'i2', 'i3'])
gams.set('s2', ['j1', 'j2', 'j3'])
$offEmbeddedCode
$onEmbeddedCode Python:
if list(gams.get('s1')) != ['i1', 'i2', 'i3']:
raise Exception("Unexpected Data in symbol s1")
if list(gams.get('s2')) != ['j1', 'j2', 'j3']:
raise Exception("Unexpected Data in symbol s2")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Load sets explicitly
$clear s1 s2
$onMulti
$onEmbeddedCode Python:
gams.set('s1', ['i1', 'i3'])
gams.set('s2', ['j1', 'j3'])
$offEmbeddedCode s1 s2
$onEmbeddedCode Python:
if list(gams.get('s1')) != ['i1', 'i3']:
raise Exception("Unexpected Data in symbol s1")
if list(gams.get('s2')) != ['j1', 'j3']:
raise Exception("Unexpected Data in symbol s2")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
$offMulti
* Mix implicit and explicit loading
$clear s1 s2
$onMulti
$onEmbeddedCode Python:
gams.set('s1', ['i1', 'i2', 'i3'])
gams.set('s2', ['j1', 'j2', 'j3'])
$offEmbeddedCode s2
$onEmbeddedCode Python:
if list(gams.get('s1')) != ['i1', 'i2', 'i3']:
raise Exception("Unexpected Data in symbol s1")
if list(gams.get('s2')) != ['j1', 'j2', 'j3']:
raise Exception("Unexpected Data in symbol s2")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
$offMulti
* Load parameter implicitly
parameter p1(s1, s2);
$onEmbeddedCode Python:
s1 = gams.get('s1')
s2 = gams.get('s2')
p1 = [(i, j, 3.14) for i,j in zip(s1, s2) ]
gams.set('p1', p1)
$offEmbeddedCode
$onEmbeddedCode Python:
if list(gams.get('p1', keyFormat=KeyFormat.FLAT)) != p1:
raise Exception("Unexpected Data in symbol p1")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Load domain set implicitly
$onMultiR
$clear s1 s2
$onEmbeddedCode Python:
s1 = ['i1', 'i2', 'i3']
s2 = ['j1', 'j2', 'j3']
gams.set('s1', s1)
gams.set('s2', s2)
p1 = [(i, j, 3.14) for i,j in zip(s1, s2) ]
gams.set('p1', p1)
$offEmbeddedCode
$onEmbeddedCode Python:
if list(gams.get('s1')) != ['i1', 'i2', 'i3']:
raise Exception("Unexpected Data in symbol s1")
if list(gams.get('s2')) != ['j1', 'j2', 'j3']:
raise Exception("Unexpected Data in symbol s2")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
$offMulti
* Generate a domain violation with implicit loading and DomainCheckType.CHECKED
$clear p1
$onMulti
$onEmbeddedCode Python:
p1 = [('i4', 'j1', 3.14)]
gams.set('p1', p1, domCheck=DomainCheckType.CHECKED)
$offEmbeddedCode
$if errorfree $abort "Expected domain violation"
$clearError
$onEmbeddedCode Python:
if len(list(gams.get('p1'))):
raise Exception("Unexpected Data in symbol p1")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
$offMulti
* Generate a domain violation with implicit loading and $offFiltered
$clear p1
$onMulti
$offFiltered
$onEmbeddedCode Python:
p1 = [('i4', 'j1', 3.14)]
gams.set('p1', p1)
$offEmbeddedCode
$if errorfree $abort "Expected domain violation"
$clearError
$onFiltered
$onEmbeddedCode Python:
if len(list(gams.get('p1'))):
raise Exception("Unexpected Data in symbol p1")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
$offMulti
* Load a set implicitly by setting the alias
set s3(*);
alias(s3, s3_alias);
$onEmbeddedCode Python:
gams.set('s3_alias', ['i1', 'i2', 'i3'])
$offEmbeddedCode
$if not errorfree $abort "Problem with alias"
* Load implicitly using the projection operator
set s4;
parameter p2(s4);
$onEmbeddedCode Python:
gams.set('p2', [('i1', 3.14), ('i2', 3.14)])
$offEmbeddedCode s4<p2
$if not errorfree $abort "Problems with the projection operator and implicit load"
$onEmbeddedCode Python:
if list(gams.get('s4')) != ['i1', 'i2']:
raise Exception("Unexpected Data in symbol s4")
if list(gams.get('p2')) != [('i1', 3.14), ('i2', 3.14)]:
raise Exception("Unexpected Data in symbol p2")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Load implicitly from a set using the projection operator
set s5(s5);
$onEmbeddedCode Python:
gams.set('s5', ['i1', 'i2'])
$offEmbeddedCode s5<s5
$if not errorfree $abort "Problems loading implicitly from a set using the projection operator"
$onEmbeddedCode Python:
if list(gams.get('s5')) != ['i1', 'i2']:
raise Exception("Unexpected Data in symbol s5")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Load implicitly using the projection operator and fixed domain position
set s6;
parameter p3(s1, s6);
$onEmbeddedCode Python:
gams.set('p3', [('i1', 'j1', 3.14), ('i2', 'j2', 3.14)])
$offEmbeddedCode s6<=p3.dim2
$if not errorfree $abort "Problems with the projection operator and implicit load and fixed domain position"
$onEmbeddedCode Python:
if list(gams.get('s6')) != ['j1', 'j2']:
raise Exception("Unexpected Data in symbol s6")
if list(gams.get('p3', keyFormat = KeyFormat.FLAT)) != [('i1', 'j1', 3.14), ('i2', 'j2', 3.14)]:
raise Exception("Unexpected Data in symbol p3")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Generate a compilation error by loading a parameter that has not been set
parameter p4(s1);
$onEmbeddedCode Python:
pass
$offEmbeddedCode p4
$if errorfree $abort "Expected an error when loading a symbol that has not been set with $onECImplicitLoad"
$clearError
$offECImplicitLoad
* Load explicitly using the projection operator
set s7;
parameter p5(s7);
$onEmbeddedCode Python:
gams.set('p5', [('i1', 3.14), ('i2', 3.14)])
$offEmbeddedCode s7<p5
$if not errorfree $abort "Problems with the projection operator and explicit load"
$onEmbeddedCode Python:
if list(gams.get('s7')) != ['i1', 'i2']:
raise Exception("Unexpected Data in symbol s7")
if len(list(gams.get('p5'))):
raise Exception("Unexpected Data in symbol p5")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Load explicitly from a set using the projection operator
set s8(s8);
$onEmbeddedCode Python:
gams.set('s8', ['i1', 'i2'])
$offEmbeddedCode s8<s8
$if not errorfree $abort "Problems loading explicitly from a set using the projection operator"
$onEmbeddedCode Python:
if list(gams.get('s8')) != ['i1', 'i2']:
raise Exception("Unexpected Data in symbol s8")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Load explicitly using the projection operator and fixed domain position
set s9;
parameter p6(s1,s9);
$onEmbeddedCode Python:
gams.set('p6', [('i1', 'j1', 3.14), ('i2', 'j2', 3.14)])
$offEmbeddedCode s9<=p6.dim2
$if not errorfree $abort "Problems with the projection operator and explicit load and fixed domain position"
$onEmbeddedCode Python:
if list(gams.get('s9')) != ['j1', 'j2']:
raise Exception("Unexpected Data in symbol s9")
if len(list(gams.get('p6'))):
raise Exception("Unexpected Data in symbol p6")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Generate a compilation error by loading a parameter that has not been set
$onEmbeddedCode Python:
pass
$offEmbeddedCode p4
$if errorfree $abort "Expected an error when loading a symbol that has not been set with $offECImplicitLoad"
$clearError
$onECImplicitLoad
* Infer dim=0 from data (parameter)
parameter p_dim0_1;
parameter p_dim0_2;
parameter p_dim0_3;
$onEmbeddedCode Python:
gams.set("p_dim0_1", [])
gams.set("p_dim0_2", [3.14])
gams.set("p_dim0_3", [(3.14,)])
$offEmbeddedCode
$onEmbeddedCode Python:
expected = [0.0]
data = list(gams.get("p_dim0_1"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim0_1")
expected = [3.14]
data = list(gams.get("p_dim0_2"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim0_2")
expected = [3.14]
data = list(gams.get("p_dim0_3"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim0_3")
$offEmbeddedCode
* Infer dim=1 from data (parameter)
parameter p_dim1_1;
parameter p_dim1_2;
parameter p_dim1_3;
parameter p_dim1_4;
parameter p_dim1_int_1;
parameter p_dim1_int_2;
parameter p_dim1_int_3;
parameter p_dim1_int_4;
$onEmbeddedCode Python:
gams.set("p_dim1_1", [("i1", 1), ("i2", 2)])
gams.set("p_dim1_2", [(("i1",), 1), (("i2",), 2)])
gams.set("p_dim1_3", [("i1", (1,)), ("i2", (2,))])
gams.set("p_dim1_4", [(("i1",), (1,)), (("i2",), (2,))])
gams.set("p_dim1_int_1", [(1, 1), (2, 2)])
gams.set("p_dim1_int_2", [((1,), 1), ((2,), 2)])
gams.set("p_dim1_int_3", [(1, (1,)), (2, (2,))])
gams.set("p_dim1_int_4", [((1,), (1,)), ((2,), (2,))])
$offEmbeddedCode
$onEmbeddedCode Python:
expected = [('i1', 1.0), ('i2', 2.0)]
data = list(gams.get("p_dim1_1"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim1_1")
expected = [('i1', 1.0), ('i2', 2.0)]
data = list(gams.get("p_dim1_2"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim1_2")
expected = [('i1', 1.0), ('i2', 2.0)]
data = list(gams.get("p_dim1_3"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim1_3")
expected = [('i1', 1.0), ('i2', 2.0)]
data = list(gams.get("p_dim1_4"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim1_4")
expected = [('i1', 1.0), ('i2', 2.0)]
data = list(gams.get("p_dim1_int_1"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim1_int_1")
expected = [('i1', 1.0), ('i2', 2.0)]
data = list(gams.get("p_dim1_int_2"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim1_int_2")
expected = [('i1', 1.0), ('i2', 2.0)]
data = list(gams.get("p_dim1_int_3"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim1_int_3")
expected = [('i1', 1.0), ('i2', 2.0)]
data = list(gams.get("p_dim1_int_4"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim1_int_4")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Infer dim=2 from data (parameter)
parameter p_dim2_1;
parameter p_dim2_2;
parameter p_dim2_3;
parameter p_dim2_4;
parameter p_dim2_int_1;
parameter p_dim2_int_2;
parameter p_dim2_int_3;
parameter p_dim2_int_4;
$onEmbeddedCode Python:
gams.set("p_dim2_1", [("i1", "j1", 1), ("i2", "j2", 2)])
gams.set("p_dim2_2", [(("i1", "j1"), 1), (("i2", "j2"), 2)])
gams.set("p_dim2_3", [("i1", "j1", (1,)), ("i2", "j2", (2,))])
gams.set("p_dim2_4", [(("i1", "j1"), (1,)), (("i2", "j2"), (2,))])
gams.set("p_dim2_int_1", [(1, 4, 1), (2, 5, 2)])
gams.set("p_dim2_int_2", [((1, 4), 1), ((2, 5), 2)])
gams.set("p_dim2_int_3", [(1, 4, (1,)), (2, 5, (2,))])
gams.set("p_dim2_int_4", [((1, 4), (1,)), ((2, 5), (2,))])
$offEmbeddedCode
$onEmbeddedCode Python:
expected = [(('i1', 'j1'), 1.0), (('i2', 'j2'), 2.0)]
data = list(gams.get("p_dim2_1"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim2_1")
expected = [(('i1', 'j1'), 1.0), (('i2', 'j2'), 2.0)]
data = list(gams.get("p_dim2_2"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim2_2")
expected = [(('i1', 'j1'), 1.0), (('i2', 'j2'), 2.0)]
data = list(gams.get("p_dim2_3"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim2_3")
expected = [(('i1', 'j1'), 1.0), (('i2', 'j2'), 2.0)]
data = list(gams.get("p_dim2_4"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim2_4")
expected = [(('i1', 'j1'), 1.0), (('i2', 'j2'), 2.0)]
data = list(gams.get("p_dim2_int_1"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim2_int_1")
expected = [(('i1', 'j1'), 1.0), (('i2', 'j2'), 2.0)]
data = list(gams.get("p_dim2_int_2"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim2_int_2")
expected = [(('i1', 'j1'), 1.0), (('i2', 'j2'), 2.0)]
data = list(gams.get("p_dim2_int_3"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim2_int_3")
expected = [(('i1', 'j1'), 1.0), (('i2', 'j2'), 2.0)]
data = list(gams.get("p_dim2_int_4"))
if data != expected:
raise Exception("Unexpected Data in symbol p_dim2_int_4")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Infer dim=1 from data (set)
set s_dim1_1;
set s_dim1_2;
set s_dim1_3;
set s_dim1_4;
set s_dim1_5;
set s_dim1_6;
set s_dim1_int_2;
set s_dim1_int_3;
set s_dim1_int_4;
set s_dim1_int_5;
set s_dim1_int_6;
$onEmbeddedCode Python:
gams.set("s_dim1_1", [])
gams.set("s_dim1_2", ["i1", "i2"])
gams.set("s_dim1_3", [("i1",), ("i2",)])
gams.set("s_dim1_4", [(("i1",), "text 1"), (("i2",), "text 2")])
gams.set("s_dim1_5", [(("i1",), ("text 1",)), (("i2",), ("text 2",))])
gams.set("s_dim1_6", [("i1", ("text 1",)), ("i2", ("text 2",))])
gams.set("s_dim1_int_2", [1, 2])
gams.set("s_dim1_int_3", [(1,), (2,)])
gams.set("s_dim1_int_4", [((1,), "text 1"), ((2,), "text 2")])
gams.set("s_dim1_int_5", [((1,), ("text 1",)), ((2,), ("text 2",))])
gams.set("s_dim1_int_6", [(1, ("text 1",)), (2, ("text 2",))])
$offEmbeddedCode
$onEmbeddedCode Python:
expected = []
data = list(gams.get("s_dim1_1", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim1_1")
expected = [('i1', ''), ('i2', '')]
data = list(gams.get("s_dim1_2", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim1_2")
expected = [('i1', ''), ('i2', '')]
data = list(gams.get("s_dim1_3", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim1_3")
expected = [('i1', 'text 1'), ('i2', 'text 2')]
data = list(gams.get("s_dim1_4", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim1_4")
expected = [('i1', 'text 1'), ('i2', 'text 2')]
data = list(gams.get("s_dim1_5", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim1_5")
expected = [('i1', 'text 1'), ('i2', 'text 2')]
data = list(gams.get("s_dim1_6", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim1_6")
expected = [('i1', ''), ('i2', '')]
data = list(gams.get("s_dim1_int_2", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim1_int_2")
expected = [('i1', ''), ('i2', '')]
data = list(gams.get("s_dim1_int_3", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim1_int_3")
expected = [('i1', 'text 1'), ('i2', 'text 2')]
data = list(gams.get("s_dim1_int_4", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim1_int_4")
expected = [('i1', 'text 1'), ('i2', 'text 2')]
data = list(gams.get("s_dim1_int_5", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim1_int_5")
expected = [('i1', 'text 1'), ('i2', 'text 2')]
data = list(gams.get("s_dim1_int_6", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim1_int_6")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Infer dim=2 from data (set)
set s_dim2_1;
set s_dim2_2;
set s_dim2_3;
set s_dim2_4;
set s_dim2_int_1;
set s_dim2_int_2;
set s_dim2_int_3;
set s_dim2_int_4;
$onEmbeddedCode Python:
gams.set("s_dim2_1", [("i1", "j1"), ("i2", "j2")])
gams.set("s_dim2_2", [(("i1", "j1"), "text 1"), (("i2", "j2"), "text 2")])
gams.set("s_dim2_3", [(("i1", "j1"), ("text 1",)), (("i2", "j2"), ("text 2",))])
gams.set("s_dim2_4", [("i1", "j1", ("text 1",)), ("i2", "j2", ("text 2",))])
gams.set("s_dim2_int_1", [(1, 4), (2, 5)])
gams.set("s_dim2_int_2", [((1, 4), "text 1"), ((2, 5), "text 2")])
gams.set("s_dim2_int_3", [((1, 4), ("text 1",)), ((2, 5), ("text 2",))])
gams.set("s_dim2_int_4", [(1, 4, ("text 1",)), (2, 5, ("text 2",))])
$offEmbeddedCode
$onEmbeddedCode Python:
expected = [(('i1', 'j1'), ''), (('i2', 'j2'), '')]
data = list(gams.get("s_dim2_1", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim2_1")
expected = [(('i1', 'j1'), 'text 1'), (('i2', 'j2'), 'text 2')]
data = list(gams.get("s_dim2_2", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim2_2")
expected = [(('i1', 'j1'), 'text 1'), (('i2', 'j2'), 'text 2')]
data = list(gams.get("s_dim2_3", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim2_3")
expected = [(('i1', 'j1'), 'text 1'), (('i2', 'j2'), 'text 2')]
data = list(gams.get("s_dim2_4", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim2_4")
expected = [(('i1', 'j1'), ''), (('i2', 'j2'), '')]
data = list(gams.get("s_dim2_int_1", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim2_int_1")
expected = [(('i1', 'j1'), 'text 1'), (('i2', 'j2'), 'text 2')]
data = list(gams.get("s_dim2_int_2", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim2_int_2")
expected = [(('i1', 'j1'), 'text 1'), (('i2', 'j2'), 'text 2')]
data = list(gams.get("s_dim2_int_3", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim2_int_3")
expected = [(('i1', 'j1'), 'text 1'), (('i2', 'j2'), 'text 2')]
data = list(gams.get("s_dim2_int_4", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_dim2_int_4")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Infer dim=0 from data (variable/equation)
variable v_dim0_1;
variable v_dim0_2;
variable v_dim0_3;
equation e_dim0_1;
equation e_dim0_2;
equation e_dim0_3;
$onEmbeddedCode Python:
gams.set("v_dim0_1", [])
gams.set("v_dim0_2", [(3.14, 0, 0, 10, 1)])
gams.set("v_dim0_3", [((3.14, 0, 0, 10, 2))])
gams.set("e_dim0_1", [])
gams.set("e_dim0_2", [(3.14, 0, 0, 10, 1)])
gams.set("e_dim0_3", [((3.14, 0, 0, 10, 2))])
$offEmbeddedCode
$onEmbeddedCode Python:
expected = [(0.0, 0.0, 0.0, 0.0, 1.0)]
data = list(gams.get("v_dim0_1"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim0_1")
expected = [(3.14, 0.0, 0.0, 10.0, 1.0)]
data = list(gams.get("v_dim0_2"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim0_2")
expected = [(3.14, 0.0, 0.0, 10.0, 2.0)]
data = list(gams.get("v_dim0_3"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim0_3")
expected = [(0.0, 0.0, 0.0, 0.0, 1.0)]
data = list(gams.get("e_dim0_1"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim0_1")
expected = [(3.14, 0.0, 0.0, 10.0, 1.0)]
data = list(gams.get("e_dim0_2"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim0_2")
expected = [(3.14, 0.0, 0.0, 10.0, 2.0)]
data = list(gams.get("e_dim0_3"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim0_3")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Infer dim=1 from data (variable/equation)
variable v_dim1_1;
variable v_dim1_2;
variable v_dim1_3;
variable v_dim1_4;
equation e_dim1_1;
equation e_dim1_2;
equation e_dim1_3;
equation e_dim1_4;
variable v_dim1_int_1;
variable v_dim1_int_2;
variable v_dim1_int_3;
variable v_dim1_int_4;
equation e_dim1_int_1;
equation e_dim1_int_2;
equation e_dim1_int_3;
equation e_dim1_int_4;
$onEmbeddedCode Python:
gams.set("v_dim1_1", [("i1", 3.14, 0.0, 0.0, 10.0, 1.0), ("i2", 3.14, 0.0, 0.0, 10.0, 1.0)])
gams.set("e_dim1_1", [("i1", 3.14, 0.0, 0.0, 10.0, 1.0), ("i2", 3.14, 0.0, 0.0, 10.0, 1.0)])
gams.set("v_dim1_int_1", [(1, 3.14, 0.0, 0.0, 10.0, 1.0), (2, 3.14, 0.0, 0.0, 10.0, 1.0)])
gams.set("e_dim1_int_1", [(1, 3.14, 0.0, 0.0, 10.0, 1.0), (2, 3.14, 0.0, 0.0, 10.0, 1.0)])
gams.set("v_dim1_2", [(("i1",), 3.14, 0.0, 0.0, 10.0, 2.0), (("i2",), 3.14, 0.0, 0.0, 10.0, 2.0)])
gams.set("e_dim1_2", [(("i1",), 3.14, 0.0, 0.0, 10.0, 2.0), (("i2",), 3.14, 0.0, 0.0, 10.0, 2.0)])
gams.set("v_dim1_int_2", [((1,), 3.14, 0.0, 0.0, 10.0, 2.0), ((2,), 3.14, 0.0, 0.0, 10.0, 2.0)])
gams.set("e_dim1_int_2", [((1,), 3.14, 0.0, 0.0, 10.0, 2.0), ((2,), 3.14, 0.0, 0.0, 10.0, 2.0)])
gams.set("v_dim1_3", [("i1", (3.14, 0.0, 0.0, 10.0, 3.0)), ("i2", (3.14, 0.0, 0.0, 10.0, 3.0))])
gams.set("e_dim1_3", [("i1", (3.14, 0.0, 0.0, 10.0, 3.0)), ("i2", (3.14, 0.0, 0.0, 10.0, 3.0))])
gams.set("v_dim1_int_3", [(1, (3.14, 0.0, 0.0, 10.0, 3.0)), (2, (3.14, 0.0, 0.0, 10.0, 3.0))])
gams.set("e_dim1_int_3", [(1, (3.14, 0.0, 0.0, 10.0, 3.0)), (2, (3.14, 0.0, 0.0, 10.0, 3.0))])
gams.set("v_dim1_4", [(("i1",), (3.14, 0.0, 0.0, 10.0, 4.0)), (("i2",), (3.14, 0.0, 0.0, 10.0, 4.0))])
gams.set("e_dim1_4", [(("i1",), (3.14, 0.0, 0.0, 10.0, 4.0)), (("i2",), (3.14, 0.0, 0.0, 10.0, 4.0))])
gams.set("v_dim1_int_4", [((1,), (3.14, 0.0, 0.0, 10.0, 4.0)), ((2,), (3.14, 0.0, 0.0, 10.0, 4.0))])
gams.set("e_dim1_int_4", [((1,), (3.14, 0.0, 0.0, 10.0, 4.0)), ((2,), (3.14, 0.0, 0.0, 10.0, 4.0))])
$offEmbeddedCode
$onEmbeddedCode Python:
expected = [('i1', (3.14, 0.0, 0.0, 10.0, 1.0)), ('i2', (3.14, 0.0, 0.0, 10.0, 1.0))]
data = list(gams.get("v_dim1_1"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim1_1")
data = list(gams.get("e_dim1_1"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim1_1")
data = list(gams.get("v_dim1_int_1"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim1_int_1")
data = list(gams.get("e_dim1_int_1"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim1_int_1")
expected = [('i1', (3.14, 0.0, 0.0, 10.0, 2.0)), ('i2', (3.14, 0.0, 0.0, 10.0, 2.0))]
data = list(gams.get("v_dim1_2"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim1_2")
data = list(gams.get("e_dim1_2"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim1_2")
data = list(gams.get("v_dim1_int_2"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim1_int_2")
data = list(gams.get("e_dim1_int_2"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim1_int_2")
expected = [('i1', (3.14, 0.0, 0.0, 10.0, 3.0)), ('i2', (3.14, 0.0, 0.0, 10.0, 3.0))]
data = list(gams.get("v_dim1_3"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim1_3")
data = list(gams.get("e_dim1_3"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim1_3")
data = list(gams.get("v_dim1_int_3"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim1_int_3")
data = list(gams.get("e_dim1_int_3"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim1_int_3")
expected = [('i1', (3.14, 0.0, 0.0, 10.0, 4.0)), ('i2', (3.14, 0.0, 0.0, 10.0, 4.0))]
data = list(gams.get("v_dim1_4"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim1_4")
data = list(gams.get("e_dim1_4"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim1_4")
data = list(gams.get("v_dim1_int_4"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim1_int_4")
data = list(gams.get("e_dim1_int_4"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim1_int_4")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Infer dim=2 from data (variable/equation)
variable v_dim2_1;
variable v_dim2_2;
variable v_dim2_3;
variable v_dim2_4;
equation e_dim2_1;
equation e_dim2_2;
equation e_dim2_3;
equation e_dim2_4;
variable v_dim2_int_1;
variable v_dim2_int_2;
variable v_dim2_int_3;
variable v_dim2_int_4;
equation e_dim2_int_1;
equation e_dim2_int_2;
equation e_dim2_int_3;
equation e_dim2_int_4;
$onEmbeddedCode Python:
gams.set("v_dim2_1", [("i1", "j1", 3.14, 0.0, 0.0, 10.0, 1.0), ("i2", "j2", 3.14, 0.0, 0.0, 10.0, 1.0)])
gams.set("e_dim2_1", [("i1", "j1", 3.14, 0.0, 0.0, 10.0, 1.0), ("i2", "j2", 3.14, 0.0, 0.0, 10.0, 1.0)])
gams.set("v_dim2_int_1", [(1, 4, 3.14, 0.0, 0.0, 10.0, 1.0), (2, 5, 3.14, 0.0, 0.0, 10.0, 1.0)])
gams.set("e_dim2_int_1", [(1, 4, 3.14, 0.0, 0.0, 10.0, 1.0), (2, 5, 3.14, 0.0, 0.0, 10.0, 1.0)])
gams.set("v_dim2_2", [(("i1", "j1"), 4.14, 0.0, 0.0, 10.0, 1.0), (("i2", "j2"), 4.14, 0.0, 0.0, 10.0, 1.0)])
gams.set("e_dim2_2", [(("i1", "j1"), 4.14, 0.0, 0.0, 10.0, 1.0), (("i2", "j2"), 4.14, 0.0, 0.0, 10.0, 1.0)])
gams.set("v_dim2_int_2", [((1, 4), 4.14, 0.0, 0.0, 10.0, 1.0), ((2, 5), 4.14, 0.0, 0.0, 10.0, 1.0)])
gams.set("e_dim2_int_2", [((1, 4), 4.14, 0.0, 0.0, 10.0, 1.0), ((2, 5), 4.14, 0.0, 0.0, 10.0, 1.0)])
gams.set("v_dim2_3", [("i1", "j1", (5.14, 0.0, 0.0, 10.0, 1.0)), ("i2", "j2", (5.14, 0.0, 0.0, 10.0, 1.0))])
gams.set("e_dim2_3", [("i1", "j1", (5.14, 0.0, 0.0, 10.0, 1.0)), ("i2", "j2", (5.14, 0.0, 0.0, 10.0, 1.0))])
gams.set("v_dim2_int_3", [(1, 4, (5.14, 0.0, 0.0, 10.0, 1.0)), (2, 5, (5.14, 0.0, 0.0, 10.0, 1.0))])
gams.set("e_dim2_int_3", [(1, 4, (5.14, 0.0, 0.0, 10.0, 1.0)), (2, 5, (5.14, 0.0, 0.0, 10.0, 1.0))])
gams.set("v_dim2_4", [(("i1", "j1"), (6.14, 0.0, 0.0, 10.0, 1.0)), (("i2", "j2"), (6.14, 0.0, 0.0, 10.0, 1.0))])
gams.set("e_dim2_4", [(("i1", "j1"), (6.14, 0.0, 0.0, 10.0, 1.0)), (("i2", "j2"), (6.14, 0.0, 0.0, 10.0, 1.0))])
gams.set("v_dim2_int_4", [((1, 4), (6.14, 0.0, 0.0, 10.0, 1.0)), ((2, 5), (6.14, 0.0, 0.0, 10.0, 1.0))])
gams.set("e_dim2_int_4", [((1, 4), (6.14, 0.0, 0.0, 10.0, 1.0)), ((2, 5), (6.14, 0.0, 0.0, 10.0, 1.0))])
$offEmbeddedCode
$onEmbeddedCode Python:
expected = [(('i1', 'j1'), (3.14, 0.0, 0.0, 10.0, 1.0)), (('i2', 'j2'), (3.14, 0.0, 0.0, 10.0, 1.0))]
data = list(gams.get("v_dim2_1"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim2_1")
data = list(gams.get("e_dim2_1"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim2_1")
data = list(gams.get("v_dim2_int_1"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim2_int_1")
data = list(gams.get("e_dim2_int_1"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim2_int_1")
expected = [(('i1', 'j1'), (4.14, 0.0, 0.0, 10.0, 1.0)), (('i2', 'j2'), (4.14, 0.0, 0.0, 10.0, 1.0))]
data = list(gams.get("v_dim2_2"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim2_2")
data = list(gams.get("e_dim2_2"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim2_2")
data = list(gams.get("v_dim2_int_2"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim2_int_2")
data = list(gams.get("e_dim2_int_2"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim2_int_2")
expected = [(('i1', 'j1'), (5.14, 0.0, 0.0, 10.0, 1.0)), (('i2', 'j2'), (5.14, 0.0, 0.0, 10.0, 1.0))]
data = list(gams.get("v_dim2_3"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim2_3")
data = list(gams.get("e_dim2_3"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim2_3")
data = list(gams.get("v_dim2_int_3"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim2_int_3")
data = list(gams.get("e_dim2_int_3"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim2_int_3")
expected = [(('i1', 'j1'), (6.14, 0.0, 0.0, 10.0, 1.0)), (('i2', 'j2'), (6.14, 0.0, 0.0, 10.0, 1.0))]
data = list(gams.get("v_dim2_4"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim2_4")
data = list(gams.get("e_dim2_4"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim2_4")
data = list(gams.get("v_dim2_int_4"))
if data != expected:
raise Exception("Unexpected Data in symbol v_dim2_int_4")
data = list(gams.get("e_dim2_int_4"))
if data != expected:
raise Exception("Unexpected Data in symbol e_dim2_int_4")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Infer dimension from _GamsSymbol
set s_read(*,*) / i1.j1 "text 1", i1.j2 "text 2" /;
set s_write;
parameter p_read(*) / i1 1.1, i2 2.2 /;
parameter p_write(*);
$onEmbeddedCode Python:
data_from = gams.db['s_read']
expected = list(gams.get("s_read", valueFormat=ValueFormat.FLAT))
gams.set("s_write", data_from)
data = list(gams.get("s_write", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_write")
data_from = gams.db['p_read']
expected = list(gams.get("p_read", valueFormat=ValueFormat.FLAT))
gams.set("p_write", data_from)
data = list(gams.get("p_write", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol p_write")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Explicitly set dimension
parameter p_exp_1;
parameter p_exp_2;
set s_exp_1;
set s_exp_2;
positive variable v_exp_1;
variable v_exp_2;
equation e_exp_1;
equation e_exp_2;
$onEmbeddedCode Python:
gams.set("p_exp_1", [("i1", "j1", 1.0), ("i1", "j2", 2.0)], dimension=2)
gams.set("p_exp_2", [("i1", "j1", "k1", 1.0), ("i1", "j1", "k2", 2.0)], dimension=3)
gams.set("s_exp_1", [("i1", "text 1"), ("i2", "text 2")], dimension=1)
gams.set("s_exp_2", [("i1", "text 1"), ("i2", "text 2")], dimension=2)
gams.set("v_exp_1", [], dimension=0)
gams.set(
"v_exp_2",
[
("i1", "j1", "k1", 0.1, 0.2, 0.3, 0.4, 0.5),
("i1", "j1", "k2", 0.2, 0.3, 0.4, 0.5, 0.6),
],
dimension=3
)
gams.set("e_exp_1", [(0.1, 0.2, 0.3, 0.4, 0.5)], dimension=0)
gams.set(
"e_exp_2",
[
("i1", "j1", 0.1, 0.2, 0.3, 0.4, 0.5),
("i1", "j2", 0.2, 0.3, 0.4, 0.5, 0.6),
],
dimension=2
)
$offEmbeddedCode
$onEmbeddedCode Python:
expected = [(('i1', 'j1'), 1.0), (('i1', 'j2'), 2.0)]
data = list(gams.get("p_exp_1"))
if data != expected:
raise Exception("Unexpected Data in symbol p_exp_1")
expected = [(("i1", "j1", "k1"), 1), (("i1", "j1", "k2"), 2)]
data = list(gams.get("p_exp_2"))
if data != expected:
raise Exception("Unexpected Data in symbol p_exp_2")
expected = [("i1", "text 1"), ("i2", "text 2")]
data = list(gams.get("s_exp_1", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_exp_1")
expected = [(("i1", "text 1"), ""), (("i2", "text 2"), "")]
data = list(gams.get("s_exp_2", valueFormat=ValueFormat.FLAT))
if data != expected:
raise Exception("Unexpected Data in symbol s_exp_2")
expected = [(0, 0, 0, float("inf"), 1)]
data = list(gams.get("v_exp_1"))
if data != expected:
raise Exception("Unexpected Data in symbol v_exp_1")
expected = [
(("i1", "j1", "k1"), (0.1, 0.2, 0.3, 0.4, 0.5)),
(("i1", "j1", "k2"), (0.2, 0.3, 0.4, 0.5, 0.6)),
]
data = list(gams.get("v_exp_2"))
if data != expected:
raise Exception("Unexpected Data in symbol v_exp_2")
expected = [(0.1, 0.2, 0.3, 0.4, 0.5)]
data = list(gams.get("e_exp_1"))
if data != expected:
raise Exception("Unexpected Data in symbol e_exp_1")
expected = [
(("i1", "j1"), (0.1, 0.2, 0.3, 0.4, 0.5)),
(("i1", "j2"), (0.2, 0.3, 0.4, 0.5, 0.6)),
]
data = list(gams.get("e_exp_2"))
if data != expected:
raise Exception("Unexpected Data in symbol e_exp_2")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Test setting symbol with unknown dimension and existing expl. text
parameter p_text text from GAMS;
$onEmbeddedCode Python:
p_text = gams.db.add_parameter("p_text", 0, "text from EC Python")
p_text.add_record().value = 1
gams.set("p_text", p_text)
$offEmbeddedCode
$onEmbeddedCode Python:
text = gams.db["p_text"].text
if text != "text from GAMS":
raise Exception("Unexpected expl. text for p_text")
$offEmbeddedCode
$if not errorfree $abort "Unexpected data"
* Invalid dimension parameter (1)
parameter p_inv_1;
$onEmbeddedCode Python:
gams.set("p_inv_1", [("i1", "j1", 1.0), ("i1", "j2", 2.0)], dimension=-1)
$offEmbeddedCode
$if errorfree $abort "Expected error for invalid dimension parameter"
$clearError
* Invalid dimension parameter (2)
set s_inv_1;
$onEmbeddedCode Python:
gams.set("s_inv_1", [("i1", "j1"), ("i1", "j2")], dimension=21)
$offEmbeddedCode
$if errorfree $abort "Expected error for invalid dimension parameter"
$clearError
* Incompatible symbol type
parameter p_incomp;
$onEmbeddedCode Python:
from gams.control import *
p_incomp = gams.db.add_variable('p_incomp', 0, VarType.Binary)
p_incomp.add_record().upper = 1
$offEmbeddedCode
$if errorfree $abort "Expected error for incompatible symbol type"
$clearError