Description
Test functionality of the GAMS embedded code facility for Python during compilation time Contributor: Clemens Westphal, July 2017
Small Model of Type : GAMS
Category : GAMS Test library
Main file : embpy01.gms
$title 'Test for embedded code facility' (EMBPY01,SEQ=735)
$onText
Test functionality of the GAMS embedded code facility for Python during compilation time
Contributor: Clemens Westphal, July 2017
$offText
$log --- Using Python library %sysEnv.GMSPYTHONLIB%
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 embpy01_log.txt
$onEchoV > checkData
$gdxOut have.gdx
$unLoad %1
$gdxOut
$call gdxdiff wanted.gdx have.gdx >> embpy01_log.txt
$if errorlevel 1 $abort %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 ***
$onMultiR
*Test 4.1: Test writing of scalars
$gdxOut wanted.gdx
$unLoad p0_want=p0
$gdxOut
$clear p0
$batInclude checkData p0 'Test 4.1a failed in GAMS'
$clear p0
$batInclude checkData p0 'Test 4.1b failed in GAMS'
*Test 4.2: Test writing of one dimensional parameters
$gdxOut wanted.gdx
$unLoad p1_want=p1
$gdxOut
$clear p1
$batInclude checkData p1 'Test 4.2a failed in GAMS'
$clear p1
$batInclude checkData p1 'Test 4.2b failed in GAMS'
$clear p1
$batInclude checkData p1 'Test 4.2c failed in GAMS'
$clear p1
$batInclude checkData p1 'Test 4.2d failed in GAMS'
*Test 4.3: Test writing of multi dimensional parameters
$gdxOut wanted.gdx
$unLoad p2_want=p2
$gdxOut
$clear p2
$batInclude checkData p2 'Test 4.3a failed in GAMS'
$clear p2
$batInclude checkData p2 'Test 4.3b failed in GAMS'
$clear p2
$batInclude checkData p2 'Test 4.3c failed in GAMS'
$clear p2
$batInclude checkData p2 'Test 4.3d failed in GAMS'
*Test 4.4: Test writing of one dimensional sets
$gdxOut wanted.gdx
$unLoad i_want=iout
$gdxOut
$clear iout
$batInclude checkData iout 'Test 4.4a failed in GAMS'
$clear iout
$batInclude checkData iout 'Test 4.4b failed in GAMS'
$gdxOut wanted.gdx
$unLoad i_want_ex=iout
$gdxOut
$clear iout
$batInclude checkData iout 'Test 4.4c failed in GAMS'
$clear iout
$batInclude checkData iout 'Test 4.4d failed in GAMS'
$clear iout
$batInclude checkData iout 'Test 4.4e failed in GAMS'
$clear iout
$batInclude checkData iout 'Test 4.4f failed in GAMS'
*Test 4.5: Test writing of multi dimensional sets
$gdxOut wanted.gdx
$unLoad ij_want=ij
$gdxOut
$clear ij
$batInclude checkData ij 'Test 4.5a failed in GAMS'
$clear ij
$batInclude checkData ij 'Test 4.5b failed in GAMS'
$gdxOut wanted.gdx
$unLoad ij_want_ex=ij
$gdxOut
$clear ij
$batInclude checkData ij 'Test 4.5c failed in GAMS'
$clear ij
$batInclude checkData ij 'Test 4.5d failed in GAMS'
$clear ij
$batInclude checkData ij 'Test 4.5e failed in GAMS'
$clear ij
$batInclude checkData ij 'Test 4.5f failed in GAMS'
*Test 4.6: Test writing of scalar variable
$gdxOut wanted.gdx
$unLoad v0_want=v0
$gdxOut
$clear v0
$batInclude checkData v0 'Test 4.6a failed in GAMS'
*Test 4.7: Test writing of one dimensonal variables
$gdxOut wanted.gdx
$unLoad v1_want=v1
$gdxOut
$clear v1
$batInclude checkData v1 'Test 4.7a failed in GAMS'
$clear v1
$batInclude checkData v1 'Test 4.7b failed in GAMS'
$clear v1
$batInclude checkData v1 'Test 4.7c failed in GAMS'
$clear v1
$batInclude checkData v1 'Test 4.7d failed in GAMS'
*Test 4.8: Test writing of multi dimensonal variables
$gdxOut wanted.gdx
$unLoad v2_want=v2
$gdxOut
$clear v2
$batInclude checkData v2 'Test 4.8a failed in GAMS'
$clear v2
$batInclude checkData v2 'Test 4.8b failed in GAMS'
$clear v2
$batInclude checkData v2 'Test 4.8c failed in GAMS'
$clear v2
$batInclude checkData v2 'Test 4.8d failed in GAMS'
*Test 4.9: Test writing of scalar equations
$gdxOut wanted.gdx
$unLoad e0_want=e0
$gdxOut
$clear e0
$batInclude checkData e0 'Test 4.9 failed in GAMS'
*Test 4.10: Test writing of one dimensonal equations
$gdxOut wanted.gdx
$unLoad e1_want=e1
$gdxOut
$clear e1
$batInclude checkData e1 'Test 4.10a failed in GAMS'
$clear e1
$batInclude checkData e1 'Test 4.10b failed in GAMS'
$clear e1
$batInclude checkData e1 'Test 4.10c failed in GAMS'
$clear e1
$batInclude checkData e1 'Test 4.10d failed in GAMS'
*Test 4.11: Test writing of multi dimensonal equations
$gdxOut wanted.gdx
$unLoad e2_want=e2
$gdxOut
$clear e2
$batInclude checkData e2 'Test 4.11a failed in GAMS'
$clear e2
$batInclude checkData e2 'Test 4.11b failed in GAMS'
$clear e2
$batInclude checkData e2 'Test 4.11c failed in GAMS'
$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
$gdxOut wanted.gdx
$unLoad p0_want=p0
$gdxOut
$clear p0
$batInclude checkData p0 'Test 5.1a failed in GAMS'
$clear p0
$batInclude checkData p0 'Test 5.1b failed in GAMS'
*Test 5.2: Test writing of one dimensional parameters using label indexes
$gdxOut wanted.gdx
$unLoad p1_want=p1
$gdxOut
$clear p1
$batInclude checkData p1 'Test 5.2a failed in GAMS'
$clear p1
$batInclude checkData p1 'Test 5.2b failed in GAMS'
$clear p1
$batInclude checkData p1 'Test 5.2c failed in GAMS'
$clear p1
$batInclude checkData p1 'Test 5.2d failed in GAMS'
*Test 5.3: Test writing of multi dimensional parameters using label indexes
$gdxOut wanted.gdx
$unLoad p2_want=p2
$gdxOut
$clear p2
$batInclude checkData p2 'Test 5.3a failed in GAMS'
$clear p2
$batInclude checkData p2 'Test 5.3b failed in GAMS'
$clear p2
$batInclude checkData p2 'Test 5.3c failed in GAMS'
$clear p2
$batInclude checkData p2 'Test 5.3d failed in GAMS'
*Test 5.4: Test writing of one dimensional sets using label indexes
$gdxOut wanted.gdx
$unLoad i_want=iout
$gdxOut
$clear iout
$batInclude checkData iout 'Test 5.4a failed in GAMS'
$clear iout
$batInclude checkData iout 'Test 5.4b failed in GAMS'
$gdxOut wanted.gdx
$unLoad i_want_ex=iout
$gdxOut
$clear iout
$batInclude checkData iout 'Test 5.4c failed in GAMS'
$clear iout
$batInclude checkData iout 'Test 5.4d failed in GAMS'
$clear iout
$batInclude checkData iout 'Test 5.4e failed in GAMS'
$clear iout
$batInclude checkData iout 'Test 5.4f failed in GAMS'
*Test 5.5: Test writing of multi dimensional sets using label indexes
$gdxOut wanted.gdx
$unLoad ij_want=ij
$gdxOut
$clear ij
$batInclude checkData ij 'Test 5.5a failed in GAMS'
$clear ij
$batInclude checkData ij 'Test 5.5b failed in GAMS'
$gdxOut wanted.gdx
$unLoad ij_want_ex=ij
$gdxOut
$clear ij
$batInclude checkData ij 'Test 5.5c failed in GAMS'
$clear ij
$batInclude checkData ij 'Test 5.5d failed in GAMS'
$clear ij
$batInclude checkData ij 'Test 5.5e failed in GAMS'
$clear ij
$batInclude checkData ij 'Test 5.5f failed in GAMS'
*Test 5.6: Test writing of scalar variable using label indexes
$gdxOut wanted.gdx
$unLoad v0_want=v0
$gdxOut
$clear v0
$batInclude checkData v0 'Test 5.6a failed in GAMS'
*Test 5.7: Test writing of one dimensonal variables using label indexes
$gdxOut wanted.gdx
$unLoad v1_want=v1
$gdxOut
$batInclude checkData v1 'Test 5.7a failed in GAMS'
$clear v1
$batInclude checkData v1 'Test 5.7b failed in GAMS'
$clear v1
$batInclude checkData v1 'Test 5.7c failed in GAMS'
$clear v1
$batInclude checkData v1 'Test 5.7d failed in GAMS'
*Test 5.8: Test writing of multi dimensonal variables using label indexes
$gdxOut wanted.gdx
$unLoad v2_want=v2
$gdxOut
$clear v2
$batInclude checkData v2 'Test 5.8a failed in GAMS'
$clear v2
$batInclude checkData v2 'Test 5.8b failed in GAMS'
$clear v2
$batInclude checkData v2 'Test 5.8c failed in GAMS'
$clear v2
$batInclude checkData v2 'Test 5.8d failed in GAMS'
*Test 5.9: Test writing of scalar equations using label indexes
$gdxOut wanted.gdx
$unLoad e0_want=e0
$gdxOut
$clear e0
$batInclude checkData e0 'Test 5.9 failed in GAMS'
*Test 5.10: Test writing of one dimensonal equations using label indexes
$gdxOut wanted.gdx
$unLoad e1_want=e1
$gdxOut
$clear e1
$batInclude checkData e1 'Test 5.10a failed in GAMS'
$clear e1
$batInclude checkData e1 'Test 5.10b failed in GAMS'
$clear e1
$batInclude checkData e1 'Test 5.10c failed in GAMS'
$clear e1
$batInclude checkData e1 'Test 5.10d failed in GAMS'
*Test 5.11: Test writing of multi dimensonal equations using label indexes
$gdxOut wanted.gdx
$unLoad e2_want=e2
$gdxOut
$clear e2
$batInclude checkData e2 'Test 5.11a failed in GAMS'
$clear e2
$batInclude checkData e2 'Test 5.11b failed in GAMS'
$clear e2
$batInclude checkData e2 'Test 5.11c failed in GAMS'
$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
$gdxOut wanted.gdx
$unLoad p2_want_merge=p2
$gdxOut
$clear p2
$batInclude checkData p2 'Test 7.1 failed in GAMS'
*Test 7.2: Test mergeType=MergeType.MERGE with sets
$gdxOut wanted.gdx
$unLoad ij_want_merge=ij
$gdxOut
$clear ij
$batInclude checkData ij 'Test 7.2 failed in GAMS'
*Test 7.3: Test mergeType=MergeType.MERGE with variables
$gdxOut wanted.gdx
$unLoad v2_want_merge=v2
$gdxOut
$clear v2
$batInclude checkData v2 'Test 7.3 failed in GAMS'
*Test 7.4: Test mergeType=MergeType.MERGE with equations
$gdxOut wanted.gdx
$unLoad e2_want_merge=e2
$gdxOut
$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
$gdxOut wanted.gdx
$unLoad p2_want_merge=p2
$gdxOut
$clear p2
$batInclude checkData p2 'Test 8.1 failed in GAMS'
*Test 8.2: Test mergeType=MergeType.MERGE with sets using label indexes
$gdxOut wanted.gdx
$unLoad ij_want_merge=ij
$gdxOut
$clear ij
$batInclude checkData ij 'Test 8.2 failed in GAMS'
*Test 8.3: Test mergeType=MergeType.MERGE with variables using label indexes
$gdxOut wanted.gdx
$unLoad v2_want_merge=v2
$gdxOut
$clear v2
$batInclude checkData v2 'Test 8.3 failed in GAMS'
*Test 8.4: Test mergeType=MergeType.MERGE with equations using label indexes
$gdxOut wanted.gdx
$unLoad e2_want_merge=e2
$gdxOut
$clear e2
$batInclude checkData e2 'Test 8.4 failed in GAMS'
*** Miscellaneous tests
*Test 9.1: Test reading of empty symbols
$clear p2
*Test 9.2: Test writing of empty symbols
$clear p2_want
$gdxOut wanted.gdx
$unLoad p2_want=p2
$gdxOut
$batInclude checkData p2 'Test 9.2 failed in GAMS'
*Test 9.3: Test writing of empty scalars
$clear p0_want
$gdxOut wanted.gdx
$unLoad p0_want=p0
$gdxOut
$batInclude checkData p0 'Test 9.3 failed in GAMS'
*Test 9.4: Test clear behavior of scalar symbols and their default values
$clear p0_want
$clear e0_want
positive variable xpos / l 0 /;
negative variable xneg / l 0 /;
$gdxOut wanted.gdx
$unLoad p0_want=p0 e0_want=e0 xpos xneg
$gdxOut
$batInclude checkData 'p0 e0 xpos xneg' 'Test 9.4 failed in GAMS'
*Test 9.5: Test special values
Set svIdx / myUNDF, myNA, myPINF, myMINF, myEPS /;
$onUNDF
Parameter sv_want(svIdx) / myUNDF UNDF, myNA NA, myPINF INF, myMINF -INF, myEPS EPS /
svX(svIdx), svY(svIdx)
$onEmbeddedCode Python:
gams.epsAsZero = False
sv = dict(gams.get('sv_want'))
if sv['myUNDF']!=GMS_SV_UNDEF:
raise ValueError('Test 9.5 failed in GAMS: Expect UNDEF')
if sv['myNA']==sv['myNA']:
raise ValueError('Test 9.5 failed in GAMS: Expect NAN')
if sv['myPINF']!=float('inf'):
raise ValueError('Test 9.5 failed in GAMS: Expect INF')
if sv['myMINF']!=float('-inf'):
raise ValueError('Test 9.5 failed in GAMS: Expect -INF')
if sv['myEPS']!= gams._eps:
raise ValueError('Test 9.5 failed in GAMS: Expect EPS')
gams.db['sv_want'].copy_symbol(gams.db['svX'])
gams.set('svY',[('myUNDF',GMS_SV_UNDEF),('myNA',float('nan')),('myPINF',float('inf')),('myMINF',float('-inf')),('myEPS',gams._eps)])
$offEmbeddedCode svX svY
$offUNDF
$gdxOut wanted.gdx
$unLoad sv_want=svX
$gdxOut
$batInclude checkData 'svX' 'Test 9.5 failed in GAMS (svX)'
$gdxOut wanted.gdx
$unLoad sv_want=svY
$gdxOut
$batInclude checkData 'svY' 'Test 9.5 failed in GAMS (svY)'
*Test 9.6: Test special values (EPS)
Parameter svZeroToEps(svIdx), svZeroToEps_want(svIdx) / myEPS EPS/;
Parameter svZeroToZero(svIdx), svZeroToZero_want(svIdx);
$onEps
$gdxOut wanted.gdx
$unLoad svZeroToEPS_want=svZeroToEPS
$gdxOut
$batInclude checkData 'svZeroToEPS' 'Test 9.6 failed in GAMS (svZeroToEPS)'
$offEps
$gdxOut wanted.gdx
$unLoad svZeroToZero_want=svZeroToZero
$gdxOut
$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
*Test 10.2: Test long strings : Skip of auto-indentation
*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 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
$onEcho > 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890%system.dirsep%ec11.gms
$offEcho
$call.checkErrorLevel gams ec11 lo=2 cdir=1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
*Test 12: Test for specifying source encoding
* UTF-8 is the default
$off
$off
$off
$off
$call.checkErrorLevel gdxdump unialias.gdx | grep Alias > unialias.txt
$call.checkErrorLevel diff unialias.txt unialias_expect.txt > %system.NullFile%
$offEcho
$call.checkErrorLevel gams ec13.gms lo=2
*Test 14: Test gams.printLog with lo=0
$onEcho > test_printlog.gms
$offEcho
$call.checkErrorLevel gams test_printlog.gms lo=0
*Test 15: Test printLog with "%"
$onEchoV > print_log_percent.gms
$offEcho
$call.checkErrorLevel gams print_log_percent.gms > print_log_percent.log
*Test 16: Test gams.printLog end parameter
$onEcho > test_printlog_newline.gms
$offEcho
$call.checkErrorLevel gams test_printlog_newline.gms > log_newline.txt
*Test 17: Test gams.transfer with alias domain
$onEcho > gt14.gms
set i /1/; alias (i,j);
$gdxUnload xxx.gdx
$offEcho
$call.checkErrorLevel gams gt14.gms lo=0
*Test 18: Test EC arguments containing quotes