Supply Chain Analytics in Python
Aaren Stubberfield
Supply Chain Analytics Mgr.
solve()
method.solve(solver=None)
solver
= Optional: the specific solver to be used, defaults to the default solver.# 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()
LpStatus[model.status]
# 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
Print variables to standard output:
for v in model.variables():
print(v.name, "=", v.varValue)
Pandas data structure:
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))
# 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))
Output:
Status: Optimal
| |A |B |C |
|:------|:------|:------|:------|
|W1 |1500.0 |0.0 |0.0 |
|W2 |0.0 |900.0 |800.0 |
Print the value of optimized objective function:
print("Objective = ", value(model.objective))
# 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
Solve Model
solve()
methodSupply Chain Analytics in Python