How to use Gurobi quicksum in Python to write this expression?
Question:
Answers:
The error message tells you that the quicksum
function’s argument is expected to be an iterable and a single Var object isn’t iterable. Instead, you could do something like this:
c2 = {}
for j in city:
c2[j] = m.addConstr(gp.quicksum(x[i,j] for i in frequency) <= 1, "c2")
Alternatively, you could use the .sum()
method:
c2 = {}
for j in city:
c2[j] = m.addConstr(x.sum('*', j) <= 1, "c2")
PS: You probably want to store the constraints in a dictionary if you want to access them later. At the moment, you’re only storing the last constraint in the variable c2
as it gets overwritten with each loop iteration.
The error message tells you that the quicksum
function’s argument is expected to be an iterable and a single Var object isn’t iterable. Instead, you could do something like this:
c2 = {}
for j in city:
c2[j] = m.addConstr(gp.quicksum(x[i,j] for i in frequency) <= 1, "c2")
Alternatively, you could use the .sum()
method:
c2 = {}
for j in city:
c2[j] = m.addConstr(x.sum('*', j) <= 1, "c2")
PS: You probably want to store the constraints in a dictionary if you want to access them later. At the moment, you’re only storing the last constraint in the variable c2
as it gets overwritten with each loop iteration.