BFGS算法python代码
时间: 2023-12-08 07:04:16 浏览: 86
bfgs.rar_BFGS
以下是BFGS算法的Python代码实现:
```python
from lbfgs import *
# 定义待优化求解的函数
def func(x):
return 7 * np.sin(x) + 11 * np.cos(5 * x)
# 定义评估函数
def new_Evaluate(w, g, n, step):
fx = func(w[0])
g[0] = 7 * np.cos(w[0]) - 55 * np.sin(5 * w[0])
return fx
# 定义进度函数
def progress(x, g, fx, xnorm, gnorm, step, n, k, ls):
print("Iteration %d:" % k)
print("x = %f, g = %f, fx = %f" % (x[0], g[0], fx))
return 0
# 设置参数
param = lbfgs_parameters(new_Evaluate, progress)
param.epsilon = 1e-5
param.max_iterations = 100
# 运行BFGS算法
x = np.array([0.0])
lbfgs(param, x)
print("Optimal solution: x = %f, f(x) = %f" % (x[0], func(x[0])))
```
阅读全文