keyerror
回答済みstation,cost=multidict({
"x1":12000,
"x2":22000,
"x3":30000,
"x4":9000,
"x5":11000,
"x6":40000,
"x7":90000,
"x8":55000,
"x9":66000,
"x10":45800,
"x11":65000
})
demand,weight=multidict({
"y1":2.2,
"y2":2,
"y3":4,
"y4":3.8,
"y5":2.7,
"y6":1.5,
"y7":0.8,
"y8":1.8,
"y9":2.9,
"y10":4.4
})
Z=m.addVars(station,demand,GRB.BINARY,"z")
for i in station:
for j in demand:
m.setObjective(quicksum(Z[i,j]*(weight[j])),GRB.MAXIMIZE)
Hi
these are a part of my code, I've set parameters and variable but when I set my objective,
I always get KeyError: ('x1', 'y1') and why?
-
正式なコメント
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,
There are two error in the code you provided. First, you define variables \(Z\) over 4 sets. The addVars function takes every suitable input as an index set unless you specify the arguments' name, i.e.,
Z=m.addVars(station, demand, vtype=GRB.BINARY, name="z")
Second, you are using the quicksum function, which takes a data set as input. However, you loop over the data trying to re-set the objective function in each \(\texttt{for}\)-loop iteration. The correct way would be
m.setObjective(quicksum(Z[i,j] * weight[j] for i in station for j in demand), GRB.MAXIMIZE)
This way, you generate a list of terms \(\texttt{Z[i,j] * weight[j]}\) which will then be summed up.
Best regards,
Jaromił0 -
Thanks for reply, but I can't understand the first part, if I want to create multiple variables base on my two dict, like
z(1,1) z(1,2).....z(1,11)
z(2,1) z(2,2).....etc.
The description page said the indices represent the key of tupledict, but isn't "station" and "demand" the key of my dict? Why are they four sets?
0 -
The variable \(\texttt{GRB.BINARY}\) which is indeed just a character \(\texttt{'B'}\) and the character \(\texttt{"z"}\) are internally converted by Python to lists holding 1 element in order to fit the functions arguments. Thus, you produce a variable tupledict with 4 indices.
0
投稿コメントは受け付けていません。
コメント
4件のコメント