I hope this is not a duplicate but i can't seem to find an specific answer to this problem. I'm pretty new to Python so it might be obvious but I can't seem to find my error.
So the problem is: I'm using gurobi to optimize a network-flow-model. But I have some trouble formulating the constraints because i have to iterate over 2 variables (I guess that is the problem).
Here is the code first:
from gurobipy import *
import optimization
def beer(P, S, W, SP, g, d, b, c, m, n):
# Create new model
m = Model("Beer-Flow")
# Create variables
x = { (i,j,p) : m.addVar(name = "x[%s,%s,%s]" % (i,j,p)) for i,j in W for p in P }
y = { (i,j) : m.addVar(name = "y[%s,%s]" % (i,j)) for i,j in W }
# Integrate variables
m.update()
# Set objective
m.setObjective(quicksum(y[i,j] * d[i,j] * c for (i,j) in W ), GRB.MINIMIZE)
# Add constraints
for i in S:
m.addConstr((quicksum(x[i2,j,p])for p in P for (i2,j) in W if i2 == i ) - (quicksum(x[i2,j,p]for p in P for (i2,j) in W if j == i)) == b[i,p])
for i,j in W:
m.addConstr(m * y[i,j] >= (quicksum(x[i,j,p] * w[p] for p in P)))
m.addConstr(n * y[i,j] >= (quicksum(x[i,j,p] for p in P)))
return m
# Fill sets and parameters
P, g = multidict({"full" : 12, "empty" : 4 })
S = {"Brauerei","München","Stuttgart","Göttingen","Bielefeld","Magdeburg" }
W, d = multidict({("Brauerei", "München") : 558, ("Brauerei", "Stuttgart") : 437, ("Brauerei","Göttingen") : 142,
("Brauerei", "Bielefeld") : 45, ("Brauerei", "Magdeburg") : 290, ("München", "Brauerei") : 558,
("München", "Stuttgart") : 232, ("München", "Göttingen") : 500, ("München", "Bielefeld") : 600,
("München", "Magdeburg") : 523, ("Stuttgart", "Brauerei") : 437, ("Stuttgart", "München") : 232,
("Stuttgart", "Göttingen") : 408, ("Stuttgart", "Bielefeld") : 492, ("Stuttgart", "Magdeburg") : 572,
("Göttingen", "Brauerei") : 142, ("Göttingen", "München") : 500, ("Göttingen", "Stuttgart") : 408,
("Göttingen", "Bielefeld") : 180, ("Göttingen", "Magdeburg") : 197, ("Bielefeld", "Brauerei") : 45,
("Bielefeld", "München") : 600, ("Bielefeld", "Stuttgart") : 492, ("Bielefeld", "Göttingen") : 180,
("Bielefeld", "Magdeburg") : 254, ("Magdeburg", "Brauerei") : 290, ("Magedeburg", "München") : 523,
("Magdeburg", "Stuttgart") : 572, ("Magdeburg", "Göttingen") : 197, ("Magdeburg", "Bielefeld") : 254})
SP, b = multidict({("München", "full") : 1840, ("Brauerei", "empty") : -1700, ("Stuttgart", "full") : 1400, ("Stuttgart", "empty") : -1550,
("Göttingen","full") : 380, ("Göttingen", "empty") : - 400, ("Bielefeld", "full") : 840, ("Bielefeld", "empty") : -800,
("Magdeburg", "full") : 600, ("Magdeburg", "empty") : -500, ("Brauerei", "full") : -5600, ("Brauerei", "empty") : 4950})
c = 1
m = 24000
n = 2000
# Create model for given sets and parameters
m = beer(P, S, W, SP, g, d, b, c, m, n)
# Run optimization and print results
m.optimize()
optimization.print_result(m)
The part that is not working is:
for i in S:
m.addConstr((quicksum(x[i2,j,p])for p in P for (i2,j) in W if i2 == i ) - (quicksum(x[i2,j,p]for p in P for (i2,j) in W if j == i)) == b[i,p])
The Error is: 'Var' type is not iterable In Java for example I could get it done by two loops for p and i (tried it in Python, didn't work), but I have no clue how I can solve this problem with Python.
Thank you in Advance
Looks like a paren in the wrong place:
m.addConstr((quicksum(x[i2,j,p])for p in P for (i2,j) in W if i2 == i ) - (quicksum(x[i2,j,p]for p in P for (i2,j) in W if j == i)) == b[i,p])
^ Closes the quicksum call on a single value, not the whole genexpr
versus the other use of quicksum
on the same lookup:
m.addConstr((quicksum(x[i2,j,p])for p in P for (i2,j) in W if i2 == i ) - (quicksum(x[i2,j,p]for p in P for (i2,j) in W if j == i)) == b[i,p])
^ Note no paren, so it's quicksum-ing the whole generator expression