开始使用免费开始使用

寻找全局最优解

下面给出一个利润最大化问题,请您求解全局最大值。

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

\(0\) 是数量的自然下界,您观察到当 \(q=30\) 时利润为负,因此 \(30\) 是一个合适的上界候选。

请找到该问题的全局最优解。

basinhopping 已为您导入。

本练习是课程的一部分

Python 优化入门

查看课程

练习说明

  • 定义关键字参数字典 kwargs,设置边界为 \(0\) 和 $30$。
  • 运行 basinhopping,目标函数取 profit 的相反数,并将初始猜测 x0kwargs 传递给最小化器。

交互式实操练习

通过完成这段示例代码来试试这个练习。

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")
编辑并运行代码