Ceres Tutorials 学习笔记(一):非线性最小二乘

介绍

1.使用ceres求解非线性最小二乘的步骤
* 自定义误差项:CostFunctor
* 建立CostFunction
* 设定初值
* 构建problem
* 构建cost_function, 并通过problem.AddResidualBlock添加
* 运行solver: Solve(options, &problem, & summary)
-定义option:liner_solver_type, minimizer_progress_to_stdout,
-定义summary
* 输出结果: cout << summary.BriefReport() << endl;

2.代码:

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, NULL, &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;
}

导数

数值导数

理论导数

1.自定义参差结构体,用来计算参差,以f(x) = 10 - x 为例:

struct NumericDiffCostFunctor {
  bool operator()(const double* const x, double* residual) const {
    residual[0] = 10.0 - x[0];
    return true;
  }
};

2.通过自定义的结构体,构建CostFunction, 并加入到problem:

CostFunction* cost_function =
  new NumericDiffCostFunction<NumericDiffCostFunctor, ceres::CENTRAL, 1, 1>(
      new NumericDiffCostFunctor);
problem.AddResidualBlock(cost_function, NULL, &x);

注意:这里使用的是NumericDiffCostFunction,而前面用的是AutoDiffFunction。更推荐使用AutoDiffFunction, 因为更加高效。

解析导数

在一些场合(不太懂),需要使用解析导数,这就需要自定义参差和Jacobian。可以通过构建一个CostFunction或者SizedCostFuntion的子类来实现。
但是,除非必须使用解析导数,一般更加推荐使用AutoDiffCostFuntion 或者 NumericDiffCosFunction

讨论

求导是使用Ceres中最复杂的过程,这里只是一个介绍而已。

例:鲍威尔方程(Powell’s Function)

问题

问题描述
第一步:定义参差项(以f4为例):

struct F4 {
  template <typename T>
  bool operator()(const T* const x1, const T* const x4, T* residual) const {
    residual[0] = T(sqrt(10.0)) * (x1[0] - x4[0]) * (x1[0] - x4[0]);
    return true;
  }
};

第二步:添加误差项

double x1 =  3.0; double x2 = -1.0; double x3 =  0.0; double x4 = 1.0;

Problem problem;

// Add residual terms to the problem using the using the autodiff
// wrapper to get the derivatives automatically.
problem.AddResidualBlock(
  new AutoDiffCostFunction<F1, 1, 1, 1>(new F1), NULL, &x1, &x2);
problem.AddResidualBlock(
  new AutoDiffCostFunction<F2, 1, 1, 1>(new F2), NULL, &x3, &x4);
problem.AddResidualBlock(
  new AutoDiffCostFunction<F3, 1, 1, 1>(new F3), NULL, &x2, &x3)
problem.AddResidualBlock(
  new AutoDiffCostFunction<F4, 1, 1, 1>(new F4), NULL, &x1, &x4);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ceres Solver 中,实现阻尼最小二乘的方法有两种: 1. 通过在 `Problem` 中添加一个阻尼项来实现。假设我们的问题中有一组残差 $r_i$,那么阻尼最小二乘的目标函数可以表示为: $$ \min \sum_{i=1}^n r_i^2 + \lambda \sum_{j=1}^m x_j^2 $$ 其中 $\lambda$ 是阻尼因子,$x_j$ 是待优化的变量。可以通过在 `Problem` 中添加一个额外的残差项来实现上述目标函数的最小化。具体实现方式如下: ```c++ // 定义残差结构体 struct Residual { Residual(double r, double lambda) : r_(r), lambda_(lambda) {} template <typename T> bool operator()(const T* const x, T* residual) const { residual[0] = T(r_); for (int i = 0; i < 3; ++i) { residual[0] += T(lambda_) * x[i] * x[i]; } return true; } double r_; double lambda_; }; // 创建 Problem 对象,并添加残差项 ceres::Problem problem; for (int i = 0; i < n; ++i) { ceres::CostFunction* cost_function = new ceres::AutoDiffCostFunction<Residual, 1, 3>(new Residual(r[i], lambda)); problem.AddResidualBlock(cost_function, NULL, x); } // 配置 Solver 并求解 ceres::Solver::Options options; options.linear_solver_type = ceres::DENSE_QR; ceres::Solver::Summary summary; ceres::Solve(options, &problem, &summary); ``` 2. 通过在优化变量的参数块中添加一个阻尼项来实现。假设我们的问题中有一组残差 $r_i$,那么阻尼最小二乘的目标函数可以表示为: $$ \min \sum_{i=1}^n r_i^2 + \lambda \sum_{j=1}^m x_j^2 $$ 其中 $\lambda$ 是阻尼因子,$x_j$ 是待优化的变量。可以通过在优化变量的参数块中添加一个额外的变量来实现上述目标函数的最小化。具体实现方式如下: ```c++ // 定义残差结构体 struct Residual { Residual(double r) : r_(r) {} template <typename T> bool operator()(const T* const x, T* residual) const { residual[0] = T(r_); return true; } double r_; }; // 创建 Problem 对象,并添加残差项 ceres::Problem problem; ceres::CostFunction* cost_function = new ceres::AutoDiffCostFunction<Residual, 1, 4>(new Residual(r[i])); problem.AddResidualBlock(cost_function, NULL, x, new double[1]{sqrt(lambda)}); // 配置 Solver 并求解 ceres::Solver::Options options; options.linear_solver_type = ceres::DENSE_QR; ceres::Solver::Summary summary; ceres::Solve(options, &problem, &summary); ``` 在上述代码中,我们将阻尼因子的平方根作为一个额外的变量添加到了优化变量的参数块中,然后在残差函数中使用该变量来计算阻尼项。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值