Powell方法中的边界

2024-06-01 06:12:43 发布

您现在位置:Python中文网/ 问答频道 /正文

让我们最小化函数

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'和python3.7.6。 有人有线索吗


Tags: messagescipyarraymethodoptimizefunboundsoptimization
1条回答
网友
1楼 · 发布于 2024-06-01 06:12:43

对于scipy 1.4.x,Powell方法无法处理约束或边界,如您所见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])

相关问题 更多 >