This shows you the differences between two versions of the page.
— |
gams:how_can_i_enumerate_all_possible_subsets_of_a_set [2007/02/02 16:47] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== How can I enumerate all possible subsets of a set ====== | ||
+ | Have a look at the following example program: | ||
+ | <code> | ||
+ | * Example showing how to enumerate all possible subsets | ||
+ | * of a set | ||
+ | |||
+ | sets | ||
+ | I / i1 * i3 /, | ||
+ | J / j1 * j2/, | ||
+ | IJ(I,J), | ||
+ | cnt / 1 * 9999 /; | ||
+ | |||
+ | scalars M, N, pMN, k, l; | ||
+ | M = card(I); | ||
+ | N = card(J); | ||
+ | pMN = power(2,(M*N)); | ||
+ | abort$(card(cnt) lt pMN) "cnt set is too small"; | ||
+ | |||
+ | loop {cnt$(ord(cnt) le pMN), | ||
+ | k = ord(cnt) - 1; | ||
+ | l = M*N; | ||
+ | IJ(I,J) = NO; | ||
+ | loop {(I,J), | ||
+ | IJ(I,J) = mod(k, 2); | ||
+ | k = floor(k / 2); | ||
+ | } | ||
+ | display IJ; | ||
+ | } | ||
+ | </code> |