让我们最小化函数
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。 有人有线索吗
对于scipy 1.4.x,Powell方法无法处理约束或边界,如您所见here。更新到scipy 1.5.x时,它可以处理边界,请参见here:
相关问题 更多 >
编程相关推荐