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で学ぶサプライチェーン分析

Ayo berlatih!

Pythonで学ぶサプライチェーン分析

Preparing Video For Download...