26 May 2023
We often need to explore variations of a model. We might need to change model parameters, add/remove constraints, or change the objective function. These variations help us understand how different formulations or parameters affect the optimal solution.
In this article we explore two aspects of working with multiple variations of an optimization model:
- Modularization. We refactor an existing model into modules, consisting of functions located in separate files.
- Adding models. We extend the existing model by adding variations that make efficient use of the modular structure.
Along the way, we look at shallow vs deep copying in Python. This is a topic that often causes subtle bugs, so it is important to understand when working with variations of a model.
Our starting point is an existing model, created by Alireza Soroudi as part of his wonderful Optimization in open-source newsletter on LinkedIn. The specific model is Optimal advisor-advisee matching, used with the kind permission of Dr. Soroudi.
28 April 2023
Price breaks, or volume discounts, are common when buying products in bulk. That is, the marginal cost of additional products falls as volume increases. In a linear program, price breaks are tricky to model because the break points are non-linear discontinuities.
In a spreadsheet, a natural way to model price breaks is to use functions like IF
, VLOOKUP
, CHOOSE
, MIN
, and/or MAX
. However, those functions are discontinuous, so we can't use the Simplex linear method in Solver. We also can't use the OpenSolver solvers at all when our model includes those functions. We could use Solver's GRG non-linear or Evolutionary methods, but they are not always reliable.
Fortunately, there is a way to express price breaks linearly, by using some binary variables, as described in our article MIP formulations and linearizations.
In this article, we describe an example of how to represent price breaks in a linear programming model, built in Excel using Solver or OpenSolver.
3 April 2023
In the previous article, we built a staff scheduling optimization model in Excel, using OpenSolver. That model enumerates all possible shift patterns and then decides how many of each shift pattern to use so that we meet the various constraints at least cost.
Our focus in this article is on replicating the Excel model by translating it into Python, using the Pyomo library. That involves formalizing the mathematical formulation, creating appropriate data structures, and representing the formulation in Pyomo syntax. Both models use the CBC solver, so the differences between the models aren't in the solving process, but rather in the build process.
Excel can be a great tool for some types of optimization modelling, either for prototyping or as a production tool. Python offers a wider range of optimization options, and it is usually better for models that have varying data sizes or need to operate at a large scale. The advantages and disadvantages of modelling in Excel and Python are discussed in more detail in our article Optimization in Excel vs Python.
27 March 2023
A common application of optimization modelling is the scheduling of staff to meet demand. Scheduling problems can be difficult to solve, as there are often very specific requirements that need to be met, including staff availability, working hours, break times, etc.
One approach for formulating scheduling problems is to enumerate all possible shift patterns and then decide how many of each shift pattern to use so that we meet the various constraints at least cost.
Enumeration of all possible shift patterns is often not as difficult as it may sound. We used a similar technique in the model Green manufacturing via cutting patterns. In that situation, there were potentially thousands of possible patterns, requiring an automated process to generate them all. In the staff scheduling situation there is usually a much smaller number of possible patterns, so manual enumeration is often possible.
This article implements a staff scheduling model in Excel and solves it using the CBC solver via OpenSolver. The next article implements the same model in Python, solves it using the CBC solver via Pyomo, then compares the Excel and Python model versions.
16 March 2023
In this article we conclude the Python Production mix series. We provide a summary of the optimization libraries that we've used, including the advantages and disadvantages of each. We also indicate which library is our first choice for different types of modelling.
Our objective in this series has been to compare creating a simple linear programming model in a variety of Python libraries, using Pyomo as a baseline.
10 March 2023
In this article we continue the Python Production mix series. Specifically, we build Model 11 using the SciPy library.
SciPy is an open source Python package for a wide range of scientific computing applications, including optimization, integration, interpolation, eigenvalue problems, algebraic equations, differential equations, statistics and many other classes of problems.
Our objective is to compare a linear programming model built using SciPy with the same model built using Pyomo.
12 December 2022
In this article we continue the Python Production mix series. Specifically, we build Model 10 using the CVXPY library.
CVXPY is an open source Python package for representing and solving convex optimization problems.
Our objective is to compare a model built using CVXPY with the same model built using Pyomo.
22 November 2022
In this article we continue the Python Production mix series. Specifically, we build Model 9 using the Gekko library.
Gekko is a Python package for machine learning, optimization of mixed-integer, and differential algebraic equations.
Our objective is to compare a model built using Gekko with the same model built using Pyomo.
31 October 2022
In this article we continue the Python Production mix series. Specifically, we build Model 8 using the OR-Tools library.
OR-Tools is a project from Google. The library is freely available, the code is open source, and it is widely used.
Our objective is to compare a model built using OR-Tools with the same model built using Pyomo.
4 October 2022
In this article we continue the Python Production mix series. Specifically, we build Model 7 using the PuLP library.
PuLP, like Pyomo, is a COIN-OR project. The library is freely available, the code is open source, and it is widely used.
Our objective is to compare a model built using PuLP with the same model built using Pyomo.