Using indicator constraint with two variables
AnsweredHi Gurobi team,
I want to use the sum of two binary decision variables (when their sum equals to one) as the condition of Model.AddGenConstrIndicator https://www.gurobi.com/documentation/current/refman/py_model_agc_indicator.html, however, it seems to be impossible, e.g. for the minimal code below which reflects the relevant part of my real problem trying the following:
import gurobipy as gp
model = gp.Model()
x = model.addVar(vtype=gp.GRB.BINARY, name="x")
y = model.addVar(vtype=gp.GRB.BINARY, name="y")
z = model.addVar(vtype=gp.GRB.CONTINUOUS, name="z")
model.update()
constr = model.addConstr((x + y == 1) >> (z <= 20))
model.setObjective(z, gp.GRB.MAXIMIZE)
# Solve the model
model.optimize()
Will result in the following error:
constr = model.addConstr((x + y == 1) >> (z <= 20))
File "src/gurobipy/model.pxi", line 3632, in gurobipy.Model.addConstr
gurobipy.GurobiError: Indicator constraints can only be triggered by a single binary variable at a given value
Is there a workaround for this?
-
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. -
Just a quick fix to the above example, what I want as the constraint is the following (I want it to equal to two instead one - that I have in the above):
constr = model.addConstr((x + y == 2) >> (z <= 20))
0 -
Hi Saeid,
if you are sure that either \(x = 1 \land y = 1\) or \(x=0 \land y=0\), you can try "Big-M" kind of constraint:
a = model.addVar(vtype="B")
model.addLConstr(x+y, GRB.LESS_EQUAL, 2*a)If you are sure the condition above is too strict, you can resort to another general constraint, the AND-constraint:
a = model.addVar(vtype="B")
model.addGenConstrAnd(a, [x, y], "andconstr")Regardless of the approach you choose, you can then use the binary \(a\) variable with your indicator constraint.
Hope this helps.
Best regards
Jonasz0 -
Hi Saeid,
See the bold part in the error message you get: "gurobipy.GurobiError: Indicator constraints can only be triggered by a single binary variable at a given value". So you may want to use an additional binary variable, say w, and an additional constraint w = x+y to work around. Similarly, to have "x+y = 2", you can add a constraint "2*w = x + y". Hope this solves your problem.
0 -
a single binary variable
0
Post is closed for comments.
Comments
5 comments