互换的青春
让我举一个动物园里的例子from docplex.mp.model import Model# DataBuses=[ (40,500), (30,400), (35,450), (20,300) ]nbKids=300# IndexesbusSize=0;busCost=1;for b in Buses: print("buses with ",b[busSize]," seats cost ",b[busCost])mdl = Model(name='buses')#decision variablesmdl.nbBus=mdl.integer_var_dict(Buses,name="nbBus")# Constraintmdl.add_constraint(sum(mdl.nbBus[b]*b[busSize] for b in Buses) >= nbKids, 'kids')# Objectivemdl.minimize(sum(mdl.nbBus[b]*b[busCost] for b in Buses))mdl.solve()# Display solutionfor b in Buses: print(mdl.nbBus[b].solution_value," buses with ",b[busSize]," seats");#Add a constraint# Number of sizes where we have 1 or 2 buses should be at least 3mdl.add(mdl.sum(mdl.logical_and(1<=mdl.nbBus[b],mdl.nbBus[b]<=2) for b in Buses) >=3)mdl.solve()# Display solutionfor b in Buses: print(mdl.nbBus[b].solution_value," buses with ",b[busSize]," seats");这使buses with 40 seats cost 500buses with 30 seats cost 400buses with 35 seats cost 450buses with 20 seats cost 3005.0 buses with 40 seats1.0 buses with 30 seats2.0 buses with 35 seats0 buses with 20 seats4.0 buses with 40 seats1.0 buses with 30 seats2.0 buses with 35 seats2.0 buses with 20 seats