17370845950

c++如何用Ceres Solver进行非线性优化_c++数值优化库Ceres Solver入门教程
首先定义残差函数并使用AutoDiffCostFunction,然后构建Problem添加残差块,最后配置Solver选项并求解;示例中通过Ceres拟合指数曲线,需安装依赖并链接库,推荐使用自动微分和合理初值,结合Huber等核函数提升鲁棒性,适用于SLAM与Bundle Adjustment。

在C++中使用Ceres Solver进行非线性优化,关键在于定义残差函数、构建问题结构并调用求解器。Ceres是Google开发的开源C++库,专为大规模非线性最小二乘问题设计,广泛应用于SLAM、Bundle Adjustment、曲线拟合等场景。

安装与配置Ceres Solver

确保系统已安装CMake和Eigen,Ceres依赖它们。推荐使用v2.1以上版本。

以Ubuntu为例:
  • 通过包管理器安装依赖:
    sudo apt-get install libeigen3-dev libsuitesparse-dev
  • 克隆并编译Ceres:
    git clone https://github.com/ceres-solver/ceres-solver
    mkdir ceres-bin && cd ceres-bin
    cmake ../ceres-solver && make -j4 && sudo make install
  • 在CMakeLists.txt中链接库:
    find\_package(Ceres REQUIRED)
    target\_link\_libraries(your\_program ${CERES\_LIBRARIES})

定义残差函数(Cost Function)

非线性优化目标是最小化残差平方和。你需要为每个观测定义一个残差块。

常用方式是继承ceres::CostFunction或使用自动微分。

示例:拟合曲线 y = exp(ax² + bx + c)
struct ExponentialResidual {
  ExponentialResidual(double x, double y) : x_(x), y_(y) {}

template bool operator()(const T const a, const T const b, const T const c, T residual) const { T model = ceres::exp(a[0] x_ x + b[0] * x + c[0]); residual[0] = T(y_) - model; return true; }

double x, y; };

使用AutoDiffCostFunction包装:

auto* cost_function =
  new ceres::AutoDiffCostFunction(
    new ExponentialResidual(x_data[i], y_data[i])
  );
problem.AddResidualBlock(cost_function, nullptr, &a, &b, &c);

构建并求解优化问题

将所有残差块加入Problem对象,设置求解选项后运行求解器。

ceres::Problem problem;
double a = 0.0, b = 0.0, c = 0.0; // 初始值

// 添加多个数据点 for (int i = 0; i < num_points; ++i) { problem.AddResidualBlock( new ceres::AutoDiffCostFunction( new ExponentialResidual(x_data[i], y_data[i])), nullptr, &a, &b, &c); }

// 配置求解器 ceres::Solver::Options options; options.linear_solver_type = ceres::DENSE_QR; options.minimizer_progress_to_stdout = true;

// 运行求解 ceres::Solver::Summary summary; ceres::Solve(options, &problem, &summary);

std::cout << summary.BriefReport() << "\n"; std::cout << "Estimated: a=" << a << ", b=" << b << ", c=" << c << "\n";

常见技巧与注意事项

  • 合理设置参数初值,避免陷入局部极小
  • 对病态问题可添加鲁棒核函数(如Huber核):
    new ceres::CauchyLoss(0.5)
  • 若函数复杂或导数难求,优先使用自动微分(AutoDiff)
  • 关注控制台输出的迭代信息,判断是否收敛
  • 对于大型稀疏问题,选用SUITE_SPARSESPARSE_NORMAL_CHOLESKY

基本上就这些。掌握从建模到求解的流程后,你可以将其扩展到更复杂的优化任务,比如位姿图优化或多变量非线性拟合。Ceres的强大之处在于灵活支持多种导数计算方式和高效求解器,适合工程与科研场景。