Skip to content

Instantly share code, notes, and snippets.

@skipperkongen
Last active November 24, 2021 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skipperkongen/0926ca685e9ccee5a4e65825ec86d3f3 to your computer and use it in GitHub Desktop.
Save skipperkongen/0926ca685e9ccee5a4e65825ec86d3f3 to your computer and use it in GitHub Desktop.
Shadow prices in PuLP
import pandas as pd
import pulp as P
# Define Constraints, Solve, Print Status, Variables, Objective
model = P.LpProblem("Maximize Bakery Profits", P.LpMaximize)
R = P.LpVariable('Regular_production', lowBound=0, cat='Continuous')
J = P.LpVariable('Jumbo_production', lowBound=0, cat='Continuous')
model += 5 * R + 10 * J, "Profit"
# Adjust the constraint
model += 0.5 * R + 1 * J <= ____
model += 1 * R + 2.5 * J <= 65
# Solve Model, Print Status, Variables, Objective, Shadow and Slack
model.solve()
print("Model Status: {}".format(P.LpStatus[model.status]))
for v in model.variables():
print(v.name, "=", v.varValue)
print("Objective = $", value(model.objective))
o = [{'name':name, 'shadow price':c.pi, 'slack': c.slack}
for name, c in model.constraints.items()]
print(pd.DataFrame(o))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment