# How to formulate "If statement with equality constraints" using big m? [duplicate]

How to convert this one to a linear program:

if $$x=1$$ then $$B=1$$; otherwise, $$B=0$$.

If I use the Big M method: \begin{align}x&\ge1-M(1-B)\\x&\le1+M(1-B)\end{align}

A) with $$B=1$$: \begin{align}x&\ge1\\x&\le1\end{align} That is corresponding to $$x=1$$

B) with $$B=0$$: \begin{align}x&\ge1-M\\x&\le1+M\end{align} That is corresponding to any value of $$x$$ even $$x=1$$. How do I exclude $$x=1$$ when $$B=0$$?

• What type of variable is $x$? Commented Nov 25, 2022 at 18:20
• If $x$ is continuous with constant bounds $-M \le x \le M$, see my answer here (take $b=1$, $L=-M$, and $U=M$): or.stackexchange.com/a/2632/500 Commented Nov 25, 2022 at 18:46
• Thanks! x is continuous Commented Nov 25, 2022 at 23:13
• @RobPratt yes, it does. Thanks! Commented Nov 26, 2022 at 5:36

I think the two constraints put in there, lead to the same thing, just change the sign of one of those and then add the two constraints, it will lead to $$M \ge 0$$. Assuming $$x$$ is continuous, then you need:
C1 = $$B \ E \ [0,1] \ and \ z \le 0$$: B is binary.
Choose U such that it's the upper bound for x
C2 = $$Uz \le x$$: z is <0 for x=99, z will remain <0 for x=-99 as U is larger than x and when x=1, z=0
C3 = $$Uz+L(z-1) \ge (1-x)*(L-U)$$: if x=1 z is forced to 0 due to C2, if x=99, z remains <0, if x=-99, z is again <0
C4 = $$B \ge z+1$$: z <0, B can be 0 but when z=0, B =1
and Choose $$M \gt \lvert {x} \rvert+1$$, $$L$$ is lower bound of $$x$$ and should be <0 and $$U$$ is upper bound of $$x$$ and >0

• x is continuous. Thanks for your trail of help, but that does not work. If x=0, then: C2 = M.B-M>=0: thus B=1 C3 = M.B-1<=M: thus, B is free. To satisfy C2 and C3, B will be 1. Thus your suggestion does not work. Commented Nov 26, 2022 at 1:09
• Now? Does it solve? Commented Nov 26, 2022 at 1:44
• Sorry no let assume 0<=x<=10 C2: Uz<=x with x=1: 10z<=1 z<=0.1 and z<=0 does not mean z==0 Commented Nov 26, 2022 at 4:22
• But look at c3, z=-0.1, would make it infeasible, so solver will choose to make it 0. Commented Nov 26, 2022 at 4:54
• I will double-check and let you know. Thank you. Commented Nov 26, 2022 at 4:59

if x and B both are binary then x+B>=2 should work. this contriant is satisfactory only under one condition that is both x and B should be 1. for every other condition the constraint will be voilated.

• If $x$ and $B$ are both binary, then $x=B$ is instead the correct constraint. Commented Nov 25, 2022 at 20:26
• That means the only solution is when x=1, but the optimal solution can be zero. By the way, the x variable is continuous Commented Nov 26, 2022 at 0:39