Generating All Combinations

Hello,

I have a column consisting of 13 rows of ones and zeros. I would like to generate all combinations where I have N 1s in the column (where N < 13). What is the fastest way to do that?

Thank you!

GAMS does not do matrices so well, but working with subsets will get you there. We use embedded Python to do the actual calculation of the combinations:

set n / 1*13 /; scalar r /4/;
* Calculate n Choose r
$eval nCr fact(card(n))/fact(r)/fact(card(n)-r)
$eval r r
set p 'possible outcomes' / p1*p%nCr% /;
set pn(p,n);
$onembeddedCode Python:
import itertools
pn = []
for t in zip(gams.get('p'),itertools.combinations(gams.get('n'),%r%)):
  for n in t[1]:
    pn.append((t[0],n))
gams.set('pn',pn)
$offembeddedCode pn
display pn;

-Michael

Thank you. Works perfectly.