Var' object is not iterable, TypeError: 'gurobipy.LinExpr' object is not iterable

1,550 views
Skip to first unread message

Kashin

unread,
Jul 19, 2018, 2:30:44 AM7/19/18
to Gurobi Optimization
Hi! I am a beginner at Gurobi.
I have a very simple question.
I am appreciate if you would kindly reply me.
Thank you very much in advance.

I wrote a python code like this. 
__________________________________________________________________

A=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

capacity={1:100,   2:100,  3:100,  4:100,
          5:100,   6:100,  7:100,  8:100,
          9:100,  10:100, 11:100, 12:100,
          13:100, 14:100, 15:100, 16:100
         }

free_flow_travel_time={1:100,   2:100,  3:100,  4:100,
                       5:100,   6:100,  7:100,  8:100,
                       9:100,  10:100, 11:100, 12:100,
                      13:100,  14:100, 15:100, 16:100
         }


N=16
alpha=10
beta=4
M=10000
k=2

model=Model("critical_combination")

x={}
for a in A:
    x[a]=model.addVar(vtype="C", name="x(%s)"%(a))

u={}
for a in A:
    u[a]=model.addVar(vtype="B", name="u(%s)"%(a))
    
t={}
for a in A:
    t[a]=model.addVar(vtype="C", name="t(%s)"%(a))

model.update()

for a in A:
    model.addConstr(quicksum(u[a])==N-k)
    
for a in A:
    model.addConstr(t[a]==free_flow_travel_time[a]*{1+alpha*(x[a]/capacity[a])}+(1-u[a]*M))

model.setObjective(quicksum(x[a]*t[a] for a in A), GRB.MAXIMIZE)

model.optimize()
__________________________________________________________________________
   
However, I got two error messages.

1) first error message
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-29-1857719c0de3> in <module>()
     37 
     38 for a in A:
---> 39     model.addConstr(quicksum(u[a])==N-k)
     40 
     41 for a in A:

gurobi.pxi in gurobipy.quicksum (../../src/python/gurobipy.c:128383)()

TypeError: 'Var' object is not iterable


2) second error mesage (if I comment out and skip the part of the first error message)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-30-a3f5b34a1f7b> in <module>()
     40 """    
     41 for a in A:
---> 42     model.addConstr(t[a]==free_flow_travel_time[a]*{1+alpha*(x[a]/capacity[a])}+(1-u[a]*M))
     43 model.setObjective(quicksum(x[a]*t[a] for a in A), GRB.MAXIMIZE)
     44 

TypeError: unhashable type: 'gurobipy.LinExpr'

Sonja Mars

unread,
Jul 19, 2018, 3:43:21 AM7/19/18
to gur...@googlegroups.com
Hi,

There are a few issues in your code. 

First of all the model is not convex and so Gurobi will not solve it. This is the error you get:
gurobipy.GurobiError: Objective Q not PSD (diagonal adjustment of 1.0e+00 would be required)

Then why are you setting an index set from 1 to 16. It is of course possible but it would make things easier to just use the normal Python indexing starting from 0 and then use a set like this

A=range(N)

This will give you an index set like this

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

and it is dependent of N. So if you change N this set changes as well. You will then of course also have to adjust your two dictionaries capacity and free_flow_travel_time to the new index.

First error:
for a in A:
    model.addConstr(quicksum(u[a])==N-k)
The question here is what the constraint should look like. Is it
sum_{a \in A} u_a = N-k
If so then the correct syntax would be:

model.addConstr(quicksum(u[a] for a in A)==N-k)

Second error:
for a in A:
    model.addConstr(t[a]==free_flow_travel_time[a]*{1+alpha*(x[a]/capacity[a])}+(1-u[a]*M))
Why are you using “{“ and “}“ here? If you change this to “(“ and “)” the code runs fine.

Additionally, you might be interested in our Python modeling features like addVars and addConstrs. This uses tupledicts instead of dictionaries for the variables. More details can be found here:

We show the details also here:

Using those will make your code much easier to read. I converted your code to use these features above. This would be the new version of your code:
N=16
alpha=10
beta=4
M=10000
k=2

A=range(N)

capacity={}
free_flow_travel_time={}
for a in A:
capacity[a]=100
free_flow_travel_time[a]=100


model=Model("critical_combination")

x=model.addVars(A, vtype="C", name="x")

u=model.addVars(A, vtype="B", name="u")
    
t=model.addVars(A, vtype="C", name="t")

model.addConstr(u.sum() == N-k)
    
model.addConstrs(t[a]==free_flow_travel_time[a]*(1+alpha*(x[a]/capacity[a]))+(1-u[a]*M) for a in A)

model.setObjective((x.prod(t)), GRB.MAXIMIZE)

model.optimize()


Best regards, 
Sonja 


----
Dr. Sonja Mars
Gurobi Optimization - Technical Support 




Kashin

unread,
Jul 19, 2018, 6:24:29 AM7/19/18
to Gurobi Optimization
Thank you very much for your kind and quick reply.
Your answer is very helpful for me.
I have a further question.
I would like to solve a following model.

N=16
alpha=0.15
beta=4
M=100000
L=-100000
ipsilon=0.00000001
k=2

A=range(N)

W=[(1,6),(6,1)]
R={(1,6):[(0,4,10),(1,7,3)],(6,1):[(14,8,2),(15,11,5)]}
demand={(1,6):2.5,(6,1):5}

capacity={}
free_flow_travel_time={}
for a in A:
    capacity[a]=10
    free_flow_travel_time[a]=10


model=Model("critical_combination")

x=model.addVars(A, vtype="C", name="x")

u=model.addVars(A, vtype="B", name="u")
    
t=model.addVars(A, vtype="C", name="t")

f={}
for w in W:
    for p in R[w]:
        f[w,p]=model.addVar(vtype="C", name="f(%s,%s)"%(w,p))

c={}
for w in W:
    for p in R[w]:
        c[w,p]=model.addVar(vtype="C", name="c(%s,%s)"%(w,p))

pi={}
for w in W:
    pi[w]=model.addVar(vtype="C", name="pi(%s)"%(w,))
    

sigma={}
for w in W:
    for p in R[w]:
        sigma[w,p]=model.addVar(vtype="B", name="sigma(%s,%s)"%(w,p))


model.addConstr(u.sum() == N-k)
    
model.addConstrs(t[a]==free_flow_travel_time[a]*(1+alpha*(x[a]/capacity[a])**beta)+(1-u[a]*M) for a in A)

model.addConstr(x.sum()<=u.sum()*M)

for w in W:
    for p in R[w]:
        model.addConstr(L*sigma[w,p]+ipsilon<=f[w,p]<=M*(1-sigma[w,p]))

for w in W:
    for p in R[w]:
        model.addConstr(L*sigma[w,p]+ipsilon<=c[w,p]-pi[w]<=M*sigma[w,p])

for w in W:
    for p in R[w]:
        model.addConstr(c[w,p]-pi[w]>=0)

for w in W:
    model.addConstr(quicksum(f[w,p] for p in R[w])==demand[w])

for a in A:
    model.addConstr(quicksum(f[w,p] for w in W for p in R[w] if a in p)==x[a])

for w in W:
    for p in R[w]:
        model.addConstr(quicksum(t[a] for a in A if a in p)==c[w,p])


model.setObjective((x.prod(t)), GRB.MAXIMIZE)

model.optimize()

______________________________________________________

However, I got an error message "unsupported operand type(s) for ** or pow(): 'gurobipy.LinExpr' and 'int'".
I think this error comes from the non-linear equation.
I would like to transform the non-linear equation to any equation which can be solved by Gurobi (e.g. piece-wise linear equation?).
However, I do not have any idea how to do this in Gurobi.
I would appreciate it if you would kindly give me any advice.

Yours sincerely, 
Kashin

2018年7月19日木曜日 15時30分44秒 UTC+9 Kashin:
Hi! I am a beginner at Gurobi.
I have a very simple question.
I am appreciate if you would kindly reply me.
Thank you very much in advance.

I wrote a python code like this. 
__________________________________________________________________

A=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

capacity={1:100,   2:100,  3:100,  4:100,
          5:100,   6:100,  7:100,  8:100,
          9:100,  10:100, 11:100, 12:100,
          13:100, 14:100, 15:100, 16:100
         }

free_flow_travel_time={1:100,   2:100,  3:100,  4:100,
                       5:100,   6:100,  7:100,  8:100,
                       9:100,  10:100, 11:100, 12:100,
                      13:100,  14:100, 15:100, 16:100
         }


N=16
alpha=10
beta=4
M=10000
k=2

model=Model("critical_combination")

x={}
for a in A:
    x[a]=model.addVar(vtype="C", name="x(%s)"%(a))

u={}
for a in A:
    u[a]=model.addVar(vtype="B", name="u(%s)"%(a))
    
t={}
for a in A:
    t[a]=model.addVar(vtype="C", name="t(%s)"%(a))

model.update()

for a in A:
    model.addConstr(quicksum(u[a])==N-k)
    
for a in A:
    model.addConstr(t[a]==free_flow_travel_time[a]*{1+alpha*(x[a]/capacity[a])}+(1-u[a]*M))


2018年7月19日木曜日 16時43分21秒 UTC+9 Sonja Mars:

Sonja Mars

unread,
Jul 19, 2018, 7:18:27 AM7/19/18
to gur...@googlegroups.com
Maybe you can post the mathematical formulation of your model. I guess this would be helpful to understand what you are exactly looking for and if there is a way to linearize it.

Sonja


Kashin

unread,
Jul 19, 2018, 8:01:48 AM7/19/18
to Gurobi Optimization
Thank you for your reply.
I attach a word file including mathematical formulation of the problem.
I think only Equation (3) is non-linear.
I would like to solve this problem with Gurobi in a faster way. 
Thank you very much in advance.

2018年7月19日木曜日 20時18分27秒 UTC+9 Sonja Mars:
problem.docx
Reply all
Reply to author
Forward
0 new messages