Set of set with unkown size

12 views
Skip to first unread message

mohammad torabi

unread,
Jun 23, 2016, 10:44:53 AM6/23/16
to AMPL Modeling Language
Hi,

I need to add K variables in the set based on the one of them: For example, suppose I have a set  K_set = (K1,K2,K3,K4,K5) , (K2,K3,K5,K7) ,  (K3,K1,K4)  where K variables are integer and I need the following results is needed:



if (first element of outer set ==  K1)
{
..... do something on the rest of the inner set like K2,K3,K4,K5
}
else
if (first element of outer set ==  K2)
{
..... do something on the rest of the set like K3,K5,K7
}
else
if (first element of outer set ==  K3)
{
... do something on the rest of the set like K1,K4
}

Robert Fourer

unread,
Jun 25, 2016, 12:21:50 PM6/25/16
to am...@googlegroups.com
There isn't a concept of a "set of variables" in AMPL, though there can be a collection of variables indexed over a set. If you want to define a set of integers and then do something with a collection of variables depending on the first member of that set, you can try an approach like this:

param N > 0;
set Kset within 1..N ordered;
var K {1..N} >= 0;

if first(Kset) = 1 then
for {i in Kset diff {1}}
{... operate on K[i] ...}

else if first(Kset) = 2 then
for {i in Kset diff {1}}
{... operate on K[i] ...}

else if first(Kset) = 3 then ...

Bob Fourer
am...@googlegroups.com

=======
Reply all
Reply to author
Forward
0 new messages