1
$\begingroup$

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!

asked Feb 7, 2022 at 0:54
$\endgroup$
2
  • $\begingroup$ Do you have a range of values for $d_i$ or it truly can be any integer? $\endgroup$ Commented Feb 7, 2022 at 1:07
  • $\begingroup$ I have an upper and lower bound on the possible values. I will add that to the question, thanks @Steven. $\endgroup$ Commented Feb 7, 2022 at 1:13

1 Answer 1

1
$\begingroup$

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. $$

answered Feb 7, 2022 at 1:23
$\endgroup$
3
  • $\begingroup$ Thanks for your response. I tested it out and I think that the equality should be flipped for your last constraint. Right? $\endgroup$ Commented Feb 7, 2022 at 2:09
  • $\begingroup$ 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$. $\endgroup$ Commented Feb 7, 2022 at 2:38
  • $\begingroup$ I took a closer look at it now and it seems right. Thanks a lot for your help @Steven! $\endgroup$ Commented Feb 7, 2022 at 10:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.