美文网首页
ceres solver 00 在linux下的安装

ceres solver 00 在linux下的安装

作者: book_02 | 来源:发表于2019-11-04 21:08 被阅读0次

    1. 安装依赖

    sudo apt-get install cmake libeigen3-dev libatlas-base-dev liblapack-dev libgflags-dev libgoogle-glog-dev libgtest-dev libsuitesparse-dev
    

    依赖主要有:

    1. cmake
    2. Eigen3
    3. BLAS & LAPACK
    4. google-glog + gflags
    5. SuiteSparse and CXSparse (optional)
      如果想要安装CXSparse,可以使用sudo apt-get install libcxsparse3.1.4,末尾的版本号可能不一样,可用自动补全来填写

    2. 下载源码,编译安装

    从github下载源码

    git clone https://github.com/ceres-solver/ceres-solver
    

    或者尝试下面的地址下载:

    wget http://ceres-solver.org/ceres-solver-1.14.0.tar.gz
    

    如果能翻墙,也可尝试下面的地址

    git clone https://ceres-solver.googlesource.com/ceres-solver
    

    编译安装:

    ## 解压。如果是github上git clone的,就已经是目录了
    tar zxf ceres-solver-1.14.0.tar.gz
    
    ## 建立编译目录,并进入
    cd ceres-solver-1.14.0
    mkdir build
    cd build
    
    ## cmake
    cmake ..
    
    ## 编译
    make -j3
    
    ## 测试,可跳过
    make test
    
    ## 安装
    sudo make install
    

    3. 调用示例

    建立CMakeLists.txt文件如下:

    cmake_minimum_required(VERSION 2.8)
    
    project(hello_world)
    
    find_package(Ceres REQUIRED)
    include_directories(${CERES_INCLUDE_DIRS})
    
    add_executable(hello_world main.cpp)
    target_link_libraries(hello_world ${CERES_LIBRARIES})
    

    建立main.cpp文件如下:

    // A simple example of using the Ceres minimizer.
    //
    // Minimize 0.5 (10 - x)^2 using jacobian matrix computed using
    // automatic differentiation.
    
    #include "ceres/ceres.h"
    
    using ceres::AutoDiffCostFunction;
    using ceres::CostFunction;
    using ceres::Problem;
    using ceres::Solver;
    using ceres::Solve;
    
    // A templated cost functor that implements the residual r = 10 -
    // x. The method operator() is templated so that we can then use an
    // automatic differentiation wrapper around it to generate its
    // derivatives.
    struct CostFunctor {
      template <typename T> bool operator()(const T* const x, T* residual) const {
        residual[0] = T(10.0) - x[0];
        return true;
      }
    };
    
    int main(int argc, char** argv) {
    
      // The variable to solve for with its initial value. It will be
      // mutated in place by the solver.
      double x = 0.5;
      const double initial_x = 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.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.48e-05    1.16e-03
       1  4.511598e-07    4.51e+01    9.50e-04   9.50e+00   1.00e+00  3.00e+04        1    4.56e-04    2.07e-03
       2  5.012552e-16    4.51e-07    3.17e-08   9.50e-04   1.00e+00  9.00e+04        1    2.82e-05    2.12e-03
    Ceres Solver Report: Iterations: 3, Initial cost: 4.512500e+01, Final cost: 5.012552e-16, Termination: CONVERGENCE
    x : 0.5 -> 10
    
    

    4. 参考

    http://ceres-solver.org/installation.html

    相关文章

      网友评论

          本文标题:ceres solver 00 在linux下的安装

          本文链接:https://www.haomeiwen.com/subject/mbfjbctx.html