CSCalcInverse.gms : Test expert level C# API to read and write indexed GDX


Contributor: Lutz Westermann, December 2014

Small Model of Type : .Net

Category : GAMS API library

Main file : CSCalcInverse.gms

$TITLE 'Test expert level C# API to read and write indexed GDX' (CSEX1,SEQ=46)

Contributor: Lutz Westermann, December 2014

$set srcRoot %gams.sysdir%apifiles%system.dirsep%CSharp%system.dirsep%

$escape &
$if %system.filesys% == UNIX
$if %sysenv.MPATH% == %&sysenv.MPATH%& $abort.noerror Need Mono to run this on Unix

$                             set COMP csc.exe
$if %system.filesys% == UNIX $set COMP $MPATH/dmcs
$                             set EXEC
$if %system.filesys% == UNIX $set EXEC $MPATH/mono

$if %system.buildcode% == VS8
$if not set flags $set flags /platform:x86
$if %system.buildcode% == WEI
$if not set flags $set flags /platform:x64
$if %system.buildcode% == LEG
$if not set flags $set flags
$if %system.buildcode% == DEG
$if not set flags $set flags /define:__APPLE__

*$call %BUILD% %flags% "%srcRoot%xp_CalcInverseIDX%system.dirsep%xp_CalcInverseIDX.csproj"
*$if errorlevel 1 $abort 'Problem compiling xp_CalcInverseIDX'
$call %COMP% %flags% -out:xp_CalcInverseIDX.exe "%srcRoot%xp_CalcInverseIDX%system.dirsep%xp_CalcInverseIDX.cs" "%srcRoot%xp_CalcInverseIDX%system.dirsep%InvertMatrix.cs" "%srcRoot%api%system.dirsep%idxcs.cs" "%srcRoot%api%system.dirsep%gamsglobals.cs"
$if errorlevel 1 $abort 'Problem compiling xp_CalcInverseIDX.cs'

$if not set size $set size 50
Set i / i1*i%size% /, j / j1*j%size% /;
Parameter ij(i,j), ji(j,i);
ij(i,j)$(uniform(0,1)<0.1) = normal(1,0.1);

$eval iCnt card(i)
$eval jCnt card(j)

Set iIdx / 1*%iCnt% /
    jIdx / 1*%jCnt% /
    iMap(i,iIdx) / #i:#iIdx /
    jMap(j,jIdx) / #j:#jIdx /;
Parameter ijIdx(iIdx,jIdx)
* Three ways to compute ijIdx from ij
* loop((iMap(i,iIdx),jMap(j,jIdx))$ij(i,j), ijIdx(iIdx,jIdx) = ij(i,j));
* ijIdx(iIdx,jIdx) = sum((iMap(i,iIdx),jMap(j,jIdx)), ij(i,j));
loop((i,j)$(sameas('i1',i) and sameas('j1',j)),
   ijIdx(iIdx,jIdx) = ij(i + (ord(iIdx) - 1), j + (ord(jIdx) - 1)));

execute_unloadIdx 'sparse_idx.gdx',ijIdx;
execute '%EXEC% xp_CalcInverseIDX.exe "%gams.sysdir%." sparse_idx.gdx';
abort$errorlevel 'problems running xp_CalcInverseIDX';
execute_load 'inverse_idx.gdx', jiIdx;

* Three ways to compute ji from jiIdx
* loop((jMap(j,jIdx),iMap(i,iIdx))$jiIdx(jIdx,iIdx), ji(j,i) = jiIdx(jIdx,iIdx));
* ji(j,i) = sum((jMap(j,jIdx),iMap(i,iIdx)), jiIdx(jIdx,iIdx));
loop((iIdx,jIdx)$(sameas('1',iIdx) and sameas('1',jIdx)),
   ji(j,i) = jiIdx(jIdx + (ord(j) - 1), iIdx + (ord(i) - 1)));

* Test that ji is inverse of ij
parameter id(i,i) calculated identiy matrix;
alias (i,ip);
id(i,ip) = round(sum(j,ij(i,j)*ji(j,ip)),12);
abort$(not(card(id)=card(i) and sum(i, id(i,i))=card(i))) 'ji is not inverse of ij';