caproject.gms : Test Connect agent Projection

Description

This test ensures the correctness of the Connect agent "Projection".

Contributor: Michael Bussieck, February 2022


Small Model of Type : GAMS


Category : GAMS Test library


Main file : caproject.gms

$title 'Test Connect agent Projection' (CAPROJECT,SEQ=889)

$ontext
This test ensures the correctness of the Connect agent "Projection".

Contributor: Michael Bussieck, February 2022
$offtext


* On the major platforms (Windows, Linux, Mac), GMSPYTHONLIB gets automatically set 
* to use the internal Python installation in sysdir/GMSPython.
$if not setEnv GMSPYTHONLIB $abort.noError Embedded code Python not ready to be used
$log --- Using Python library %sysEnv.GMSPYTHONLIB%

set i /i1*i5/;
set j /j1*j5/;
set k /k1*k5/;

scalar q0, p0;
parameter q2(j,i), p2(j,i);
parameter q3(k,i,j), p3(i,j,k) /
'i1'.'j2'.'k4' 0.596558144
'i2'.'j4'.'k4' 0.511448928
'i3'.'j1'.'k5' 0.045066059
'i3'.'j2'.'k4' 0.783102004
'i3'.'j3'.'k1' 0.945749415
'i3'.'j4'.'k1' 0.596462556
'i4'.'j1'.'k5' 0.607341301
'i4'.'j2'.'k2' 0.362509471
'i4'.'j5'.'k4' 0.594067961
'i5'.'j1'.'k1' 0.679854079
'i5'.'j1'.'k2' 0.506588022
'i5'.'j4'.'k4' 0.159253884
/;

variable v2(j,i);
variable v3(i,j,k), w3(k,j,i);

equation e3(i,j,k);

set t3(k,i,j), s3(i,j,k) /
'i1'.'j2'.'k4'
'i2'.'j4'.'k4'
'i3'.'j1'.'k5'
'i3'.'j2'.'k4'
'i3'.'j3'.'k1'
'i3'.'j4'.'k1'
'i4'.'j1'.'k5'
'i4'.'j2'.'k2'
'i4'.'j5'.'k4'
'i5'.'j1'.'k1' First
'i5'.'j1'.'k2' Last
'i5'.'j4'.'k4'
/;
set t2(j,i), s2(j,i);

v3.l(s3) = p3(s3); v3.scale(s3) = p3(s3);
e3.l(s3) = p3(s3);

$log test newName missing
$onEmbeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'p3'}]
- Projection:
    name: p3(i,j,k)
$offEmbeddedCode
$if errorFree $abort 'Expect errors'
$clearErrors
$log test name missing
$onEmbeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'p3'}]
- Projection:
    newName: p3(i,j,k)
$offEmbeddedCode
$if errorFree $abort 'Expect errors'
$clearErrors
$log test dim>0
$onEmbeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'p0'}]
- Projection:
    name: p0
    newName: pX
$offEmbeddedCode
$if errorFree $abort 'Expect errors'
$clearErrors
$log test correct dim on name
$onEmbeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'p3'}]
- Projection:
    name: p3(i,j)
    newName: p2(j,i)
$offEmbeddedCode
$if errorFree $abort 'Expect errors'
$clearErrors
$log test suffix on parameter symbol
$onEmbeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'p3'}]
- Projection:
    name: p3.l(i,j,k)
    newName: p2(j,i)
$offEmbeddedCode
$if errorFree $abort 'Expect errors'
$clearErrors
$log test wrong suffix name
$onEmbeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'v3'}]
- Projection:
    name: v3.lower(i,j,k)
    newName: p2(j,i)
$offEmbeddedCode
$if errorFree $abort 'Expect errors'
$clearErrors
$log test aggregationMethod callable
$onEmbeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'v3'}]
- Projection:
    name: v3.l(i,j,k)
    newName: p2(j,k)
    aggregationMethod: groups
$offEmbeddedCode
$if errorFree $abort 'Expect errors'
$clearErrors
$log test aggregationMethod unknown
$onEmbeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'v3'}]
- Projection:
    name: v3.l(i,j,k)
    newName: p2(j,k)
    aggregationMethod: pingpong
$offEmbeddedCode
$if errorFree $abort 'Expect errors'
$clearErrors
$log test target parameter exists
$onEmbeddedCode Connect:
$offEmbeddedCode
$if errorFree $abort 'Expect errors'
$clearErrors

put_utility 'log' / 'test parameter permutation';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'p3'}]
- Projection:
    name: p3(i,j,k)
    newName: q3(k,i,j)
- GAMSWriter:
    symbols: [{'name':'q3'}]
endEmbeddedCode q3
abort$(sum((i,j,k), abs(p3(i,j,k)-q3(k,i,j)))>1e-6) 'wrong q3', p3, q3;

put_utility 'log' / 'test variable level permutation';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'v3'}]
- Projection:
    name: v3.l(i,j,k)
    newName: q3(k,i,j)
- GAMSWriter:
    symbols: [{'name':'q3'}]
endEmbeddedCode q3
abort$(sum((i,j,k), abs(v3.l(i,j,k)-q3(k,i,j)))>1e-6) 'wrong q3', v3.l, q3;

put_utility 'log' / 'test variable scale permutation';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'v3'}]
- Projection:
    name: v3.scale(i,j,k)
    newName: q3(k,i,j)
- GAMSWriter:
    symbols: [{'name':'q3'}]
endEmbeddedCode q3
abort$(sum((s3(i,j,k)), abs(v3.scale(i,j,k)-q3(k,i,j)))>1e-6) 'wrong q3', v3.scale, q3;

put_utility 'log' / 'test equation level permutation';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'e3'}]
- Projection:
    name: e3.l(i,j,k)
    newName: q3(k,i,j)
- GAMSWriter:
    symbols: [{'name':'q3'}]
endEmbeddedCode q3
abort$(sum((i,j,k), abs(e3.l(i,j,k)-q3(k,i,j)))>1e-6) 'wrong q3', v3.l, q3;

put_utility 'log' / 'test variable permutation';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'v3'}]
- Projection:
    name: v3(i,j,k)
    newName: w3(k,j,i)
- GAMSWriter:
    symbols: [{'name':'w3'}]
endEmbeddedCode w3
abort$(sum((i,j,k), abs(v3.l(i,j,k)-w3.l(k,j,i)))>1e-6) 'wrong w3.l', v3.l, w3.l;
abort$(sum((i,j,k), abs(v3.scale(i,j,k)-w3.scale(k,j,i)))>1e-6) 'wrong w3.scale', v3.scale, w3.scale;

put_utility 'log' / 'test parameter projection (max)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'p3'}]
- Projection:
    name: p3(i,j,k)
    newName: p2(j,i)
    aggregationMethod: max
- GAMSWriter:
    symbols: [{'name':'p2'}]
endEmbeddedCode p2
q2(j,i) = smax(k, p3(i,j,k));
abort$(sum((j,i), abs(p2(j,i)-q2(j,i)))>1e-6) 'wrong p2', p2, q2;

put_utility 'log' / 'test parameter projection (count)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'p3'}]
- Projection:
    name: p3(i,j,k)
    newName: p2(j,i)
    aggregationMethod: count
- GAMSWriter:
    symbols: [{'name':'p2'}]
endEmbeddedCode p2
q2(j,i) = sum(k$p3(i,j,k),1);
abort$(sum((j,i), abs(p2(j,i)-q2(j,i)))>1e-6) 'wrong p2', p2, q2;

put_utility 'log' / 'test parameter projection (implicit first)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'p3'}]
- Projection:
    name: p3(i,j,k)
    newName: p2(j,i)
- GAMSWriter:
    symbols: [{'name':'p2'}]
endEmbeddedCode p2
q2(j,i) = sum(k, p3(i,j,k));
q2('j1','i5') = 0.679854079;
abort$(sum((j,i), abs(p2(j,i)-q2(j,i)))>1e-6) 'wrong p2', p2, q2;

put_utility 'log' / 'test variable projection (implicit first)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'v3'}]
- Projection:
    name: v3(i,j,k)
    newName: v2(j,i)
- GAMSWriter:
    symbols: [{'name':'v2'}]
endEmbeddedCode v2
abort$(sum((j,i), abs(v2.l(j,i)-q2(j,i)))>1e-6) 'wrong v2.l', v2.l, q2;
abort$(sum((j,i)$q2(j,i), abs(v2.scale(j,i)-q2(j,i)))>1e-6) 'wrong v2.scale', v2.scale, q2;

put_utility 'log' / 'test parameter projection (last)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'p3'}]
- Projection:
    name: p3(i,j,k)
    newName: p2(j,i)
    aggregationMethod: last
- GAMSWriter:
    symbols: [{'name':'p2'}]
endEmbeddedCode p2
q2(j,i) = sum(k, p3(i,j,k));
q2('j1','i5') = 0.506588022;
abort$(sum((j,i), abs(p2(j,i)-q2(j,i)))>1e-6) 'wrong p2', p2, q2;

put_utility 'log' / 'test variable projection (last)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'v3'}]
- Projection:
    name: v3(i,j,k)
    newName: v2(j,i)
    aggregationMethod: last
- GAMSWriter:
    symbols: [{'name':'v2'}]
endEmbeddedCode v2
abort$(sum((j,i), abs(v2.l(j,i)-q2(j,i)))>1e-6) 'wrong v2.l', v2.l, q2;
abort$(sum((j,i)$q2(j,i), abs(v2.scale(j,i)-q2(j,i)))>1e-6) 'wrong v2.scale', v2.scale, q2;

put_utility 'log' / 'test parameter projection (sum)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'p3'}]
- Projection:
    name: p3(i,j,k)
    newName: p2(j,i)
    aggregationMethod: sum
- GAMSWriter:
    symbols: [{'name':'p2'}]
endEmbeddedCode p2
q2(j,i) = sum(k, p3(i,j,k));
abort$(sum((j,i), abs(p2(j,i)-q2(j,i)))>1e-6) 'wrong p2', p2, q2;

put_utility 'log' / 'test variable level projection (sum)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'v3'}]
- Projection:
    name: v3.l(i,j,k)
    newName: p2(j,i)
    aggregationMethod: sum
- GAMSWriter:
    symbols: [{'name':'p2'}]
endEmbeddedCode p2
abort$(sum((j,i), abs(p2(j,i)-q2(j,i)))>1e-6) 'wrong p2', p2, q2;

put_utility 'log' / 'test variable scale projection (sum)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'v3'}]
- Projection:
    name: v3.scale(i,j,k)
    newName: p2(j,i)
    aggregationMethod: sum
- GAMSWriter:
    symbols: [{'name':'p2'}]
endEmbeddedCode p2
abort$(sum((j,i), abs(p2(j,i)-q2(j,i)))>1e-6) 'wrong p2', p2, q2;

put_utility 'log' / 'test equation level projection (sum)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'e3'}]
- Projection:
    name: e3.l(i,j,k)
    newName: p2(j,i)
    aggregationMethod: sum
- GAMSWriter:
    symbols: [{'name':'p2'}]
endEmbeddedCode p2
abort$(sum((j,i), abs(p2(j,i)-q2(j,i)))>1e-6) 'wrong p2', p2, q2;

put_utility 'log' / 'test parameter projection to scalar(sum)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'p3'}]
- Projection:
    name: p3(i,j,k)
    newName: p0
    aggregationMethod: sum
- GAMSWriter:
    symbols: [{'name':'p0'}]
endEmbeddedCode p0
q0 = sum((i,j,k), p3(i,j,k));
abort$(abs(p0-q0)>1e-6) 'wrong p0', p0, q0;

put_utility 'log' / 'test variable level projection to scalar (sum)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'v3'}]
- Projection:
    name: v3.l(i,j,k)
    newName: p0
    aggregationMethod: sum
- GAMSWriter:
    symbols: [{'name':'p0'}]
endEmbeddedCode p0
abort$(abs(p0-q0)>1e-6) 'wrong p0', p0, q0;

put_utility 'log' / 'test permutation of set';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'s3'}]
- Projection:
    name: s3(i,j,k)
    newName: t3(k,i,j)
- GAMSWriter:
    symbols: [{'name':'t3'}]
endEmbeddedCode t3
abort$(sum((i,j,k), s3(i,j,k) xor t3(k,i,j))>1e-6) 'wrong t3', s3, t3;

put_utility 'log' / 'test projection of set (implicit first)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'s3'}]
- Projection:
    name: s3(i,j,k)
    newName: s2(j,i)
- GAMSWriter:
    symbols: [{'name':'s2'}]
endEmbeddedCode s2
t2(j,i) = sum(k, s3(i,j,k));
abort$(sum((j,i), s2(j,i) xor t2(j,i))>1e-6) 'wrong t3', s2, t2;

put_utility 'log' / 'test projection of set (implicit first)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'s3'}]
- Projection:
    name: s3(i,j,k)
    newName: s2(j,i)
- GAMSWriter:
    symbols: [{'name':'s2'}]
endEmbeddedCode s2
t2(j,i) = sum(k, s3(i,j,k));
put_utility 'shell.checkErrorLevel' / 'test ' s2.te('j1','i5') ' = First';
abort$(sum((j,i), s2(j,i) xor t2(j,i))>1e-6) 'wrong t3', s2, t2;

put_utility 'log' / 'test projection of set (last)';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'s3'}]
- Projection:
    name: s3(i,j,k)
    newName: s2(j,i)
    aggregationMethod: last
- GAMSWriter:
    symbols:
      - name: s2
        mergeType: replace
endEmbeddedCode s2
put_utility 'shell.checkErrorLevel' / 'test ' s2.te('j1','i5') ' = Last';
abort$(sum((j,i), s2(j,i) xor t2(j,i))>1e-6) 'wrong t3', s2, t2;

set am "aggregation methods" / mad, max, mean, median, min, prod, sem, sum, std, nunique, first, last /;
loop(am,
  put_utility 'log' / 'test aggregation method ' am.tl:0;
  put_utility 'ecarguments' / '--AGGMETHOD=' am.tl:0;
  embeddedCodeV Connect:
  - GAMSReader:
      symbols: [{'name':'p3'}]
  - Projection:
      name: p3(i,j,k)
      newName: p2(j,i)
      aggregationMethod: %AGGMETHOD%
  - GAMSWriter:
      symbols: [{'name':'p2'}]
  endEmbeddedCode p2
)

put_utility 'log' / 'test dropText';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'s3'}]
- Projection:
    name: s3(i,j,k)
    newName: s3noText(j,i)
    dropText: True
- PythonCode:
    code: |
      expected = [
        ('j1', 'i3', ''), ('j1', 'i4', ''), ('j1', 'i5', ''),
        ('j2', 'i1', ''), ('j2', 'i3', ''), ('j2', 'i4', ''),
        ('j3', 'i3', ''), ('j4', 'i2', ''),
        ('j4', 'i3', ''), ('j4', 'i5', ''),
        ('j5', 'i4', '')
      ]
      data = [(*r.keys, r.text) for r in connect.db['s3notext']]
      if data != expected:
        raise Exception("Unexpected Data")
endEmbeddedCode

put_utility 'log' / 'test asSet';
embeddedCode Connect:
- GAMSReader:
    symbols: [{'name':'s3'}, {'name':'p3'}, {'name':'v3'}, {'name':'e3'}]
- Projection:
    name: s3(i,j,k)
    newName: s2_p1(i)
- Projection:
    name: s3(i,j,k)
    newName: s2_p2(i)
    dropText: True
- Projection:
    name: p3(i,j,k)
    newName: s2_p3(i)
    asSet: True
- Projection:
    name: v3(i,j,k)
    newName: s2_p4(i)
    asSet: True
- Projection:
    name: v3.l(i,j,k)
    newName: s2_p5(i)
    asSet: True
- Projection:
    name: e3(i,j,k)
    newName: s2_p6(i)
    asSet: True
- Projection:
    name: e3.l(i,j,k)
    newName: s2_p7(i)
    asSet: True
- PythonCode:
    code: |
      expected_p1 = [('i1', ''), ('i2', ''), ('i3', ''), ('i4', ''), ('i5', 'First')]
      expected_p2 = [('i1', ''), ('i2', ''), ('i3', ''), ('i4', ''), ('i5', '')]
      expected_p3 = [('i1', ''), ('i2', ''), ('i3', ''), ('i4', ''), ('i5', '')]
      expected_p4 = [('i1', ''), ('i2', ''), ('i3', ''), ('i4', ''), ('i5', '')]
      expected_p5 = [('i1', ''), ('i2', ''), ('i3', ''), ('i4', ''), ('i5', '')]
      expected_p6 = [('i1', ''), ('i2', ''), ('i3', ''), ('i4', ''), ('i5', '')]
      expected_p7 = [('i1', ''), ('i2', ''), ('i3', ''), ('i4', ''), ('i5', '')]
      data = [(*r.keys, r.text) for r in connect.db['s2_p1']]
      if data != expected_p1:
        raise Exception("Unexpected Data")
      data = [(*r.keys, r.text) for r in connect.db['s2_p2']]
      if data != expected_p2:
        raise Exception("Unexpected Data")
      data = [(*r.keys, r.text) for r in connect.db['s2_p3']]
      if data != expected_p3:
        raise Exception("Unexpected Data")
      data = [(*r.keys, r.text) for r in connect.db['s2_p4']]
      if data != expected_p4:
        raise Exception("Unexpected Data")
      data = [(*r.keys, r.text) for r in connect.db['s2_p5']]
      if data != expected_p5:
        raise Exception("Unexpected Data")
      data = [(*r.keys, r.text) for r in connect.db['s2_p6']]
      if data != expected_p6:
        raise Exception("Unexpected Data")
      data = [(*r.keys, r.text) for r in connect.db['s2_p7']]
      if data != expected_p7:
        raise Exception("Unexpected Data")
endEmbeddedCode