linear programming set a variable the max between two another variables i'm having problems with this. Suppose i have two real variables, A and B, and another one C. I want to store the max between A and B in C for a problem im modeling. I can't use a max function, neither multiply variables. What can I do? 
 A: I arrived here looking for a general formulation for a $max(a_0, ..., a_n)$ function. I found a formulation here but re-wrote it to better understand the binary variables' behavior.
Let's define $U$ as the codification of the max function $U = max(a_0, ..., a_n)$ then, the linear formulation will be:
\begin{align}
U &\ge a_i   &\forall i \in N \\ 
U &\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".
A: 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)$.
A: I came here looking for a way to select the top m (say top 5) of $a_0, ..., a_n$. Thanks to Palbos answer I think I got it. Let's define $C$ as the codification of the top m function such that C will be  $\le$ to m of the $a_0, ..., a_n$ and at least one of them will be equal to C.
Then the linear formulation will be:
\begin{align}
C &\ge a_i - l_i*M  &\forall i \in N \\ 
C &\le a_i + (1-u_i)*M  & \forall i \in N \\
\sum_{i \in N} l_i &= m -1\\
\sum_{i \in N} u_i &= m
\end{align}
where the $l_i \in \{0,1\}$ and $u_i \in \{0,1\}$ are binary variables.
$u_i$ indicates the top m. $a_i$ is in the top m if $u_i = 1$.
Since exactly m-1 of the lower bound constraints and m of the upper bound constraints can be violated, C will be $\ge$ than all except m-1 of the $a_i$ and $\le$ than all except m of the $a_i$.
This should set it equal the $m_{th}$ largest $a_i$. If we removed the -1 in the lower bounds then C could be anywhere between the $m_{th}$ largest $a_i$ and the $m+1_{th}$ largest $a_i$
