• Ceres Solver简介及使用


          Ceres Solver是一个开源的C++库,用于建模和解决大型、复杂的优化问题。它是一个成熟、功能丰富且高性能的库,自2010年以来一直在Google生产中使用。最新发布版本为2.1.0,license为BSD,它支持在Windows、Linux、Mac、Android、iOS上编译,源码地址:https://ceres-solver.googlesource.com/ceres-solver 
          Ceres Solver可用于解决两类问题
          (1).具有边界约束的非线性最小二乘(Non-linear Least Squares)问题;
          (2).一般无约束优化问题
          Ceres Solver依赖项
          (1).必须的:CMake、Eigen、glog、gflags;
          (2).可选的:SuiteSparse、BLAS and LAPACK、CUDA。
          Ceres Solver在Ubuntu 20.04上编译:eigen, glog, gflags, ceres-solver都在/home/spring/Soft目录下

          (1).clone eigen源码:这里使用3.3.7版本,依次执行如下命令:

    1. git clone https://gitlab.com/libeigen/eigen.git
    2. cd eigen
    3. git checkout 3.3.7

          编译eigen,在eigen目录下执行build_eigen.sh,其内容如下:

    1. #! /bin/bash
    2. if [[ ! -d "build" ]]; then
    3. mkdir build
    4. cd build
    5. else
    6. cd build
    7. fi
    8. cmake \
    9. -DCMAKE_CXX_FLAGS=-fPIC \
    10. -DCMAKE_C_FLAGS=-fPIC \
    11. -DCMAKE_BUILD_TYPE=Release \
    12. -DCMAKE_INSTALL_PREFIX=../install \
    13. ..
    14. make -j2
    15. make install

          (2).clone glog源码:这里使用v0.4.0版本,依次执行如下命令:

    1. git clone https://github.com/google/glog.git
    2. cd glog
    3. git checkout v0.4.0

          编译glog,在glog目录下直接执行build_glog.sh,其内容与build_eigen.sh完全一致。

          (3).clone gflags源码:这里使用v2.2.2版本,依次执行如下命令:

    1. git clone https://github.com/gflags/gflags.git
    2. cd gflags
    3. git checkout v2.2.2

          编译gflags,在gflags目录执行build_gflags.sh,其内容与build_eigen.sh完全一致。

          (4).clone Ceres Solver源码:这里使用1.14.0版本,依次执行如下命令:

    1. git clone https://ceres-solver.googlesource.com/ceres-solver
    2. cd ceres-solver
    3. git checkout 1.14.0

          编译ceres-solver,在ceres-solver目录执行build_ceres-solver.sh,其内容如下:

    1. #! /bin/bash
    2. if [[ ! -d "build" ]]; then
    3. mkdir build
    4. cd build
    5. else
    6. cd build
    7. fi
    8. path=/home/spring/Soft
    9. cmake \
    10. -DCMAKE_CXX_FLAGS=-fPIC \
    11. -DCMAKE_C_FLAGS=-fPIC \
    12. -DCMAKE_BUILD_TYPE=Release \
    13. -DCMAKE_INSTALL_PREFIX=../install \
    14. -DEigen3_DIR=${path}/eigen/install/share/eigen3/cmake \
    15. -Dglog_DIR=${path}/glog/install/lib/cmake/glog \
    16. -Dgflags_DIR=${path}/gflags/install/lib/cmake/gflags \
    17. ..
    18. make -j2
    19. make install

          Ceres Solver调用:在/home/spring/Soft目录下新建test目录,此目录内文件内容依次如下

          (1).main.cpp:

    1. #include
    2. #include
    3. #include
    4. using namespace ceres;
    5. struct CostFunctor {
    6. template <typename T>
    7. bool operator()(const T* const x, T* residual) const {
    8. residual[0] = 10.0 - x[0];
    9. return true;
    10. }
    11. };
    12. int main(int argc, char** argv)
    13. {
    14. // reference: http://ceres-solver.org/nnls_tutorial.html
    15. google::InitGoogleLogging(argv[0]);
    16. // The variable to solve for with its initial value.
    17. double initial_x = 5.0;
    18. double x = initial_x;
    19. // Build the problem.
    20. Problem problem;
    21. // Set up the only cost function (also known as residual). This uses
    22. // auto-differentiation to obtain the derivative (jacobian).
    23. CostFunction* cost_function = new AutoDiffCostFunction1, 1>(new CostFunctor);
    24. problem.AddResidualBlock(cost_function, nullptr, &x);
    25. // Run the solver!
    26. Solver::Options options;
    27. options.linear_solver_type = ceres::DENSE_QR;
    28. options.minimizer_progress_to_stdout = true;
    29. Solver::Summary summary;
    30. Solve(options, &problem, &summary);
    31. std::cout << summary.BriefReport() << "\n";
    32. std::cout << "x : " << initial_x << " -> " << x << "\n";
    33. return 0;
    34. }

          (2).build.sh:

    1. #! /bin/bash
    2. if [[ ! -d "build" ]]; then
    3. mkdir build
    4. cd build
    5. else
    6. cd build
    7. fi
    8. path=/home/spring/Soft
    9. cp ${path}/glog/install/lib/libglog.a .
    10. cp ${path}/gflags/install/lib/libgflags.a .
    11. cp ${path}/ceres-solver/install/lib/libceres.a .
    12. cmake ..
    13. make

          (3).CMakeLists.txt:

    1. cmake_minimum_required(VERSION 3.15)
    2. project(test_ceres-solver)
    3. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
    4. include_directories(
    5. ${CMAKE_CURRENT_SOURCE_DIR}/../eigen/install/include/eigen3
    6. ${CMAKE_CURRENT_SOURCE_DIR}/../glog/install/include
    7. ${CMAKE_CURRENT_SOURCE_DIR}/../gflags/install/include
    8. ${CMAKE_CURRENT_SOURCE_DIR}/../ceres-solver/install/include
    9. )
    10. add_executable(${CMAKE_PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp)
    11. target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/build)
    12. target_link_libraries(${CMAKE_PROJECT_NAME} ceres glog gflags pthread)

          执行:

          首先执行:./build.sh

          然后执行:./build/test_ceres-solver ,结果如下图所示:

     

  • 相关阅读:
    hive 之select 中文乱码
    记录一次通过openVPN访问域名无法访问的问题
    软件测试工程师需要掌握哪些技能呢?
    2022-08-10 学习日记(30th day)注解
    团建游戏------踩数字
    Spring Security是什么? - 密码认证(四)
    2022.7.4【Python语法、Pytorch模型保存/加载】
    阿里p8软测专家耗时一个月整理出,从0基础自学到功能测试再到自动化测试超全学习指南
    机器学习概论
    语音分离---学习笔记(1)
  • 原文地址:https://blog.csdn.net/fengbingchun/article/details/131748924