常見約束錯誤

Python 的供應鏈分析

Aaren Stubberfield

Supply Chain Analytics Mgr.

相依需求約束

情境

  • 生產計畫
  • 規劃 2 種產品(A、B)
  • 規劃 3 個月的生產(1 月至 3 月)
  • 產品 A 作為產品 B 的投入

約束問題

  • 每 1 單位的 B,必須至少有 3 單位的 A
Python 的供應鏈分析

相依需求約束

每 1 單位的 B,必須至少有 3 單位的 A

  • 3B ≤ A
  • 3(2) ≤ A
  • 6 ≤ A

常見錯誤:

  • B ≤ 3A
  • 3B = A
Python 的供應鏈分析

程式碼範例

from pulp import *
demand = {'A':[0,0,0],'B':[8,7,6]}
costs = {'A':[20,17,18],'B':[15,16,15]}

# Initialize Model
model = LpProblem("Aggregate Production Planning",
                   LpMinimize)

# Define Variables
time = [0, 1, 2]
prod = ['A', 'B']
X = LpVariable.dicts(
     "prod", [(p, t) for p in prod for t in time], 
      lowBound=0, cat="Integer")
# Define Objective
model += lpSum([costs[p][t] * X[(p, t)] 
                for p in prod for t in time])

# Define Constraint So Production is >= Demand
for p in prod:
    for t in time:
        model += X[(p, t)] >= demand[p][t]

Python 的供應鏈分析

程式碼範例(續)

for t in time:
    model += 3*X[('B',t)] <= X[('A',t)]
Python 的供應鏈分析

延伸約束

每 1 單位的 B,必須至少有 3 單位的 A,且要考量 A 直接售予顧客

  • 3B + Demand$_{\text{A}}$ ≤ A
Python 的供應鏈分析

組合約束

情境

  • 倉儲配送計畫
  • 2 個倉庫(WH1、WH2)
  • 每個倉庫出貨 2 種產品(A、B)
  • 倉庫 WH1 規模小,每週可出 12 個 A 或 15 個 B

約束問題

  • 4 週內 A、B 可有哪些組合?
Python 的供應鏈分析
  • 僅 1 週: (1/12)A + (1/15)B ≤ 1

正確形式

  • (1/12)A + (1/15)B ≤
  • (1/12)(32) + (1/15)(20) ≤ 4
  • (32/12) + (20/15) ≤ 4
  • 4 ≤ 4

常見錯誤

  • 12A + 15B ≤ 4
  • (1/12)A + (1/15)B = 4
Python 的供應鏈分析
from pulp import *
import pandas as pd
demand = pd.read_csv("Warehouse_Constraint_Demand.csv", index_col=['Product'])
costs = pd.read_csv("Warehouse_Constraint_Cost.csv", index_col=['WH','Product'])

# Initialize Model
model = LpProblem("Distribution Planning", LpMinimize)

# Define Variables
wh = ['W1','W2']
prod = ['A', 'B']
cust = ['C1', 'C2', 'C3', 'C4']
X = LpVariable.dicts("ship", [(w, p, c) for c in cust for p in prod for w in wh],
                      lowBound=0, cat="Integer")
Python 的供應鏈分析

程式碼範例(續)

# Define Objective
model += lpSum([X[(w, p, c)]*costs.loc[(w, p), c]  
               for c in cust for p in prod for w in wh])

# Define Constraint So Demand Equals Total Shipments
for c in cust:
    for p in prod:
        model += lpSum([X[(w, p, c)] for w in wh]) == demand.loc[p, c]
Python 的供應鏈分析

程式碼範例(續)

約束

model += ((1/12) * lpSum([X['W1', 'A', c] for c in cust]) 
          + (1/15) * lpSum([X['W1', 'B', c] for c in cust])) <= 4
Python 的供應鏈分析

延伸約束

倉庫 WH1 規模小,每週可出 12 個 A、15 個 B,或 5 個 C。4 週內 A、B、C 可有哪些組合?

  • (1/12)A + (1/15)B + (1/5)C ≤ 4
Python 的供應鏈分析

總結

  • 常見錯誤
    • 相依約束
    • 組合選擇約束
  • 如何延伸約束
  • 代入數值檢查約束
Python 的供應鏈分析

一起來練習吧!

Python 的供應鏈分析

Preparing Video For Download...