線性限制的餅乾生產
恭喜!你的餅乾事業擴張了。你現在有兩間烘焙坊,\(A\) 與 $B$,幫你把餅乾送到全國各地。
每間烘焙坊每天都能製作 100 片餅乾。在烘焙坊 $A$,每片餅乾的製作成本為 \(1.5\) 倍的數量 $q$;在烘焙坊 $B$,成本為 $1.75q$。
售價定義為 $150 - q$。
生意興隆!今天已經有 140 片餅乾的預購單。你想要把今日利潤最大化。每間烘焙坊應該各製作多少片餅乾?
minimize、Bounds 和 LinearConstraint 已為你載入,且營收函式 R 已經定義好。
本練習屬於課程
Python 最佳化入門
練習說明
- 使用烘焙坊 \(A\) 的數量
q[0]與烘焙坊 \(B\) 的數量q[1]定義成本函式C。 - 定義
profit(利潤)函式。 - 為你的最佳化問題定義
bounds與constraints。 - 進行最佳化,並將結果儲存到
result。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
def R(q):
return (150 - q[0] - q[1]) * (q[0] + q[1])
# Define the cost function
def C(q):
return ____
# Define the profit function
def profit(q):
return ____
# Define the bounds and constraints
bounds = Bounds(____, ____)
constraints = LinearConstraint([1, 1], ____)
# Perform optimization
result = ____(lambda q: ____,
[50, 50],
bounds=bounds,
constraints=constraints)
print(result.message)
print(f'The optimal number of biscuits to bake in bakery A is: {result.x[0]:.2f}')
print(f'The optimal number of biscuits to bake in bakery B is: {result.x[1]:.2f}')
print(f'The bakery company made: ${-result.fun:.2f}')