Optimization modelling in Python
The Python programming language is used for a huge variety of data science applications, including optimization modelling. We link to the most commonly-used Python optimization modelling libraries, including: Pyomo, PuLP, OR-Tools, SciPy, and Gekko.
CVXPY is an open source Python-embedded modeling language for convex optimization problems. It lets you express your problem in a natural way that follows the math, rather than in the restrictive standard form required by solvers.
CVXPY can solve the following types of model:
- Convex programming.
- A generalization of geometric programming.
- Mixed-integer convex programs.
- Quasi-convex programs.
CVXPY relies on the open source solvers OSQP, SCS, and ECOS. Additional solvers are supported but must be installed separately.
For more information: CVXPY
GEKKO is an object-oriented Python library that facilitates model construction, analysis tools, and visualization of simulation and optimization in a single package.
As a differential and algebraic modeling language, it facilitates the use of advanced modeling and solvers. Gekko simplifies the process by allowing the model to be written in a simple, intuitive format. It accepts a model consisting of constraints and an objective to optimize.
Gekko provides versatility with objects designed to model, estimate, optimize and control in an easily readable Python environment:
- Free for academic and commercial use (MIT License).
- Hundreds of industrial applications.
- Compiled bytecode backend for efficient solutions.
- Automatic differentiation for exact first and second derivatives.
- Interior point (IPOPT, BPOPT) and active set SQP (APOPT) solvers.
For more information: Gekko
OR-Tools is open source software for combinatorial optimization, which seeks to find the best solution to a problem out of a very large set of possible solutions.
Examples of problems that OR-Tools solves include:
- Vehicle routing: Find optimal routes for vehicle fleets that pick up and deliver packages given constraints (e.g., "this truck can't hold more than 20,000 pounds" or "all deliveries must be made within a two-hour window").
- Scheduling: Find the optimal schedule for a complex set of tasks, some of which need to be performed before others, on a fixed set of machines, or other resources.
- Bin packing: Pack as many objects of various sizes as possible into a fixed number of bins with maximum capacities.
In addition to these specialized applications, OR-Tools can solve general Constraint Programming problems along with Linear and Mixed-Integer Programming models.
For more information: OR-Tools in Python
PuLP is free, open source software written in Python. It is used to describe optimisation problems as mathematical models.
PuLP can generate MPS or LP files to solve linear and integer problems using any of the following solvers:
- GLPK.
- COIN-OR CLP/CBC.
- CPLEX.
- GUROBI.
- MOSEK.
- XPRESS.
- CHOCO.
- MIPCL.
- SCIP.
After calling the solver, PuLP can use Python commands to manipulate and display the solution.
For more information: PuLP
Pyomo is a Python-based, open-source algebraic modelling language (AML) with a diverse set of optimization capabilities. It uses an object-oriented design for the definition of optimization models.
In Pyomo, the basic steps of the modelling process are:
- Create model.
- Declare components.
- Instantiate the model.
- Apply solver.
- Interrogate solver results.
Access to a wide range of solvers is build-in, including CBC, GLPK, IPOPT, BARON, CPLEX, and Gurobi – though the solvers need to be installed separately. Pyomo can also send a model to the NEOS Server for solving in the cloud.
For more information: Pyomo
Python-MIP is a collection of Python tools for the modeling and solution of Mixed-Integer Linear programs (MIPs). Its syntax was inspired by PuLP, but the package also provides access to advanced solver features like cut generation, lazy constraints, MIP starts and solution pools.
The goals of Python-MIP are:
- Ease of use. Python-MIP is an intuitive high level modeling tool. Operator overloading makes it straightforward to write linear expressions.
- High performance. Yes, high performance with Python! Integration with C code and compatibility with PyPy compiler results in very fast model generation.
- Extensibility. Extensibility and configurability are priorities. Support to additional solvers and non-standard configurations can be easily included.
For more information: Python-MIP
SciPy is a widely-used and versatile open-source Python library. SciPy provides algorithms for optimization, integration, interpolation, eigenvalue problems, algebraic equations, differential equations, statistics and many other classes of problems.
When applied to optimization, SciPy provides functions for minimizing (or maximizing) objective functions, possibly subject to constraints, including solvers for:
- Non-linear problems (both local and global optimization algorithms).
- Linear and mixed-integer programming.
- Constrained and non-linear least-squares.
- Root finding.
- Curve fitting.
For more information: SciPy