What is the best way to model or represent an equality constraint which includes an absolute term in the expression:

$$ x = |y| $$

$x \in \mathbb{R^+}$ and $y \in \mathbb{R}$

  • 1
    $\begingroup$ How are $x$ and $y$ used elsewhere in the model? $\endgroup$
    – RobPratt
    Oct 7, 2022 at 21:59

1 Answer 1


If the objective function and the remaining constraints are such that the solver will always prefer smaller values of $x$ over larger values, you can get by with two constraints: $x\ge y$ and $x\ge -y.$ Otherwise, assuming you know an upper bound $U$ for $\vert y\vert,$ you can introduce a binary variable $z$ and the constraints $$0\le x-y \le 2U\cdot z$$and $$0 \le x+y \le 2U\cdot (1-z).$$ When $z=0,$ the first constraint forces $x=y$ (meaning $y$ must be $\ge 0$) and the second constraint is harmless (since $x+y=2y=2\vert y\vert \le 2U$). When $z=1,$ the second constraint forces $x=-y$ (meaning $y$ must be $\le 0$) and the first constraint is harmless (since $x-y=2\vert y\vert$).

  • $\begingroup$ Exactly what I needed, thank you sir. $\endgroup$
    – Ahmed
    Oct 13, 2022 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.