開始使用免費開始

非線性受限的餅乾問題

烘焙成果很不錯!

現在,能不能改用 NonlinearConstraint 再解一次同樣的問題?

回想一下烘焙坊的約束:必須至少滿足 140 份預購,而且每家工廠每天可生產 100 片餅乾。

minimizeBoundsNonlinearConstraint 已為你載入,另外也有收益函式 R、成本函式 C,以及利潤函式 profit

本練習屬於課程

Python 最佳化入門

檢視課程

練習說明

  • 使用 lambda 函式 q 定義限制條件,並設定下限與上限。
  • 執行最佳化:將目標函式、界線與限制條件一併傳入 miminize()

動手互動練習

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

# Redefine the problem with NonlinearConstraint
constraints = NonlinearConstraint(lambda q: ____, ____, ___)

# Perform optimization
result = minimize(lambda q: ____, 
                  [50, 50], 
                  bounds=____,
                  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}')
編輯並執行程式碼