Python 供应链分析
Aaren Stubberfield
Supply Chain Analytics Mgr.
背景
约束问题
每生产 1 个 B,还必须至少有 3 个 A
常见错误:
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]
for t in time:
model += 3*X[('B',t)] <= X[('A',t)]
每生产 1 个 B,还必须至少有 3 个 A,并且考虑 A 的直销需求。
背景
约束问题
正确形式
常见错误
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")
# 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]
约束
model += ((1/12) * lpSum([X['W1', 'A', c] for c in cust])
+ (1/15) * lpSum([X['W1', 'B', c] for c in cust])) <= 4
WH1 很小,每周要么发 12 个 A、15 个 B,或 5 个 C。4 周内 A、B、C 的可发运组合?
Python 供应链分析