User Tools

Site Tools


gams:how_can_i_enumerate_all_possible_subsets_of_a_set

How can I enumerate all possible subsets of a set

Have a look at the following example program:

* 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;
}
gams/how_can_i_enumerate_all_possible_subsets_of_a_set.txt · Last modified: 2007/02/02 15:47 (external edit)