重訪飲食問題
你正在替一位農民檢視財務,他請你重新評估豬隻的飼料配方,若能降低成本更好。現行的「成本最小化」配方依據獸醫建議:蛋白至少 17%、脂肪至少 2%、飼料總量 7 lb,並遵循下列規格:
| Food | Cost ($/lb) | Protein (%) | Fat (%) |
|---|---|---|---|
| corn | 0.11 | 10 | 2.5 |
| soybean | 0.28 | 40 | 1 |
你得知 7 lb 是四捨五入後的數字,實際可降到 6.6 lb。現在請你分別單獨調整「重量」或「脂肪」的限制,觀察對最小成本的影響。你會先照原始問題求解,然後檢視鬆弛量(slack)與影子價格(shadow price)。
pulp 已為你匯入,model、以及代表玉米和黃豆的變數 C 與 S 也都已定義好。
本練習屬於課程
Python 最佳化入門
練習說明
- 列印 Weight 限制的鬆弛量(slack)。
- 檢查 Weight 限制的影子價格(shadow price)是否大於 0。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
model.constraints['Weight'] = C + S >= 7
model.solve()
print(f"Status: {LpStatus[model.status]}\n")
# Print the slack of the weight constraint
print("The slack of the Weight constraint is {}",
____.constraints['Weight'].____)
# Check if the shadow price is greater than 0
if ____.constraints['Weight'].____ > 0:
print('Tightening the constraint will increase minimum cost')