tensorgams-math

Iterating multidimensional sets in gams


I want to use a set of tensors in gams. My idea was to represent them as follows (because there are no 1-to-1 tensor sets in gams):

Lets say I have a tensor set which looks as follows: { [[1,2],[3,4]], [[5,6],[7,8]] } (i.e. a set containing to tensor with the dimension 2:2 each).

My idea was using the multidimensional sets gams provides, where the first index is the index of the tensor in the set and the following indices are the indices of the entries in the tensor. For the set above, my gams set would look as follows

Set S /1.1.1 =1,1.1.2 =2,1.2.1 =3,1.2.2 =4, 2.1.1 =5,2.1.2 =6,2.2.1 =7,2.2.2 =8/;

which seems a little bloated, but I did not come up with a better solution.

Is there now any way that I can iterate above this set (in an indexed equation), but only the tensors, not every entry?

equation eq; eq(S).. x =g= (??);

It should be possible to use the entries, over which we are iterating, either as value or as an index.

I will show an example in pseudo code.

Variable x in [0, 10]; # variable value to be optimized, between 0 and 10
Set S := { ((1,2),(3,4)), ((5,6),(7,8)) }; # set containing two Tensors of the dimension 2:2 each
Tensor T := (1,2,3,4,5,6); # tensor / vector containing 6 entries

forall i in S: # iterating over every tensor in S
    i[1,1] <= x; # x has to be larger than the entry of index 1,1 of every tensor in S (in this instance 1 and 5)

forall j in S: # iterating over every tensor in S
   T[ j[1,2] ] <= x; # x has to be larger than the entry of T with the index that equals the entry of index 1,2 of every tensor in S (the indices of T in this instance are 2 and 6, the values of the picked entries of T are therefore 2 and 6).

Solution

  • Does this work for you (this is actually infeasible but could hopefully show an idea to model something like this)?

    Alias (*,a,b,c,T);
    Set ST(a,b,c,T) /1.(1.(1.1,
                           2.2),
                        2.(1.3,
                           2.4)),
                     2.(1.(1.5,
                           2.6),
                        2.(1.7,
                           2.8))/
         S(a,b,c);
    * Project ST into S
    option S<ST;
    
    Parameter K (a) /1=1,2=2/;
    Parameter Te(T) /1=1,2=2,3=3,4=4,5=5,6=6/; 
    
    Variable x; x.up = 10;
    
    equation eq1(a);
    eq1(a)$S[a,'1','1'].. x =g= K(a);
    
    equation eq2(a);
    eq2(a).. x =g= sum(ST(a,b,c,T)$(sameas(b,'1') and sameas(c,'2')),Te(T));
    
    Model dummy /all/;
    solve dummy min x use lp;