Wednesday, July 9, 2008

Ordering of set elements in GAMS

> I don't understand the ordering of set elements
> in my display of a set.


If you look at:

set
i /a,c/
j /b,c/
;
display j;

then this will display:

---- 5 SET j
c, b


To understand this you'll need a little bit of knowledge how GAMS stores and orders set elements. Set elements are unique and stored in single large pool. They are ordered as they come. To display the pool (a.k.a universe) you can use:

alias (pool,*);
display pool;

This will show:

---- 8 SET pool Aliased with *
a, c, b

The ordering of elements (e.g. in display statements) is determined how they are ordered in the pool. It is also important in determining if a set is ordered (in that case the set follows the same ordering as the universe).

This can lead to surprises:

set
t1 /2001*2005/
t2 /2000*2006/;
display t2;

shows:

---- 5 SET t2
2001, 2002, 2003, 2004, 2005, 2000, 2006


A quick fix is to start with a dummy set to get the ordering correct:

set
dummy /2000*2010/
t1 /2001*2005/
t2 /2000*2006/;
display t2;