None
Pyomo is an example of a recent generation of modeling languages that are fully integrated with an underlying scripting language.
The purpose of this tutorial is to introduce the basics of constructing Pyomo models. In this first example we consider the production of two products, 'A' and 'B', subject to constraints. The objective is to maximize profit
\begin{align} \max_{x,y} \mbox{profit} = 40 x + 30 y \end{align}subject to \begin{align} x & \leq 40 \\ x + 2y & \leq 80 \\ x + y & \leq 100 \end{align}
where $x$ refers the production of 'A', and $y$ to the production of 'B'.
At the highest level, Pyomo employs two objects -- a model and a solver -- to generate a resulting solution.
from pyomo.environ import *
model = ConcreteModel()
solver = SolverFactory('glpk')
from pyomo.environ import *
# create a model
model = ConcreteModel()
# index for x
idx = ['Product A', 'Product B']
# create decision variables
model.x = Var(idx, domain=NonNegativeIntegers)
# create objective
model.maxprofit = Objective(expr = 40*model.x['Product A'] + 30*model.x['Product B'], sense=maximize)
# create constraints
model.constraints = ConstraintList()
model.constraints.add(model.x['Product A'] <= 40)
model.constraints.add(model.x['Product A'] + model.x['Product B'] <= 80.5)
model.constraints.add(model.x['Product A'] + model.x['Product B'] <= 100)
solver = SolverFactory('glpk')
results = solver.solve(model)
for i in idx:
print(i, model.x[i]())
Product A 0.0 Product B 80.0
data = {'penny':1, 'nickel':5, 'dime':10, 'quarter':25, 'half-dollar':50}
data
{'dime': 10, 'half-dollar': 50, 'nickel': 5, 'penny': 1, 'quarter': 25}
data.keys()
dict_keys(['penny', 'nickel', 'dime', 'quarter'])
data.values()
dict_values([1, 5, 10, 25])
model = ConcreteModel()
model.x = Var(data.keys(), domain=NonNegativeIntegers)
model.ncoins = Objective(expr = sum(model.x[c] for c in data.keys()), sense=minimize)
model.cons = Constraint(expr = 83 == sum(data[c]*model.x[c] for c in data.keys()))
solver = SolverFactory('glpk')
solver.solve(model)
model.pprint()
1 Set Declarations x_index : Dim=0, Dimen=1, Size=5, Domain=None, Ordered=False, Bounds=None ['dime', 'half-dollar', 'nickel', 'penny', 'quarter'] 1 Var Declarations x : Size=5, Index=x_index Key : Lower : Value : Upper : Fixed : Stale : Domain dime : 0 : 0.0 : None : False : False : NonNegativeIntegers half-dollar : 0 : 1.0 : None : False : False : NonNegativeIntegers nickel : 0 : 1.0 : None : False : False : NonNegativeIntegers penny : 0 : 3.0 : None : False : False : NonNegativeIntegers quarter : 0 : 1.0 : None : False : False : NonNegativeIntegers 1 Objective Declarations ncoins : Size=1, Index=None, Active=True Key : Active : Sense : Expression None : True : minimize : x[penny] + x[nickel] + x[dime] + x[quarter] + x[half-dollar] 1 Constraint Declarations cons : Size=1, Index=None, Active=True Key : Lower : Body : Upper : Active None : 83.0 : x[penny] + 5*x[nickel] + 10*x[dime] + 25*x[quarter] + 50*x[half-dollar] : 83.0 : True 4 Declarations: x_index x ncoins cons