0

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).
wittn
  • 298
  • 5
  • 16

1 Answers1

1

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;
Lutz
  • 2,197
  • 1
  • 10
  • 12
  • This seems like a great start, when using the tensor entries as value rather then indices. But I would like to do the latter as well (I have added an example to my question), do you suppose this is possible at all? – wittn Mar 18 '22 at 16:14
  • That is not completely clear to me. In your last code snippet, you have `x =g= (S[*,'1','1']);`. If `S` should be a Set and not a Parameter, what should the =g= mean here? Greater than what? We need some value here. – Lutz Mar 18 '22 at 16:45
  • In the last snippet, your solution works. Please look at the new snippet above, were I describe the intended usage as an index of a parameter. – wittn Mar 18 '22 at 19:34
  • I updated the example in my reply – Lutz Mar 21 '22 at 08:24
  • I think unfortunaly no quite, as the set does not contain the actual values of the tensor entries, if I am interpreting this right. I apologize for having a hard time to clearly presenting the problem; I have tried again to update my question and have inserted an example. – wittn Mar 21 '22 at 14:43
  • I updated the example again. Hopefully, that provides some ideas how to model something you want to do (I cannot claim, that I understood your request completley... ;) ) – Lutz Mar 22 '22 at 09:48
  • The issue is still, that for the second equation the iterating parameter is still not used as an index for the tensor (as can also been seen that in your solution x is minimum 11 and in my example its minimum 6). I guess then probably cannot be modeled in gams at all.. (I am working on converting an optimization problem from another language to gams, this why this is needed). – wittn Mar 24 '22 at 07:55
  • Just saw, that my eq2 was actually wrong. I updated my reply. Now the solution is 6. Is that any better? – Lutz Mar 24 '22 at 08:10
  • I think that looks very comprising, thank you a lot. Would you mind providing a small explanation (or a link to the corresponding doc) about what exactly you did in eq2, I am especially a little bit confused about why you projected ST into S and about the sum in eq2. – wittn Mar 24 '22 at 09:08
  • I did the projection from ST into S to have the same database for eq1 and eq2. S is used in eq1 only, so for eq2 S is not relevant at all. – Lutz Mar 24 '22 at 10:18
  • In eq2 I/you want one restriction for each `a` and that should depend on certain `T`s matched to each `a` with fixed elements for `b` and `c`. `ST` is the overall mapping between `a`, `b`, `c` and `T`. The sum over this mapping makes sure to pick the right `T` for the given `a` while the $-sameAs condition ensures to fix `b` and `c` at certain records. – Lutz Mar 24 '22 at 10:22