Pyomo
Pyomo is a Python-based open-source optimization modeling language that enables users to define and solve mathematical optimization problems. It provides a flexible and expressive way to formulate optimization models using Python syntax.
Key Features
- Modeling Language: Pyomo allows users to define variables, objectives, and constraints using a high-level, declarative syntax.
- Solver Integration: Pyomo can interface with a variety of optimization solvers, both open-source and commercial, to solve the defined models.
- Extensibility: Pyomo is designed to be extensible, allowing users to create custom components and integrate with other Python libraries.
Solvers
Pyomo supports a wide range of solvers, including:
GLPK(GNU Linear Programming Kit) - Open-source solver for linear programming (LP) and mixed-integer programming (MIP). Based on simplex and interior-point methods.IPOPT- Open-source solver for large-scale nonlinear programming (NLP) problems. Based on interior-point methods.
Example usage
Question (Modeling & Formulation)
A small workshop produces two products, Product X and Product Y. Each unit of X yields a profit of $40, and each unit of Y yields $30. At most 40 units of X can be sold. Producing one unit of X or Y requires 1 hour of Labor A. A total of 80 hours of labor A are available. X requires 2 hours and Y requires 1 hour of Labor B. A total of 100 hours of labor B are available. Formulate a linear program to determine the production quantities that maximize profit.
- Decision Variables: X, Y
- Objective:
- Constraints
import pyomo.environ as pyo
model = pyo.ConcreteModel()
# define decision varsmodel.x = pyo.Var(domain=pyo.NonNegativeReals)model.y = pyo.Var(domain=pyo.NonNegativeReals)
# define objective functionmodel.profit = pyo.Objective(expr = 40*model.x + 30*model.y, sense=pyo.maximize)
# define constraintsmodel.constr1 = pyo.Constraint(expr = model.x + model.y <= 80)model.constr2 = pyo.Constraint(expr = 2*model.x + model.y <= 100)
# print modelmodel.pprint()
# Solveresults = pyo.SolverFactory('glpk').solve(model) #glpk is for linear programming, based on simplex method
# Print resultsprint("profit= ", model.profit())
model.display()You can also use matrices to write the constraints.
import pyomo.environ as pyo
c = [40, 30]A = [[1,0], [1,1], [2,1]]b = (40,80,100)
model = pyo.ConcreteModel()
model.x = pyo.Var(range(2), domain=pyo.NonNegativeReals)model.profit = pyo.Objective(expr = sum(c[i]*model.x[i] for i in range(2)), sense=pyo.maximize)model.Constraint_list = pyo.ConstraintList()for i in range(3): model.Constraint_list.add(expr = sum(A[i][j]*model.x[j] for j in range(2)) <= b[i])Output:
2 Var Declarations x : Size=1, Index=None Key : Lower : Value : Upper : Fixed : Stale : Domain None : 0 : None : None : False : True : NonNegativeReals y : Size=1, Index=None Key : Lower : Value : Upper : Fixed : Stale : Domain None : 0 : None : None : False : True : NonNegativeReals
1 Objective Declarations profit : Size=1, Index=None, Active=True Key : Active : Sense : Expression None : True : maximize : 40*x + 30*y
3 Constraint Declarations constr1 : Size=1, Index=None, Active=True Key : Lower : Body : Upper : Active None : -Inf : x + y : 100.0 : True constr2 : Size=1, Index=None, Active=True Key : Lower : Body : Upper : Active None : -Inf : 2*x + y : 150.0 : True constr3 : Size=1, Index=None, Active=True Key : Lower : Body : Upper : Active None : -Inf : x : 40.0 : True
6 Declarations: x y profit constr1 constr2 constr3profit= 3400.0Model unknown
Variables: x : Size=1, Index=None Key : Lower : Value : Upper : Fixed : Stale : Domain None : 0 : 40.0 : None : False : False : NonNegativeReals y : Size=1, Index=None Key : Lower : Value : Upper : Fixed : Stale : Domain None : 0 : 60.0 : None : False : False : NonNegativeReals
Objectives: profit : Size=1, Index=None, Active=True Key : Active : Value None : True : 3400.0
Constraints: constr1 : Size=1 Key : Lower : Body : Upper None : None : 100.0 : 100.0 constr2 : Size=1 Key : Lower : Body : Upper None : None : 140.0 : 150.0 constr3 : Size=1 Key : Lower : Body : Upper None : None : 40.0 : 40.0