import pulp
import numpy as np
"""
# shadow prices
: Constraint의 RHS(Right Hand Side)가 1 증가할 때, 변화할 때, obj value가 어떻게 달라지는지를 보여주는 것
"""
np.random.seed(0)
# Initialize Class, Define Vars., and Objective
model = pulp.LpProblem(
name="MODEL1",
sense=pulp.LpMaximize)
######################################
# Define VARIABLE
vars = pulp.LpVariable.dicts(
name = 'var', # prefix of each LP var
indexs = ['A', 'B', 'C'],
lowBound = 0,
cat = 'Continuous'
)
print(vars)
######################################
# Define OBJECTIVE FUNCTION
coef_dict = {'A':500, 'B':450, 'C':600}
model.objective = pulp.lpSum(
coef_dict[k]*var for k, var in vars.items()
)
print(model.objective)
######################################
# Define CONSTRAINTS
RHS = [60, 150, 8]
LHS = [
[6, 5, 8],
[10.5, 20, 10],
[1, 0, 0]
]
for i in range(0, 3):
constraint = LHS[i][0]*vars['A'] + LHS[i][1]*vars['B'] + LHS[i][2]*vars['C']<= RHS[i]
model.constraints[f"const_{i}"] = constraint
# SOLVE it
model.solve()
print(f"Status: {pulp.LpStatus[model.status]}")
print(f"Objective: {pulp.value(model.objective)}")
for v in model.variables():
print(f"{v.name}: {v.varValue:7.3f}")
print("=="*20)
print("== shadow price")
# shadow price: constraint의 RHS가 1증가할 때, obj가 얼마나 커지는가를 의미함.
# slack: RHS가 얼마나 남아 있는지를 의미함. 따라서, slack이 0이라는 말은 binding, 해당 constraint가 equality라는 것을 의미함.
for name, c in model.constraints.items():
shadow_p, slack = c.pi, c.slack
print(f"{name} - shadow price: {shadow_p:6.2f}, slack: {slack:6.2f}")
for i in range(0, 3):
print("=="*30)
댓글남기기