AddConstrs - "'Var' object is not iterable"
回答済みHi,
I am struggelig with adding certain constraints to a model I am working on, and hope to get some help. I am not particularly skilled in Python, and hope someone can help me understand how I should write my constraint. It is part of a vehicle routing problem, with considerations for electric vehicles, it that information is of any help-

Above is the formula for my constraint. With the following information:
H is a constant float value for maximum time available
x is a binary decision variable
tau is a continuous decision variable for tracking of time with tau[0]=0
t is the time to get from i to j, or the time on arc between two vertices
h is the handling time at vertex (fixed value).
G is the list of all vertices
N is part of G, containting vertices with certain attributes.
The two decision variables are written the following way in python:
x = mdl.addVars(G, G, M, vtype=GRB.BINARY)
tau = mdl.addVars(G, G, vtype=GRB.CONTINUOUS)
As the addConstrs() function only allows for one comparison, I have divided the contraint into the following two:
mdl.addConstrs((H*(quicksum(x[i,j,m])-1))<=
(tau[k,i] + quicksum(x[i,j] * ttravel[i,j]) + (h[j])
- (tau[i,j]))
for k in G for i in G for m in M for j in N if k!=i if k!=j if i!=j)
mdl.addConstrs((H*(1 - quicksum(x[i,j,m]))>=
(tau[k,i] + quicksum(x[i,j] * ttravel[i,j]) + (h[j])
- (tau[i,j]))
for k in G for i in G for m in M for j in N if k!=i if k!=j if i!=j)
I know I might have gone overboard on the use of parentheses, but that shouldn't be the cause of my problem.
Trying to run this code, I run into the following error code:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-120-05250b3eca12> in <module>
4 tau[k,i] + quicksum(x[i,j] * ttravel[i,j]) + h[j] - tau[i,j]<=
5 H*(1 - quicksum(x[i,j,m]))
----> 6 for k,i in A for j in N for m in M)
src\gurobipy\model.pxi in gurobipy.Model.addConstrs()
<ipython-input-120-05250b3eca12> in <genexpr>(.0)
4 tau[k,i] + quicksum(x[i,j] * ttravel[i,j]) + h[j] - tau[i,j]<=
5 H*(1 - quicksum(x[i,j,m]))
----> 6 for k,i in A for j in N for m in M)
src\gurobipy\gurobi.pxi in gurobipy.quicksum()
TypeError: 'Var' object is not iterable
If anyone can help me with the problem I would be forever grateful.
-
正式なコメント
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 Viktor,
Your code is missing the information what are the sets \(M\) and \(S\).
Nevertheless, you define your variables \(x\) and \(t\) over \(G\) and \(M\) but not over the set \(S\), which is required for the summation. Additionally, you have two \(x\) variables, where one has 3 indices and one has only 2. I assume that you want to use the \(x\) variables with 3 indices. A working code would be
import gurobipy
from gurobipy import *
m = Model('test')
G = [1,2,3,4,5]
M = [1,2,3,4]
N = [1,2,3]
S = [1,2,3]
H = 2
x = m.addVars(S, G, G, M, vtype=GRB.BINARY, name="x")
ttravel = m.addVars(S, G, G, vtype=GRB.CONTINUOUS, name="t")
tau = m.addVars(G, G, vtype=GRB.CONTINUOUS, name="tau")
h = m.addVars(G, vtype=GRB.CONTINUOUS, name="h")
m.addConstrs( (H * (quicksum(x[s,i,j,m] for s in S) -1) ) <=
(tau[k,i] + quicksum(x[s,i,j,m] * ttravel[s,i,j] for s in S) + (h[j]) - (tau[i,j]))
for k in G
for i in G
for m in M
for j in N
if k!=i
if k!=j
if i!=j)
m.write("myLP.lp")The quicksum function requires a list of terms to be added, thus simply calling
quicksum(x[s,i,j,m])
is not enough, since the Var object \(\texttt{x[s,i,j,m]}\) is not iterable, because it is not a list.
The second inequality can be fixed in an analogous way.
Best regards,
Jaromił1 -
Thank you for your inputs Jaromił!
It became evident that I had cut some corners during my programming which first came to haunt me in the restriction in question. You helped clarify what was missing, and I now make great progress.
Thank you so much for your time and help.
Best regards,
Viktor
0
投稿コメントは受け付けていません。
コメント
3件のコメント