Page 1 of 1

### Loops over sets and conditions

Posted: Sat Feb 03, 2018 11:15 pm
Hi everyone,

I am working on the following problem but can't get my head around it:
I have a set, say
Sets
i "units" / A, B, C, D, E, F, ... /
;
This set may have many units (100+). I now search for the subsets of this set that includes all units which together fulfill certain conditions. My thought was to, write a loop, start with e.g. unit A, then use another loop that adds units one at a time in every conceivable way, and for every unit added check a statement that says 'if all units together in this new set fulfill a certain condition then keep these units in the set and add another unit'. I just cannot seem to figure out how to write the loops properly so that GAMS goes over all potential combinations of units. Would anyone be able to point me at some literature or give me an idea how to set this up? Thanks indeed!

### Re: Loop over sets with conditions

Posted: Mon Feb 05, 2018 9:31 am
Hi GamsisGreat ( Here a small example:
set i Overall set /i1*i200/,
is(i) Dynamic set based on certain conditions;

parameter testcond(i);

testcond(i) = uniform(0,1);

is(i)\$(testcond(i) > 0.5) = YES;

display is;
Hope this helps

Cheers
Renger

### Re: Loop over sets with conditions

Posted: Mon Feb 05, 2018 3:47 pm
Thanks for this quick reply. My question may have been not clear enough. The subset itself needs to fulfill additional conditions. If it fulfills these conditions, I add another member of the original set, and then I need to check again if the joint conditions are fulfilled. I keep on adding until either all original set members are in this new set, or until the new set contains a member that does not make all members fulfill this condition. I need to check all conceivable combinations of members of the original set. Something like this:

I have a set of three members, A, B and C. Assume F and G are general functions. What I need to get is something like this:

Start with F(A) and G(B,C). If F and G fulfill a certain condition then make a note (e.g. write A in F and B and C in G). Then continue. If F and G do not fulfill this condition, simply continue.
Continue with F(A,B) and G(C). If these fulfill a condition, make a note (e.g. write A and B in F and C in G).
Continue with F(A,B,C) and G(0)...
Continue with F(B) and G(A,C)...
then check conditions for F(B,C) and G(A)...
then for F(C) and F(A,B)...
then for F(0) and F(A,B,C)...
then for F(A,C) and G(B)...
etc...
so go through all possible combinations, which lead to different conditions for F and G, note down which combinations fulfill the conditions. I could do it manually, but the problem is that the set may contain a very large number of members (100+). I hope this explanation is a bit better than the previous one.

### Re: Loop over sets with conditions

Posted: Mon Feb 05, 2018 4:34 pm
Hi
sorry, my mistake. Can you send some code with the restrictions so it becomes more clear?
Cheers
Renger

### Re: Loop over sets with conditions

Posted: Tue Feb 06, 2018 9:50 am
Dear Renger,
great, thanks so much i see the issue now. Thanks for the answer to "loop over parameters". Unfortunately the admin put the questions "loop and parameters" and "Loop over sets and conditions" together, which should be two distinct questions. So back to the original question about sets with conditions.
You asked for some more specific code. Here is another try:
Say I have a set, restrict it to three members for visualization (but it could be many):
Sets
i /A, B, C /
;
The order does not matter. There are seven potential subsets (call them X) with seven complements (call them Y).
A; B; C; A,B; A,C; B,C; and A,B,C.
For each subset I want to see whether certain conditions hold, then tell GAMS to remember this subset in a put file. Call each subset X, and its complement Y. Let's say the conditions are
sum((X), 2*p(X))=g= 5,
sum((Y),g(Y))=l=2,
where p(X) and g(Y) are parameters. If both conditions hold, then GAMS notes this down in a put file and goes to the next potential subset and checks this.

### Re: Loop over sets with conditions

Posted: Tue Feb 06, 2018 1:23 pm
Just like in the other post: I would use R to do the writing.
Cheers
Renger