# 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 ? – Kuifje Apr 14 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$$.
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 34, "wp-katex-eq": 0, "align": 2, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://or.stackexchange.com/questions/6090/model-if-else-statement/6092", "fetch_time": 1620498390000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2021-21/segments/1620243988923.22/warc/CC-MAIN-20210508181551-20210508211551-00235.warc.gz", "warc_record_offset": 465676333, "warc_record_length": 35117, "token_count": 583, "char_count": 1640, "score": 3.671875, "int_score": 4, "crawl": "CC-MAIN-2021-21", "snapshot_type": "longest", "language": "en", "language_score": 0.6961917281150818, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00020-of-00064.parquet"}