enumerate all subsets of a set

Frequently asked questions about GAMS

Moderator: aileen

Forum rules
Please ask questions in the other sub-forums
Locked
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

enumerate all subsets of a set

Post by abhosekar »

An easy way to get this is using powerset system attribute. Here, we want all subsets of the set i. We know that there are 2**card(i) subsets of the set i. We use a set j containing two elements and system.powersetright to create a set IJ0. Any column of IJ0 is a powerset IJ as shown by display statement.

Code: Select all

sets
    I		/ i1 * i3 /
;
Scalar nsubsets;

$eval nsubsets power(2, card(I))
Sets
  J		/ j1 * j2/
  nsets /n1*n%nsubsets%/
  IJ0(nsets,i,j) / system.powersetRight /
  IJ(nsets,i) 'power set'
;
IJ(nsets, i) = IJ0(nsets, i, 'j1');

display IJ;
Locked