Ceres solver是一个开源C++库,用于建模和解决大型、复杂的优化问题。它可用于解决具有边界约束的非线性最小二乘问题和一般无约束优化问题。它是一个成熟,功能丰富且性能强大的库,自2010年以来一直在Google的生产中使用。
对于 Ceres solver的安装有很多说明,但是对于 Ceres solver的测试却没有很明确的,因为自己对ubuntu系统并不熟悉,因此,在对 Ceres solver进行测试时,多番百度,得出以下方式测试 Ceres solver的安装是否成功,为防止忘记,特此写下。
1.从github上获取最近源代码
传送门:https://github.com/ceres-solver/ceres-solver/tags
注意:这里需要注意自己需要下载的版本,注意ceres和eigen版本不兼容的问题,这里我下在的1.14.0版本。
下载后对其进行解压缩。因为我是为了安装colmap才对ceres-solver进行安装,因此,一些环境已经安装好了,这里在别的地方找到了ceres-solver所需要的依赖。
2.相关依赖安装
# 安装相关依赖
# CMake
sudo apt-get install cmake
# google-glog + gflags
sudo apt-get install libgoogle-glog-dev libgflags-dev
# BLAS & LAPACK
sudo apt-get install libatlas-base-dev
# Eigen3
sudo apt-get install libeigen3-dev
# SuiteSparse and CXSparse (optional)
sudo apt-get install libsuitesparse-dev
3.安装Ceres 1.14.0
一句句复制在终端运行,基本不会出错
// 在ceres-solver-1.14.0文件中打开终端。
sudo apt-get install libatlas-base-dev libsuitesparse-dev
git clone https://ceres-solver.googlesource.com/ceres-solver
cd ceres-solver-1.14.0 //打开下载的包
mkdir build //创建build
cd build //打开build
cmake .. -DBUILD_TESTING=OFF -DBUILD_EXAMPLES=OFF //cmake
make
sudo make install
这样,Ceres solver的安装就已经完成了,接下来需要对Ceres solver进行测试。
4.Ceres solver测试
首先要编写CMakeLists.txt文件保证头文件和动态库可以被找到。
打开文档编辑器对其进行保存。如上图所示。
/*CMakeLists.txt*/
cmake_minimum_required(VERSION 3.8.0)
project(ceres_example)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Ceres REQUIRED)
include_directories(
${CERES_INCLUDE_DIRS}
)
add_executable(ceres_example
ceres_example.cpp)
target_link_libraries(ceres_example
${CERES_LIBRARIES}
)
然后是Ceres官网的一个简单的例程。
#include <ceres/ceres.h>
class CostFunctor {
public:
template <typename T>
bool operator()(const T* const x, T* residual) const
{
residual[0] = 10.0 - x[0];
return true;
}
};
int main(int argc, char const* argv[])
{
double initial_x = 5.0;
double x = initial_x;
// Build the problem.
ceres::Problem problem;
// Set up the only cost function (also known as residual). This uses
// auto-differentiation to obtain the derivative (jacobian).
ceres::CostFunction* cost_function = new ceres::AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
problem.AddResidualBlock(cost_function, nullptr, &x);
// Run the solver!
ceres::Solver::Options options;
options.linear_solver_type = ceres::DENSE_QR;
options.minimizer_progress_to_stdout = true;
ceres::Solver::Summary summary;
Solve(options, &problem, &summary);
std::cout << summary.BriefReport() << "\n";
std::cout << "x : " << initial_x
<< " -> " << x << "\n";
return 0;
}
存储后在此界面打开终端编译,
mkdir build
cd build
cmake ..
make
在此终端下输入./ceres_example
即可得出下列输出。
出现下列输出,说明Ceres安装正常。
iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time
0 1.250000e+01 0.00e+00 5.00e+00 0.00e+00 0.00e+00 1.00e+04 0 2.69e-05 8.58e-05
1 1.249750e-07 1.25e+01 5.00e-04 5.00e+00 1.00e+00 3.00e+04 1 8.01e-05 2.31e-04
2 1.388518e-16 1.25e-07 1.67e-08 5.00e-04 1.00e+00 9.00e+04 1 1.00e-05 2.56e-04
Ceres Solver Report: Iterations: 3, Initial cost: 1.250000e+01, Final cost: 1.388518e-16, Termination: CONVERGENCE
x : 5 -> 10
就此,Ceres solver安装与测试就完成啦。