For a binary variable $y$ and another decision variable $x$, $x$ being integer, I want to be able to use the following two non-linear constraints into a linear solver:
\begin{align}
x\ge 0 \implies y = 1\\
x< 0 \implies y = 0
\end{align}
What I've tried so far is:
$$\dfrac{x}{M} + \varepsilon \le y$$
$$\dfrac{x}{M}+1\ge y$$
With $M$ being a large constant and $\varepsilon$ a very small constant.
I then wanted to use those constraints in a linear programming solver and my problem is that whatever value of $M$ and $\varepsilon$, when $x = 0$, $y$ is always $0$ instead of $1$. All other cases are working. What mistake could have made? Is there another way without constants $M$ and $\varepsilon$?
Best Answer
$$ \begin{align} x\ge 0 \implies y = 1\\ x< 0 \implies y = 0 \end{align} $$
I assume your variable $y\in \{0,1\}$ because there is no another possibility. Let $M$ be a big positive number.
$$ \begin{align} x \geq (y-1)M\\ x < yM\\ -M \leq x < M \end{align} $$
Using the intervals $[-M, 0), [0, M)$ this set of constraints guarantees the asked properties. OK, this is exactly what you do, but when $x=0$ the second constraint $0<yM$ then $y\neq 0$.
You can rewrite as follow ($\epsilon > 0$) :
$$ \begin{align} x \geq (y-1)M\\ x + \epsilon \leq yM\\ -M \leq x < M \end{align} $$
Note that $x + \epsilon \leq yM$ and $\frac{x}{M}+\epsilon \leq y$ are the same thing. It is correct. Probably, you make a mistake in your code because when $(x,y)=(0,0)$ the second constraint becomes $\epsilon \leq 0$. It is false for all $\epsilon >0$.
UPDATE
I think you proof these constraints using the following idea. Let $M>0$ be a big number and $\epsilon>0$ a small number. Assume $-M \leq x \leq M-\epsilon$.
$$y=\left\lfloor\frac{x}{M}\right\rfloor+1$$
Note that $0 \leq x \leq M-\epsilon \implies y=1$ and $-M \leq x < 0 \implies \lfloor\frac{x}{M}\rfloor = -1 \implies y=0$
$$y-1=\left\lfloor\frac{x}{M}\right\rfloor$$
$$y-1\leq \frac{x}{M} < y $$
$$y-1\leq \frac{x}{M} \leq y -\frac{\epsilon}{M}$$
$$ \left\{\begin{align} & x \geq (y-1)M\\ & x \leq yM -\epsilon \end{align}\right. $$