首页 > 解决方案 > 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。有人有任何线索吗?

标签: pythonscipyminimizeboundary

解决方案


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

推荐阅读