1.ceres解决诸如下图形式的有边界约束的鲁棒非线性最小二乘问题,其中,求和符号后的一整部分即为残差块,fi()是一个costfunction(残差项),ρ 为鲁棒核函数,用来降低错误数据对整体优化的影响,[xi1…xik]为参数块。
2.官方教程里给出的解决简单的残差项(求导不麻烦)优化问题的步骤是:
例如要求 1/2* (10-x)^2 最小值时的x
- 定义计算残差的函数子
- 将costfunction作为输入调用自动求导AutoDiffCostFunction来定义一个ceres::CostFunction*类型的变量,AutoDiffCostFunction会自动识别它并返回个ceres::CostFunction的接口,AutoDiffCostFunction尖括号<>中第一个参数后面的参数分别是残差维度以及各个优化变量的维度。
- 最后将CostFunction、lossfunction以及优化变量作为AddResidualBlock的输入构造残差块,则优化问题构造完毕。
- 调用ceres::Solver解问题即可
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, nullptr, &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;
}
如下是优化结果:
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.33e-04 3.46e-03
1 4.511598e-07 4.51e+01 9.50e-04 9.50e+00 1.00e+00 3.00e+04 1 5.00e-04 4.05e-03
2 5.012552e-16 4.51e-07 3.17e-08 9.50e-04 1.00e+00 9.00e+04 1 1.60e-05 4.09e-03
Ceres Solver Report: Iterations: 2, Initial cost: 4.512500e+01, Final cost: 5.012552e-16, Termination: CONVERGENCE
x : 0.5 -> 10
由于给出的例子实际上是一个线性问题,故只需要迭代一次即可获得满足条件的解。