首先先仅满足课程数最少:
model: sets: item/1..9/:c,x; endsetsdata: c=5,4,4,3,4,3,2,2,3; enddatamin=@sum(item(i):x(i)); !课程数约束; x(1)+x(2)+x(3)+x(4)+x(5)>=2; x(3)+x(5)+x(6)+x(8)+x(9)>=3; x(4)+x(6)+x(7)+x(9)>=2; !先修课约束; x(3)<=x(2);x(3)<=x(1); x(4)<=x(7); x(5)<=x(2);x(5)<=x(1); x(6)<=x(7); x(8)<=x(5); x(9)<=x(1);x(9)<=x(2); for(item(i):@bin(x(i))); end
求得课程数最少为6;后面又引进了学分最多,可以在课程数最少的基础上进行约束
model: sets: item/1..9/:c,x; endsetsdata: c=5,4,4,3,4,3,2,2,3; enddatamax=@sum(item(i):c(i)*x(i)); item(i):x(i))=6; (!课程数约束; x(1)+x(2)+x(3)+x(4)+x(5)>=2; x(3)+x(5)+x(6)+x(8)+x(9)>=3; x(4)+x(6)+x(7)+x(9)>=2; !先修课约束; x(3)<=x(2);x(3)<=x(1); x(4)<=x(7); x(5)<=x(2);x(5)<=x(1); x(6)<=x(7); x(8)<=x(5); x(9)<=x(1);x(9)<=x(2); for(item(i):@bin(x(i))); end
即可求得结果