0% found this document useful (0 votes)
59 views7 pages

LP 3 - Solving MILPs With PuLP

This document provides a comprehensive guide on solving Mixed Integer Linear Programming (MILP) problems using the PuLP package in Python. It covers installation instructions for PuLP and the GLPK solver, formulation of a linear programming model, and methods for extracting solutions. Additionally, it discusses the formulation of canonical models and the declaration of different types of variables.

Uploaded by

yoncakoymen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views7 pages

LP 3 - Solving MILPs With PuLP

This document provides a comprehensive guide on solving Mixed Integer Linear Programming (MILP) problems using the PuLP package in Python. It covers installation instructions for PuLP and the GLPK solver, formulation of a linear programming model, and methods for extracting solutions. Additionally, it discusses the formulation of canonical models and the declaration of different types of variables.

Uploaded by

yoncakoymen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

25-2-2024

Solving MILPs with PuLP


Additional info: Hands-On Linear Programming: Optimization With Python – Real Python

Rob Broekmeulen

Installing the PuLP package


This presentation assumes that you use Anaconda/Spyder as Python IDE.
To check if your Anaconda installation has Pulp installed, run:
try: import pulp
print("The package is installed")
except ImportError as error:
print(error)
If PuLP is not installed, run the following command from the Anaconda Prompt:
conda install –c conda-forge pulp

2 LP with Pulp
1
2
25-2-2024

Installing the GLPK solver


This presentation assumes that you use the free GLPK solver.
To check if your Anaconda installation has GLPK installed, run:
from pulp import listSolvers
print(listSolvers(onlyAvailable=True))

If GLPK (or GLPK_CMD) is not listed, run the following command from the Anaconda
Prompt:
conda install –c conda-forge glpk

3 LP with Pulp

Formulating the Blue Ridge LP model in PuLP

First, we need to import the relevant PuLP modeler functions:


from pulp import LpProblem, LpMaximize, LpVariable, GLPK, LpStatus

Next, we follow the four steps outlined earlier.


1. Understand the problem: minimization or maximization?
model = LpProblem(name="BlueRidge", sense=LpMaximize)

2. Identify the decision variables and identify any upper or lower bounds.
x1 = LpVariable(name="x1", lowBound=0)
x2 = LpVariable(name="x2", lowBound=0)

4 LP with Pulp
2
4
25-2-2024

Formulating the Blue Ridge LP model in PuLP (2)

3. State the objective function as a linear combination of the decision


variables.
model += 350*x1+300*x2

4. State the constraints as linear combinations of the decision variables.


model += (x1+x2 <= 200, "pumps")
model += (9*x1+6*x2 <= 1566, "labor")
model += (12*x1+16*x2 <= 2880, "tubing")

5 LP with Pulp

Solving the Blue Ridge LP model in PuLP

Now, we can solve the model (with GLPK) and check the status:
model.solve(GLPK(msg=False))
print(f"Status: {model.status}, {LpStatus[model.status]}")

The returned status can have the following values:


• 0: “Not Solved”
• 1: “Optimal”
• -1: “Infeasible”
• -2: “Unbounded”
• -3: “Undefined”
6 LP with Pulp
3
6
25-2-2024

Extracting the solution from the Blue Ridge LP model


Objective value:
print(f"Objective: {model.objective.value()}")

Variables (values):
print("Variables")
for var in model.variables():
print(f"{var.name}: {var.value()}")

Constraints (slack):
print("Constraints: slack")
for name, constraint in model.constraints.items():
print(f"{name}: {constraint.value()}")
For the full listing of the code, see lp_example_base.py

7 LP with Pulp

Recap: Canonical Linear Programming model


Min 𝒁 = ∑𝒏𝒊 𝟏 𝑪𝒊 𝒙𝒊
Subject to:
∀𝒋: ∑𝒏𝒊 𝟏 𝑨𝒋𝒊 𝒙𝒊 ≤ 𝑩𝒋 or ‘>=‘ or ‘=‘

∀𝒊: 𝒙𝒊 ≥ 𝟎
Instead of adding variables and constraints individually, we can use the list data structure and
the lpSum function of PuLP.
In the Blue Ridge problem, we have the following data (as lists):
• A = [[1, 1], [6, 9], [12, 16]]
• B = [200, 1560, 2880]
• C = [-350, -300] (profits are negative costs).
8 Introduction LP
4
8
25-2-2024

Formulating the Blue Ridge LP canonical model


We are using the LpMinimize and lpSum functions of PuLP.
from pulp import LpProblem, LpMinimize, LpVariable, GLPK, LpStatus, LpSum

Introduce the set variables and set constraints:


set_i = range(2)
set_con = ["pumps", "labor", "tubing"]

Enter the problem data as lists.


A = [[1, 1], [9, 6], [12, 16]]
B = [200, 1566, 2880]
C = [-350, -300]

9 LP with Pulp

Formulating the Blue Ridge LP canonical model (2)


Next, we again follow the four steps outlined earlier.
1. Understand the problem: minimization or maximization?
model = LpProblem(name="BlueRidge", sense=LpMinimize)

2. Identify the decision variables and identify any upper or lower bounds.
x = {i: LpVariable(name=f"x{i}", lowBound=0) for i in set_i}

Note that we have now created x0 and x1, instead of x1 and x2 (Python convention).
An alternative way to create a dictionary of variables is:
x = LpVariable.dicts("x", indices=set_i, lowBound=0)

10 LP with Pulp
5
10
25-2-2024

Formulating the Blue Ridge LP canonical model (23

3. State the objective function as a linear combination of the decision


variables.
model += lpSum([C[i]*x[i] for i in set_i])

4. State the constraints as linear combinations of the decision variables.


for k, name in enumerate(set_con):
model += (lpSum([A[k][i]*x[i] for i in set_i]) <= B[k], name)

For the full listing of the code, see lp_example_canon.py

11 LP with Pulp

11

Writing the model


We can print the model and/or write it to a file:
print(model)
model.writeLP(“BlueRidge.lp”)

With as result for print: BlueRidge:


MINIMIZE
-350*x0 + -300*x1 + 0
SUBJECT TO
pumps: x0 + x1 <= 200

labor: 9 x0 + 6 x1 <= 1566

tubing: 12 x0 + 16 x1 <= 2880

VARIABLES
x0 Continuous
x1 Continuous

12 LP with Pulp
6
12
25-2-2024

Declaring binary, integer, and semi-continuous variables


In the LpVariable function, the default category is “Continuous”.
y = LpVariable(name=“y", cat=“Binary”)
z = LpVariable(name=“z", lowBound=0, upbound=9, cat=“Integer”)

With these variables y and the original x2, we can create a new semi-continuous variable with
a minimum of 100 Hydro-Luxes (minimal order quantity), by adding the following two
constraints to the non-canonical model:
BIGM = 200
model += (100*y-x2 <= 0, “lowSC")
model += (x2-BIGM*y <= 0, “highSC")

The value 200 for BIGM is sufficient, due to the limited number of pumps.

13 LP with Pulp

13

Options for GLPK


GLPK (= GNU Linear Programming Kit) options for MILP problems:
• No output messages (basic)

• Write output log to file xx (useful for debugging): --log xx

• Do not enter the Branch & Bound phase (relaxed solution only): --nomip

• Limit solution time to nn seconds: --tmlim nn

14 LP with Pulp
7
14

You might also like