效用極大化
Bill 是一位立志學習鋼琴的學生,他每天把練習時間分配在古典($c$)與現代($m$)音樂上。他的偏好由你剛剛繪製過的同一個效用函式表示:
$U(c, m)=c^{0.7}m^{0.3}$。
每天的練習時間總和為 2($c+m=2$)。請幫 Bill 找到最佳的練習規劃。
np 與 minimize 已為你載入。我們也已從 SymPy 匯入 symbols、diff 與 solve,並把 c 與 m 定義為 symbols,同時替你定義好效用函式 U。
本練習屬於課程
Python 最佳化入門
練習說明
- 透過解包
vars並回傳取負的函式來定義效用函式。 - 定義限制式函式。
- 以
type與fun設定限制式。 - 進行最佳化並取出
c與m的結果。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define the utility function
def utility_function(vars):
____
return ____
# Define the constraint function
def constraint(vars):
return ____
initial_guess = [12, 12]
# Set up the constraint
constraint_definition = {____}
# Perform optimization
result = ____
____ = result.____
print("Optimal study hours for classical music:", round(c, 2))
print("Optimal study hours for modern music:", round(m, 2))