Modeling with LPs |
Now that we can solve arbitrary LPs, let's see what other problems we can solve with it.
The first real LP is the "Stigler Diet" (1945)
Question: What is the cheapest diet (out of 77 foods) that provides enough calories and a minimum of 8 different nutrients?
The model is very similar to our Muffin Problem
\[ \sum_i a_{ji} x_i \geq b_j \qquad \forall j \]
Next example: We have 3 Muffin factories with different production capacities in
For the muffins, we have the following markets with different demands:
We want to transport the muffins from the factories to the sales markets in a way that minimizes the number of muffin kilometers and meets the demand everywhere.
We assume that the distance between factory $i$ and market $j$ is given as $\text{distance}_{ij}$.
Which variables do we need?
For each factory $i$ and market $j$, we need a variable $x_{ij} \geq 0$ that represents how many muffins should be transported from $i$ to $j$.
Objective function:
\[ \min \sum_{i,j} \text{distance}_{ij} x_{ij} \]From a factory $i$, no more products can be delivered than can be produced there:
\[ \sum_j x_{ij} \leq \text{capacity}_i \qquad \forall i \]At a market $j$, at least as much goods must be transported as there is demand:
\[ \sum_i x_{ij} \geq \text{demand}_j \qquad \forall j \]The whole model:
\[ \begin{aligned} \min\quad & \sum_{i,j} \text{distance}_{ij} x_{ij} \\ \text{s.t.}\quad &\sum_j x_{ij} \leq \text{capacity}_i \quad & \forall i \\ &\sum_i x_{ij} \geq \text{demand}_j \quad & \forall j \\ &x_{ij} \geq 0 \quad & \forall i,j \end{aligned} \]New Example:
Which variables do we need?
For each person $i$ and group $j$, a variable $x_{ij} \geq 0$ is needed, representing whether person $i$ is in group $j$.
Let $p_{ij}$ be the preference of $i$ for group $j$
Objective function:
\[ \max \sum_{i,j} p_{ij} x_{ij} \]Every person is only allowed to join one group:
\[ \sum_j x_{ij} = 1 \qquad \forall i \]Each group may only be booked by a maximum of 5 people.
\[ \sum_i x_{ij} \leq 5 \qquad \forall j \]Modification: We now want to ensure that the smallest preference is maximized.
We introduce a new variable $x_\text{minpref}$ for the minimum preference.
The new variable must not exceed any of the preferences:
\[ x_\text{minpref} \leq \sum_j p_{ij} x_{ij} \qquad \forall i \]General Modeling Trick:
We can determine the minimum of multiple variables using an auxiliary variable that is maximized in the objective function.
Analogously, the maximum of multiple variables can be determined using minimization in the objective function.
Similar: We have a variable $x$ that can potentially take negative values. We want to minimize its absolute value $|x|$:
\[ \begin{aligned} \min\quad & x_\text{abs} \\ \text{s.t.}\quad & x_\text{abs} \geq -x \\ & x_\text{abs} \geq x \end{aligned} \]The trick here is that for the auxiliary variable, one condition always applies and the other one is irrelevant. The direction of the objective function always determines the correct condition.
Next Example: We are looking for the shortest path from A to D through a network of nodes.
Of course, we can solve this problem differently (e.g. using Dijkstra's algorithm).
As a starting point for more complex models, we still want to model it as an LP.
Most important question: What are the variables?
For an edge $ij$ from node $i$ to $j$, we take a variable $x_{ij} \geq 0$ that indicates whether we traverse the edge.
We want to minimize the sum of the edge weights:
\[ \min 2 x_{AB} + 4 x_{AC} + 1 x_{BC} + 1 x_{CB} +\\ 3 x_{BD} + 1 x_{CD} \]
We need to make sure that we leave node A:
\[ x_{AB} + x_{AC} = 1 \]
When entering node B, we also have to leave it again:
\[ x_{AB} + x_{CB} = x_{BC} + x_{BD} \]
Analog: When entering node C, we must also leave it:
\[ x_{AC} + x_{BC} = x_{CB} + x_{CD} \]
Optional: We want to re-enter node D as well.
\[ x_{BD} + x_{CD} = 1 \]
Why "Optional"?
Now we can search for shortest paths in any graph.
Next step: What if we want to send more than one resource through the network?
Now we have not only the costs per edge, but also capacities:
Two different objectives possible:
First: Maximize flow from A to D (Max Flow)
As before, we take a variable $x_{ij} \geq 0$ for each edge $ij$, in which we represent the flow from $i$ to $j$.
The constraints work the same way as in the shortest paths: what enters a node must also leave it:
\[ x_{AB} + x_{CB} = x_{BC} + x_{BD}\\ x_{AC} + x_{BC} = x_{CB} + x_{CD} \]Generally speaking, for a node $i$:
\[ \sum_{j: i \text{ has an edge to } j} x_{ij} - \sum_{j: j \text{ has an edge to } i} x_{ji}= 0 \]"Flow conservation"
For each individual edge, we need to ensure that the capacity is not exceeded:
\[ x_{AB} \leq 2 \quad x_{AC} \leq 3 \\ x_{BD} \leq 1 \quad x_{CD} \leq 2 \\ x_{BC} + x_{CB} \leq 5 \]
As an objective function, we want to maximize the flow leaving node A:
\[ \max x_{AB} + x_{AC} \]
Alternatively, we could also demand that the maximum flow arrives at D.
Now let's consider the costs
(Min-Cost Flow)
Minimize the costs for a flow of 3 units from A to D.
We start from the current model and modify the objective function:
\[ \min 2 x_{AB} + 4 x_{AC} + 1 x_{BC} + 1 x_{CB} +\\ 3 x_{BD} + 1 x_{CD} \]
Analogous to the shortest path.
We don't need to touch the constraints for flow conservation and edge capacity.
We need a new condition for A or B (or both):
\[ x_{AB} + x_{AC} = 3 \\ x_{BD} + x_{CD} = 3 \]
Generalized flow conservation:
\[ \sum_{j: i \text{ has edge to } j} x_{ij} - \sum_{j: j \text{ has edge to } i} x_{ji}= d_i \]$d_i$ is the difference between the inflow and outflow of the node: Positive for a source and negative for a sink.
In this formulation, we can generally easily incorporate multiple sources and sinks.