embpy02.gms : Test for embedded code facility

Description

Test functionality of the GAMS embedded code facility for Python during execution time

Contributor: Clemens Westphal, July 2017


Small Model of Type : GAMS


Category : GAMS Test library


Main file : embpy02.gms

$title 'Test for embedded code facility' (EMBPY02,SEQ=736)

$ontext
Test functionality of the GAMS embedded code facility for Python during execution time

Contributor: Clemens Westphal, July 2017
$offtext


* On the major platforms, GMSPYTHONHOME gets set automatically, otherwise the user has to set it
* This condition can also be removed, if one has set up its Python environment appropriately
$escape &
$if "%sysenv.GMSPYTHONHOME%"=="%&sysenv.GMSPYTHONHOME%&" $abort.noError Embedded code Python not ready to be used


Set i       / i1 text for i1
              i2 text for i2 /
    iout(i)
    j       / j1*j3 /
    ij(i,j) / #i.#j /
    i_want(i) / i1, i2 /
    i_want_ex(i) / i1 text 1, i2 text 2 /
    ij_want(i,j) / i1.j1, i1.j2 /
    ij_want_merge(i,j) / i1.j1, i1.j2, i1.j3 /
    ij_want_ex(i,j) / i1.j1 text 1, i1.j2 text 2/;

Scalar p0 /3.14/
       p0_want / 3.14 /;

Parameter p1(i)   / #i 3.14 /
          p2(i,j) / #i.#j 3.14 /
          p1_want(i) / #i 3.14 /
          p2_want(i,j) / i1.j1 3.14, i1.j2 3.14 /
          p2_want_merge(i,j) / i1.j1 3.14, i1.j2 3.14, i1.j3 3.14 /;

Variable v0      / fx 3.14 /
         v1(i)   / #i.fx 3.14 /
         v2(i,j) / #i.#j.fx 3.14 /
         v0_want / l 3.14, m 0, lo 0, up 10, scale 1 /
         v1_want(i) / i1.(l 3.14, m 0, lo 0, up 10, scale 1),
                      i2.(l 3.14, m 0, lo 0, up 10, scale 1) /
         v2_want(i,j) / i1.j1.(l 3.14, m 0, lo 0, up 10, scale 1),
                        i1.j2.(l 3.14, m 0, lo 0, up 10, scale 1) /
         v2_want_merge(i,j) / i1.j1.(l 3.14, m 0, lo 0, up 10, scale 1),
                              i1.j2.(l 3.14, m 0, lo 0, up 10, scale 1), 
                              i1.j3.(l 3.14, m 0, lo 0, up 10, scale 1) /;

equation e0      / fx 3.14 /
         e1(i)   / #i.fx 3.14 /
         e2(i,j) / #i.#j.fx 3.14 /
         e0_want / l 3.14, m 0, lo 0, up 10, scale 1 /
         e1_want(i) / i1.(l 3.14, m 0, lo 0, up 10, scale 1),
                      i2.(l 3.14, m 0, lo 0, up 10, scale 1) /
         e2_want(i,j) / i1.j1.(l 3.14, m 0, lo 0, up 10, scale 1),
                        i1.j2.(l 3.14, m 0, lo 0, up 10, scale 1) /
         e2_want_merge(i,j) / i1.j1.(l 3.14, m 0, lo 0, up 10, scale 1),
                              i1.j2.(l 3.14, m 0, lo 0, up 10, scale 1), 
                              i1.j3.(l 3.14, m 0, lo 0, up 10, scale 1) /;

$call rm -f embpy02_log.txt
$onechoV > checkData
execute_unload 'have.gdx', %1;
execute 'gdxdiff wanted.gdx have.gdx >> embpy02_log.txt';
abort$errorlevel '%2';
$offecho


*** Test default list behavior ***

*Test 1.1: Test default list behavior of scalars
embeddedCode Python:
  p0 = list(gams.get('p0'))
  assert p0 == [3.14], \
  "Test 1.1 failed in Python"
endEmbeddedCode

*Test 1.2: Test default list behavior of one dimensional parameters
embeddedCode Python:
  p1 = list(gams.get('p1'))
  assert p1 == [('i1', 3.14), ('i2', 3.14)], \
  "Test 1.2 failed in Python"
endEmbeddedCode




*Test 1.3: Test default list behavior of multi dimensional parameters
embeddedCode Python:
  p2 = list(gams.get('p2'))
  assert p2 == [(('i1', 'j1'), 3.14), \
                (('i1', 'j2'), 3.14), \
                (('i1', 'j3'), 3.14), \
                (('i2', 'j1'), 3.14), \
                (('i2', 'j2'), 3.14), \
                (('i2', 'j3'), 3.14)], \
  "Test 1.3 failed in Python"
endEmbeddedCode

*Test 1.4: Test default list behavior of one dimensional sets
embeddedCode Python:
  i = list(gams.get('i'))
  assert i == ['i1', 'i2'], \
  "Test 1.4 failed in Python"
endEmbeddedCode

*Test 1.5: Test default list behavior of multi dimensional sets
embeddedCode Python:
  ij = list(gams.get('ij'))
  assert ij == [('i1', 'j1'), ('i1', 'j2'), \
                ('i1', 'j3'), ('i2', 'j1'), \
                ('i2', 'j2'), ('i2', 'j3')], \
  "Test 1.5 failed in Python"
endEmbeddedCode

*Test 1.6: Test default list behavior of scalar variables
embeddedCode Python:
  v0 = list(gams.get('v0'))
  assert v0 == [(3.14, 0, 3.14, 3.14, 1)], \
  "Test 1.6 failed in Python"
endEmbeddedCode

*Test 1.7: Test default list behavior of one dimensional variables
embeddedCode Python:
  v1 = list(gams.get('v1'))
  assert v1 == [('i1', (3.14, 0, 3.14, 3.14, 1)), \
                ('i2', (3.14, 0, 3.14, 3.14, 1))], \
  "Test 1.7 failed in Python"
endEmbeddedCode

*Test 1.8: Test default list behavior of multi dimensional variables
embeddedCode Python:
  v2 = list(gams.get('v2'))
  assert v2 == [(('i1', 'j1'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i1', 'j2'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i1', 'j3'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i2', 'j1'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i2', 'j2'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i2', 'j3'), (3.14, 0, 3.14, 3.14, 1))], \
  "Test 1.8 failed in Python"
endEmbeddedCode

*Test 1.9: Test default list behavior of scalar equations
embeddedCode Python:
  e0 = list(gams.get('e0'))
  assert e0 == [(3.14, 0, 3.14, 3.14, 1)], \
  "Test 1.9 failed in Python"
endEmbeddedCode

*Test 1.10: Test default list behavior of one dimensional equations
embeddedCode Python:
  e1 = list(gams.get('e1'))
  assert e1 == [('i1', (3.14, 0, 3.14, 3.14, 1)), \
                ('i2', (3.14, 0, 3.14, 3.14, 1))], \
  "Test 1.10 failed in Python"
endEmbeddedCode

*Test 1.11: Test default list behavior of multi dimensional equations
embeddedCode Python:
  e2 = list(gams.get('e2'))
  assert e2 == [(('i1', 'j1'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i1', 'j2'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i1', 'j3'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i2', 'j1'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i2', 'j2'), (3.14, 0, 3.14, 3.14, 1)), \
                (('i2', 'j3'), (3.14, 0, 3.14, 3.14, 1)) ], \
  "Test 1.11 failed in Python"
endEmbeddedCode

*** Test different aspects of list behavior ***

*Test 2.1: Test list behavior of explanatory text
embeddedCode Python:
  i = list(gams.get('i', valueFormat=ValueFormat.FLAT))
  assert i == [('i1', 'text for i1'), ('i2', 'text for i2')], \
  "Test 2.1a failed in Python"
endEmbeddedCode

*Test 2.1: Test list behavior of explanatory text
embeddedCode Python:
  i = list(gams.get('i', valueFormat=ValueFormat.TUPLE))
  assert i == [('i1', ('text for i1',)), ('i2', ('text for i2',))], \
  "Test 2.1b failed in Python"
endEmbeddedCode

*** Test default list behavior using label indexes instead of strings ***

*Test 3.1: Test list behavior of scalars using label indexes
embeddedCode Python:
  p0 = list(gams.get('p0', KeyType.INT))
  assert p0 == [3.14], \
  "Test 3.1 failed in Python"
endEmbeddedCode

*Test 3.2: Test list behavior of one dimensional parameters using label indexes
embeddedCode Python:
  p1 = list(gams.get('p1', KeyType.INT))
  assert p1 == [(1, 3.14), (2, 3.14)], \
  "Test 3.2 failed in Python"
endEmbeddedCode

*Test 3.3: Test list behavior of multi dimensional parameters using label indexes
embeddedCode Python:
  p2 = list(gams.get('p2', KeyType.INT))
  assert p2 == [((1, 3), 3.14), \
                ((1, 4), 3.14), \
                ((1, 5), 3.14), \
                ((2, 3), 3.14), \
                ((2, 4), 3.14), \
                ((2, 5), 3.14)], \
  "Test 3.3 failed in Python"
endEmbeddedCode

*Test 3.4: Test list behavior of one dimensional sets using label indexes
embeddedCode Python:
  i = list(gams.get('i', KeyType.INT))
  assert i == [1, 2], \
  "Test 3.4 failed in Python"
endEmbeddedCode

*Test 3.5: Test list behavior of multi dimensional sets using label indexes
embeddedCode Python:
  ij = list(gams.get('ij', KeyType.INT))
  assert ij == [(1, 3), (1, 4), \
                (1, 5), (2, 3), \
                (2, 4), (2, 5)], \
  "Test 3.5 failed in Python"
endEmbeddedCode

*Test 3.6: Test list behavior of scalar variables using label indexes
embeddedCode Python:
  v0 = list(gams.get('v0', KeyType.INT))
  assert v0 == [(3.14, 0, 3.14, 3.14, 1)], \
  "Test 3.6 failed in Python"
endEmbeddedCode

*Test 3.7: Test list behavior of one dimensional variables using label indexes
embeddedCode Python:
  v1 = list(gams.get('v1', KeyType.INT))
  assert v1 == [(1, (3.14, 0, 3.14, 3.14, 1)), \
                (2, (3.14, 0, 3.14, 3.14, 1))], \
  "Test 3.7 failed in Python"
endEmbeddedCode

*Test 3.8: Test list behavior of multi dimensional variables using label indexes
embeddedCode Python:
  v2 = list(gams.get('v2', KeyType.INT))
  assert v2 == [((1, 3), (3.14, 0, 3.14, 3.14, 1)), \
                ((1, 4), (3.14, 0, 3.14, 3.14, 1)), \
                ((1, 5), (3.14, 0, 3.14, 3.14, 1)), \
                ((2, 3), (3.14, 0, 3.14, 3.14, 1)), \
                ((2, 4), (3.14, 0, 3.14, 3.14, 1)), \
                ((2, 5), (3.14, 0, 3.14, 3.14, 1))], \
  "Test 3.8 failed in Python"
endEmbeddedCode

*Test 3.9: Test list behavior of scalar equations using label indexes
embeddedCode Python:
  e0 = list(gams.get('e0', KeyType.INT))
  assert e0 == [(3.14, 0, 3.14, 3.14, 1)], \
  "Test 3.9 failed in Python"
endEmbeddedCode

*Test 3.10: Test list behavior of one dimensional equations using label indexes
embeddedCode Python:
  e1 = list(gams.get('e1', KeyType.INT))
  assert e1 == [(1, (3.14, 0, 3.14, 3.14, 1)), \
                (2, (3.14, 0, 3.14, 3.14, 1))], \
  "Test 3.10 failed in Python"
endEmbeddedCode

*Test 3.11: Test list behavior of multi dimensional equations using label indexes
embeddedCode Python:
  e2 = list(gams.get('e2', KeyType.INT))
  assert e2 == [((1, 3), (3.14, 0, 3.14, 3.14, 1)), \
                ((1, 4), (3.14, 0, 3.14, 3.14, 1)), \
                ((1, 5), (3.14, 0, 3.14, 3.14, 1)), \
                ((2, 3), (3.14, 0, 3.14, 3.14, 1)), \
                ((2, 4), (3.14, 0, 3.14, 3.14, 1)), \
                ((2, 5), (3.14, 0, 3.14, 3.14, 1)) ], \
  "Test 3.11 failed in Python"
endEmbeddedCode


*** Test writing symbol data using labels ***

*Test 4.1: Test writing of scalars
execute_unload 'wanted.gdx', p0_want=p0;

option clear=p0;
embeddedCode Python:
  data = [3.14]
  gams.set('p0', data)
endEmbeddedCode p0
$batinclude checkData p0 'Test 4.1a failed in GAMS'

option clear=p0;
embeddedCode Python:
  data = [(3.14,)]
  gams.set('p0', data)
endEmbeddedCode p0
$batinclude checkData p0 'Test 4.1b failed in GAMS'

*Test 4.2: Test writing of one dimensional parameters
execute_unload 'wanted.gdx', p1_want=p1;

option clear=p1;
embeddedCode Python:
  data = [("i1", 3.14), ("i2", 3.14)]
  gams.set('p1', data)
endEmbeddedCode p1
$batinclude checkData p1 'Test 4.2a failed in GAMS'

option clear=p1;
embeddedCode Python:
  data = [(("i1",), 3.14), (("i2",), 3.14)]
  gams.set('p1', data)
endEmbeddedCode p1
$batinclude checkData p1 'Test 4.2b failed in GAMS'

option clear=p1;
embeddedCode Python:
  data = [("i1", (3.14,)), ("i2", (3.14,))]
  gams.set('p1', data)
endEmbeddedCode p1
$batinclude checkData p1 'Test 4.2c failed in GAMS'

option clear=p1;
embeddedCode Python:
  data = [(("i1",), (3.14,)), (("i2",), (3.14,))]
  gams.set('p1', data)
endEmbeddedCode p1
$batinclude checkData p1 'Test 4.2d failed in GAMS'

*Test 4.3: Test writing of multi dimensional parameters
execute_unload 'wanted.gdx', p2_want=p2;

option clear=p2;
embeddedCode Python:
  data = [('i1', 'j1', 3.14), ('i1', 'j2', 3.14)]
  gams.set('p2', data)
endEmbeddedCode p2
$batinclude checkData p2 'Test 4.3a failed in GAMS'

option clear=p2;
embeddedCode Python:
  data = [(('i1', 'j1'), 3.14), (('i1', 'j2'), 3.14)]
  gams.set('p2', data)
endEmbeddedCode p2
$batinclude checkData p2 'Test 4.3b failed in GAMS'

option clear=p2;
embeddedCode Python:
  data = [('i1', 'j1', (3.14,)), ('i1', 'j2', (3.14,))]
  gams.set('p2', data)
endEmbeddedCode p2
$batinclude checkData p2 'Test 4.3c failed in GAMS'

option clear=p2;
embeddedCode Python:
  data = [(('i1', 'j1'), (3.14,)), (('i1', 'j2'), (3.14,))]
  gams.set('p2', data)
endEmbeddedCode p2
$batinclude checkData p2 'Test 4.3d failed in GAMS'

*Test 4.4: Test writing of one dimensional sets
execute_unload 'wanted.gdx', i_want=iout;

option clear=iout;
embeddedCode Python:
  data = ['i1', 'i2']
  gams.set('iout', data)
endEmbeddedCode iout
$batinclude checkData iout 'Test 4.4a failed in GAMS'

option clear=iout;
embeddedCode Python:
  data = [('i1',), ('i2',)]
  gams.set('iout', data)
endEmbeddedCode iout
$batinclude checkData iout 'Test 4.4b failed in GAMS'

execute_unload 'wanted.gdx', i_want_ex=iout;

option clear=iout;
embeddedCode Python:
  data = [('i1', "text 1"), ('i2', "text 2")]
  gams.set('iout', data)
endEmbeddedCode iout
$batinclude checkData iout 'Test 4.4c failed in GAMS'

option clear=iout;
embeddedCode Python:
  data = [(('i1',), "text 1"), (('i2',), "text 2")]
  gams.set('iout', data)
endEmbeddedCode iout
$batinclude checkData iout 'Test 4.4d failed in GAMS'

option clear=iout;
embeddedCode Python:
  data = [('i1', ("text 1",)), ('i2', ("text 2",))]
  gams.set('iout', data)
endEmbeddedCode iout
$batinclude checkData iout 'Test 4.4e failed in GAMS'

option clear=iout;
embeddedCode Python:
  data = [(('i1',), ("text 1",)), (('i2',), ("text 2",))]
  gams.set('iout', data)
endEmbeddedCode iout
$batinclude checkData iout 'Test 4.4f failed in GAMS'

*Test 4.5: Test writing of multi dimensional sets
execute_unload 'wanted.gdx', ij_want=ij;

option clear=ij;
embeddedCode Python:
  data = [('i1', 'j1'), ('i1', 'j2')]
  gams.set('ij', data)
endEmbeddedCode ij
$batinclude checkData ij 'Test 4.5a failed in GAMS'

option clear=ij;
embeddedCode Python:
  data = [(('i1', 'j1'),), (('i1', 'j2'),)]
  gams.set('ij', data)
endEmbeddedCode ij
$batinclude checkData ij 'Test 4.5b failed in GAMS'

execute_unload 'wanted.gdx', ij_want_ex=ij;

option clear=ij;
embeddedCode Python:
  data = [('i1', 'j1', 'text 1'), ('i1', 'j2', 'text 2')]
  gams.set('ij', data)
endEmbeddedCode ij
$batinclude checkData ij 'Test 4.5c failed in GAMS'

option clear=ij;
embeddedCode Python:
  data = [(('i1', 'j1'), 'text 1'), (('i1', 'j2'), 'text 2')]
  gams.set('ij', data)
endEmbeddedCode ij
$batinclude checkData ij 'Test 4.5d failed in GAMS'

option clear=ij;
embeddedCode Python:
  data = [('i1', 'j1', ('text 1',)), ('i1', 'j2', ('text 2',))]
  gams.set('ij', data)
endEmbeddedCode ij
$batinclude checkData ij 'Test 4.5e failed in GAMS'

option clear=ij;
embeddedCode Python:
  data = [(('i1', 'j1'), ('text 1',)), (('i1', 'j2'), ('text 2',))]
  gams.set('ij', data)
endEmbeddedCode ij
$batinclude checkData ij 'Test 4.5f failed in GAMS'

*Test 4.6: Test writing of scalar variable
execute_unload 'wanted.gdx', v0_want=v0;

option clear=v0;
embeddedCode Python:
  data = [(3.14, 0, 0, 10, 1)]
  gams.set('v0', data)
endEmbeddedCode v0
$batinclude checkData v0 'Test 4.6a failed in GAMS'

*Test 4.7: Test writing of one dimensonal variables
execute_unload 'wanted.gdx', v1_want=v1;

option clear=v1;
embeddedCode Python:
  data = [("i1", 3.14, 0, 0, 10, 1), ("i2", 3.14, 0, 0, 10, 1)]
  gams.set('v1', data)
endEmbeddedCode v1
$batinclude checkData v1 'Test 4.7a failed in GAMS'

option clear=v1;
embeddedCode Python:
  data = [(("i1",), 3.14, 0, 0, 10, 1), (("i2",), 3.14, 0, 0, 10, 1)]
  gams.set('v1', data)
endEmbeddedCode v1
$batinclude checkData v1 'Test 4.7b failed in GAMS'

option clear=v1;
embeddedCode Python:
  data = [("i1", (3.14, 0, 0, 10, 1)), ("i2", (3.14, 0, 0, 10, 1))]
  gams.set('v1', data)
endEmbeddedCode v1
$batinclude checkData v1 'Test 4.7c failed in GAMS'

option clear=v1;
embeddedCode Python:
  data = [(("i1",), (3.14, 0, 0, 10, 1)), (("i2",), (3.14, 0, 0, 10, 1))]
  gams.set('v1', data)
endEmbeddedCode v1
$batinclude checkData v1 'Test 4.7d failed in GAMS'

*Test 4.8: Test writing of multi dimensonal variables
execute_unload 'wanted.gdx', v2_want=v2;

option clear=v2;
embeddedCode Python:
  data = [("i1", "j1", 3.14, 0, 0, 10, 1), ("i1", "j2", 3.14, 0, 0, 10, 1)]
  gams.set('v2', data)
endEmbeddedCode v2
$batinclude checkData v2 'Test 4.8a failed in GAMS'

option clear=v2;
embeddedCode Python:
  data = [(("i1", "j1"), 3.14, 0, 0, 10, 1), (("i1", "j2"), 3.14, 0, 0, 10, 1)]
  gams.set('v2', data)
endEmbeddedCode v2
$batinclude checkData v2 'Test 4.8b failed in GAMS'

option clear=v2;
embeddedCode Python:
  data = [("i1", "j1", (3.14, 0, 0, 10, 1)), ("i1", "j2", (3.14, 0, 0, 10, 1))]
  gams.set('v2', data)
endEmbeddedCode v2
$batinclude checkData v2 'Test 4.8c failed in GAMS'

option clear=v2;
embeddedCode Python:
  data = [(("i1", "j1"), (3.14, 0, 0, 10, 1)), (("i1", "j2"), (3.14, 0, 0, 10, 1))]
  gams.set('v2', data)
endEmbeddedCode v2
$batinclude checkData v2 'Test 4.8d failed in GAMS'

*Test 4.9: Test writing of scalar equations
execute_unload 'wanted.gdx', e0_want=e0;

option clear=e0;
embeddedCode Python:
  data = [(3.14, 0, 0, 10, 1)]
  gams.set('e0', data)
endEmbeddedCode e0
$batinclude checkData e0 'Test 4.9 failed in GAMS'


*Test 4.10: Test writing of one dimensonal equations
execute_unload 'wanted.gdx', e1_want=e1;

option clear=e1;
embeddedCode Python:
  data = [("i1", 3.14, 0, 0, 10, 1), ("i2", 3.14, 0, 0, 10, 1)]
  gams.set('e1', data)
endEmbeddedCode e1
$batinclude checkData e1 'Test 4.10a failed in GAMS'

option clear=e1;
embeddedCode Python:
  data = [(("i1",), 3.14, 0, 0, 10, 1), (("i2",), 3.14, 0, 0, 10, 1)]
  gams.set('e1', data)
endEmbeddedCode e1
$batinclude checkData e1 'Test 4.10b failed in GAMS'

option clear=e1;
embeddedCode Python:
  data = [("i1", (3.14, 0, 0, 10, 1)), ("i2", (3.14, 0, 0, 10, 1))]
  gams.set('e1', data)
endEmbeddedCode e1
$batinclude checkData e1 'Test 4.10c failed in GAMS'

option clear=e1;
embeddedCode Python:
  data = [(("i1",), (3.14, 0, 0, 10, 1)), (("i2",), (3.14, 0, 0, 10, 1))]
  gams.set('e1', data)
endEmbeddedCode e1
$batinclude checkData e1 'Test 4.10d failed in GAMS'

*Test 4.11: Test writing of multi dimensonal equations
execute_unload 'wanted.gdx', e2_want=e2;

option clear=e2;
embeddedCode Python:
  data = [("i1", "j1", 3.14, 0, 0, 10, 1), ("i1", "j2", 3.14, 0, 0, 10, 1)]
  gams.set('e2', data)
endEmbeddedCode e2
$batinclude checkData e2 'Test 4.11a failed in GAMS'

option clear=e2;
embeddedCode Python:
  data = [(("i1", "j1"), 3.14, 0, 0, 10, 1), (("i1", "j2"), 3.14, 0, 0, 10, 1)]
  gams.set('e2', data)
endEmbeddedCode e2
$batinclude checkData e2 'Test 4.11b failed in GAMS'

option clear=e2;
embeddedCode Python:
  data = [("i1", "j1", (3.14, 0, 0, 10, 1)), ("i1", "j2", (3.14, 0, 0, 10, 1))]
  gams.set('e2', data)
endEmbeddedCode e2
$batinclude checkData e2 'Test 4.11c failed in GAMS'

option clear=e2;
embeddedCode Python:
  data = [(("i1", "j1"), (3.14, 0, 0, 10, 1)), (("i1", "j2"), (3.14, 0, 0, 10, 1))]
  gams.set('e2', data)
endEmbeddedCode e2
$batinclude checkData e2 'Test 4.11d failed in GAMS'


*** Test writing symbol data using label indexes instead of strings ***

*Test 5.1: Test writing of scalars using label indexes
execute_unload 'wanted.gdx', p0_want=p0;

option clear=p0;
embeddedCode Python:
  data = [3.14]
  gams.set('p0', data)
endEmbeddedCode p0
$batinclude checkData p0 'Test 5.1a failed in GAMS'

option clear=p0;
embeddedCode Python:
  data = [(3.14,)]
  gams.set('p0', data)
endEmbeddedCode p0
$batinclude checkData p0 'Test 5.1b failed in GAMS'

*Test 5.2: Test writing of one dimensional parameters using label indexes
execute_unload 'wanted.gdx', p1_want=p1;

option clear=p1;
embeddedCode Python:
  data = [(1, 3.14), (2, 3.14)]
  gams.set('p1', data)
endEmbeddedCode p1
$batinclude checkData p1 'Test 5.2a failed in GAMS'

option clear=p1;
embeddedCode Python:
  data = [((1,), 3.14), ((2,), 3.14)]
  gams.set('p1', data)
endEmbeddedCode p1
$batinclude checkData p1 'Test 5.2b failed in GAMS'

option clear=p1;
embeddedCode Python:
  data = [(1, (3.14,)), (2, (3.14,))]
  gams.set('p1', data)
endEmbeddedCode p1
$batinclude checkData p1 'Test 5.2c failed in GAMS'

option clear=p1;
embeddedCode Python:
  data = [((1,), (3.14,)), ((2,), (3.14,))]
  gams.set('p1', data)
endEmbeddedCode p1
$batinclude checkData p1 'Test 5.2d failed in GAMS'

*Test 5.3: Test writing of multi dimensional parameters using label indexes
execute_unload 'wanted.gdx', p2_want=p2;

option clear=p2;
embeddedCode Python:
  data = [(1, 3, 3.14), (1, 4, 3.14)]
  gams.set('p2', data)
endEmbeddedCode p2
$batinclude checkData p2 'Test 5.3a failed in GAMS'

option clear=p2;
embeddedCode Python:
  data = [((1, 3), 3.14), ((1, 4), 3.14)]
  gams.set('p2', data)
endEmbeddedCode p2
$batinclude checkData p2 'Test 5.3b failed in GAMS'

option clear=p2;
embeddedCode Python:
  data = [(1, 3, (3.14,)), (1, 4, (3.14,))]
  gams.set('p2', data)
endEmbeddedCode p2
$batinclude checkData p2 'Test 5.3c failed in GAMS'

option clear=p2;
embeddedCode Python:
  data = [((1, 3), (3.14,)), ((1, 4), (3.14,))]
  gams.set('p2', data)
endEmbeddedCode p2
$batinclude checkData p2 'Test 5.3d failed in GAMS'


*Test 5.4: Test writing of one dimensional sets using label indexes
execute_unload 'wanted.gdx', i_want=iout;

option clear=iout;
embeddedCode Python:
  data = [1, 2]
  gams.set('iout', data)
endEmbeddedCode iout
$batinclude checkData iout 'Test 5.4a failed in GAMS'

option clear=iout;
embeddedCode Python:
  data = [(1,), (2,)]
  gams.set('iout', data)
endEmbeddedCode iout
$batinclude checkData iout 'Test 5.4b failed in GAMS'

execute_unload 'wanted.gdx', i_want_ex=iout;

option clear=iout;
embeddedCode Python:
  data = [(1, "text 1"), (2, "text 2")]
  gams.set('iout', data)
endEmbeddedCode iout
$batinclude checkData iout 'Test 5.4c failed in GAMS'

option clear=iout;
embeddedCode Python:
  data = [((1,), "text 1"), ((2,), "text 2")]
  gams.set('iout', data)
endEmbeddedCode iout
$batinclude checkData iout 'Test 5.4d failed in GAMS'

option clear=iout;
embeddedCode Python:
  data = [(1, ("text 1",)), (2, ("text 2",))]
  gams.set('iout', data)
endEmbeddedCode iout
$batinclude checkData iout 'Test 5.4e failed in GAMS'

option clear=iout;
embeddedCode Python:
  data = [((1,), ("text 1",)), ((2,), ("text 2",))]
  gams.set('iout', data)
endEmbeddedCode iout
$batinclude checkData iout 'Test 5.4f failed in GAMS'

*Test 5.5: Test writing of multi dimensional sets using label indexes
execute_unload 'wanted.gdx', ij_want=ij;

option clear=ij;
embeddedCode Python:
  data = [(1, 3), (1, 4)]
  gams.set('ij', data)
endEmbeddedCode ij
$batinclude checkData ij 'Test 5.5a failed in GAMS'

option clear=ij;
embeddedCode Python:
  data = [((1, 3),), ((1, 4),)]
  gams.set('ij', data)
endEmbeddedCode ij
$batinclude checkData ij 'Test 5.5b failed in GAMS'

execute_unload 'wanted.gdx', ij_want_ex=ij;

option clear=ij;
embeddedCode Python:
  data = [(1, 3, 'text 1'), (1, 4, 'text 2')]
  gams.set('ij', data)
endEmbeddedCode ij
$batinclude checkData ij 'Test 5.5c failed in GAMS'

option clear=ij;
embeddedCode Python:
  data = [((1, 3), 'text 1'), ((1, 4), 'text 2')]
  gams.set('ij', data)
endEmbeddedCode ij
$batinclude checkData ij 'Test 5.5d failed in GAMS'

option clear=ij;
embeddedCode Python:
  data = [(1, 3, ('text 1',)), (1, 4, ('text 2',))]
  gams.set('ij', data)
endEmbeddedCode ij
$batinclude checkData ij 'Test 5.5e failed in GAMS'

option clear=ij;
embeddedCode Python:
  data = [((1, 3), ('text 1',)), ((1, 4), ('text 2',))]
  gams.set('ij', data)
endEmbeddedCode ij
$batinclude checkData ij 'Test 5.5f failed in GAMS'

*Test 5.6: Test writing of scalar variable using label indexes
execute_unload 'wanted.gdx', v0_want=v0;

option clear=v0;
embeddedCode Python:
  data = [(3.14, 0, 0, 10, 1)]
  gams.set('v0', data)
endEmbeddedCode v0
$batinclude checkData v0 'Test 5.6a failed in GAMS'

*Test 5.7: Test writing of one dimensonal variables using label indexes
execute_unload 'wanted.gdx', v1_want=v1;

embeddedCode Python:
  data = [(1, 3.14, 0, 0, 10, 1), (2, 3.14, 0, 0, 10, 1)]
  gams.set('v1', data)
endEmbeddedCode v1
$batinclude checkData v1 'Test 5.7a failed in GAMS'

option clear=v1;
embeddedCode Python:
  data = [((1,), 3.14, 0, 0, 10, 1), ((2,), 3.14, 0, 0, 10, 1)]
  gams.set('v1', data)
endEmbeddedCode v1
$batinclude checkData v1 'Test 5.7b failed in GAMS'

option clear=v1;
embeddedCode Python:
  data = [(1, (3.14, 0, 0, 10, 1)), (2, (3.14, 0, 0, 10, 1))]
  gams.set('v1', data)
endEmbeddedCode v1
$batinclude checkData v1 'Test 5.7c failed in GAMS'

option clear=v1;
embeddedCode Python:
  data = [((1,), (3.14, 0, 0, 10, 1)), ((2,), (3.14, 0, 0, 10, 1))]
  gams.set('v1', data)
endEmbeddedCode v1
$batinclude checkData v1 'Test 5.7d failed in GAMS'

*Test 5.8: Test writing of multi dimensonal variables using label indexes
execute_unload 'wanted.gdx', v2_want=v2;

option clear=v2;
embeddedCode Python:
  data = [(1, 3, 3.14, 0, 0, 10, 1), (1, 4, 3.14, 0, 0, 10, 1)]
  gams.set('v2', data)
endEmbeddedCode v2
$batinclude checkData v2 'Test 5.8a failed in GAMS'

option clear=v2;
embeddedCode Python:
  data = [((1, 3), 3.14, 0, 0, 10, 1), ((1, 4), 3.14, 0, 0, 10, 1)]
  gams.set('v2', data)
endEmbeddedCode v2
$batinclude checkData v2 'Test 5.8b failed in GAMS'

option clear=v2;
embeddedCode Python:
  data = [(1, 3, (3.14, 0, 0, 10, 1)), (1, 4, (3.14, 0, 0, 10, 1))]
  gams.set('v2', data)
endEmbeddedCode v2
$batinclude checkData v2 'Test 5.8c failed in GAMS'

option clear=v2;
embeddedCode Python:
  data = [((1, 3), (3.14, 0, 0, 10, 1)), ((1, 4), (3.14, 0, 0, 10, 1))]
  gams.set('v2', data)
endEmbeddedCode v2
$batinclude checkData v2 'Test 5.8d failed in GAMS'

*Test 5.9: Test writing of scalar equations using label indexes
execute_unload 'wanted.gdx', e0_want=e0;

option clear=e0;
embeddedCode Python:
  data = [(3.14, 0, 0, 10, 1)]
  gams.set('e0', data)
endEmbeddedCode e0
$batinclude checkData e0 'Test 5.9 failed in GAMS'

*Test 5.10: Test writing of one dimensonal equations using label indexes
execute_unload 'wanted.gdx', e1_want=e1;

option clear=e1;
embeddedCode Python:
  data = [(1, 3.14, 0, 0, 10, 1), (2, 3.14, 0, 0, 10, 1)]
  gams.set('e1', data)
endEmbeddedCode e1
$batinclude checkData e1 'Test 5.10a failed in GAMS'

option clear=e1;
embeddedCode Python:
  data = [((1,), 3.14, 0, 0, 10, 1), ((2,), 3.14, 0, 0, 10, 1)]
  gams.set('e1', data)
endEmbeddedCode e1
$batinclude checkData e1 'Test 5.10b failed in GAMS'

option clear=e1;
embeddedCode Python:
  data = [(1, (3.14, 0, 0, 10, 1)), (2, (3.14, 0, 0, 10, 1))]
  gams.set('e1', data)
endEmbeddedCode e1
$batinclude checkData e1 'Test 5.10c failed in GAMS'

option clear=e1;
embeddedCode Python:
  data = [((1,), (3.14, 0, 0, 10, 1)), ((2,), (3.14, 0, 0, 10, 1))]
  gams.set('e1', data)
endEmbeddedCode e1
$batinclude checkData e1 'Test 5.10d failed in GAMS'

*Test 5.11: Test writing of multi dimensonal equations using label indexes
execute_unload 'wanted.gdx', e2_want=e2;

option clear=e2;
embeddedCode Python:
  data = [(1, 3, 3.14, 0, 0, 10, 1), (1, 4, 3.14, 0, 0, 10, 1)]
  gams.set('e2', data)
endEmbeddedCode e2
$batinclude checkData e2 'Test 5.11a failed in GAMS'

option clear=e2;
embeddedCode Python:
  data = [((1, 3), 3.14, 0, 0, 10, 1), ((1, 4), 3.14, 0, 0, 10, 1)]
  gams.set('e2', data)
endEmbeddedCode e2
$batinclude checkData e2 'Test 5.11b failed in GAMS'

option clear=e2;
embeddedCode Python:
  data = [(1, 3, (3.14, 0, 0, 10, 1)), (1, 4, (3.14, 0, 0, 10, 1))]
  gams.set('e2', data)
endEmbeddedCode e2
$batinclude checkData e2 'Test 5.11c failed in GAMS'

option clear=e2;
embeddedCode Python:
  data = [((1, 3), (3.14, 0, 0, 10, 1)), ((1, 4), (3.14, 0, 0, 10, 1))]
  gams.set('e2', data)
endEmbeddedCode e2
$batinclude checkData e2 'Test 5.11d failed in GAMS'


*** Test adding/retrieving labels/label indexes

*Test 6.1: Test retrieving label indexes from label
embeddedCode Python:
  label = gams.getUel(1)
  assert label == 'i1', \
  "Test 6.1 failed in Python"
  label = gams.getUel(3)
  assert label == 'j1', \
  "Test 6.1 failed in Python"
endEmbeddedCode


*Test 6.2: Test merging/adding of labels
embeddedCode Python:
  labelIndex = gams.mergeUel("i2")
  assert labelIndex == 2, \
  "Test 6.2 failed in Python"
  nrUels = gams.getUelCount()
  assert nrUels == 5, \
  "Test 6.2 failed in Python"
  #Note that we can not add new UELs using mergeUel during execution time
endEmbeddedCode


*** Test set() method using merge=True using labels

*Test 7.1: Test merge=True with parameters
execute_unload 'wanted.gdx', p2_want_merge=p2;
option clear=p2;
embeddedCode Python:
  data = [('i1', 'j1', 3.14), ('i1', 'j2', 3.14)]
  gams.set('p2', data)
endEmbeddedCode p2
embeddedCode Python:
  data = [('i1', 'j2', 3.14), ('i1', 'j3', 3.14)]
  gams.set('p2', data, merge=True)
endEmbeddedCode p2
$batinclude checkData p2 'Test 7.1 failed in GAMS'

*Test 7.2: Test merge=True with sets
execute_unload 'wanted.gdx', ij_want_merge=ij;
option clear=ij;
embeddedCode Python:
  data = [('i1', 'j1'), ('i1', 'j2')]
  gams.set('ij', data)
endEmbeddedCode ij
embeddedCode Python:
  data = [('i1', 'j2'), ('i1', 'j3')]
  gams.set('ij', data, merge=True)
endEmbeddedCode ij
$batinclude checkData ij 'Test 7.2 failed in GAMS'

*Test 7.3: Test merge=True with variables
execute_unload 'wanted.gdx', v2_want_merge=v2;
option clear=v2;
embeddedCode Python:
  data = [("i1", "j1", 3.14, 0, 0, 10, 1), ("i1", "j2", 3.14, 0, 0, 10, 1)]
  gams.set('v2', data)
endEmbeddedCode v2
embeddedCode Python:
  data = [("i1", "j2", 3.14, 0, 0, 10, 1), ("i1", "j3", 3.14, 0, 0, 10, 1)]
  gams.set('v2', data, merge=True)
endEmbeddedCode v2
$batinclude checkData v2 'Test 7.3 failed in GAMS'

*Test 7.4: Test merge=True with equations
execute_unload 'wanted.gdx', e2_want_merge=e2;
option clear=e2;
embeddedCode Python:
  data = [("i1", "j1", 3.14, 0, 0, 10, 1), ("i1", "j2", 3.14, 0, 0, 10, 1)]
  gams.set('e2', data)
endEmbeddedCode e2
embeddedCode Python:
  data = [("i1", "j2", 3.14, 0, 0, 10, 1), ("i1", "j3", 3.14, 0, 0, 10, 1)]
  gams.set('e2', data, merge=True)
endEmbeddedCode e2
$batinclude checkData e2 'Test 7.4 failed in GAMS'


*** Test set() method using merge=True using label indexes instead of strings

*Test 8.1: Test merge=True with parameters using label indexes
execute_unload 'wanted.gdx', p2_want_merge=p2;
option clear=p2;
embeddedCode Python:
  data = [(1, 3, 3.14), (1, 4, 3.14)]
  gams.set('p2', data)
endEmbeddedCode p2
embeddedCode Python:
  data = [(1, 4, 3.14), (1, 5, 3.14)]
  gams.set('p2', data, merge=True)
endEmbeddedCode p2
$batinclude checkData p2 'Test 8.1 failed in GAMS'

*Test 8.2: Test merge=True with sets using label indexes
execute_unload 'wanted.gdx', ij_want_merge=ij;
option clear=ij;
embeddedCode Python:
  data = [(1, 3), (1, 4)]
  gams.set('ij', data)
endEmbeddedCode ij
embeddedCode Python:
  data = [(1, 4), (1, 5)]
  gams.set('ij', data, merge=True)
endEmbeddedCode ij
$batinclude checkData ij 'Test 8.2 failed in GAMS'

*Test 8.3: Test merge=True with variables using label indexes
execute_unload 'wanted.gdx', v2_want_merge=v2;
option clear=v2;
embeddedCode Python:
  data = [(1, 3, 3.14, 0, 0, 10, 1), (1, 4, 3.14, 0, 0, 10, 1)]
  gams.set('v2', data)
endEmbeddedCode v2
embeddedCode Python:
  data = [(1, 4, 3.14, 0, 0, 10, 1), (1, 5, 3.14, 0, 0, 10, 1)]
  gams.set('v2', data, merge=True)
endEmbeddedCode v2
$batinclude checkData v2 'Test 8.3 failed in GAMS'

*Test 8.4: Test merge=True with equations using label indexes
execute_unload 'wanted.gdx', e2_want_merge=e2;
option clear=e2;
embeddedCode Python:
  data = [(1, 3, 3.14, 0, 0, 10, 1), (1, 4, 3.14, 0, 0, 10, 1)]
  gams.set('e2', data)
endEmbeddedCode e2
embeddedCode Python:
  data = [(1, 4, 3.14, 0, 0, 10, 1), (1, 5, 3.14, 0, 0, 10, 1)]
  gams.set('e2', data, merge=True)
endEmbeddedCode e2
$batinclude checkData e2 'Test 8.4 failed in GAMS'


*** Miscellaneous tests

*Test 9.1: Test reading of empty symbols
option clear=p2;
embeddedCode Python:
  p2 = list(gams.get('p2'))
  assert p2 == [], \
  "Test 9.1 failed in Python"
endEmbeddedCode

*Test 9.2: Test writing of empty symbols
option clear=p2_want;
execute_unload 'wanted.gdx', p2_want=p2;
embeddedCode Python:
  data = []
  gams.set('p2', data)
endEmbeddedCode p2
$batinclude checkData p2 'Test 9.2 failed in GAMS'