Sensitivity analysis in PuLP

Introduction to Optimization in Python

Jasmin Ludolf

Content Developer

Sensitivity analysis in linear programming

  • Sensitivity analysis changes one parameter and to see changes in the output/outcome
  • Linear programming: how much the objective changes
  • Three approaches:
    1. Change a coefficient in the objective
    2. Change a coefficient in a constraint
    3. Change the constant term in a constraint
  • Solve original and changed model
  • Use PuLP!

Gauge indicator with left green sector vector icon to represent sensitivity

Introduction to Optimization in Python

Definitions

  • Constraint:
    • Holds with equality: active, binding
    • Holds with strict inequality: non active, non-binding, slack (adj.)
  • Slack (noun):
    • Amount to add to the left of constraint to make it binding
  • Shadow price:
    • Marginal increase in the objective with respect to the right of the constraint
Introduction to Optimization in Python

The diet problem in PuLP

Food Cost ($/lb) Protein (%) Fat (%)
corn 0.11 10 2.5
soybean 0.28 40 1

 

  • Recommendation: at least 17% protein, 2% fat, 7lb food
  • Objective: $0.11C + 0.28S$
  • Protein: $10C+40S \geq 17(C+S)$
  • Fat: $2.5C + S \geq 2(C+S)$
  • Weight: $C + S \geq 7$
from pulp import *

model = LpProblem('diet', LpMinimize)

C = LpVariable('C', lowBound=0)
S = LpVariable('S', lowBound=0)

model += 0.11*C + 0.28*S

model += 10*C + 40*S >= 17 * (C+S), 'Protein' 
model += 2.5*C + S >= 2 * (C+S), 'Fat'
model += C + S >= 7, 'Weight' 

model.solve()
Introduction to Optimization in Python

Slack and shadow price in PuLP

print(f"Status: {LpStatus[model.status]}\n")
for name, c in model.constraints.items():
    print(f"{name}: slack = {c.slack:.2f}, shadow price = {c.pi:.2f}")
Status: Optimal
Protein: slack = -0.00, shadow price = 0.01

Fat: slack = -1.05, shadow price = 0.00
Weight: slack = -0.00, shadow price = 0.15
Introduction to Optimization in Python

Let's practice!

Introduction to Optimization in Python

Preparing Video For Download...