果汁中的影子价格
一家公司使用两台机器 \(M_1\) 和 \(M_2\) 将葡萄柚汁($g$)和橙汁($o$)灌装入瓶。其目标是在以下约束条件下最大化利润:
M1:\(6g + 5.5o \leq 40\) 以及 M2:\(3g + 2.5o \leq 20\)
这些约束反映了机器的产能和可用时间。例如,M1 每周可用 40 小时,灌装 1 吨葡萄柚汁需要 6 小时,灌装 1 吨橙汁需要 5.5 小时。
另有供给约束:公司每周最多仅能获得 6 吨葡萄柚和 12 吨橙子。这些是上界。
pulp 已为您导入,model 已定义,同时还定义了变量 g 和 o,分别表示葡萄柚汁和橙汁。
本练习是课程的一部分
Python 优化入门
练习说明
- 完成 for 循环,加入对影子价格是否为正的检查。
- 输入当放宽约束时,衡量目标函数边际增量的变量。
- 输入衡量约束"紧不紧"的变量。
交互式实操练习
通过完成这段示例代码来试试这个练习。
print(LpStatus[model.status])
print(f'The optimal amount of {g.name} embottled is: {g.varValue:.2f} tons')
print(f'The optimal amount of {o.name} embottled is: {o.varValue:.2f} tons')
for name, c in model.constraints.items():
# Check if shadow value is positive
if c.____ > 0:
# Enter the variable that measures marginal increase in objective when constraint is relaxed
print(f"Increasing the capacity of {name} by one unit would increase profit by {c.____} units.")
else:
# Enter the variable that measures how tight the constraint is
print(f"{name} has {c.____} units of unused capacity.")