# Model if-else statement

I need to build a if-else constraint for this statement, where $$x_P$$ and $$x_I$$ are decision variables, and $$C$$ is a constant:

if $$x_P \ge C$$ then $$x_I = x_P - C$$ else $$x_I = 0$$.

Any help is greatly appreciated.

• Is $x_P$ continuous, non negative ? Apr 14, 2021 at 7:09

You want to enforce $$x_I=\max(x_P-C,0).$$ See https://math.stackexchange.com/a/4086955/683666, where the various big-M values are specified explicitly.

More generally, see Linearizing a Max Function in the constraint - not working to linearize the max of $$n$$ linear functions.

You can model it by adding a binary variable $$b$$ and the following four constraints. \begin{align} x_I &\geq x_P - C & x_I &\geq 0\\ x_I &\leq x_P - C + Mb & x_I &\leq M(1-b) \end{align} where $$M$$ is a big constant.

Note that if $$x_P > C$$, $$b$$ can't be $$1$$ as otherwise $$0\geq x_I > 0$$, which would lead to infeasibility. Thus $$b=0$$ and the left two inequalities force $$x_I = x_P-C$$.

On the other hand if $$x_P < C$$, $$b$$ can't be $$0$$ as otherwise $$0>x_I>0$$ by the left side. Thus $$b=1$$ and the right to constraints give $$x_I=0$$.

For $$x_P=C$$ it doesn't matter.

I hope this helps. ;)

Assuming $$x_p$$ is a continuous variable, you could use the following big-M inequalities: \begin{align} C - M_2(1- y) &\le x_p \le C-1 + M_1 y \\ x_p - C - M_4(1-y) &\le x_I \le x_p - C + M_3(1-y) \\ 0 & \le x_I \le M_5y \\ y &\in \{0,1\} \end{align}

So if $$y=0$$, $$x_P \le C-1$$, or by contrapositive, if $$x_P \ge C$$, $$y=1$$.

And if $$y=1$$, then $$x_P -C \le x_I\le x_P -C$$, and if $$y=0$$, then $$0 \le x_I\le 0$$.