AMPL variable size set iteration

て烟熏妆下的殇ゞ 提交于 2019-12-13 18:11:50

问题


I'm learning AMPL so that I can use it some time later in my programs. I have a small question though that I couldn't find its answer yet.

Suppose I have a set, this set will contain some subsets, those subsets may differ in their dimensions. For example:

set x:= (a,b,c) (a,c) (d,t,r,e,s);

and so on ..

Now I want to write a constraint which will have to deal with all elements in each subset (similar to a loop that will iterate between a,b, and c in the first subset, a and c in the second and so on). The problem is that we don't know the size of each so we can't use the following:

set x dimen 3;
subject to constraint {(i,j,k) in x}: "some constraint";
data;
set x:= (a,b,c) (a,c) (d,t,r,e,s);

Is there a way to do so? If yes, can you please provide me with some sample code or links to learn from? Thanks in advance. Your help is appreciated :D


回答1:


You can have an indexed set in AMPL which is kind of "set of sets", but all elements of it should have the same dimension:

set S;
set T{S} dimen 3; # indexed set with all elements of dimension 3 (triples).

However, you can achieve the same effect with parameters:

set V ordered;
param values{V} symbolic;

# Indices in values where each subset starts.
set Indices ordered;

data;
param:
V: values :=
 1 a
 2 b
 3 c
 4 a
 5 c
 6 d
 7 t
 8 r
 9 e
10 s;

set Indices := 1 4 6 11;

print {i in Indices: i != last(Indices)}: {j in i .. next(i) - 1} values[j];

Running this code will print

a b c
a c
d t r e s


来源:https://stackoverflow.com/questions/24727818/ampl-variable-size-set-iteration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!