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.
-
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
Please sign in to leave a comment.
Comments
1 comment