在游戏中迭代多维集
我想在游戏中使用一组张量。我的想法是将它们表示如下(因为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对你有用吗(这实际上是不可行的,但希望能够展示一个对类似的东西进行建模的想法)?
Does this work for you (this is actually infeasible but could hopefully show an idea to model something like this)?