single04.gms : Check handling of singleton sets assigned and referenced in a loop

Description

This test checks that the singleton sets can be used as expected, especially if
they are both refernced and assigned inside a loop. Note that there is more than
the assignment statement to do the later: clear, kill, projection, matching,
execute_load, ...

Contributor: Lutz Westermann, February 2015


Small Model of Type : GAMS


Category : GAMS Test library


Main file : single04.gms

$title 'Check handling of singleton sets assigned and referenced in a loop' (SINGLE04,SEQ=677)

$ontext
This test checks that the singleton sets can be used as expected, especially if
they are both refernced and assigned inside a loop. Note that there is more than
the assignment statement to do the later: clear, kill, projection, matching,
execute_load, ...

Contributor: Lutz Westermann, February 2015
$offtext

set i /i1*i3/
    j /j1*j3/
    k /k1*k3/
    l /l1*l3/;

singleton set si(i)    /i2/
              sj(j)    /j3/
              sk(k)    /k2/
              sl(l)    /l2/
              sij(i,j) /i2.j2/
              sjk(j,k) /j2.k2/
              skl(k,l) /k2.l2/ ;

parameter pi(i)
          pii(i,i)
          pij(i,j)
          pijk(i,j,k)
          pijkl(i,j,k,l);

alias (i,a);
pi(i)          =      ord(i);
pij(i,j)       = 10  *ord(i)+    ord(j);
pii(i,a)       = 10  *ord(i)+    ord(a);
pijk(i,j,k)    = 100 *ord(i)+10 *ord(j)+   ord(k);
pijkl(i,j,k,l) = 1000*ord(i)+100*ord(j)+10*ord(k)+ord(l);

scalar x,y,z;

loop(i,
  x = sum(j,pij(si,j));
  y = sum(j,sum(si,pij(si,j)));
);
abort$(x<>y) 'Unexpected difference',x,y;

loop(i,
  x = sum(j,pij(si,j));
  y = sum(j,sum(si,pij(si,j)));
  si(i)=yes;
);
abort$(x<>y) 'Unexpected difference',x,y;

loop(i,
  x = sum(j,pij(si,j));
  y = sum(j,sum(si,pij(si,j)));
  option clear=si;
);
abort$(x<>y) 'Unexpected difference',x,y;

si('i3') = yes;

loop(i,
  x = sum(j,pij(si,j));
  y = sum(j,sum(si,pij(si,j)));
  option kill=si;
);
abort$(x<>y) 'Unexpected difference',x,y;

option StrictSingleton=0;
loop(i,
  option si<pii;
  x = sum(j,pij(si,j));
  y = sum(j,sum(si,pij(si,j)));
);
abort$(x<>y) 'Unexpected difference',x,y;

loop(i,
  option si<=pii;
  x = sum(j,pij(si,j));
  y = sum(j,sum(si,pij(si,j)));
);
abort$(x<>y) 'Unexpected difference',x,y;

loop(i,
  option sij(i:j);
  x = pij(sij);
  y = sum(sij,pij(sij));
);
abort$(x<>y) 'Unexpected difference',x,y;

$onEcho > x.gms
Set           j  / j1*j3/
Singleton Set sj / j2   /;
$offecho

$call gams x.gms lo=%GAMS.lo% gdx x
$if errorlevel 1 $abort Error running x.gms

loop(i,
  x = pij(si,sj);
  y = sum((si,sj),pij(si,sj));
  execute_load 'x.gdx', sj;
);
abort$(x<>y) 'Unexpected difference',x,y;


Singleton Set ii(i) / /;
x = 0;
loop(i,
  x$sameas(i,ii)=x+1;
  ii('i2') = yes;
);
y = sum(i$sameas(i,'i2'),1);
abort$(x<>y) 'Unexpected difference',x,y;

x = 0;
y = 0;
loop((i,j),
    if(sameas(i,'i2'), sj('j1') = yes; );
    if(sameas(j,'j2'), sj(j)    = yes; );

    x$sameas(j, sj) = x+1;
    loop(sj,
        if(sameas(j, sj), y = y+1);
    );
);
abort$(x<>y) 'Unexpected difference',x,y;