Get the element in a set for a given index

Problems with syntax of GAMS
Post Reply
rookie
User
User
Posts: 5
Joined: 2 years ago

Get the element in a set for a given index

Post by rookie »

Is it possible to get the element of a set for a given index?

I would like to take this minimum: x(k) = min{j: y(j) >= k}

Code: Select all

Sets
 k /k1, k2/
 j /j1, j2/;
 
Parameter 
y(j)
aux
x(k);

loop(k,
    aux = smin(j$(y(j) ge ord(k)), y(j));
    x(k)=smin(j$(y(j)=aux), ord(j));
);
But this returns x(k1)=1 and x(k2) = 2, which I suppose are the indices for j1 and j2. Is there any way to get x(k1)=j1 and x(k2) = j2 ???

Thanks
User avatar
bussieck
Moderator
Moderator
Posts: 1033
Joined: 7 years ago

Re: Get the element in a set for a given index

Post by bussieck »

GAMS parameters have numerical values, not indexes. Such constructs are done with multidimensional (dynamic) sets, like this:

Code: Select all

Sets
 k /k1, k2/
 j /j1, j2/;
 
Parameter 
y(j) / j1 1, j2 2 /,aux,cont;
set x(k,j);
loop(k,
    aux = smin(j$(y(j) ge ord(k)), y(j));
    cont=1; loop(j$(y(j)=aux), x(k,j) = yes; cont=0);
);
display x;
-Michael
rookie
User
User
Posts: 5
Joined: 2 years ago

Re: Get the element in a set for a given index

Post by rookie »

Great, thanks!
Post Reply