Solve the PuLP model

Supply Chain Analytics in Python

Aaren Stubberfield

Supply Chain Analytics Mgr.

Common modeling process for PuLP

  1. Initialize Model
  2. Define Decision Variables
  3. Define the Objective Function
  4. Define the Constraints
  5. Solve Model
    • call the solve() method
    • check the status of the solution
    • print optimized decision variables
    • print optimized objective function
Supply Chain Analytics in Python

Solve model - solve method

.solve(solver=None)
  • solver = Optional: the specific solver to be used, defaults to the default solver.
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

Solve model - status of the solution

LpStatus[model.status]
  • Not Solved: The status prior to solving the problem.
  • Optimal: An optimal solution has been found.
  • Infeasible: There are no feasible solutions (e.g. if you set the constraints x ≤ 1 and x ≥ 2).
  • Unbounded: The object function is not bounded, maximizing or minimizing the objective will tend towards infinity (e.g. if the only constraint was x ≥ 3).
  • Undefined: The optimal solution may exist but may not have been found.
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 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))
  • loop model variables
  • store values in a 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

Solve model - optimized objective function

Print the value of optimized objective function:

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

Summary

Solve Model

  • Call the solve() method
  • Check the status of the solution
  • Print values of decision variables
  • Print value of objective function
Supply Chain Analytics in Python

Let's practice!

Supply Chain Analytics in Python

Preparing Video For Download...