Los het PuLP‑model op

Supply Chain Analytics in Python

Aaren Stubberfield

Supply Chain Analytics Mgr.

Standaard modelleerproces voor PuLP

  1. Initialiseer model
  2. Definieer beslisvariabelen
  3. Definieer de doelfunctie
  4. Definieer de constraints
  5. Los model op
    • roep de methode solve() aan
    • controleer de status van de oplossing
    • print geoptimaliseerde beslisvariabelen
    • print geoptimaliseerde doelfunctie
Supply Chain Analytics in Python

Model oplossen - solve‑methode

.solve(solver=None)
  • solver = Optioneel: de specifieke solver die je wilt gebruiken; standaard wordt de default-solver gebruikt.
Supply Chain Analytics in 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()
Supply Chain Analytics in Python

Model oplossen - status van de oplossing

LpStatus[model.status]
  • Not Solved: Status vóór het oplossen.
  • Optimal: Er is een optimale oplossing gevonden.
  • Infeasible: Er zijn geen haalbare oplossingen (bijv. als je x ≤ 1 en x ≥ 2 zet).
  • Unbounded: De doelfunctie is onbegrensd; maximaliseren of minimaliseren gaat naar oneindig (bijv. als de enige constraint x ≥ 3 is).
  • Undefined: Er kan een optimale oplossing bestaan, maar die is mogelijk niet gevonden.
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}}
Supply Chain Analytics in 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
Supply Chain Analytics in Python

Print variabelen naar standaarduitvoer:

for v in model.variables():
    print(v.name, "=", v.varValue)

Pandas-datastructuur:

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))
  • loop door modelvariabelen
  • sla waarden op in een pandas DataFrame
Supply Chain Analytics in 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))

  Output:

Status: Optimal
|       |A      |B      |C      |
|:------|:------|:------|:------|
|W1     |1500.0 |0.0    |0.0    |
|W2     |0.0    |900.0  |800.0  |
Supply Chain Analytics in Python

Model oplossen - geoptimaliseerde doelfunctie

Print de waarde van de geoptimaliseerde doelfunctie:

print("Objective = ", value(model.objective))
Supply Chain Analytics in 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
Supply Chain Analytics in Python

Samenvatting

Los model op

  • Roep de methode solve() aan
  • Controleer de oplossingsstatus
  • Print waarden van beslisvariabelen
  • Print waarde van doelfunctie
Supply Chain Analytics in Python

Laten we oefenen!

Supply Chain Analytics in Python

Preparing Video For Download...