linear programming set a variable the max between two another variables

Solution 1:

Clearly you cannot use $\max(A,B)$ directly in the model if you want to have a linear formulation. We can define an auxiliary continuous variable $C$ to be able to develop a linear formulation. If your problem is to minimize $\max(A,B)$ then you can easily formulate it as follows: \begin{align} \min ~& C \\ \text{subject to}~~~~~~~~ & C \ge A \\ & C \ge B\\ &\text{the rest of constraints} \end{align}

Otherwise, you cannot safely formulate the problem as a linear program. You need to formulate it as a mixed integer linear programming formulation. Let $M$ (the so-called big-$M$ parameter) be an upper bound on $\max(A,B)$. You should select the smallest possible upper bound that you can find for $\max(A,B)$. We can now formulate the problem by defining the auxiliary binary variable $b \in \{0,1\}$. It is enough to add the following constraints to the original model

\begin{align} & C \ge A \\ & C \ge B\\ & C \le A + Mb\\ & C \le B + M(1-b)\\ \end{align}

You can now make sure that variable $C$ always takes the value of $\max(A,B)$.

Solution 2:

I arrive here looking for a more general formulation for a $max(a_0, ..., a_n)$ function. I found a formulation here but I re-wrote it to understand better the behavior of the binary variables. Let's define $C$ as the codification of the max function $C = max(a_0, ..., a_n)$ then, the linear formulation will be:

\begin{align} C &\ge a_i &\forall i \in N \\ C &\le a_i + (1-b_i)*M & \forall i \in N \\ \sum_{i \in N} b_i &= 1 \end{align}

where the $b_i \in \{0,1\}$ is a binary variable that indicates the maximum $a_i$ ($b_i = 1$ when $a_i$ is the max value), and $M$ it's a "big number".