embpy12.gms : Test implicit/explicit loading in Embedded Code

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