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

In [1]:
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:

In [2]:
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

In [3]:
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():

In [4]:
model.x.get_values()
Out[4]:
{None: 2.0}
In [5]:
model.y.get_values()
Out[5]:
{None: 6.0}
In [6]:
model.z.expr()
Out[6]:
36.0

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.,

In [7]:
model.x.get_values()[None]
Out[7]:
2.0

It is possible to "pretty-print" the complete set of information stored within the model object (useful for debugging):

In [8]:
model.pprint()
2 Var Declarations
    x : Size=1, Index=None, Domain=NonNegativeReals
        Key  : Lower : Value : Upper : Fixed : Stale
        None :     0 :   2.0 :  None : False : False
    y : Size=1, Index=None, Domain=NonNegativeReals
        Key  : Lower : Value : Upper : Fixed : Stale
        None :     0 :   6.0 :  None : False : False

1 Objective Declarations
    z : Size=1, Index=None, Active=True
        Key  : Active : Sense    : Expression
        None :   True : maximize : 3*x + 5*y

3 Constraint Declarations
    c1 : Size=1, Index=None, Active=True
        Key  : Lower : Body : Upper : Active
        None :  -Inf :    x :   4.0 :   True
    c2 : Size=1, Index=None, Active=True
        Key  : Lower : Body  : Upper : Active
        None :  -Inf : 2 * y :  12.0 :   True
    c3 : Size=1, Index=None, Active=True
        Key  : Lower : Body      : Upper : Active
        None :  -Inf : 3*x + 2*y :  18.0 :   True

6 Declarations: x y c1 c2 c3 z