Skip to main content

Using indicator constraint with two variables

Answered

Comments

5 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    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.
  • Saeid Ghafouri
    • Gurobi-versary
    • Curious
    • Conversationalist

    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
  • Jonasz Staszek
    • Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    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
    Jonasz

    0
  • Jue Xue
    • Gurobi Staff

    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
  • Jue Xue
    • Gurobi Staff

    a single binary variable

    0

Post is closed for comments.