Skip to main content

How to build an IF A and B THEN C constraint?

Answered

Comments

1 comment

  • Maliheh Aramon
    Gurobi Staff Gurobi Staff

    Hi Luo, 

    One idea is to use auxiliary binary variables \(b_1\) and \(b_2\) to model the following:

    1. \(b_1 = \begin{cases} 1, ~~ \mbox{if}~ A \geq D+\epsilon \\ 0, ~~ \mbox{if}~ A \leq D \end{cases}\)
    2. \(b_2 = \begin{cases} 1, ~~ \mbox{if}~ B \leq D-\epsilon \\ 0, ~~ \mbox{if}~ B \geq D \end{cases}\)
    3. \(C = \begin{cases} 1, ~~ \mbox{if}~ b_1=1 ~ \mbox{and} ~ b_2=1 \\ 0, \mbox{otherwise} \end{cases}\)

    Since Gurobi does not support strict inequality constraints, the value \(\epsilon > 0\) represents a small positive tolerance value to model inequality constraints. 

    Expression 1 is logically equivalent to:

    • \(\mbox{if}~ b_1 = 0 \rightarrow A \leq D\)
    • \(\mbox{if}~ b_1 = 1 \rightarrow A \geq D + \epsilon\)

    Expression 2 is logically equivalent to:

    • \(\mbox{if}~ b_2 = 0 \rightarrow B \geq D\)
    • \(\mbox{if}~ b_2 = 1 \rightarrow B \leq D - \epsilon\)

    Each of the four items above can be implemented using the Model.addGenConstrIndicator() API. Expression 3 can be implemented using the Model.addGenConstrAnd() API.

    Best regards,

    Maliheh

    0

Please sign in to leave a comment.