使用界線(Bounds)
有界限制(bound-constrained)問題中的變數會被限制在一定範圍內。
你正在經營一家咖啡店,需要下訂兩種咖啡豆:b[0] 與 b[1]。你想要把成本降到最低。供應商只接受每種豆子至少 2 單位的訂單,而你的預算最多可負擔每種豆子 100 單位。
scipy.optimize 中的 minimize 與 Bounds 已為你載入,並且已提供目標函式。
本練習屬於課程
Python 最佳化入門
練習說明
- 將
b[0]與b[1]的下界與上界設定為bounds。 - 使用
scipy找到最小值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
def objective_function(b):
return (b[0] - 6)**2 + (b[1] - 8)**2 + 3
# Set the bounds of your problem
bounds = ____
x0 = [10, 5]
# Find the minimum
result = ____(____)
print(result.x)