# Absolute value in an equality constraint

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}$$

• How are $x$ and $y$ used elsewhere in the model? Oct 7, 2022 at 21:59

## 1 Answer

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$$).

• Exactly what I needed, thank you sir. Oct 13, 2022 at 21:55