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
$log --- Using Python library %sysEnv.GMSPYTHONLIB%
$onUNDF
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/
svIdx / myUNDF, myNA, myPINF, myMINF, myEPS /;
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 /
sv_want(svIdx) / myUNDF UNDF, myNA NA, myPINF INF, myMINF -INF, myEPS EPS /
svX(svIdx)
svY(svIdx);
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 that an empty code block works
*** Test that an code block with comments only works
*** Test default list behavior ***
*Test 1.1: Test default list behavior of scalars
*Test 1.2: Test default list behavior of one dimensional parameters
*Test 1.3: Test default list behavior of multi dimensional parameters
*Test 1.4: Test default list behavior of one dimensional sets
*Test 1.5: Test default list behavior of multi dimensional sets
*Test 1.6: Test default list behavior of scalar variables
*Test 1.7: Test default list behavior of one dimensional variables
*Test 1.8: Test default list behavior of multi dimensional variables
*Test 1.9: Test default list behavior of scalar equations
*Test 1.10: Test default list behavior of one dimensional equations
*Test 1.11: Test default list behavior of multi dimensional equations
*** Test different aspects of list behavior ***
*Test 2.1: Test list behavior of explanatory text
*Test 2.1: Test list behavior of explanatory text
*** Test default list behavior using label indexes instead of strings ***
*Test 3.1: Test list behavior of scalars using label indexes
*Test 3.2: Test list behavior of one dimensional parameters using label indexes
*Test 3.3: Test list behavior of multi dimensional parameters using label indexes
*Test 3.4: Test list behavior of one dimensional sets using label indexes
*Test 3.5: Test list behavior of multi dimensional sets using label indexes
*Test 3.6: Test list behavior of scalar variables using label indexes
*Test 3.7: Test list behavior of one dimensional variables using label indexes
*Test 3.8: Test list behavior of multi dimensional variables using label indexes
*Test 3.9: Test list behavior of scalar equations using label indexes
*Test 3.10: Test list behavior of one dimensional equations using label indexes
*Test 3.11: Test list behavior of multi dimensional equations using label indexes
*** Test writing symbol data using labels ***
*Test 4.1: Test writing of scalars
execute_unload 'wanted.gdx', p0_want=p0;
option clear=p0;
$batInclude checkData p0 'Test 4.1a failed in GAMS'
option clear=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;
$batInclude checkData p1 'Test 4.2a failed in GAMS'
option clear=p1;
$batInclude checkData p1 'Test 4.2b failed in GAMS'
option clear=p1;
$batInclude checkData p1 'Test 4.2c failed in GAMS'
option clear=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;
$batInclude checkData p2 'Test 4.3a failed in GAMS'
option clear=p2;
$batInclude checkData p2 'Test 4.3b failed in GAMS'
option clear=p2;
$batInclude checkData p2 'Test 4.3c failed in GAMS'
option clear=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;
$batInclude checkData iout 'Test 4.4a failed in GAMS'
option clear=iout;
$batInclude checkData iout 'Test 4.4b failed in GAMS'
execute_unload 'wanted.gdx', i_want_ex=iout;
option clear=iout;
$batInclude checkData iout 'Test 4.4c failed in GAMS'
option clear=iout;
$batInclude checkData iout 'Test 4.4d failed in GAMS'
option clear=iout;
$batInclude checkData iout 'Test 4.4e failed in GAMS'
option clear=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;
$batInclude checkData ij 'Test 4.5a failed in GAMS'
option clear=ij;
$batInclude checkData ij 'Test 4.5b failed in GAMS'
execute_unload 'wanted.gdx', ij_want_ex=ij;
option clear=ij;
$batInclude checkData ij 'Test 4.5c failed in GAMS'
option clear=ij;
$batInclude checkData ij 'Test 4.5d failed in GAMS'
option clear=ij;
$batInclude checkData ij 'Test 4.5e failed in GAMS'
option clear=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;
$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;
$batInclude checkData v1 'Test 4.7a failed in GAMS'
option clear=v1;
$batInclude checkData v1 'Test 4.7b failed in GAMS'
option clear=v1;
$batInclude checkData v1 'Test 4.7c failed in GAMS'
option clear=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;
$batInclude checkData v2 'Test 4.8a failed in GAMS'
option clear=v2;
$batInclude checkData v2 'Test 4.8b failed in GAMS'
option clear=v2;
$batInclude checkData v2 'Test 4.8c failed in GAMS'
option clear=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;
$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;
$batInclude checkData e1 'Test 4.10a failed in GAMS'
option clear=e1;
$batInclude checkData e1 'Test 4.10b failed in GAMS'
option clear=e1;
$batInclude checkData e1 'Test 4.10c failed in GAMS'
option clear=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;
$batInclude checkData e2 'Test 4.11a failed in GAMS'
option clear=e2;
$batInclude checkData e2 'Test 4.11b failed in GAMS'
option clear=e2;
$batInclude checkData e2 'Test 4.11c failed in GAMS'
option clear=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;
$batInclude checkData p0 'Test 5.1a failed in GAMS'
option clear=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;
$batInclude checkData p1 'Test 5.2a failed in GAMS'
option clear=p1;
$batInclude checkData p1 'Test 5.2b failed in GAMS'
option clear=p1;
$batInclude checkData p1 'Test 5.2c failed in GAMS'
option clear=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;
$batInclude checkData p2 'Test 5.3a failed in GAMS'
option clear=p2;
$batInclude checkData p2 'Test 5.3b failed in GAMS'
option clear=p2;
$batInclude checkData p2 'Test 5.3c failed in GAMS'
option clear=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;
$batInclude checkData iout 'Test 5.4a failed in GAMS'
option clear=iout;
$batInclude checkData iout 'Test 5.4b failed in GAMS'
execute_unload 'wanted.gdx', i_want_ex=iout;
option clear=iout;
$batInclude checkData iout 'Test 5.4c failed in GAMS'
option clear=iout;
$batInclude checkData iout 'Test 5.4d failed in GAMS'
option clear=iout;
$batInclude checkData iout 'Test 5.4e failed in GAMS'
option clear=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;
$batInclude checkData ij 'Test 5.5a failed in GAMS'
option clear=ij;
$batInclude checkData ij 'Test 5.5b failed in GAMS'
execute_unload 'wanted.gdx', ij_want_ex=ij;
option clear=ij;
$batInclude checkData ij 'Test 5.5c failed in GAMS'
option clear=ij;
$batInclude checkData ij 'Test 5.5d failed in GAMS'
option clear=ij;
$batInclude checkData ij 'Test 5.5e failed in GAMS'
option clear=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;
$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;
$batInclude checkData v1 'Test 5.7a failed in GAMS'
option clear=v1;
$batInclude checkData v1 'Test 5.7b failed in GAMS'
option clear=v1;
$batInclude checkData v1 'Test 5.7c failed in GAMS'
option clear=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;
$batInclude checkData v2 'Test 5.8a failed in GAMS'
option clear=v2;
$batInclude checkData v2 'Test 5.8b failed in GAMS'
option clear=v2;
$batInclude checkData v2 'Test 5.8c failed in GAMS'
option clear=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;
$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;
$batInclude checkData e1 'Test 5.10a failed in GAMS'
option clear=e1;
$batInclude checkData e1 'Test 5.10b failed in GAMS'
option clear=e1;
$batInclude checkData e1 'Test 5.10c failed in GAMS'
option clear=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;
$batInclude checkData e2 'Test 5.11a failed in GAMS'
option clear=e2;
$batInclude checkData e2 'Test 5.11b failed in GAMS'
option clear=e2;
$batInclude checkData e2 'Test 5.11c failed in GAMS'
option clear=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
*Test 6.2: Test merging/adding of labels
*** Test set() method using mergeType=MergeType.MERGE using labels
*Test 7.1: Test mergeType=MergeType.MERGE with parameters
execute_unload 'wanted.gdx', p2_want_merge=p2;
option clear=p2;
$batInclude checkData p2 'Test 7.1 failed in GAMS'
*Test 7.2: Test mergeType=MergeType.MERGE with sets
execute_unload 'wanted.gdx', ij_want_merge=ij;
option clear=ij;
$batInclude checkData ij 'Test 7.2 failed in GAMS'
*Test 7.3: Test mergeType=MergeType.MERGE with variables
execute_unload 'wanted.gdx', v2_want_merge=v2;
option clear=v2;
$batInclude checkData v2 'Test 7.3 failed in GAMS'
*Test 7.4: Test mergeType=MergeType.MERGE with equations
execute_unload 'wanted.gdx', e2_want_merge=e2;
option clear=e2;
$batInclude checkData e2 'Test 7.4 failed in GAMS'
*** Test set() method using mergeType=MergeType.MERGE using label indexes instead of strings
*Test 8.1: Test mergeType=MergeType.MERGE with parameters using label indexes
execute_unload 'wanted.gdx', p2_want_merge=p2;
option clear=p2;
$batInclude checkData p2 'Test 8.1 failed in GAMS'
*Test 8.2: Test mergeType=MergeType.MERGE with sets using label indexes
execute_unload 'wanted.gdx', ij_want_merge=ij;
option clear=ij;
$batInclude checkData ij 'Test 8.2 failed in GAMS'
*Test 8.3: Test mergeType=MergeType.MERGE with variables using label indexes
execute_unload 'wanted.gdx', v2_want_merge=v2;
option clear=v2;
$batInclude checkData v2 'Test 8.3 failed in GAMS'
*Test 8.4: Test mergeType=MergeType.MERGE with equations using label indexes
execute_unload 'wanted.gdx', e2_want_merge=e2;
option clear=e2;
$batInclude checkData e2 'Test 8.4 failed in GAMS'
*** Miscellaneous tests
*Test 9.1: Test reading of empty symbols
option clear=p2;
*Test 9.2: Test writing of empty symbols
option clear=p2_want;
execute_unload 'wanted.gdx', p2_want=p2;
$batInclude checkData p2 'Test 9.2 failed in GAMS'
*Test 9.3: Test writing of empty scalars
option clear=p0_want;
execute_unload 'wanted.gdx', p0_want=p0;
$batInclude checkData p0 'Test 9.3 failed in GAMS'
*Test 9.4: Test clear behavior of scalar symbols and their default values
option clear=p0_want;
option clear=e0_want;
positive variable xpos / l 0 /;
negative variable xneg / l 0 /;
execute_unload 'wanted.gdx', p0_want=p0, e0_want=e0, xpos, xneg;
$batInclude checkData 'p0 e0 xpos xneg' 'Test 9.4 failed in GAMS'
*Test 9.5: Test special values
execute_unload 'wanted.gdx', sv_want=svX;
$batInclude checkData 'svX' 'Test 9.5 failed in GAMS (svX)'
execute_unload 'wanted.gdx', sv_want=svY;
$batInclude checkData 'svY' 'Test 9.5 failed in GAMS (svY)'
*Test 9.6: Test special values (EPS)
*Note that this test is different from the corresponding test in embpy01 since $onEps and $offEps do not have an effect during GAMS runtime
*Therefore we just check that $onEps has no effect
Parameter svZeroToZero(svIdx), svZeroToZero_want(svIdx);
$onEps
execute_unload 'wanted.gdx', svZeroToZero_want=svZeroToZero;
$batInclude checkData 'svZeroToZero' 'Test 9.6 failed in GAMS (svZeroToZero)'
parameter witheps(*) / i1 1, i2 EPS /;
* Tests for long strings and auto indentation behavior
*Test 10.1: Test long strings : Skip of auto-indentation 1
*Test 10.2: Test long strings : Skip of auto-indentation 2
*Test 10.3: Test long strings : embed """ in '''
*Test 10.4: Test long strings : embed ''' in """
*Test 10.5: Test behavior of line continuation character
*Test 10.6: Mix line continuation character with long strings
*Test 11: Run in a directory with long path name
$call mkdir 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
$onEcho > 123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890%system.dirsep%ec11.gms
$offEcho
$call.checkErrorLevel gams ec11 lo=%GAMS.lo% cdir=123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
*Test 12: Test for specifying source encoding
* UTF-8 is the default
$off
$off
$off
$off
execute.checkErrorLevel 'gdxdump unialias.gdx | grep Alias > unialias.txt'
execute.checkErrorLevel 'diff unialias.txt unialias_expect.txt > %system.NullFile%'
$offEcho
$call.checkErrorLevel gams ec13.gms lo=%GAMS.lo%
*Test 14: Test implicit dimension setting when loading symbol with unknown dimension
$onEcho > ec14.gms
Scalar f /90/;
$offEcho
$call.checkErrorLevel gams ec14.gms lo=%GAMS.lo% gdx=ec14
Parameter f Parameter with unknown dimension;
execute_load 'ec14.gdx', f;
abort$(f<>90) 'f should be 90', f;
*Test 15: Test gams.printLog with lo=0
$onEcho > test_printlog.gms
$offEcho
execute.checkErrorLevel "gams test_printlog.gms lo=0"
*Test 16: Test printLog with "%"
$onEchoV > print_log_percent.gms
$offEcho
$call.checkErrorLevel gams print_log_percent.gms > print_log_percent.log
*Test 17: Test gams.printLog end parameter
$onEcho > test_printlog_newline.gms
$offEcho
execute.checkErrorLevel "gams test_printlog_newline.gms > log_newline.txt";
*Test 18: Test EC arguments containing quotes
put_utility "ecArguments" / "argument containing 'single' and " '"double" quotes';