Solving linear programming problems (LPPs) using Pyomo

Solving linear programming problems (LPPs) using Pyomo

Example problem

  • Each unit of A requires 1 unit of Milk and 3 units of Choco,
  • Each unit of B requires 1 unit of Milk and 2 units of Choco.
  • Rs. 6 per unit A sold,
  • Rs. 5 per unit B sold.

Mathematical formulation

Decision variables:

  • x be the total number of units produced by A,
  • y be the total number of units produced by B.

Objective function:

Constraints:

Installing Pyomo

pip install pyomo

Pyomo model

Import required packages

from pyomo.environ import * 
from pyomo.opt import SolverFactory

Create a model

model = ConcreteModel(name=”(My-Pyomo-Model)”)

Define decision variables of the model

model.x = Var(domain = NonNegativeReals, name = ‘x’) 
model.y = Var(domain = NonNegativeReals, name = ‘y’)

Define the objective function of the model

model.objective = Objective(expr = 6*model.x + 5*model.y, sense = maximize)

Add constraints to the model

model.constraints = ConstraintList()model.constraints.add(model.x + model.y <= 5); # constraint-1 
model.constraints.add(3*model.x + 2*model.y <= 12); # constraint-2
# to suppress unnecessary outputs, use a semicolon at the end

Create a solver

solver = SolverFactory(“/opt/ibm/ILOG/CPLEX_Studio1271/cplex/bin/x86–64_linux/cplexamp”)

Solve the model

sol = solver.solve(model)

Print the solution

print(sol)
Problem: 
- Lower bound: -inf
Upper bound: inf
Number of objectives: 1
Number of constraints: 2
Number of variables: 2
Sense: unknown
Solver:
- Status: ok
Message: CPLEX 12.7.1.0\x3a optimal solution; objective 27; 2 dual simplex iterations (1 in phase I)
Termination condition: optimal
Id: 0
Error rc: 0
Time: 0.2711672782897949
Solution:
- number of solutions: 0
number of solutions displayed: 0

Retrieving decision variables’ values

print(‘x =’, model.x.value)
print(‘y =’, model.y.value)
x = 2.0000000000000004
y = 2.9999999999999996

Retrieve the objective value

print(‘The objective value is =’,model.objective())
The objective value is = 27.0

Save the model in a file

model.write();
\* Source Pyomo model name=‘(My-Pyomo-Model)’ *\max 
x3:
+6 x1
+5 x2
s.t.c_u_x4_:
+1 x1
+1 x2
<= 5
c_u_x5_:
+3 x1
+2 x2
<= 12
c_e_ONE_VAR_CONSTANT:
ONE_VAR_CONSTANT = 1.0
bounds
0 <= x1 <= +inf
0 <= x2 <= +inf
end

Display model information

model.display()
Model ‘(My-Pyomo-Model)’Variables:
x : Size=1, Index=None
Key : Lower : Value : Upper : Fixed : Stale : Domain
None : 0 : 2.0000000000000004 : None : False : False : NonNegativeReals
y : Size=1, Index=None
Key : Lower : Value : Upper : Fixed : Stale : Domain
None : 0 : 2.9999999999999996 : None : False : False : NonNegativeReals
Objectives:
objective : Size=1, Index=None, Active=True
Key : Active : Value
None : True : 27.0
Constraints:
constraints : Size=2
Key : Lower : Body : Upper
1 : None : 5.0 : 5.0
2 : None : 12.0 : 12.0

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dr. Soumen Atta, Ph.D.

Dr. Soumen Atta, Ph.D.

Postdoctoral Researcher at Laboratoire des Sciences du Numérique de Nantes (LS2N), Université de Nantes, IMT Atlantique, Nantes, France.