開始使用免費開始

找出全域最適解

以下是一個利潤極大化問題,你的任務是找出全域最大值。

\(\Pi= -\frac{1}{4}q^4 + 11q^3 - 160q^2 + 900q\)

數量的自然下界是 $0$,而你觀察到在 \(q=30\) 時利潤為負,因此 \(30\) 是合適的上界候選。

請找出此問題的全域最適解。

basinhopping 已為你匯入。

本練習屬於課程

Python 最佳化入門

檢視課程

練習說明

  • 定義關鍵字引數的字典 kwargs,其界限為下界 $0$、上界 $30$。
  • 執行 basinhopping,將目標設為 profit 的相反數,並將初始猜測 x0 與最小化器的 kwargs 一併傳入。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

def profit(q): 
	return -q**4 / 4 + 11 * q**3 - 160 * q**2 + 900 * q
  
x0 = 0

# Define the keyword arguments for bounds
kwargs = {"bounds": [(____, ____)]} 

# Run basinhopping to find the optimal quantity
result = basinhopping(____ q: -profit(q), ____, ____=kwargs)

print(f"{result.message}")
print(f"The maximum according to basinhopping(x0={x0}) is at {result.x[0]:.2f}\n")
編輯並執行程式碼