使用 lpSum

Python 的供應鏈分析

Aaren Stubberfield

Supply Chain Analytics Mgr.

從簡單到複雜

簡易烘焙坊範例

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

較複雜的烘焙坊範例

# 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')
Python 的供應鏈分析

從簡單到複雜

複雜烘焙坊的目標函式

# Define Objective Function
model += 20*A + 40*B + 33*C + 14*D + 6*E + 60*F

需要可擴充的方法

含多變數的函式圖

Python 的供應鏈分析

使用 lpSum()

lpSum(vector)
  • vector = 線性表示式的清單

因此…

# Define Objective Function
model += 20*A + 40*B + 33*C + 14*D + 6*E + 60*F

等同於…

# Define Objective Function
var_list = [20*A, 40*B, 33*C, 14*D, 6*E, 60*F]
model += lpSum(var_list)
Python 的供應鏈分析

搭配清單生成式的 lpSum

# 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])
Python 的供應鏈分析

重點整理

  • 需要彙總多個變數的方法
  • lpSum()
  • 可用於清單生成式
Python 的供應鏈分析

Practice time!

Python 的供應鏈分析

Preparing Video For Download...