Basics of
optimization
S U P P LY C H A I N A N A LY T I C S I N P Y T H O N
Aaren Stubber eld
Supply Chain Analytics Mgr.
What is a supply chain
A Supply Chain consist of all parties involved, directly or indirectly, in ful lling a customer’s request.¹
Includes:
Suppliers
Internal Manufacturing
Outsourced Logistics Suppliers (i.e. Third Party Suppliers)
1 Chopra, Sunil, and Peter Meindl. _Supply Chain Management: Strategy, Planning, and Operations._ Pearson Prentice 2
Hall, 2007.
SUPPLY CHAIN ANALYTICS IN PYTHON
What is a supply chain optimization
Involves nding the best path to achieve an objective based on constraints
SUPPLY CHAIN ANALYTICS IN PYTHON
Crash course in LP
Linear Programing (LP) is a Powerful Modeling Tool for Optimization
Optimization method using a mathematical model whose requirements are linear relationships
There are 3 Basic Components in LP:
Decision Variables - what you can control
Objective Function - math expression that uses variables to express goal
Constraints - math expression that describe the limits of a solutions
SUPPLY CHAIN ANALYTICS IN PYTHON
Introductory example
Use LP to decide on an exercise routine to burn as many calories as possible.
Pushup Running
Minutes 0.2 per pushup 10 per mile
Calories 3 per pushup 130 per mile
Constraint - only 10 minutes to exercise
SUPPLY CHAIN ANALYTICS IN PYTHON
Basic components of an LP
Decision Variables - What we can control:
Number of Pushups & Number of Miles Ran
Objective Function - Math expression that uses variables to express goal:
Max (3 * Number of Pushups + 130 * Number of Miles)
Constraints - Math expression that describe the limits of a solutions:
0.2 * Number of Pushups + 10 * Number of Miles ≤ 10
Number of Pushups ≥ 0
Number of Miles ≥ 0
SUPPLY CHAIN ANALYTICS IN PYTHON
Example solution
Optimal Solution:
50 Pushups
0 Miles Ran
Calories Burned: 150
SUPPLY CHAIN ANALYTICS IN PYTHON
LP vs IP vs MIP
Terms Decision Variables
Linear Programing (LP) Only Continuous
Integer Programing (IP) Only Discrete or Integers
Mixed Integer Programing (MIP) Mix of Continuous and Discrete
SUPPLY CHAIN ANALYTICS IN PYTHON
Summary
De ned Supply Chain Optimization
De ned Linear Programing and Basic Components
Decision Variables
Objective Function
Constraints
De ned LP vs IP vs MIP
SUPPLY CHAIN ANALYTICS IN PYTHON
Let's practice!
S U P P LY C H A I N A N A LY T I C S I N P Y T H O N
Basics of PuLP
modeling
S U P P LY C H A I N A N A LY T I C S I N P Y T H O N
Aaren Stubber eld
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
Pro t $20.00 $40.00
SUPPLY CHAIN ANALYTICS IN PYTHON
PuLP example – resource scheduling
Objective is to Maximize Pro t
Pro t = 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. De ne Decision Variables
3. De ne the Objective Function
4. De ne 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 le, 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
De ne decision variables - LpVariable()
LpVariable(name, lowBound=None, upBound=None, cat='Continuous', e=None)
name = Name of the variable used in the output .lp le
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. De ne 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. De ne Variables
3. De ne Objective Function
# Define Objective Function
model += 20 * A + 40 * B
SUPPLY CHAIN ANALYTICS IN PYTHON
PuLP example – resource scheduling
1. Initialize Class
2. De ne Variables
3. De ne Objective Function
4. De ne 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. De ne Variables
3. De ne Objective Function
4. De ne Constraints
5. Solve Model
# Solve Model
[Link]()
print("Produce {} Cake A".format([Link]))
print("Produce {} Cake B".format([Link]))
SUPPLY CHAIN ANALYTICS IN PYTHON
PuLP example – resource scheduling
from pulp import * # Define Constraints
model += 0.5 * A + 1 * B <= 30
# Initialize Class model += 1 * A + 2.5 * B <= 60
model = LpProblem("Maximize Bakery Profits", model += 1 * A + 2 * B <= 22
LpMaximize)
# Solve Model
# Define Decision Variables [Link]()
A = LpVariable('A', lowBound=0, print("Produce {} Cake A".format([Link]))
cat='Integer') print("Produce {} Cake B".format([Link]))
B = LpVariable('B', lowBound=0,
cat='Integer')
# Define Objective Function
model += 20 * A + 40 * B
SUPPLY CHAIN ANALYTICS IN PYTHON
Summary
PuLP is a Python LP / IP modeler
Reviewed 5 Steps of PuLP modeling process
1. Initialize Model
2. De ne Decision Variables
3. De ne the Objective Function
4. De ne the Constraints
5. Solve Model
Completed Resource Scheduling Example
SUPPLY CHAIN ANALYTICS IN PYTHON
Let's practice!
S U P P LY C H A I N A N A LY T I C S I N P Y T H O N
Using lpSum
S U P P LY C H A I N A N A LY T I C S I N P Y T H O N
Aaren Stubber eld
Supply Chain Analytics Mgr.
Moving from simple to complex
Simple Bakery Example More Complex Bakery Example
# Define Decision Variables # Define Decision Variables
A = LpVariable('A', lowBound=0, cat='Integer') A = LpVariable('A', lowBound=0, cat='Integer')
B = LpVariable('B', lowBound=0, cat='Integer') B = LpVariable('B', lowBound=0, cat='Integer')
C = LpVariable('C', lowBound=0, cat='Integer')
D = LpVariable('D', lowBound=0, cat='Integer')
E = LpVariable('E', lowBound=0, cat='Integer')
F = LpVariable('F', lowBound=0, cat='Integer')
SUPPLY CHAIN ANALYTICS IN PYTHON
Moving from simple to complex
Objective Function of Complex Bakery Example
# Define Objective Function
model += 20*A + 40*B + 33*C + 14*D + 6*E + 60*F
Need method to scale
SUPPLY CHAIN ANALYTICS IN PYTHON
Using lpSum()
lpSum(vector) Therefore ...
vector = A list of linear expressions # Define Objective Function
model += 20*A + 40*B + 33*C + 14*D + 6*E + 60*F
Equivalent to ...
# Define Objective Function
var_list = [20*A, 40*B, 33*C, 14*D, 6*E, 60*F]
model += lpSum(var_list)
SUPPLY CHAIN ANALYTICS IN PYTHON
lpSum with list comprehension
# Define Objective Function
cake_types = ["A", "B", "C", "D", "E", "F"]
profit_by_cake = {"A":20, "B":40, "C":33, "D":14, "E":6, "F":60}
var_dict = {"A":A, "B":B, "C":C, "D":D, "E":E, "F":F}
model += lpSum([profit_by_cake[type] * var_dict[type]
for type in cake_types])
SUPPLY CHAIN ANALYTICS IN PYTHON
Summary
Need way to sum many variables
lpSum()
Used in list comprehension
SUPPLY CHAIN ANALYTICS IN PYTHON
Practice time!
S U P P LY C H A I N A N A LY T I C S I N P Y T H O N