鲍威尔方法中的边界Scipy

问题描述

让功能最小化

f =lambda x: (x+1)**2 

在scipy中使用鲍威尔方法

如果我们使用

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,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,鲍威尔方法无法处理约束或界限,如您所见here。更新到scipy 1.5.x,它可以处理边界,请参见here

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])

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...