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


$log --- Using Python library %sysEnv.GMSPYTHONLIB%

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)

* Load filtered data - This example lead to an error at some stage because of out-of-order UELs when writing to GDX, see #5016
Set code, labId, rr(code,labId);
parameter raw(*,*,*);

$onEmbeddedCode Python:
gams.wsWorkingDir = '.'
cdb = gams.ws.add_database_from_gdx('getdata.gdx')
cdb['raw'].copy_symbol(gams.db['raw'])
cdb['rr'].copy_symbol(gams.db['rr'])
$offEmbeddedCode code<raw.dim1 labId<raw.dim2 rr

execute_unload 'dd.gdx' rr;

* 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
$clearErrors