How to use Gurobi quicksum in Python to write this expression?

Question:

I am trying to figure out how to write this constraint for Gurobi in Python.

enter image description here

for j in city:
   for i in frequency:
     c2 = m.addConstr(gp.quicksum(x[i,j]) <= 1, "c2")

But I am getting this error and I am a bit confused why it is wrong…

enter image description here

Asked By: DJ-coding

||

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.

Answered By: joni
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.