Checking if an element is part of a set Topic is solved

Problems with modeling
Post Reply
MES
User
User
Posts: 14
Joined: 4 years ago

Checking if an element is part of a set

Post by MES »

I want to check if an set element is part of another set. I wrote it here into the code as a bold highlighted text, to show where the code should be.
I tried some different things, but none worked. Does one of you have an idea how it could work?
Notice that the set "bearbeitet" is a dynamic set and only the here relevant part of the code is shown.

Code: Select all

set i /some elements/;
set bearbeitet /some elements/;
binary variable hilf;
hilf=1;

loop (i, 
                                     if ([b]i is not element of bearbeitet[/b],
                                         hilf.l=0;
                                         break;
                                     );
                                     
*       ... some further stuff, if hilf.l<>0 ...
       
       );
User avatar
bussieck
Moderator
Moderator
Posts: 1042
Joined: 7 years ago

Re: Checking if an element is part of a set

Post by bussieck »

The best way to resolve this is a proper set hierarchy. So if you need the question is element i of set s also member of sp sets s and sp either or subsets of another or they share a common superset:

Code: Select all

set ss / 1*10 /
set s(ss) /2,4,6,8 /, sp(ss) / 1*5 /;
alias (i,ss);
loop(s(i),
  if (sp(i), put_utility 'log' / i.tl:4 ' is member of s and sp');
);
There are ways to check if there is a set element of the same name is member in a completely unrelated set, this looks ugly by design, because in GAMS it is of utmost importance to get your set hierarchy in shape:

Code: Select all

set ss / 1*10 /
set s /2,4,6,8 /, sp / 1*5 /;
loop(s,
  if (sum(sameas(s,sp),1), put_utility 'log' / s.tl:4 ' is member of s and sp');
);

-Michael
MES
User
User
Posts: 14
Joined: 4 years ago

Re: Checking if an element is part of a set

Post by MES »

Thank you very much!

I tried, among others, the syntax if(i$sp(i), ...

I didn´t knew that it just has to be if(sp(i),..., when i is allready a fixed value because of the loop.
Post Reply