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.
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
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
Post is closed for comments.
Comments
2 comments