3.6 Facility location problem
This notebook illustrates Example 20 from Chapter 3 of the textbook Hands-On Mathematical Optimization with Python, Cambridge University Press, 2024.
For details about these models and more information we refer to the book.
Also notice that at the end of Chapter 3 you may find a number of exercises that show that versions of this problem aiming at maximizing the importance of the customers served, when facing constraints that limit the possibility of serving all, leads to even more striking evidence for the importance of selecting the right model: it can offer large factors of speedup in solving the relevant instances!
Preamble: Install Pyomo and solvers
The following cell sets and verifies a Pyomo installation and the solver CBC. If run on Google Colab, the cell installs Pyomo and the CBC solver, while, if run elsewhere, it assumes they all have been previously installed.
Problem description
Consider the problem of a supplier facing the task of fulfilling specific customer demands with minimal costs while simultaneously deciding how many facilities to build and where. In terms of data, we are given a set of customers, a set of possible locations, the cost of building facility , and the cost incurred to satisfy the demands of customer at facility .
In this notebook we will formulate two equivalent models for this problem and compare their performance.
First MILO formulation
Introduce two sets of binary variables,
$$ \begin{equation*} x_j:= \begin{cases} 1 & \text{ if facility $jParseError: KaTeX parse error: Expected 'EOF', got '}' at position 11: is built,}̲\\ 0 & \tex…$
and
$$ \begin{equation*} y_{ij}:= \begin{cases} 1 & \text{ if customer $ijParseError: KaTeX parse error: Expected 'EOF', got '}' at position 2: ,}̲\\ 0 & \tex…$
The resulting MILO is
Second MILO formulation
Let be the number of possible facility locations and the number of customers. Note that, since for every , we can replace the constraints by only constraints, namely
The second MILO is
This approach leads to a more concise mathematical formulation of the model. However, it may not be a good idea if we want to solve the problem using its linear relaxation. Indeed, by reducing the number of constraints, we inadvertently made the feasible region of the relaxation larger and less tight around the feasible integer points. This fact becomes clearly evident in the increased run-time required to solve the optimization problem when working with these weaker constraints, see figures below.
Instead of defining two separate Pyomo models, we will define a single common model and then add either the strong or weaker formulation of the "facility built before use" constraint.
Solvers and options
The models
We introduce functions to generate random instances for the problem and to then visualize the optimal solution.
We then generate an instance of the problem with 10 facilities and 100 customers and initialize both a strong and a weak Pyomo models.
However, this is not a proper performance comparison between the two model formulations, since under the hood cbc is already solving the instance in a clever way. The commercial solver Gurobi and the open-source solver HiGHS also have a similar feature.
If run on Google Colab, the following cell installs three additional commercial solvers; if run elsewhere, it assumes they all have been previously installed.
We now run a more extensive performance comparison considering increasingly larger instances of the problem. We will see that the strong formulation is consistently faster than the weak formulation.
We solve instances with 10 to 100 facilities and 100 to 1000 customers. We then plot the run-time of the strong and weak formulations as a function of the number of facilities.