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 lot depends on how the objective function looks like. – Erwin Kalvelagen Sep 26 '17 at 22:05
-
Take a look at (https://stackoverflow.com/q/10792139/5802041). – Jean Marie Sep 26 '17 at 22:17
3 Answers
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)$.
-
1
-
@opt how is b chosen? could you look https://math.stackexchange.com/questions/2749811/on-a-condition-in-real-linear-programming? – Turbo Apr 23 '18 at 07:22
-
@Robut The current accepted answer to that question is correct. $b$ is a (binary) decision variable. If you only have one binary variable (i.e., $b$) in your model then you must solve two independent linear programs corresponding to $b=0$ and $b=1$ and then compare the optimal values of the two linear programs. The better one determines whether $b=0$ is optimal or $b=1$ (recall that $b$ is a decision variable). – Apr 23 '18 at 13:48
-
@Robut In general, mixed integer linear programs are NP-Hard. Simply put, such problems become exponentially hard to solve by increase in the number of variables and constraints. If you have $n$ binary variables, in the worst case you must solve $2^n$ linear programs. However, there are efficient algorithms to solve mixed integer linear problems and such algorithms are advancing rapidly. Standard linear solvers such as CPLEX and GUROBI are able to solve such problems in a reasonable amount of time. – Apr 23 '18 at 13:55
-
-
2In fact, one can take $M$ to be the maximum possible value of $|A-B|$. It just needs to be big enough so that if $A\ge B$ then $C\le B+M$ is feasible, and if $B\ge A$ then $C\le A+M$ is feasible. – Timothy Chow Apr 29 '20 at 14:53
-
Hi! How should I to modify these constraints to get the minimum instead of the maximum value? – Á. Garzón Jul 11 '22 at 16:56
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".

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

- 1