求解 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 供应链分析

Passons à la pratique !

Python 供应链分析

Preparing Video For Download...