Basics of PuLP modeling

Supply Chain Analytics in Python

Aaren Stubberfield

Supply Chain Analytics Mgr.

What is PuLP

  • PuLP is a modeling framework for Linear (LP) and Integer Programing (IP) problems written in Python

  • Maintained by COIN-OR Foundation (Computational Infrastructure for Operations Research)

  • PuLP interfaces with Solvers

    • CPLEX
    • COIN
    • Gurobi
    • etc…
Supply Chain Analytics in Python

PuLP example – resource scheduling

  • Consultant for boutique cake bakery that sell 2 types of cakes
  • 30 day month
  • There is:
    • 1 oven
    • 2 bakers
    • 1 packaging packer – only works 22 days
Supply Chain Analytics in Python

PuLP example – resource scheduling

  • Different resource needs for the 2 types of cakes:
Cake A Cake B
Oven 0.5 days 1 day
Bakers 1 day 2.5 days
Packers 1 day 2 days

.

Cake A Cake B
Profit $20.00 $40.00
Supply Chain Analytics in Python

PuLP example – resource scheduling

  • Objective is to Maximize Profit
    • Profit = 20*A + 40*B
  • Subject to:
    • A ≥ 0
    • B ≥ 0
    • 0.5A + 1B ≤ 30
    • 1A + 2.5B ≤ 60
    • 1A + 2B ≤ 22
Supply Chain Analytics in Python

Common modeling process for PuLP

  1. Initialize Model
  2. Define Decision Variables
  3. Define the Objective Function
  4. Define the Constraints
  5. Solve Model
Supply Chain Analytics in Python

Initializing model - LpProblem()

LpProblem(name='NoName', sense=LpMinimize)
  • name = Name of the problem used in the output .lp file, i.e. "My LP Problem"
  • sense = Maximize or minimize the objective function
    • Minimize = LpMinimize (default)
    • Maximize = LpMaximize
Supply Chain Analytics in Python

PuLP example – resource scheduling

  1. Initialize Model
from pulp import *

# Initialize Class
model = LpProblem("Maximize Bakery Profits", LpMaximize)
Supply Chain Analytics in Python

Define decision variables - LpVariable()

LpVariable(name, lowBound=None, upBound=None, cat='Continuous', e=None)
  • name = Name of the variable used in the output .lp file
  • lowBound = Lower bound
  • upBound = Upper bound
  • cat = The type of variable this is
    • Integer
    • Binary
    • Continuous (default)
  • e = Used for column based modeling
Supply Chain Analytics in Python

PuLP example – resource scheduling

  1. Initialize Class
  2. Define Variables
# Define Decision Variables
A = LpVariable('A', lowBound=0, cat='Integer')
B = LpVariable('B', lowBound=0, cat='Integer')
Supply Chain Analytics in Python

PuLP example – resource scheduling

  1. Initialize Class
  2. Define Variables
  3. Define Objective Function
# Define Objective Function
model += 20 * A + 40 * B
Supply Chain Analytics in Python

PuLP example – resource scheduling

  1. Initialize Class
  2. Define Variables
  3. Define Objective Function
  4. Define Constraints
# Define Constraints
model += 0.5 * A + 1 * B <= 30
model += 1 * A + 2.5 * B <= 60
model += 1 * A + 2 * B <= 22
Supply Chain Analytics in Python

PuLP example – resource scheduling

  1. Initialize Class
  2. Define Variables
  3. Define Objective Function
  4. Define Constraints
  5. Solve Model
# Solve Model
model.solve()
print("Produce {} Cake A".format(A.varValue))
print("Produce {} Cake B".format(B.varValue))
Supply Chain Analytics in Python

PuLP example – resource scheduling

from pulp import *

# Initialize Class
model = LpProblem("Maximize Bakery Profits",
                   LpMaximize)

# Define Decision Variables
A = LpVariable('A', lowBound=0,
                cat='Integer')
B = LpVariable('B', lowBound=0, 
                cat='Integer')

# Define Objective Function
model += 20 * A + 40 * B
# Define Constraints
model += 0.5 * A + 1 * B <= 30
model += 1 * A + 2.5 * B <= 60
model += 1 * A + 2 * B <= 22

# Solve Model
model.solve()
print("Produce {} Cake A".format(A.varValue))
print("Produce {} Cake B".format(B.varValue))
Supply Chain Analytics in Python

Summary

  • PuLP is a Python LP / IP modeler
  • Reviewed 5 Steps of PuLP modeling process

    1. Initialize Model
    2. Define Decision Variables
    3. Define the Objective Function
    4. Define the Constraints
    5. Solve Model
  • Completed Resource Scheduling Example

Supply Chain Analytics in Python

Let's practice!

Supply Chain Analytics in Python

Preparing Video For Download...