ceres学习笔记(一)

本来还想着先对照着官方doc来学习的。突然在csdn里面搜了下,发现了几篇高质量的文章,就先对应这几篇文章学习,来快速入门。

一、ceres求解问题一般步骤

使用ceres-solver求解优化问题一般分为下面三步:

1.第一步:构建代价函数costFunction,就是寻优的目标表达式。具体做法是定义一个costFunction的结构体,然后在结构体内重载operator()运算符,有博主指出这是c++中一种仿函数的技巧。

2.第二步:通过代价函数构建待求解的优化问题。

3.第三步:配置求解器,就是设置方程怎么求解、求解过程是否输出等,最后调用solve方法。

二、实际代码求解过程

以官网的的入门例子来说明实际代码如何编写。

求解\frac{1}{2}*\left ( 10-x \right )^2的最小值。

按上面的求解步骤来:

第一步构造代价函数

struct CostFunctor {
   template <typename T>
   bool operator()(const T* const x, T* residual) const {
     residual[0] = 1/2*(T(10.0) - x[0])*(T(10.0) - x[0]);
     return true;
   }
};

这里是写了一个struct来构建代价函数CostFunctor,它里面重载了operator()运算符。其中x表示自变量,residual表示因变量。注意它们都是数组的,使用的全是数组名为参数。还有一点是这里使用了三个const,最后一个const是表明不会改变数据成员函数。前面两个const是指声明该指针变量里面的内容不可改变,同时该内容指向的内容亦不可改变。

参考资料:

const int、const int *、int *cosnt、const int * const、const int &的区别_Ruo_Xiao的博客-CSDN博客_const int[3]

C++之const类成员变量,const成员函数 - CTHON - 博客园

第二步通过代价函数构建待求解的优化问题

Problem problem;
  CostFunction* cost_function =
      new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor); 
  problem.AddResidualBlock(cost_function, NULL, &x); 

首先生成问题对象,然后生成代价函数cost_function,它使用第一步定义的代价函数结构体CostFunctor来作为参数生成,使用自动求导的方式。第一个1表示输出的纬度,就是residual的纬度。第二个1表示输入的纬度就是x。后面的就是向问题中添加误差项,第二个参数NULL表示不用添加核函数,在aloam中就设置的核函数,这里没有设置,第三个参数就是待寻找最优解的x。

第三步配置problem并求解它

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

创建一个options它用来设置求解的一些选项。使用ceres::DENSE_QR线性求解器,设置可以输出到cout,然后就是设置了summary的优化信息,主要是一些文字信息,记录了优化的过程,和结果什么的。然后使用Solve函数开始求解,第一个参数是设置的选项,第二个是问题,第三个是信息。

完整代码如下:

#include<iostream>
#include<ceres/ceres.h>

using namespace std;
using namespace ceres;

//第一部分:构建代价函数,重载()符号,仿函数的小技巧
struct CostFunctor {
   template <typename T>
   bool operator()(const T* const x, T* residual) const {
     residual[0] = 0.5*(T(10.0) - x[0])*(T(10.0) - x[0]);
     return true;
   }
};

//主函数
int main(int argc, char** argv) {
  google::InitGoogleLogging(argv[0]);

  // 寻优参数x的初始值,为5
  double initial_x = 5.0;
  double x = initial_x;

  // 第二部分:构建寻优问题
Problem problem;
  CostFunction* cost_function=
      new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor); //使用自动求导,将之前的代价函数结构体传入,第一个1是输出维度,即残差的维度,第二个1是输入维度,即待寻优参数x的维度。
  problem.AddResidualBlock(cost_function, NULL, &x); //向问题中添加误差项,本问题比较简单,添加一个就行。

  //第三部分: 配置并运行求解器
  Solver::Options options;
  options.linear_solver_type = ceres::DENSE_QR; //配置增量方程的解法
  options.minimizer_progress_to_stdout = true;//输出到cout
  Solver::Summary summary;//优化信息
  Solve(options, &problem, &summary);//求解!!!

  // std::cout << summary.BriefReport() << "\n";//输出优化的简要信息
//最终结果
  std::cout << "x : " << initial_x
            << " -> " << x << "\n";
  return 0;
}

还有CMakeLists.txt内容:

cmake_minimum_required(VERSION 2.8)
project(ceres)

find_package(Ceres REQUIRED)
include_directories(${CERES_INCLUDE_DIRS})

add_executable(xxx xxx.cpp)
target_link_libraries(xxx ${CERES_LIBRARIES})

比较简单不做解释。

参考资料:

一文助你Ceres 入门——Ceres Solver新手向全攻略_福尔摩睿的博客-CSDN博客_ceres solver

三、非线性拟合求解

使用ceres进行非线性拟合求解,它指定一系列的点对来拟合一个曲线的系数。

首先有一系列的点,这些点是是通过对曲线y=e^{0.3x+0.1}插值的点然后加上高斯噪声获得的。通过这些点来拟合曲线,y=a*e^{bx+c}。这个形似是这个,就是通过这些点来优化算出算出a,b,c的值。

第一步构造代价函数:

struct ExponentialResidual {
    ExponentialResidual(double x, double y)
            : x_(x), y_(y) {}

    template <typename T> bool operator()(const T* const a, const T* const b, const T* const c, T* residual) const {
        residual[0] = y_ - a[0] * exp(b[0] * x_ + c[0]);
        return true;
    }

private:
    const double x_;
    const double y_;
};

第二步通过代价函数设置优化问题:

    Problem problem;
    for (int i = 0; i < kNumObservations; ++i) {
        CostFunction* cost_function = new AutoDiffCostFunction<ExponentialResidual, 1, 1, 1, 1>(new ExponentialResidual(data[2 * i], data[2 * i + 1]));
        problem.AddResidualBlock(
                cost_function,
                NULL,
                &a, &b, &c);
    }

这里和前面的不一样的是这里有很多的数据,需要不断的把数据添加进problem。

第三步配置problem并求解:

    Solver::Options options;
    options.max_num_iterations = 25;
    options.linear_solver_type = ceres::DENSE_QR;
    options.minimizer_progress_to_stdout = true;

    Solver::Summary summary;
    Solve(options, &problem, &summary);

一些对于options的设置。

完整的程序:

#include <iostream>
#include "ceres/ceres.h"

using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solver;
using ceres::Solve;


const int kNumObservations = 67;
//观测值
const double data[] = {
        0.000000e+00, 1.133898e+00,
        7.500000e-02, 1.334902e+00,
        1.500000e-01, 1.213546e+00,
        2.250000e-01, 1.252016e+00,
        3.000000e-01, 1.392265e+00,
        3.750000e-01, 1.314458e+00,
        4.500000e-01, 1.472541e+00,
        5.250000e-01, 1.536218e+00,
        6.000000e-01, 1.355679e+00,
        6.750000e-01, 1.463566e+00,
        7.500000e-01, 1.490201e+00,
        8.250000e-01, 1.658699e+00,
        9.000000e-01, 1.067574e+00,
        9.750000e-01, 1.464629e+00,
        1.050000e+00, 1.402653e+00,
        1.125000e+00, 1.713141e+00,
        1.200000e+00, 1.527021e+00,
        1.275000e+00, 1.702632e+00,
        1.350000e+00, 1.423899e+00,
        1.425000e+00, 1.543078e+00,
        1.500000e+00, 1.664015e+00,
        1.575000e+00, 1.732484e+00,
        1.650000e+00, 1.543296e+00,
        1.725000e+00, 1.959523e+00,
        1.800000e+00, 1.685132e+00,
        1.875000e+00, 1.951791e+00,
        1.950000e+00, 2.095346e+00,
        2.025000e+00, 2.361460e+00,
        2.100000e+00, 2.169119e+00,
        2.175000e+00, 2.061745e+00,
        2.250000e+00, 2.178641e+00,
        2.325000e+00, 2.104346e+00,
        2.400000e+00, 2.584470e+00,
        2.475000e+00, 1.914158e+00,
        2.550000e+00, 2.368375e+00,
        2.625000e+00, 2.686125e+00,
        2.700000e+00, 2.712395e+00,
        2.775000e+00, 2.499511e+00,
        2.850000e+00, 2.558897e+00,
        2.925000e+00, 2.309154e+00,
        3.000000e+00, 2.869503e+00,
        3.075000e+00, 3.116645e+00,
        3.150000e+00, 3.094907e+00,
        3.225000e+00, 2.471759e+00,
        3.300000e+00, 3.017131e+00,
        3.375000e+00, 3.232381e+00,
        3.450000e+00, 2.944596e+00,
        3.525000e+00, 3.385343e+00,
        3.600000e+00, 3.199826e+00,
        3.675000e+00, 3.423039e+00,
        3.750000e+00, 3.621552e+00,
        3.825000e+00, 3.559255e+00,
        3.900000e+00, 3.530713e+00,
        3.975000e+00, 3.561766e+00,
        4.050000e+00, 3.544574e+00,
        4.125000e+00, 3.867945e+00,
        4.200000e+00, 4.049776e+00,
        4.275000e+00, 3.885601e+00,
        4.350000e+00, 4.110505e+00,
        4.425000e+00, 4.345320e+00,
        4.500000e+00, 4.161241e+00,
        4.575000e+00, 4.363407e+00,
        4.650000e+00, 4.161576e+00,
        4.725000e+00, 4.619728e+00,
        4.800000e+00, 4.737410e+00,
        4.875000e+00, 4.727863e+00,
        4.950000e+00, 4.669206e+00,
};
//1.代价函数结构体
struct ExponentialResidual {
    ExponentialResidual(double x, double y)
            : x_(x), y_(y) {}

    template <typename T> bool operator()(const T* const a, const T* const b, const T* const c, T* residual) const {
        residual[0] = y_ - a[0] * exp(b[0] * x_ + c[0]);
        return true;
    }

private:
    const double x_;
    const double y_;
};

int main(int argc, char** argv) {

    double a = 0.0;
    double b = 0.0;
    double c = 0.0;
  
  //构建寻优问题
    Problem problem;
    for (int i = 0; i < kNumObservations; ++i) {
        CostFunction* cost_function = new AutoDiffCostFunction<ExponentialResidual, 1, 1, 1, 1>(new ExponentialResidual(data[2 * i], data[2 * i + 1]));
        problem.AddResidualBlock(
                cost_function,
                NULL,
                &a, &b, &c);
    }
   //配置并运行求解器
    Solver::Options options;
    options.max_num_iterations = 25;
    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 << "Real a: " << 1.0 << "      b: " << 0.3 << "      c: " << 0.1<< "\n";
    std::cout << "Initial a: "<< 0.0 <<"Initial b: " << 0.0 << " c: " << 0.0 << "\n";
    std::cout << "Final   a: " << a << "Final   b: " << b << " c: " << c << "\n";
    return 0;
}

最后输出不太对,有点问题。但主要是理解,不用太在意。

 参考资料:

Ceres入门——Ceres的基本使用方法_Andy是个男子名的博客-CSDN博客_ceres使用

四、总结

感觉ceres理解起来比g2o容易多了,不知道是不是因为google文档写的比较好,大家就用的很多,相关的参考资料也很多,所以很好上手。之后内容准备先把doc刷一遍,加强印象。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值