15

I want to express the following constraint, in an integer linear program:

$$y = \begin{cases} 0 &\text{if } x=0\\ 1 &\text{if } x\ne 0. \end{cases}$$

I already have the integer variables $x,y$ and I'm promised that $-100 \le x \le 100$. How can I express the above constraint, in a form suitable for use with an integer linear programming solver?

This will presumably require introducing some additional variables. What new variables and constraints do I need to add? Can it be done cleanly with one new variable? Two?

Equivalently, this is asking how to enforce the constraint

$$y \ne 0 \text{ if and only if } x \ne 0.$$

in the context where I already have constraints that imply $|x| \le 100$ and $0 \le y \le 1$.


(My goal is to fix an error in https://cs.stackexchange.com/a/12118/755.)

D.W.
  • 159,275
  • 20
  • 227
  • 470

3 Answers3

7

I think I can do it with one extra binary variable $\delta \in \{0,1\}$:

$$ -100y \le x \le 100 y $$ $$ 0.001y-100.001\delta \le x \le -0.001y+100.001 (1-\delta) $$

Update

This assumes $x$ is a continuous variable. If we restrict $x$ to be integer valued, then the second constraint can be simplified to: $$ y-101\delta \le x \le -y+101 (1-\delta) $$

  • 1
    I verified this correct by testing it exhaustively with a little program. Thank you for the solution! – D.W. Apr 24 '16 at 04:03
  • @ErwinKalvelagen, could you please explain the your logic with binary variable delta, for more general case, for instance, if y={a: x>0, b: x<0}. – Nick Oct 19 '16 at 00:14
  • 1
    @Nick The binary variable is used to model an 'OR' construct. See here for an answer to your question. – Erwin Kalvelagen Oct 19 '16 at 01:21
  • @ErwinKalvelagen, the great answer, I tried to applied the your approach to my question here http://cs.stackexchange.com/questions/64794/how-to-move-logical-operator-or-from-an-objective-function-into-constraints-in-i. – Nick Oct 19 '16 at 07:48
  • Cool answer! However I think that you could avoid using that epsilon = 0.001 and just use epsilon = 1, so the inequality will look better without affecting the outcome, am I right? It would be: y - 101\delta <= x <= -y + 101(1-\delta) – Gonzalo Solera Dec 14 '19 at 21:43
  • @GonzaloSolera You can make the 100.001 constant larger, but not smaller. For numerical reasons, I chose the smallest possible value. This is a standard big-M approach. The 0.001 constant should not be made 1. That would cause small values of $x$ to be infeasible when $y=1$. – Erwin Kalvelagen Dec 16 '19 at 01:53
  • 1
    @GonzaloSolera Actually I was wrong: I assumed $x$ to be a continuous variable. Indeed when $x$ is integer valued we can move 0.001 up to 1 as you suggested. – Erwin Kalvelagen Dec 16 '19 at 06:17
1

The following isn't pretty by any means, but it works. Let $0 \leq x \leq N$, $N=100$ in the specific case in the question. Then we have the following constraints.

  1. $0 \leq z_1, z_2, z \leq 1$
  2. $x - N(1-z_1) \leq 0$
  3. $-x -Nz_1 \leq -1$
  4. $-x -N(1-z_2) \leq 0$
  5. $x -Nz_2 \leq -1$
  6. $z_1 + z_2 - 1 \leq z$
  7. $z \leq z_1$
  8. $z \leq z_2$

The intuition is as follows. $z_1 = 1 \iff x \leq 0$. This is encoded in constraints 2 and 3. Similarly constraints 4 and 5 encode $z_2 = 1 \iff x \geq 0$. The last three constraints express $z = z_1 \land z_2$.

Pramod
  • 111
  • 2
  • This seems to have a bug. I assume you intend $z=1-y$. However, it's still wrong for $x=100$: we want to force $y=1$ ($z=0$) in this case, but there's no choice for $z_1,z_2$ that satisfies all of the equations, as the equation $x-Nz_2 \le -1$ requires $x<N$ (i.e., $x \le 99$). Thus, this ILP gives the wrong result when $x=99$: we want $y=1$, but we got $y=0$. Also the desired range for $x$ as listed in the question is $-N \le x \le N$, not $0 \le x \le N$. – D.W. Apr 24 '16 at 04:01
1

Here's a solution that uses two temporary variables. Let $t,u$ be integer zero-or-one variables, with the intended meaning that $t=1$ if $x \ge 0$, $u=1$ if $x \le 0$, and $y=\neg(t \land u)$. These can be enforced with the following constraints:

$$\begin{align*} 0 &\le t,u,y \le 1\\ 1+x &\le 101t \le 101 + x\\ 1-x &\le 101u \le 101-x\\ t+u-1 &\le 1-y\\ 1-y &\le t\\ 1-y &\le u \end{align*}$$

D.W.
  • 159,275
  • 20
  • 227
  • 470
  • This answer is incorrect, unfortunately. It will constrain $x \leq 99$ by the first part of the first non-trivial constraint when the question asks given $x \leq 100$. Aren't fencepost errors fun? (Ditto for $x \geq -99$.) – TLW Feb 02 '16 at 02:04
  • @TLW, thank you for catching that! I've edited my answer to fix the bug. I tested it exhaustively with a little program and I think it should be correct now. – D.W. Apr 24 '16 at 03:47