4 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
6 from pyscipopt
import Model, quicksum, multidict
9 def mkp(I, J, v, a, b):
10 """mkp -- model for solving the multi-constrained knapsack
12 - I: set of dimensions
14 - v[j]: value of item j
15 - a[i,j]: weight of item j on dimension i
16 - b[i]: capacity of knapsack on dimension i
17 Returns a model, ready to be solved.
24 x[j] = model.addVar(vtype=
"B", name=
"x(%s)" % j)
28 model.addCons(
quicksum(a[i, j] * x[j]
for j
in J) <= b[i],
"Capacity(%s)" % i)
31 model.setObjective(
quicksum(v[j] * x[j]
for j
in J),
"maximize")
38 """creates example data set"""
39 J, v =
multidict({1: 16, 2: 19, 3: 23, 4: 28})
40 a = {(1, 1): 2, (1, 2): 3, (1, 3): 4, (1, 4): 5,
41 (2, 1): 3000, (2, 2): 3500, (2, 3): 5100, (2, 4): 7200,
47 if __name__ ==
"__main__":
49 model =
mkp(I, J, v, a, b)
53 print(
"Optimal value:", model.getObjVal())
59 if model.getVal(v) > EPS:
60 print(v.name,
"=", model.getVal(v))