addConstrs behaviour
AnsweredHi,
I've been modeling a network flow optimization, and stumbled upon what appears to be a bug in the Python API.
for eq in lin_eqs.select('*', '*', '*'):
self.model.addConstr(eq==0)
work just fine, but
self.model.addConstrs(
(eq == 0 for eq in lin_eqs.select('*', '*', '*')), name="eq_is_zero")
throws
TypeError: unhashable type: 'gurobipy.LinExpr'
Could it be that addConstrs under the hood only accepts variables and not expressions?
I would have posted an minimum reproducible example but am in a bit of a rush, sorry about that. Hope you can reproduce it still.
-
Hi Zvonimir,
As mentioned in our documentation, the Model.addConstrs() method returns a tupledict where the constraints are indexed by the values of the generator expression. In your code, I guess the \(\texttt{lin_eqs.select("*", "*", "*")}\) is a list of Gurobi LinExpr() objects which cannot be used as keys of the tupledict. One idea to fix this is given below:
lin_eqs_list = lin_eqs.select('*', '*', '*')
m.addConstrs((lin_eqs_list[i] == 0 for i in range(len(lin_eqs_list))), name="qe")Best regards,
Maliheh
2
Please sign in to leave a comment.
Comments
1 comment