解 PuLP 模型

Python 的供應鏈分析

Aaren Stubberfield

Supply Chain Analytics Mgr.

PuLP 常見建模流程

  1. 初始化模型
  2. 定義決策變數
  3. 定義目標函式
  4. 定義限制式
  5. 求解模型
    • 呼叫 solve() 方法
    • 檢查解的狀態
    • 列印最佳化的決策變數
    • 列印最佳化的目標函式
Python 的供應鏈分析

求解模型 - solve 方法

.solve(solver=None)
  • solver = 選填:要使用的特定解算器,預設為系統預設解算器。
Python 的供應鏈分析
# Initialize, Define Decision Vars., Objective Function, and Constraints
from pulp import *
import pandas as pd
model = LpProblem("Minimize Transportation Costs", LpMinimize)
cust = ['A','B','C']
warehouse = ['W1','W2']
demand = {'A': 1500, 'B': 900, 'C': 800}
costs = {('W1','A'): 232, ('W1','B'): 255, ('W1','C'): 264, 
         ('W2','A'): 255, ('W2','B'): 233, ('W2','C'): 250}
ship = LpVariable.dicts("s_", [(w,c) for w in warehouse for c in cust], 
                         lowBound=0, cat='Integer')
model += lpSum([costs[(w, c)] * ship[(w, c)] for w in warehouse for c in cust])
for c in cust: model += lpSum([ship[(w, c)] for w in warehouse]) == demand[c]

# Solve Model
model.solve()
Python 的供應鏈分析

求解模型 - 解的狀態

LpStatus[model.status]
  • Not Solved: 求解前的狀態。
  • Optimal: 已找到最佳解。
  • Infeasible: 無可行解(例如設為 x ≤ 1 且 x ≥ 2)。
  • Unbounded: 目標函式無界,極大或極小會趨向無窮(例如唯一限制為 x ≥ 3)。
  • Undefined: 可能存在最佳解,但尚未找到。
1 Keen, Ben Alex. "Linear Programming with Python and PuLP 2 Part 2." _Ben Alex Keen_, 1 Apr. 2016, benalexkeen.com/linear-programming-with-python-and-pulp-part-2/._{{5}}
Python 的供應鏈分析
# Initialize, Define Decision Vars., Objective Function, and Constraints
from pulp import *
import pandas as pd
model = LpProblem("Minimize Transportation Costs", LpMinimize)
cust = ['A','B','C']
warehouse = ['W1','W2']
demand = {'A': 1500, 'B': 900, 'C': 800}
costs = {('W1','A'): 232, ('W1','B'): 255, ('W1','C'): 264,
         ('W2','A'): 255, ('W2','B'): 233, ('W2','C'): 250}
ship = LpVariable.dicts("s_", [(w,c) for w in warehouse for c in cust], lowBound=0, cat='Integer')
model += lpSum([costs[(w, c)] * ship[(w, c)] for w in warehouse for c in cust])
for c in cust: model += lpSum([ship[(w, c)] for w in warehouse]) == demand[c]
# Solve Model
model.solve()
print("Status:", LpStatus[model.status])
Status: Optimal
Python 的供應鏈分析

將變數列印到標準輸出:

for v in model.variables():
    print(v.name, "=", v.varValue)

Pandas 資料結構:

o = [{A:ship[(w,'A')].varValue, B:ship[(w,'B')].varValue, C:ship[(w,'C')].varValue}
     for w in warehouse]
print(pd.DataFrame(o, index=warehouse))
  • 迴圈走訪模型變數
  • 將數值存入 pandas DataFrame
Python 的供應鏈分析
# Solve Model
model.solve()
print(LpStatus[model.status])
o = [{A:ship[(w,'A')].varValue, B:ship[(w,'B')].varValue, C:ship[(w,'C')].varValue}
     for w in warehouse]
print(pd.DataFrame(o, index=warehouse))

  輸出:

Status: Optimal
|       |A      |B      |C      |
|:------|:------|:------|:------|
|W1     |1500.0 |0.0    |0.0    |
|W2     |0.0    |900.0  |800.0  |
Python 的供應鏈分析

求解模型 - 最佳化目標函式

列印最佳化目標函式值:

print("Objective = ", value(model.objective))
Python 的供應鏈分析
# Solve Model
model.solve()
print(LpStatus[model.status])
output = []
for w in warehouse: t = [ship[(w,c)].varValue for c in cust] output.append(t)
opd = pd.DataFrame.from_records(output, index=warehouse, columns=cust)
print(opd)
print("Objective = ", value(model.objective))
Status: Optimal
|       |A      |B      |C      |
|:------|:------|:------|:------|
|W1     |1500.0 |0.0    |0.0    |
|W2     |0.0    |900.0  |800.0  |
Objective = 757700.0
Python 的供應鏈分析

重點整理

求解模型

  • 呼叫 solve() 方法
  • 檢查解的狀態
  • 列印決策變數的數值
  • 列印目標函式的數值
Python 的供應鏈分析

一起來練習吧!

Python 的供應鏈分析

Preparing Video For Download...