6

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

RobPratt
  • 32,006
  • 1
  • 44
  • 84
Ahmed
  • 113
  • 4

1 Answers1

7

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

prubin
  • 39,078
  • 3
  • 37
  • 104