* * GAMS program for knapsack * David Pisinger * $eolcom // option iterlim=999999999,reslim=300,optcr=0.0,optca=0.0,solprint=OFF,mip=XPRESS; SETS i 'genstand' /i1, i2, i3, i4, i5, i6, i7, i8, i9, i10/ PARAMETERS b 'max energi' / 25 / e(i) 'energi' / i1 12 i2 7 i3 8 i4 11 i5 15 i6 12 i7 4 i8 6 i9 13 i10 14 / s(i) 'smag' / i1 13 i2 12 i3 7 i4 8 i5 10 i6 21 i7 4 i8 12 i9 8 i10 16 / ; VARIABLES z 'total smag som skal optimeres' binary variables x(i) 'beslutningsvariable' EQUATIONS smag 'total smag' energi 'total energi' ; smag .. z =E= sum(i, s(i)*x(i)) ; energi .. sum(i, e(i)*x(i)) =L= b ; MODEL knapsack /ALL/ ; knapsack.workspace=10; SOLVE knapsack USING MIP MAXIMIZING z ; DISPLAY z.L; // value of the objective DISPLAY x.L; // x.L : Level, i.e. result