logical constraint
AnsweredHi,
Can we write logical constraints in Gurobi? For example, CPLEX can read below constraints.
1. if then else constraints
(x + y >= 1) => z >= 1, (if (x+y) is greater than equal to 1 then z will be greater than equal to 1)
2. counting constraints
(x1 >= 1) + (x2>=1) + (x3>=1) >= 2, (at least two of X has to be greater than equal to 1)
Thank you for your time in advance.
-
Official comment
This post is more than three 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 why not try our AI Gurobot?. -
Yes, you can model logic like this using indicator constraints. For example, consider the conditional statement
$$\begin{align*}x + y \geq 1 &\implies z \geq 1.\end{align*}$$
First, we create a new binary variable \( w \). Then, the above is equivalent to
$$\begin{align*} x + y \geq 1 &\implies w = 1 \\ w = 1 &\implies z \geq 1.\end{align*}$$
Indicator constraints must be of the form
$$\begin{align*}\textrm{binary variable equals } 0 \textrm{ or } 1 &\implies \textrm{some constraint.}\end{align*}$$
So, we can re-write the first conditional statement \( x + y \geq 1 \implies w = 1 \) with its logically equivalent contrapositive: \( w = 0 \implies x + y < 1 \). Strict inequalities aren't allowed in a math programming framework, but we can closely approximate this constraint with \( x + y \leq 1 - \epsilon \) for some small \( \epsilon > 0 \). Note that we can use \( \epsilon = 1 \) if \( x \) and \( y \) are both integer variables.
Altogether, we add the following two indicator constraints:
$$\begin{align*} w = 0 &\implies x + y \leq 1 - \epsilon \\ w = 1 &\implies z \geq 1.\end{align*}$$
Alternatively, you can model this logic yourself without using indicator constraints. E.g., if \( z \geq 0 \), then the conditional statement \( w = 1 \implies z \geq 1 \) is equivalent to \( z \geq w \).
1
Post is closed for comments.
Comments
2 comments