我正在尝试找到函数Minimum of a function with BFGS method的最小值(PDF文档的第29页)
而且我没有得到与链接中报告的结果相同的结果,我已经尝试过没有jacobian而没有运气。任何帮助,将不胜感激。
到目前为止的代码:
import numpy as np
from scipy.optimize import minimize
def objective(x):
x1=x[0]
x2=x[1]
print ("x1: ",x1," ","x2: ",x2)
return pow(x1,4.0)-2*x2*pow(x1,2.0)+pow(x2,2.0)+pow(x1,2.0)-2.0*x1+5.0
def jacobiano(x):
x1=x[0]
x2=x[1]
jaco=np.zeros(2)
jaco[0]=4.0*x1-4.0*x2*x1+2.0*x1-2.0
jaco[1]=-2.0*pow(x1,2.0)+2.0*x2
print ("dx1: ",jaco[0]," ","dx2: ",jaco[1])
return jaco
x0=np.array([1.0,2.0], dtype=np.double)
print(objective(x0))
sol=minimize(objective,x0,method='BFGS',jac=jacobiano, options={'disp': True})
print(sol)
答案 0 :(得分:1)
问题出现是因为您错误地计算了雅可比行列式,在您的情况下df/dx1
不正确。
如果f = x1**4 -2*x2*x1**2 +x2**2+ x1**2 -2.0*x1+5.0
然后df/dx1 = 4.0*x1**3 -4.0*x2*x1 + 2.0*x1-2.0
import numpy as np
from scipy.optimize import minimize
def objective(x):
x1, x2 = x
print ("x1: ",x1," ","x2: ",x2)
return x1**4 -2*x2*x1**2 +x2**2+ x1**2 -2.0*x1+5.0
def jacobiano(x):
x1, x2 = x
jaco=np.zeros(2)
jaco[0]=4.0*x1**3 -4.0*x2*x1 + 2.0*x1-2.0
jaco[1]=-2.0*x1**2.+2.0*x2
print("dx1: ",jaco[0]," ","dx2: ",jaco[1])
return jaco
x0=np.array([1.0,2.0], dtype=np.double)
sol=minimize(objective,
x0,method='BFGS',jac=jacobiano, options={'disp': True})
print(sol)
输出:
Optimization terminated successfully.
Current function value: 4.000000
Iterations: 7
Function evaluations: 9
Gradient evaluations: 9
fun: 4.000000000002963
hess_inv: array([[ 0.50324351, 1.0154575 ],
[ 1.0154575 , 2.55695728]])
jac: array([ 7.65547714e-06, -2.90129716e-06])
message: 'Optimization terminated successfully.'
nfev: 9
nit: 7
njev: 9
status: 0
success: True
x: array([ 1.00000093, 1.0000004 ])
Matlab:
x1=1.00863, x2=1.01932, f=4.00008
<强>的Python:强>
x1=1.00000093, x2=1.0000004, f=4.000000000002963
最佳解决方案
x1=1.0, x2=1.0, f=4.0