pyomo
An object-oriented algebraic modeling language in Python for structured optimization problems.
Top Related Projects
Quick Overview
Pyomo is an open-source optimization modeling language in Python. It provides a powerful and intuitive way to formulate and solve complex optimization problems, supporting a wide range of problem types including linear, nonlinear, integer, and mixed-integer programming.
Pros
- Flexible and extensible framework for modeling optimization problems
- Supports a variety of solvers and can interface with commercial and open-source solvers
- Integrates well with other Python scientific computing libraries
- Active development and community support
Cons
- Steeper learning curve compared to some other optimization libraries
- Performance can be slower for large-scale problems compared to specialized solvers
- Documentation can be overwhelming for beginners
- Some advanced features may require additional dependencies
Code Examples
- Simple linear programming problem:
from pyomo.environ import *
model = ConcreteModel()
model.x = Var([1,2], domain=NonNegativeReals)
model.OBJ = Objective(expr = 2*model.x[1] + 3*model.x[2])
model.Constraint1 = Constraint(expr = 3*model.x[1] + 4*model.x[2] >= 1)
solver = SolverFactory('glpk')
results = solver.solve(model)
model.display()
- Creating a transportation problem:
from pyomo.environ import *
model = ConcreteModel()
model.i = Set(initialize=['Warehouse1', 'Warehouse2'])
model.j = Set(initialize=['Customer1', 'Customer2', 'Customer3'])
model.x = Var(model.i, model.j, bounds=(0,None))
def obj_rule(model):
return sum(model.x[i,j] for i in model.i for j in model.j)
model.obj = Objective(rule=obj_rule, sense=minimize)
def supply_rule(model, i):
return sum(model.x[i,j] for j in model.j) <= 100
model.supply = Constraint(model.i, rule=supply_rule)
def demand_rule(model, j):
return sum(model.x[i,j] for i in model.i) >= 50
model.demand = Constraint(model.j, rule=demand_rule)
- Solving a nonlinear problem:
from pyomo.environ import *
model = ConcreteModel()
model.x = Var(initialize=1.0)
model.y = Var(initialize=1.0)
def obj_rule(model):
return model.x**2 + model.y**2
model.obj = Objective(rule=obj_rule, sense=minimize)
def con_rule(model):
return model.x + model.y >= 1
model.con = Constraint(rule=con_rule)
solver = SolverFactory('ipopt')
results = solver.solve(model)
model.display()
Getting Started
-
Install Pyomo and a solver (e.g., GLPK):
pip install pyomo pip install glpk
-
Create a simple model:
from pyomo.environ import * model = ConcreteModel() model.x = Var(domain=NonNegativeReals) model.y = Var(domain=NonNegativeReals) model.obj = Objective(expr = model.x + 2*model.y, sense=maximize) model.con = Constraint(expr = 3*model.x + 4*model.y <= 12) solver = SolverFactory('glpk') results = solver.solve(model) model.display()
This example creates a simple linear programming model, solves it using GLPK, and displays the results.
Competitor Comparisons
Modeling language for Mathematical Optimization (linear, mixed-integer, conic, semidefinite, nonlinear)
Pros of JuMP
- Written in Julia, offering better performance and native integration with Julia's ecosystem
- More extensive support for nonlinear optimization problems
- Active development with frequent updates and improvements
Cons of JuMP
- Limited to Julia programming language, while Pyomo supports Python
- Smaller user community compared to Pyomo, potentially leading to fewer resources and third-party extensions
Code Comparison
JuMP example:
using JuMP, GLPK
model = Model(GLPK.Optimizer)
@variable(model, x >= 0)
@variable(model, y >= 0)
@constraint(model, 2x + y <= 4)
@objective(model, Max, 5x + 3y)
optimize!(model)
Pyomo example:
from pyomo.environ import *
model = ConcreteModel()
model.x = Var(domain=NonNegativeReals)
model.y = Var(domain=NonNegativeReals)
model.c = Constraint(expr=2*model.x + model.y <= 4)
model.obj = Objective(expr=5*model.x + 3*model.y, sense=maximize)
Both examples demonstrate the creation of a simple linear programming model with two variables, one constraint, and an objective function to maximize.
COIN-OR Interior Point Optimizer IPOPT
Pros of Ipopt
- Highly efficient and robust nonlinear optimization solver
- Specialized for large-scale problems with continuous variables
- Widely used in industrial applications and academic research
Cons of Ipopt
- Limited to continuous optimization problems
- Requires more low-level implementation compared to Pyomo
- Steeper learning curve for users new to optimization
Code Comparison
Ipopt (C++ interface):
#include "IpIpoptApplication.hpp"
#include "IpTNLP.hpp"
// Define problem class inheriting from TNLP
class MyNLP : public Ipopt::TNLP {
// Implement problem-specific methods
};
int main() {
Ipopt::SmartPtr<Ipopt::IpoptApplication> app = IpoptApplicationFactory();
app->Initialize();
// Set options and solve
}
Pyomo:
from pyomo.environ import *
model = ConcreteModel()
model.x = Var()
model.y = Var()
model.obj = Objective(expr=model.x**2 + model.y**2)
model.con = Constraint(expr=model.x + model.y >= 1)
solver = SolverFactory('ipopt')
results = solver.solve(model)
Pyomo provides a higher-level, more user-friendly interface for defining and solving optimization problems, while Ipopt offers a lower-level, more specialized approach for nonlinear optimization. Pyomo can use Ipopt as a solver, combining the strengths of both tools.
Google's Operations Research tools:
Pros of OR-Tools
- Faster solving times for large-scale optimization problems
- Broader range of supported algorithms and solvers
- More comprehensive documentation and examples
Cons of OR-Tools
- Steeper learning curve for beginners
- Less flexibility in model formulation compared to Pyomo
- Primarily focused on C++, with Python bindings
Code Comparison
Pyomo example:
from pyomo.environ import *
model = ConcreteModel()
model.x = Var(domain=NonNegativeReals)
model.y = Var(domain=NonNegativeReals)
model.obj = Objective(expr=model.x + 2*model.y, sense=maximize)
model.con = Constraint(expr=3*model.x + 4*model.y <= 12)
OR-Tools example:
from ortools.linear_solver import pywraplp
solver = pywraplp.Solver.CreateSolver('GLOP')
x = solver.NumVar(0, solver.infinity(), 'x')
y = solver.NumVar(0, solver.infinity(), 'y')
solver.Add(3 * x + 4 * y <= 12)
solver.Maximize(x + 2 * y)
Both libraries offer powerful optimization capabilities, but OR-Tools generally provides better performance for large-scale problems and supports a wider range of algorithms. Pyomo, on the other hand, offers more flexibility in model formulation and is often easier for beginners to grasp. The choice between the two depends on the specific requirements of your optimization project.
A Python-embedded modeling language for convex optimization problems.
Pros of CVXPY
- More intuitive syntax for defining optimization problems
- Better support for convex optimization and disciplined convex programming
- Faster solution times for certain problem types
Cons of CVXPY
- Limited support for mixed-integer programming compared to Pyomo
- Less flexibility in modeling complex, non-convex problems
- Smaller ecosystem and fewer solver interfaces
Code Comparison
CVXPY example:
import cvxpy as cp
x = cp.Variable()
y = cp.Variable()
constraints = [x + y == 1, x >= 0, y >= 0]
objective = cp.Minimize(cp.square(x) + cp.square(y))
problem = cp.Problem(objective, constraints)
problem.solve()
Pyomo example:
from pyomo.environ import *
model = ConcreteModel()
model.x = Var(domain=NonNegativeReals)
model.y = Var(domain=NonNegativeReals)
model.c = Constraint(expr=model.x + model.y == 1)
model.obj = Objective(expr=model.x**2 + model.y**2, sense=minimize)
solver = SolverFactory('ipopt')
results = solver.solve(model)
Both CVXPY and Pyomo are powerful optimization modeling libraries, but they have different strengths. CVXPY excels in convex optimization problems with a more intuitive syntax, while Pyomo offers greater flexibility for complex, non-convex problems and mixed-integer programming.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Pyomo Overview
Pyomo is a Python-based open-source software package that supports a diverse set of optimization capabilities for formulating and analyzing optimization models. Pyomo can be used to define symbolic problems, create concrete problem instances, and solve these instances with standard solvers. Pyomo supports a wide range of problem types, including:
- Linear programming
- Quadratic programming
- Nonlinear programming
- Mixed-integer linear programming
- Mixed-integer quadratic programming
- Mixed-integer nonlinear programming
- Mixed-integer stochastic programming
- Generalized disjunctive programming
- Differential algebraic equations
- Mathematical programming with equilibrium constraints
- Constraint programming
Pyomo supports analysis and scripting within a full-featured programming
language. Further, Pyomo has also proven an effective framework for
developing high-level optimization and analysis tools. For example, the
mpi-sppy
package provides generic
solvers for stochastic programming. mpi-sppy
leverages the fact that
Pyomo's modeling objects are embedded within a full-featured high-level
programming language, which allows for transparent parallelization of
subproblems using Python parallel communication libraries.
Pyomo was formerly released as the Coopr software library.
Pyomo is available under the BSD License - see the
LICENSE.md
file.
Pyomo is currently tested with the following Python implementations:
- CPython: 3.8, 3.9, 3.10, 3.11, 3.12
- PyPy: 3.9
Testing and support policy:
At the time of the first Pyomo release after the end-of-life of a minor Python version, we will remove testing for that Python version.
Installation
PyPI
pip install pyomo
Anaconda
conda install -c conda-forge pyomo
Tutorials and Examples
- Pyomo â Optimization Modeling in Python
- Pyomo Workshop Slides
- Prof. Jeffrey Kantor's Pyomo Cookbook
- The companion notebooks for Hands-On Mathematical Optimization with Python
- Pyomo Gallery
Getting Help
To get help from the Pyomo community ask a question on one of the following:
Developers
Pyomo development moved to this repository in June 2016 from Sandia National Laboratories. Developer discussions are hosted by Google Groups.
The Pyomo Development team holds weekly coordination meetings on Tuesdays 12:30 - 14:00 (MT). Please contact wg-pyomo@sandia.gov to request call-in information.
By contributing to this software project, you are agreeing to the following terms and conditions for your contributions:
- You agree your contributions are submitted under the BSD license.
- You represent you are authorized to make the contributions and grant the license. If your employer has rights to intellectual property that includes your contributions, you represent that you have received permission to make contributions and grant the required license on behalf of that employer.
Related Packages
See https://pyomo.readthedocs.io/en/latest/related_packages.html.
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot