# Working with absolute values in constraint in a LP or MILP

Having all the approaches explained in the blog called "OR in an OB World" (this address) in my mind, I would like to ask the following question:

What is the best practice to make a constraint linear when for a variable in constraints, there is an absolute value expression which has lower and upper bounds? In other words, if a variable needs to cover two separate symmetric areas around zero (but not zero itself), how should it be enforced in the model?

• If I understand you correctly, you can introduce two variables; one taking the value if the expression was negative and one taking the value if it is positive. – Albert Schrotenboer May 30 '19 at 22:02
• Are you saying that variable $x$ should have domain $[-b, -a]\cup [a,b]$ where $a\neq 0$ and $b>a$ are parameters? – prubin May 30 '19 at 22:25
• @prubin this is exactly what I want. – Oguz Toragay Jun 1 '19 at 2:28

You need to model disjunctive constraints.

I will assume that variable $$x$$ is constrained to lie in $$L_1 \le x \le U_1$$ or $$L_2 \le x \le U_2$$.

For instance, if you have the constraint $$2 \le |x| \le 5$$, then choose $$L_1 = -5$$, $$U_1 = -2$$, $$L_2 = 2$$, $$U_2 = 5$$.

My solution handles a more general case than what you require, but includes your situation as a special case.

Model this as:

b : constrained to be binary (zero or one)


The following constraints encode the disjunctive constraints based on $$x$$ being in $$[L_1,U_1]$$ if $$b = 0$$ and in $$[L_2,U_2]$$ if $$b = 1$$.

x <= U1 + b*(U2 - U1)
x >= L1 + b*(L2 - L1)

• Hey Mark! SE kindly set us up with MathJax from the get go; care to edit your answer to use it? :) – LarrySnyder610 May 30 '19 at 22:36
• In this case I prefer text which can be copied and pasted as live code, but I;ll change the non-code portion. – Mark L. Stone May 30 '19 at 22:38
• Thanks @MarkL.Stone that works correctly in my model. – Oguz Toragay Jun 1 '19 at 2:30
• Another way to express this is $$L_1(1-b)+L_2 b \le x \le U_1(1-b)+U_2 b.$$ More generally, for $n$ intervals $[L_i,U_i]$, we have \begin{align} \sum_{i=1}^n L_i b_i \le x &\le \sum_{i=1}^n U_i b_i \\ \sum_{i=1}^n b_i &=1\end{align} – RobPratt Nov 14 '20 at 2:53