quicksum-list
Hi. I'm new to Gurobi. I have a question for a basic model, knapsack. When I use list dot product in a for loop in both objective function and constrain, I have an arro, KeyError : 0. What may be problem in this formulation. Could you please help me?
from gurobipy import *
import numpy as np
rnd = np.random
from gurobipy import Model, GRB, quicksum
mdl = Model("Knapsack")
n = 100
X = list(range(1,n+1))
W = list(rnd.randint(1,100,n))#weight
P = list(rnd.randint(1,30,n))#price
x = mdl.addVars(X,vtype=GRB.BINARY, name = "x")
mdl.setObjective(quicksum(x[i]*P[i] for i in range(100)),GRB.MAXIMIZE)
mdl.addConstrs((x[i]*W[i] for i in range(100)) <=1000, name = "knapsack")
mdl.optimize()
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-4-080bb259bd7d> in <module>
17
18
---> 19 mdl.setObjective(quicksum(x[i]*P[i] for i in range(100)),GRB.MAXIMIZE)
20
21 mdl.addConstrs((x[i]*W[i] for i in range(100)) <=1000, name = "knapsack")
gurobi.pxi in gurobipy.quicksum()
<ipython-input-4-080bb259bd7d> in <genexpr>(.0)
17
18
---> 19 mdl.setObjective(quicksum(x[i]*P[i] for i in range(100)),GRB.MAXIMIZE)
20
21 mdl.addConstrs((x[i]*W[i] for i in range(100)) <=1000, name = "knapsack")
KeyError: 0
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hi Adem,
Apparently, you are using a 1-indexed list to index your variables:
X = list(range(1,n+1))
x = mdl.addVars(X,vtype=GRB.BINARY, name = "x")Later, when you are setting the objective, you are no longer looping over the X list but rather over the 0-indexed Python list range(100) that goes from 0 to 99. There is no x[0].
I suggest you drop the X list and just use range(100) consistently instead.
Cheers,
Matthias0 -
Hi Mattihas, Thank you for your reply. As far as I understand, variables must be in dict structure, not a list or tuple, isn't it? I have another question? Is there a resource that covers modeling from the scratch in gurobi aside from your quick start document? Regards.
0 -
Hi Adem,
Variables will be stored in a TupleDict object when created via addVars(). You can use any other storage if you like by adding the variables one by one:
var_list = [model.addVar(name=f"x_{i}") for i in range(100)]
We actually have quite a few webinars and tutorials on our webpage dedicated to Python modeling. You should also check out our Python examples directory that is included in the installation package.
Cheers,
Matthias0 -
Hi Matthias. Thank you for your interest. I have another question. How can I formulate three-index formulation in quicksum. Let's say I hava an equation like this:
mdl.addConstrs((x[i,j,w]* x[j,i,w] for w in W if j != i) == 1 for i in N for j in N)
or
for i in N:
mdl.addConstrs((x[i,j,w]* x[j,i,w] for w in W if j != i) == 1 for j in N)Which one is correct? My best regards.
0
Post is closed for comments.
Comments
5 comments