AMPL中产生幂集

有时做穷举时需要得到某集合的幂集,使用AMPL实现的代码如下:

set S ordered;
set P{j in 0..2^card(S)-1} ordered by S :=
  {i in S: (j div (2**(ord(i)-1))) mod 2 =1};
data;
set S:=A B C;
display P;

其基本思想就是用自然数的二进制表示和幂集做了一个一一对应。到的幂集存储在P所表示的集簇中,输出结果如下:

set P[0] := ; # empty
set P[1] := A;
set P[2] := B;
set P[3] := A B;
set P[4] := C;
set P[5] := A C;
set P[6] := B C;
set P[7] := A B C;

你可能感兴趣的:(c,存储,div,dataset)