1

I have a variable $d_{i} \in \mathbb{Z}$ with an upper and lower bound. I also have a binary variable $v_{i}$ which I want to $=1$ if $d_{i} \geq 0$; else $v_{i} = 0$. How do I enforce this as a linear constraint?

I have seen this post but it is different to my problem as I have the inequality ($\geq$) whereas they merely have ($>$). I presume this changes the problem but please correct me if I am wrong as I am new to this.

I thought about adding a slack variable so that my constraint becomes like that in the post referenced; however, my problem is one of maximization and I do not want to include $-d_{i}$ in my maximization objective as it does not make sense for my problem.

Any help is much appreciated!

1 Answers1

1

Assuming that $L \le d_i < U$ with $L<0$ and $U>0$, you can add the following two constraints.

The following encodes "if $d_i \ge 0$ then $v_i=1$": $$U v_i - d_i > 0.$$

The following encodes "if $d_i < 0$ then $v_i=0$": $$ -L (1-v_i) + d_i \ge 0. $$

Steven
  • 29,419
  • 2
  • 28
  • 49
  • Thanks for your response. I tested it out and I think that the equality should be flipped for your last constraint. Right? – Alex Pharaon Feb 07 '22 at 02:09
  • The second constrant looks good to me. If $d_i$ is negative then, in order to satisfy the constant, $−L(1−v_i)$ must be positive and hence $v_i=0$ (recall that $-L$ is positive). Once we set $v_i=0$ the constraint is satisfied since it simplifies to $-L + d_i \ge 0$, which is always true. If $d_i$ is positive then the second constraint is always satisfied, regardless of the value of $v_i$. Overall, the second constraint is encoding the implication $d_i < 0 \implies v_i = 0$. – Steven Feb 07 '22 at 02:38
  • I took a closer look at it now and it seems right. Thanks a lot for your help @Steven! – Alex Pharaon Feb 07 '22 at 10:29