使用边界条件
带边界约束的问题中,变量被限制在一定取值范围内。
您经营一家咖啡店,需要订购两种咖啡豆: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)