在游戏中迭代多维集

发布于 2025-01-15 03:20:35 字数 1246 浏览 1 评论 0原文

我想在游戏中使用一组张量。我的想法是将它们表示如下(因为 gams 中没有一对一的张量集):

假设我有一个如下所示的张量集: { [[1,2],[3, 4]], [[5,6],[7,8]] } (即包含每个维度为 2:2 的张量的集合)。

我的想法是使用 gams 提供的多维集合,其中第一个索引是集合中张量的索引,后面的索引是张量中条目的索引。对于上面的设置,我的游戏设置如下所示,

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/;

看起来有点臃肿,但我没有想出更好的解决方案。

现在有什么方法可以迭代这个集合(在索引方程中),但只有张量,而不是每个条目?

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

应该可以使用我们正在迭代的条目作为值或索引。

我将用伪代码展示一个示例。

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).

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2025-01-22 03:20:36

这对你有用吗(这实际上是不可行的,但希望能够展示一个对类似的东西进行建模的想法)?

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文