Hello,
In my gams code, by using loops I have iterations and I have a set p which has different elements in each iteration.
p(i) = y.l(i) ;
I define the set p as above, I have a binary variable y and at each iteration 3 of them take the value 1 so I create my set p according to these binary variables.
i.e.
p=(65,75,82) at 1st iteration
p=(22,35,95) at 2nd iteration...
my aim is to select only a single element from this set p at each iteration, I am trying to create a subset of p which only contains a single element from it.
but unfortunately I am not able to.
I am waiting for your help
thanks
Any element from a set, urgent

 User
 Posts: 6
 Joined: 1 year ago
Re: Any element from a set, urgent
Hi,
If i am understand it corrctly, you want to get a single any single value (let's say frist) from the parameter p that contains three values. If this is the case then you may try this:
set i /1*3/;
** here i assume that p has only 3 three elements
parameter p1
j;
loop(i$(j=0),
p1 (i)=p(i);
j=1$(p1(i) ge 1);
);
Now p1 will contain the first element of the p.
Hopefully, it will help you.
If i am understand it corrctly, you want to get a single any single value (let's say frist) from the parameter p that contains three values. If this is the case then you may try this:
set i /1*3/;
** here i assume that p has only 3 three elements
parameter p1
j;
loop(i$(j=0),
p1 (i)=p(i);
j=1$(p1(i) ge 1);
);
Now p1 will contain the first element of the p.
Hopefully, it will help you.

 User
 Posts: 2
 Joined: 1 week ago
Re: Any element from a set, urgent
Hi Hussain,
Firstly thank you very much for your answer
In fact I am not trying to obtain the value of an element.
I am working on a network, assume there are 100 nodes.
Set P contains 3 of these nodes say 15,17,65
I need to send a flow from one of these nodes so in fact I need to create again a set i.e. P1 not a parameter
P1 will contain one of the elements of P
It does not matter if it is 15 or 17 or 65
and when I write my constraint I want to say that for example i$P1 so that flow will go out from the single node which is in the set P1
maybe there may be other ways to overcome this
Firstly thank you very much for your answer
In fact I am not trying to obtain the value of an element.
I am working on a network, assume there are 100 nodes.
Set P contains 3 of these nodes say 15,17,65
I need to send a flow from one of these nodes so in fact I need to create again a set i.e. P1 not a parameter
P1 will contain one of the elements of P
It does not matter if it is 15 or 17 or 65
and when I write my constraint I want to say that for example i$P1 so that flow will go out from the single node which is in the set P1
maybe there may be other ways to overcome this

 User
 Posts: 6
 Joined: 1 year ago
Re: Any element from a set, urgent
Hi,
As far as i know, set need to be defined in advanced. You can also use subset concept e.g, P can be a super set while P1 can a subset. The issue you raised may be overcomes suing $ operator in constraint.
As far as i know, set need to be defined in advanced. You can also use subset concept e.g, P can be a super set while P1 can a subset. The issue you raised may be overcomes suing $ operator in constraint.
Re: Any element from a set, urgent
Hi betulkayisoglu,
It seems that p(i) is already a subset of superset p. You may want to introduce another subset pp(i) that selects only a single element from this set p at each iteration. Given that, pp(i) can be declared as singleton set (https://www.gams.com/latest/docs/UG_Set ... gletonSets). Since you can pick any element of p to be the single element in pp, you can for example just pick the first one using the corresponding set attribute .first (https://www.gams.com/latest/docs/UG_Set ... Attributes) .
I hope the following example illustrates how to do this.
Best,
Fred
It seems that p(i) is already a subset of superset p. You may want to introduce another subset pp(i) that selects only a single element from this set p at each iteration. Given that, pp(i) can be declared as singleton set (https://www.gams.com/latest/docs/UG_Set ... gletonSets). Since you can pick any element of p to be the single element in pp, you can for example just pick the first one using the corresponding set attribute .first (https://www.gams.com/latest/docs/UG_Set ... Attributes) .
I hope the following example illustrates how to do this.
Code: Select all
set i / 1*100 /
p(i)
singleton set pp(i);
p('65') = yes;
p('75') = yes;
p('82') = yes;
pp(p) = p.first;
display p,pp;
option clear=p;
p('22') = yes;
p('35') = yes;
p('95') = yes;
pp(p) = p.first;
display p,pp;
Fred