This file refers to the prototype linear programming "WYNDOOR GLASS CO." example from Hillier and Lieberman's Introduction to Operations Research, Section 3.1.
We show how to solve the problem using the Pyomo modeling environment in the simplest possible way.
Step 1: Load the Pyomo environment and the solver
from pyomo.environ import *
from pyomo.opt import *
opt = solvers.SolverFactory("glpk")
Step 2: Define the Model. Use Var
to declare the decision variables, Constraint
to define the constraints, and Objective
to define the objective function:
model = ConcreteModel()
model.x = Var(within=NonNegativeReals)
model.y = Var(within=NonNegativeReals)
model.c1 = Constraint(expr = model.x <= 4)
model.c2 = Constraint(expr = 2*model.y <= 12)
model.c3 = Constraint(expr = 3*model.x + 2*model.y <= 18)
model.z = Objective(expr = 3*model.x + 5*model.y, sense=maximize)
Step 3: Call the solver
results = opt.solve(model)
Step 4: Inspect the results. Note that the value of the objective function is not stored, but needs to be evaluated with expr()
:
model.x.get_values()
model.y.get_values()
model.z.expr()
Note: The values of decision variables are returned as Python dictionaries. For scalar variables, these are "trivial" dictionaries with None
as key. To extract the numerical value, write, e.g.,
model.x.get_values()[None]
It is possible to "pretty-print" the complete set of information stored within the model
object (useful for debugging):
model.pprint()