Using lpSum

Supply Chain Analytics in Python

Aaren Stubberfield

Supply Chain Analytics Mgr.

Moving from simple to complex

Simple Bakery Example

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

More Complex Bakery Example

# Define Decision Variables
A = LpVariable('A', 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

Function image with many variables

Supply Chain Analytics in Python

Using lpSum()

lpSum(vector)
  • vector = A list of linear expressions

Therefore ...

# 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!

Supply Chain Analytics in Python

Preparing Video For Download...