June 2022

Python optimization Rosetta Stone

22 June 2022 (1,222 words)

Rosetta Stone

To start our exploration of optimization modelling in Python, we'll build the same linear programming model using several Python tools. We will, in a sense, develop a Rosetta Stone – with the same model translated into the different "languages" of the Python optimization modelling libraries. Our main purpose is to see how the libraries compare when applied to the same task.

Specifically, we'll replicate the linear program described in our article Production mix via graphical LP. That version of the Production mix model was built in Excel, using Solver/OpenSolver. In this series of articles, we'll build the Production mix model using the Python libraries:

  • Pyomo.
  • PuLP.
  • OR Tools.
  • Gekko.
  • CVXPY.
  • SciPy.

These are, as far as we're aware, the Python libraries that are most used for building linear programming models. Later, we'll add another library, Python MIP, when we use a variety of libraries to build mixed integer programming models.


Set up a Python modelling environment

9 June 2022 (2,271 words)

Hello world!

A common issue encountered by new Python optimization modellers is setting up a Python environment. There are many libraries that can be used, and some – like Pyomo – require solvers to be installed separately. Getting everything working can be tricky and frustrating.

So, in this article we'll describe the steps we used to set up a new virtual environment, including Python, Jupyter Lab, several optimization modelling libraries, and a selection of solvers. We'll use this environment for subsequent blog articles about building and solving optimization models in Python.

We hope this article helps you create a working Python environment that enables you to replicate our models and build your own models. Note that we are using 64-bit Windows 10, so everything we do is in that context. If you are using a different operating system, then you'll need to adapt the instructions accordingly.


Python, welcome

3 June 2022 (353 words)

Python optimization tools

We're expanding the scope of this blog to include optimization modelling using Python.

Python is a popular programming language for many data science applications, including optimization modelling. A key feature of Python is the availability of many packages for building and solving optimization models, including:

  • CVXPY. Modeling language for convex optimization problems.
  • Gekko. Modelling language for machine learning and optimization.
  • OR-Tools. General purpose linear, mixed integer, and constraint programming, plus specific tools for vehicle routing and graph algorithms.
  • PuLP. Optimization modelling language written in Python.
  • Pyomo. Optimization modelling language with a diverse set of optimization capabilities.
  • Python-MIP. Collection of tools for the modelling and solution of mixed integer linear programs.
  • SciPy. General purpose numerical and scientific computing library.

These tools provide access to a variety of solvers, which help us solve a wide range of problem types. In addition to the solvers built into some of the packages, we'll also use:

  • Bonmin. A solver for general mixed integer non-linear programs.
  • CBC. A linear and mixed integer program solver.
  • Couenne. A solver that aims to find global optima of non-convex mixed integer non-linear programs.
  • GLPK. Package for solving large-scale linear and mixed integer programs.
  • Ipopt. Package for large-scale non-linear optimization.
  • NEOS Server. Internet-based service that provides remote access to many solvers.
  • Octeract. A global mixed integer non-linear solver.

As discussed in our article Optimization in Excel vs Python, the choice of optimization tool depends on the circumstances, including the model's features and the modellers' and users' familiarity with the tools. As optimization tools, Excel and Python both have advantages and disadvantages. By expanding our blog's scope to include Python tools, we're looking to enable you to use the approach that works best in your situation.

We'll focus on free and open-source packages and solvers, though the concepts can also be applied to commercial software like CPLEX and Gurobi.

In our next article, we'll step through the process of setting up an environment for Python modelling. In subsequent articles, we'll explore a variety of Python optimization packages, sometimes contrasting a Python model implementation with an Excel implementation.


Results 1 - 3 of 3