Friday, June 25, 2010

GAMS augmenting a set

Ina project we encountered the problem where we loaded a GDX file and subsequently wanted to augment one of the sets in that GDX file with some elements. The following trick will do this:

set i(*);
$gdxin data.gdx
$load i
* i has now elements a,b

$onmulti
set i /c/;
$offmulti
* i has now elements a,b,c

parameter p(i) /
 
a 1
 
b 2
 
c 3
/;
display i,p;

In the GDX file the set i has elements a and b. In the GAMS code we add the element c. This is not very intuitive, but it works. The advantage of this method is that we do this at compile time, i.e. we can declare parameters and variables over the set i. Note that we cannot use DISPLAY to debug this:

set i(*);
$gdxin data.gdx
$load i

display i;

$onmulti
set i /c/;
$offmulti

display i;

This code will show the same output for both display statements:

----      5 SET i 

a,    b,    c

----     11 SET i 

a,    b,    c

This is because the display statement is executed at execution time while the sets are formed at compile time.