embpy03.gms : Test projection operator when loading data from embedded code

Description

Contributor: Lutz Westermann, September 2017


Small Model of Type : GAMS


Category : GAMS Test library


Main file : embpy03.gms

$title 'Test projection operator when loading data from embedded code' (EMBPY03,SEQ=739)

$onText
Contributor: Lutz Westermann, September 2017
$offText


* On the major platforms (Windows, Linux, Mac), GMSPYTHONHOME gets set automatically 
* if pySetup=1 (default). If the %gams.sysdir%GMSPython directory does not exist on 
* these platforms pySetup will be reset to 0 and GAMS relies on an external Python 
* setup.
$if not setEnv GMSPYTHONHOME
$if %gams.pySetup%==1 $abort.noError Embedded code Python not ready to be used
$if not setEnv GMSPYTHONHOME $log --- Using external Python

Parameter dataUniverse(*,*,*);
Set       d1Want / i1 /
          d2Want / j1, j2 /
          d3Want / k1, k3 /
          d1(*), d2(*), d3(*);

$macro checkIfAinB(a,b,Error) Set Error; Error(a) = not b(a); abort$card(Error) Error;

* Get set elements from specified dim of the parameter
$onEmbeddedCode Python:
  data = [('i1', 'j1', 'k1', 11.9), ('i1', 'j2', 'k3', 9.11)]
  gams.set('dataUniverse', data)
$offEmbeddedCode d1<dataUniverse.dim1 d2<dataUniverse.dim2 d3<dataUniverse.dim3

checkIfAinB(d1,d1Want,Error1a)
checkIfAinB(d2,d2Want,Error2a)
checkIfAinB(d3,d3Want,Error3a)
checkIfAinB(d1Want,d1,Error1b)
checkIfAinB(d2Want,d2,Error2b)
checkIfAinB(d3Want,d3,Error3b)

Set       e1(*), e2(*), e3(*);
Parameter dataDomain(e1,e2,e3);

* Get set elements by domain matching of the parameter
$onEmbeddedCode Python:
  data = [('i1', 'j1', 'k1', 11.9), ('i1', 'j2', 'k3', 9.11)]
  gams.set('dataDomain', data)
$offEmbeddedCode e1<dataDomain e2<dataDomain e3<dataDomain

checkIfAinB(e1,d1Want,Error1c)
checkIfAinB(e2,d2Want,Error2c)
checkIfAinB(e3,d3Want,Error3c)
checkIfAinB(d1Want,e1,Error1d)
checkIfAinB(d2Want,e2,Error2d)
checkIfAinB(d3Want,e3,Error3d)


Set       f1(*);
Parameter dataDomainF1(f1,f1,f1);

* Get set elements by domain matching of the parameter, start searching from the left
$onEmbeddedCode Python:
  data = [('i1', 'j1', 'k1', 11.9), ('i1', 'j2', 'k3', 9.11)]
  gams.set('dataDomainF1', data)
$offEmbeddedCode f1<=dataDomainF1

checkIfAinB(f1,d1Want,Error1e)
checkIfAinB(d1Want,f1,Error1f)

Set       f2(*);
Parameter dataDomainF2(f2,f2,f2);

* Get set elements by domain matching of the parameter, start searching from the right
$onEmbeddedCode Python:
  data = [('i1', 'j1', 'k1', 11.9), ('i1', 'j2', 'k3', 9.11)]
  gams.set('dataDomainF2', data)
$offEmbeddedCode f2<dataDomainF2

checkIfAinB(f2,d3Want,Error3e)
checkIfAinB(d3Want,f2,Error3f)

* For the following operations we expect failures

$onEcho > noDomMatch.gms
Parameter dataUniverse(*,*,*);
Set       d1(*), d2(*), d3(*);

$onEmbeddedCode Python:
  data = [('i1', 'j1', 'k1', 11.9), ('i1', 'j2', 'k3', 9.11)]
  gams.set('dataUniverse', data)
$offEmbeddedCode d1<dataUniverse d2<dataUniverse d3<dataUniverse
$offEcho

$call gams noDomMatch.gms lo=%GAMS.lo%
$if not errorlevel 1 $abort Expected error from noDomMatch.gms

$onEcho > noParameter.gms
Parameter dataUniverse(*,*,*);
Parameter d1(*), d2(*), d3(*);

$onEmbeddedCode Python:
  data = [('i1', 'j1', 'k1', 11.9), ('i1', 'j2', 'k3', 9.11)]
  gams.set('dataUniverse', data)
$offEmbeddedCode d1<dataUniverse.dim1 d2<dataUniverse.dim2 d3<dataUniverse.dim3
$offEcho

$call gams noParameter.gms lo=%GAMS.lo%
$if not errorlevel 1 $abort Expected error from noParameter.gms

$onEcho > wrongDim.gms
Parameter dataUniverse(*,*,*);
Set       d1(*,*), d2(*,*,*), d3(*,*,*);

$onEmbeddedCode Python:
  data = [('i1', 'j1', 'k1', 11.9), ('i1', 'j2', 'k3', 9.11)]
  gams.set('dataUniverse', data)
$offEmbeddedCode d1<dataUniverse.dim1 d2<dataUniverse.dim2 d3<dataUniverse.dim3
$offEcho

$call gams wrongDim.gms lo=%GAMS.lo%
$if not errorlevel 1 $abort Expected error from wrongDim.gms