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.

## Articles in this series

Articles in the Python *Production mix* series:

- Python optimization Rosetta Stone
- Production mix - Model 1, Pyomo concrete
- Production mix - Model 2, Pyomo separate data
- Production mix - Model 3, Pyomo external data
- Production mix - Model 4, Pyomo json file
- Production mix - Model 5, Pyomo using def
- Production mix - Model 6, Pyomo abstract
- Production mix - Model 7, PuLP
- Production mix - Model 8, OR-Tools
- Production mix - Model 9, Gekko
- Production mix - Model 10, CVXPY
- Production mix - Model 11, SciPy
- Production mix - Conclusions

## Overview of the optimization libraries

In Figure 1 we show a summary of the libraries given some relevant attributes.

The last row of the figure indicates which library is our first choice for specific modelling situations. That is:

- Linear and integer programming models. We prefer Pyomo, which is a capable, general purpose optimization library. It has helpful modelling objects, good support for a range of solvers, and a large and active user community.
- Constraint satisfaction models. OR-Tools is our first choice for constraint satisfaction models, when a linear or integer programming model cannot be solved in a reasonable time. OR-Tools has a focus on applications like assignment, packing, scheduling, knapsack, network flow, and routing – so it is often the best library for those types of situations.
- General convex models. CVXPY provides a variety of tools for modelling and solving Disciplined Convex Programming (DCP) problems. If we need to look beyond linear and integer programming models, then CVXPY is our first choice.
- Global optimization. Non-linear models can be especially difficult to solve. SciPy provides a wide variety of tools and techniques for modelling and solving global optimization models, making it our first choice for this type of situation.

The following sections provide some additional detail for each library.

### Pyomo

#### Description

Pyomo is a Python-based, open-source, BSD license software package that supports a diverse set of optimization capabilities for formulating and analyzing optimization models.

#### Advantages

The Pyomo library is written specifically for building optimization models, so the syntax is designed to make the process relatively easy.

There is a large and active Pyomo community, which is useful when you need to find information to help create and debug models.

Pyomo supports a wide range of mathematical programming problem types, including linear, quadratic, non-linear, integer, and stochastic programming. It also supports other problem types, including generalized disjunctive programming, differential algebraic equations, bilevel programming, and equilibrium constraints.

The syntax for creating models mirrors the syntax of mathematical formulations, which makes the process of translating the maths into Python models relatively straightforward.

#### Disadvantages

The Pyomo package does not include any solvers, so you need to separately install solvers before Pyomo can be used. However, the installation process is straightforward, thanks in part to organizations like AMPL who make compiled solvers available.

#### Overall

Pyomo is our first choice Python library for linear and integer mathematical programming models. It is also acceptable for some types of non-linear models, when using solvers like Bonmin, Couenne, and Ipopt.

### PuLP

#### Description

PuLP is an open source, MIT license Python library for linear and integer programming.

#### Advantages

PuLP's modelling syntax is quite similar to Pyomo's syntax, with both being well-suited to translated mathematical formulations into Python models.

Some solvers are installed with PuLP, so it can be used without additional setup. Other solvers can also be added.

#### Disadvantages

The scope of PuLP is quite narrow, being focussed solely on linear and integer programs. While PuLP is a good tool for those types of models, the narrow scope can be quite limiting.

#### Overall

There is a close similarity between our PuLP model and our Pyomo models. Although the syntax of the two libraries is somewhat different in detail, the general structure of the model definitions and solution process is familiar. This isn't surprising, as both PuLP and Pyomo are COIN-OR projects.

Despite the similarities, we prefer Pyomo simply because it provides easier access to a wider range of solvers and support and more problem types, while PuLP offers no significant advantages relative to Pyomo.

### OR-Tools

#### Description

OR-Tools is open source, Apache 2.0 license software for solving constraint programming, linear programming, and integer programming models. OR-Tools models can be written in Python, C++, C#, and Java.

#### Advantages

OR-Tools has solvers for specific types of constraint programming problems, including assignment, packing, scheduling, knapsack, network flow, and routing. This makes solving these types of problems somewhat easier than using a general-purpose language.

The CP-SAT solver is a particularly versatile constraint satisfaction engine for solving integer problems. With constraint satisfaction solvers there is generally no guarantee that an optimal solution will be found – though often a good solution is sufficient.

OR-Tools has a large and active community, so it is easy to find information to help create and debug OR-Tools models.

#### Disadvantages

The built-in GLOP solver for linear and integer programs is OK but not great. OR-Tools also provides access to the SCIP solver, which is better. Until late 2022, SCIP had a restrictive license, but since then it has a license that allows a much wider range of uses.

The online documentation is somewhat lacking if you want to do something that isn't covered by the available examples.

#### Overall

OR-Tools is OK for solving linear and integer programming models. Where OR-Tools really excels is solving constraint programming problems, where OR-Tools is our first choice Python library.

### Gekko

#### Description

Gekko is an open source, MIT license Python package for machine learning and optimization of mixed-integer and differential algebraic equations.

#### Advantages

Gekko's notation for defining variables, constraints, and the objective function is broadly like Pyomo's notation. This makes the model-building process straightforward, especially when implementing a model's mathematical formulation.

Gekko comes with pre-installed solvers that can handle a variety of problem types, which can be convenient when developing a model whose final form is not yet clear.

#### Disadvantages

There appears to be a much smaller community for Gekko compared with Pyomo. While the online documentation is good, if you encounter an issue, it can be more difficult to find information to help you solve that issue.

#### Overall

Our linear program model using Gekko is very similar to our Pyomo models. Although the syntax of the two libraries is somewhat different, the general structure of the model definitions and solution process is similar.

Despite the similarities, we tend to prefer Pyomo for solving linear programs, because Pyomo has a larger user base. In addition, Pyomo is focussed on optimization modelling, while Gekko is focussed more on machine learning and related applications, rather than optimization.

### CVXPY

#### Description

CVXPY is an open source, Apache 2.0 license Python package for representing and solving convex optimization problems. CVXPY uses the concept of Disciplined Convex Programming (DCP) to ensure that the optimization problem is convex. DCP restricts the model definition to a given library of base functions that are known to be convex.

#### Advantages

By restricting models to being convex, CVXPY can apply a variety of solution techniques and solvers. This approach makes CVXPY versatile and reliable, within the scope of convex optimization problems.

Convex optimization includes linear and integer programming, though it also covers a much broader range of situations.

CVXPY has a moderately sized user base, so there is some online support, if required.

#### Disadvantages

A central aspect of CVXPY modelling language is the use of matrix multiplication. While matrix notation can be mathematically succinct, it can be difficult to work with from a modeller's perspective.

Working out exactly how to build a model using Disciplined Convex Programming can be difficult. Like most programming languages, CVXPY's error messages can be quite cryptic, often providing little useful guidance about how to fix a model that isn't working properly.

#### Overall

CVXPY is a capable library for solving convex models. For convex models beyond linear and integer programs, CVXPY is our first choice Python library.

However, for linear and integer programming problems, CVXPY offers no significant advantage compared with Pyomo. Therefore, we tend to prefer Pyomo over CVXPY for this type of model.

### SciPy

#### Description

SciPy is an open source, BSD license 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.

#### Advantages

The SciPy library includes many functions for specific tasks, such as root finding, clustering, non-linear optimization, and global optimization. Consequently, it provides a versatile set of tools that can be applied to a wide range of situations.

SciPy has a large and active community, though most online content is related to features other than optimization modelling.

#### Disadvantages

For linear and integer mathematical programming problems, SciPy uses a matrix notation that reflects the standard definition of a mathematical program. Although some mathematical purists prefer matrix notation, from a modelling perspective it can be less intuitive. This is especially important when implementing a model from its formulation expressed in mathematical notation.

SciPy is not focussed solely on optimization modelling, so it lacks some of the specific modelling support that makes Pyomo easier to use for optimization problems.

#### Overall

SciPy is a capable modelling library, with a scope that is much broader than optimization.

For LP/MILP optimization problems, SciPy offers no significant advantage compared with Pyomo. Therefore, we tend to prefer Pyomo over SciPy for this type of model. But for non-convex global optimization problems, SciPy is our first choice Python library.

## Conclusion

We hope this article series is useful to you. If you would like to know more about optimization modelling in Python, or you want help with your own models, then please contact us.