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!
-
$\begingroup$ Do you have a range of values for $d_i$ or it truly can be any integer? $\endgroup$Steven– Steven2022年02月07日 01:07:49 +00:00Commented 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$Alex Pharaon– Alex Pharaon2022年02月07日 01:13:00 +00:00Commented Feb 7, 2022 at 1:13
1 Answer 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. $$
-
$\begingroup$ Thanks for your response. I tested it out and I think that the equality should be flipped for your last constraint. Right? $\endgroup$Alex Pharaon– Alex Pharaon2022年02月07日 02:09:46 +00:00Commented 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$Steven– Steven2022年02月07日 02:38:27 +00:00Commented 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$Alex Pharaon– Alex Pharaon2022年02月07日 10:29:50 +00:00Commented Feb 7, 2022 at 10:29
Explore related questions
See similar questions with these tags.