How to build an IF A and B THEN C constraint?
AnsweredA:continuous
B:continuous
C:binary
D:constant
I want model IF(A>D and B<D) THEN C=1, ELSE C=0, How to build it in gurobi?
0
-
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. -
Hi Luo,
One idea is to use auxiliary binary variables \(b_1\) and \(b_2\) to model the following:
- \(b_1 = \begin{cases} 1, ~~ \mbox{if}~ A \geq D+\epsilon \\ 0, ~~ \mbox{if}~ A \leq D \end{cases}\)
- \(b_2 = \begin{cases} 1, ~~ \mbox{if}~ B \leq D-\epsilon \\ 0, ~~ \mbox{if}~ B \geq D \end{cases}\)
- \(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
Post is closed for comments.
Comments
2 comments