Pyomo Optimization Modeling Framework
Pyomo is an open-source Python-based optimization modeling language that allows users to formulate optimization problems in a high-level, object-oriented fashion. It supports a wide range of problem types including linear, mixed-integer, nonlinear, and generalized disjunctive programming. Pyomo provides interfaces to numerous commercial and open-source solvers. It is currently at version 6.10.0 and sees frequent releases with bug fixes, performance enhancements, and new solver interfaces.
Warnings
- breaking Pyomo regularly drops support for older Python versions. Pyomo 6.10.0 removes support for Python 3.9. Pyomo 6.9.x removed support for Python 3.8.
- gotcha Pyomo itself does NOT include optimization solvers. You must install external solver executables (e.g., GLPK, CBC, Ipopt, Gurobi, CPLEX) on your system and ensure they are in your system's PATH, or configure Pyomo with their specific location.
- breaking Internal data storage for Constraint objects changed significantly in Pyomo 6.8.0. Code that directly accessed or manipulated internal Constraint attributes might break.
- gotcha While `import pyomo.environ as pyo` is convenient, for very large or performance-critical models, using direct imports from specific submodules (e.g., `from pyomo.core import ConcreteModel, Var`) can improve startup performance by avoiding the overhead of `environ`'s extensive imports.
- deprecated The hard dependency on `ply` was removed in Pyomo 6.10.0. While not strictly a breaking change for most users, if you were implicitly relying on `ply` being installed alongside Pyomo, you might need to add it as an explicit dependency for your project.
Install
-
pip install pyomo -
pip install pyomo[optional_dependency]
Imports
- pyomo.environ
import pyomo.environ as pyo
- ConcreteModel
model = pyo.ConcreteModel()
- Var
model.x = pyo.Var(bounds=(0, 10))
- Constraint
model.con = pyo.Constraint(expr=...)
- Objective
model.obj = pyo.Objective(expr=..., sense=pyo.maximize)
- SolverFactory
solver = pyo.SolverFactory('glpk')
Quickstart
import pyomo.environ as pyo
# Create a concrete model
model = pyo.ConcreteModel()
# Define variables
model.x = pyo.Var(bounds=(0, 10), within=pyo.Reals)
model.y = pyo.Var(bounds=(0, 10), within=pyo.Reals)
# Define objective function: Maximize x + y
model.obj = pyo.Objective(expr=model.x + model.y, sense=pyo.maximize)
# Define constraints
model.con1 = pyo.Constraint(expr=2*model.x + model.y <= 15)
model.con2 = pyo.Constraint(expr=model.x + 3*model.y <= 20)
# Solve the model (requires a solver, 'glpk' is a common choice)
# Ensure 'glpk' or another solver is installed and accessible in your system PATH.
# For example, on Ubuntu: 'sudo apt-get install glpk-utils'
# On macOS: 'brew install glpk'
# On Windows: download GLPK binaries and add to PATH.
try:
solver = pyo.SolverFactory('glpk')
results = solver.solve(model, tee=False) # tee=True shows solver output
# Check solver status and print results
if (results.solver.status == pyo.SolverStatus.ok) and \
(results.solver.termination_condition == pyo.TerminationCondition.optimal):
print(f"Optimization successful!")
print(f"Objective value: {pyo.value(model.obj)}")
print(f"x = {pyo.value(model.x)}")
print(f"y = {pyo.value(model.y)}")
else:
print(f"Solver did not find an optimal solution. Status: {results.solver.status}, Termination: {results.solver.termination_condition}")
except Exception as e:
print(f"Error solving model: {e}")
print("Please ensure a solver like 'glpk' is installed and configured for Pyomo.")