传统常见最优化方法封装,支持跨平台:Windows, Linux , macOS,包括单峰区间搜索、黄金分割区间搜索、精确线搜索(单峰区间+黄金分割)、Armijo 非精确线搜索、最速下降法、拟牛顿BFGS算法、拟牛顿DFP算法等。
spdlog
- 轻量C++日志库
Eigen
- 矩阵C++库
使用最速下降法求
double target_function(Eigen::VectorXd x)
{
double const x0 = x[0];
double const y0 = x[1];
return 3 * x0 * x0 + 2 * y0 * y0 - 4 * x0 - 6 * y0;
}
int main()
{
Eigen::VectorXd x0(2);
x0 << 0, 0;
OptimizeMethod opti = OptimizeMethod(target_function, spdlog::level::debug);
opti.gradient_descent(100, 1e-6, 0.01, x0);
#if defined(_WIN32) || defined(_WIN64)
system("pause");
#endif
}
分别使用拟牛顿方法中BFGS方法与DFP方法,采用Armijo准则进行非精确线搜索方法,使用迭代初始点
#include <OptimizeMethod.h>
#include <spdlog/spdlog.h>
#include <cstdlib>
double target_function(Eigen::VectorXd x)
{
double const x1 = x[0];
double const x2 = x[1];
return x1 * x1 - x1 * x2 + x2 * x2 + 2 * x1 - 4 * x2;
}
Eigen::VectorXd grad_target_function(Eigen::VectorXd x)
{
Eigen::VectorXd grad = Eigen::VectorXd::Zero(x.size());
double const x1 = x[0];
double const x2 = x[1];
grad[0] = 2 * x1 - x2 + 2;
grad[1] = 2 * x2 - x1 - 4;
return grad;
}
int main()
{
Eigen::VectorXd x0(2);
x0 << -2, 2;
OptimizeMethod opti = OptimizeMethod(target_function, grad_target_function, spdlog::level::debug);
opti.Newton_BFGS(100, 0.5, 0.2, 1e-6, 0.0001, x0);
opti.Newton_DFP(100, 0.5, 0.2, 1e-6, 0.0001, x0);
#if defined(_WIN32) || defined(_WIN64)
system("pause");
#endif
}