Write if else statement in Linear programming in Python

linear programmingmixed-integer programming

I am new in Linear programming and I need to write linearly the following if-else statement:
If a < b then a else b.

How can we write a linear program without write explicitly the if-else statement?

Best Answer

Let $y$ represent $\min(a,b)$. I'm assuming that $a$ and $b$ are variables. If the nature of the model (including its objective function) is such that larger values of $y$ are always preferable to smaller values, then you just need two constraints: $$y\le a\\ y\le b.$$ Since larger values of $y$ are better, the solver will make $y$ as big as possible, which will be the smaller of $a$ and $b$.

If you cannot be sure that larger values of $y$ are always better, then you will need to introduce a binary variable that signals which of $a$ and $b$ is smaller (making the problem an integer linear program). How to do that has been explained multiple times on this site. See the "Related" section on the right.

Related Question