There are two real variables $x$ and $y$. The conditions are such that:

  • if $y\le 0$, then $x=0$
  • if $y>0$, then $x=y$

How to write linear equations or inequalities to satisfy both the conditions?

  • $\begingroup$ Note this is also sometimes noted $x = y^+$ or $x = \max(0, y)$, and outloud "x is the positive part of y" $\endgroup$
    – Stef
    Apr 25 at 9:25

2 Answers 2


Let $L_x,U_x$ denote a lower and upper bound for $x$, and $L_y,U_y$ denote a lower and upper bound for $y$.

Define an additional binary variable $\delta \in \{0,1\}$ and enforce the following:

$$ y \le 0 \implies \delta = 1 \implies x=0 \tag{1} $$

$$ y> 0 \implies \delta = 0 \implies x=y \tag{2} $$

Or equivalently via contrapositive:

$$ \delta = 0 \implies x=y \quad \text{and} \quad y> 0 \tag{3} $$

$$ \delta = 1 \implies x=0 \quad \text{and} \quad y\le 0 \tag{4} $$

$(3)$ can be modeled as follows: \begin{align} y+L_x\cdot\delta&\le x\le y+U_x\cdot\delta \tag{5}\\ \epsilon+L_y\cdot \delta &\le y \tag{6} \end{align}

$\epsilon$ is a small positive constant (tolerance).

And $(4)$ as follows: \begin{align} 0+L_x\cdot(1-\delta)\le x&\le 0+ U_x\cdot(1-\delta) \tag{7}\\ y&\le 0+U_y \cdot(1-\delta) \tag{8} \end{align}

As per RobPratt's comments:

  • $\epsilon$ can be set to $0$ (or omitted) as $(0,0)$ is a breakpoint
  • $L_x=0$ ; $L_y=:L$
  • $U_x=U_y=:U$
  • $(8)$ is redundant with $(5)$ and $(7)$

These simplifications yield:

\begin{align} L\cdot \delta &\le y \\ y&\le x \\ x&\le y+U\cdot\delta \\ x&\le U\cdot(1-\delta) \\ x&\ge 0 \end{align}

  • 1
    $\begingroup$ You did not define $\epsilon$, but you can omit it here because $(0,0)$ is a breakpoint. $\endgroup$
    – RobPratt
    Apr 24 at 12:55
  • $\begingroup$ You can simplify further by noting that $L_x=0$ and $U_x=U_y$. $\endgroup$
    – RobPratt
    Apr 24 at 16:16
  • $\begingroup$ You have some sign errors for $L_x$ and $L_y$, and the constraints that involve $y$ but not $x$ are linear combinations of the other constraints. $\endgroup$
    – RobPratt
    Apr 24 at 17:36
  • $\begingroup$ Thanks @RobPratt for the useful comments. $\endgroup$
    – Kuifje
    Apr 24 at 18:59
  • 1
    $\begingroup$ Glad to help. Looks better now, but you can strengthen $x \le y + U \cdot \delta$ to $x \le y - L \cdot \delta$. $\endgroup$
    – RobPratt
    Apr 24 at 19:31

It is worth noting that the desired relationship can be expressed as $x=\max(y,0)$. You want to model a disjunction of two rays from the origin. Impose finite bounds \begin{align} 0 \le x \le U \tag1\\ L \le y \le U \tag2 \end{align} and model a disjunction of two line segments (from $(0,0)$ to $(U,U)$ and from $(0,0)$ to $(0,L)$). For both, we must have $$y \le x \tag3$$ Now introduce a binary decision variable $\delta$ and enforce \begin{align} \delta = 0 &\implies y \ge x\\ \delta = 1 &\implies x \le 0 \end{align} either by using indicator constraints or by explicitly imposing big-M constraints \begin{align} x - y &\le -L\delta \tag4\\ x &\le U(1-\delta) \tag5 \end{align}

  • 1
    $\begingroup$ For some models, it suffices to require $x \geq {\rm max}(y,0)$, which is easily linearized as $x \geq y$, $x \geq 0$. But if also $x \leq {\rm max}(y,0)$ must be enforced explicitly, then the binary $\delta$ must be introduced. $\endgroup$
    – 4er
    Apr 25 at 15:25

Your Answer

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

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