ceres-solver——Non-linear Least Squares

1.ceres解决诸如下图形式的有边界约束的鲁棒非线性最小二乘问题,其中,求和符号后的一整部分即为残差块,fi()是一个costfunction(残差项),ρ 为鲁棒核函数,用来降低错误数据对整体优化的影响,[xi1…xik]为参数块。
在这里插入图片描述
2.官方教程里给出的解决简单的残差项(求导不麻烦)优化问题的步骤是:
例如要求 1/2* (10-x)^2 最小值时的x

  1. 定义计算残差的函数子
    在这里插入图片描述
  2. 将costfunction作为输入调用自动求导AutoDiffCostFunction来定义一个ceres::CostFunction*类型的变量,AutoDiffCostFunction会自动识别它并返回个ceres::CostFunction的接口,AutoDiffCostFunction尖括号<>中第一个参数后面的参数分别是残差维度以及各个优化变量的维度。
  3. 最后将CostFunction、lossfunction以及优化变量作为AddResidualBlock的输入构造残差块,则优化问题构造完毕。
  4. 调用ceres::Solver解问题即可
int main(int argc, char** argv) {
  google::InitGoogleLogging(argv[0]);

  // The variable to solve for with its initial value.
  double initial_x = 5.0;
  double x = initial_x;

  // Build the problem.
  Problem problem;

  // Set up the only cost function (also known as residual). This uses
  // auto-differentiation to obtain the derivative (jacobian).
  CostFunction* cost_function =
      new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
  problem.AddResidualBlock(cost_function, nullptr, &x);

  // Run the solver!
  Solver::Options options;
  options.linear_solver_type = ceres::DENSE_QR;
  options.minimizer_progress_to_stdout = true;
  Solver::Summary summary;
  Solve(options, &problem, &summary);

  std::cout << summary.BriefReport() << "\n";
  std::cout << "x : " << initial_x
            << " -> " << x << "\n";
  return 0;
}

如下是优化结果:

iter      cost      cost_change  |gradient|   |step|    tr_ratio  tr_radius  ls_iter  iter_time  total_time
   0  4.512500e+01    0.00e+00    9.50e+00   0.00e+00   0.00e+00  1.00e+04       0    5.33e-04    3.46e-03
   1  4.511598e-07    4.51e+01    9.50e-04   9.50e+00   1.00e+00  3.00e+04       1    5.00e-04    4.05e-03
   2  5.012552e-16    4.51e-07    3.17e-08   9.50e-04   1.00e+00  9.00e+04       1    1.60e-05    4.09e-03
Ceres Solver Report: Iterations: 2, Initial cost: 4.512500e+01, Final cost: 5.012552e-16, Termination: CONVERGENCE
x : 0.5 -> 10

由于给出的例子实际上是一个线性问题,故只需要迭代一次即可获得满足条件的解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值