Conditional Constraints in Python: better error messages
AnsweredIt appears that MVars are not allowed in conditional constraints in the Python interface, but the error messages are rather mystifying. For example
import gurobipy as gp
model = gp.Model()
avars = model.addMVar((10, 10), vtype=gp.GRB.BINARY, name='a')
svars = model.addMVar((10, 10), vtype=gp.GRB.BINARY, name='s')
model.addConstr((avars[0, 0] == 1) >> (svars[0, 0] <= 0))
gives the error message
AttributeError: 'MLinExpr' object has no attribute 'size'
If I then do
import numpy as np
tempa = np.array(avars.tolist())
model.addConstr((tempa[0, 0] == 1) >> (svars[0, 0] <= 0))
I get the error
TypeError: must be a real number, not MLinExpr
But if I then do
temps = np.array(svars.tolist())
model.addConstr(tempa[0, 0] == 1) >> (temps[0, 0] <= 0))
everything works ok. I now undertand the current restriction that every variable involved in a conditional constraint must be a Var and not part of an MVar, but it would be really helpful if the error messages were not so cryptic.
-
Official comment
This post is more than three 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. -
This issue has been addressed in the latest Gurobi version. Which Gurobi version are you using? Could you please update to the latest version 9.5.0?
Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments