python - Powell 方法 Scipy 中的边界
问题描述
让我们最小化函数
f =lambda x: (x+1)**2
在 scipy 中使用 Powell 方法
如果我们使用
scipy.optimize.minimize(f, 1, method='Powell', bounds=None)
回报是
direc: array([[1.]])
fun: array(0.)
message: 'Optimization terminated successfully.'
nfev: 20
nit: 2
status: 0
success: True
x: array(-1.)
即最小值应为-1。如果我们提供界限
scipy.optimize.minimize(f, 1, method='Powell', bounds=[(0,2)])
回报又来了
direc: array([[1.]])
fun: array(0.)
message: 'Optimization terminated successfully.'
nfev: 20
nit: 2
status: 0
success: True
x: array(-1.)
现在是错误的!正确答案应该是 0。就像没有考虑边界一样。我正在使用 scipy '1.4.1' 和 python 3.7.6。有人有任何线索吗?
解决方案
使用 scipy 1.4.x,Powell 方法无法处理约束和边界,如您在此处看到的。更新到 scipy 1.5.x,它可以处理边界,见这里:
In [11]: scipy.optimize.minimize(f, x0=1.0, method='Powell', bounds=[(0.0,2.0)])
Out[11]:
direc: array([[1.64428414e-08]])
fun: array(1.)
message: 'Optimization terminated successfully.'
nfev: 103
nit: 2
status: 0
success: True
x: array([2.44756652e-12])
推荐阅读
- android - 使用 firebase 回收器适配器读取复杂和嵌套的 firebase 实时数据库
- reactjs - Openlayers geojson-vt 集成问题
- python - 使用来自另一个“Pandas 数据框”的标准迭代“Pandas 数据框”?
- python-3.x - 当我在 VSCode 中运行模块“http.client”时,它没有属性“HTTPSConnection”
- mysql - 过滤日期列mysql
- c++ - c++命名空间类冲突
- python - 用于匹配 [^whatever-here] 和限制 \[^...] 的正则表达式
- html - 两个部分的意外影响是:在FF中更改设备后立即目标
- android - Jetpack Compose:材质颜色在不同方向之间切换时消失
- xamarin - Xamarin 表单:报告问题 - 屏幕截图和电子邮件