Create macro/loop in GAMS

164 views Asked by At

Consider the following set and parameter in GAMS:

set i  / 1,3 /
    j  / j1,j2,j3 /;

parameter stock(i,j);

Consider I want to put a specific value in the parameter:

stock("1","j1") = 10;
stock("3","j3") = 10;

Instead of writing that in two lines, can I use macro, $setGlobal, loop (or similar) to first (1) specify which set it should do it for, and (2) secondly only writing the equation in one line?

I can create a subset:

set subset_i(i) / 1,3/
    subset_j(j) / j1,j3 /;

And then loop through these subset:

loop(subset_i,
   loop(subset_j,
      stock(i,j) = 10;
   );
);

But then I get the value for each combination:

1   j1  10
1   j2  10
1   j3  10
3   j1  10
3   j2  10
3   j3  10

I only want:

1   j1  10
3   j3  10

How do I do that?

1

There are 1 answers

3
Lutz On BEST ANSWER

Is it exactly the diagonal (1-j1; 2-j2), you want to set? Or was that just a random example, and the mapping could also be another one? If it is the diagonal, you can do it like this:

set i  / 1,2 /
    j  / j1,j2 /;

parameter stock(i,j);

stock(i,j)$(ord(i)=ord(j)) = 10;

display stock;

EDIT (based on the comments/the edited question):

One can also define a more general mapping explicitly, if it is not just about the diagonal. This could be done like this:

set i        / 1,  3      /
    j        / j1, j2, j3 /
    map(i,j) / 1.j1, 3.j3 /;

parameter stock(i,j);

stock(map(i,j)) = 10;

display stock;