Referencing n-th element of a subset

Problems with modeling
Post Reply
Roving2416
User
User
Posts: 2
Joined: 1 year ago

Referencing n-th element of a subset

Post by Roving2416 »

Hi everyone, I am having trouble referencing the n-th element of a subset. One example is the following:

Set
i /a1*a20/
ii(i) /a2, a7, a13, a15, a18/
;

I want to write a constraint for ii = {a7,a13,a15} which starts from the 2nd element of ii to the 2nd to last element.

Is there a syntax on GAMS for this purpose?

Thank you!
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Referencing n-th element of a subset

Post by bussieck »

.pos (https://www.gams.com/41/docs/UG_SetDefi ... Attributes) does the trick (even for dynamic sets):

Code: Select all

Set
i /a1*a20/
ii(i) /a2, a7, a13, a15, a18/
iii(i) '2nd element of ii to the 2nd to last element'
;
iii(ii) = ii.pos>=2 and ii.pos<=card(ii)-1;
display iii;
this gives you:

Code: Select all

----      7 SET iii  2nd element of ii to the 2nd to last element
a7 ,    a13,    a15
-Michael
Roving2416
User
User
Posts: 2
Joined: 1 year ago

Re: Referencing n-th element of a subset

Post by Roving2416 »

Thank you, Michael. This solution works for a set with one index. What if we have sets with 2 indices.

Code: Select all

Set
i /a1*a20/
n /n1, n2/
ii(n, i) /n1.a2, n1.a7, n1.a13, n1.a15, n1.a18, n2.a3, n2.a5, n2.a20/
iii(n, i) '2nd element of ii to the 2nd to last element'
;
In this case, how do I get iii('n2', i) -- 2nd element of ii to the 2nd to last element when n=n2?
Post Reply