Reverse loops in GAMS

Frequently asked questions about GAMS

Moderator: aileen

Forum rules
Please ask questions in the other sub-forums
Locked
aileen
User
User
Posts: 136
Joined: 3 years ago

Reverse loops in GAMS

Post by aileen »

Is it possible in a LOOP statement of GAMS to execute BACKWARD RECURSIVE statements, i.e. to traverse the members of the driving set of the loop in the reverse order?
aileen
User
User
Posts: 136
Joined: 3 years ago

Re: Reverse loops in GAMS

Post by aileen »

There is no syntax designed especially for this, so you will want to avoid doing this if possible. If you must use a reverse loop, here is an example of how one can set this up:

Code: Select all

set i / 1 * 10 /;
alias (i,ri);
set revi(i,ri);
* The idea is to populate the sw-ne diagonal of revi.
* Then looping through i in forward order gives us
* i in reverse order by taking the second index of revi.
* The follwing assignment would create revi, but it is very slow, o(n^2)!
* revi(i,ri)$(ord(i) + ord(ri) eq card(i)+1) = yes;
*
* This formulation is much faster, o(n).
revi(i,i+[card(i)-2*ord(i)+1]) = yes;

parameter c(i);
c(i) = uniform(0,1);
file fp / reverse.out /;
put fp;
loop (revi(i,ri),
      put ri.tl, c(ri)/;
     );
put /;
The put file shows:

Code: Select all

10                  0.50
9                   0.07
8                   0.86
7                   0.35
6                   0.22
5                   0.29
4                   0.30
3                   0.55
2                   0.84
1                   0.17
Just a note on efficiency: The loop over revi goes very fast, since we are taking the first index, i, in order, and each row of revi has only one element. However, accessing c(ri) in reverse order is not so efficient, and may be a problem for very large i. To see this work, increase the dimension of set i, run the model using profiling and analyze what happens if you replace the “slow”

Code: Select all

put ri.tl, c(ri)/;

by

Code: Select all

put i.tl, c(i)/;
Locked