介绍
1.使用ceres求解非线性最小二乘的步骤
* 自定义误差项:CostFunctor
* 建立CostFunction
* 设定初值
* 构建problem
* 构建cost_function, 并通过problem.AddResidualBlock添加
* 运行solver: Solve(options, &problem, & summary)
-定义option:liner_solver_type, minimizer_progress_to_stdout,
-定义summary
* 输出结果: cout << summary.BriefReport() << endl;
2.代码:
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, NULL, &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;
}
导数
数值导数
理论导数
1.自定义参差结构体,用来计算参差,以f(x) = 10 - x 为例:
struct NumericDiffCostFunctor {
bool operator()(const double* const x, double* residual) const {
residual[0] = 10.0 - x[0];
return true;
}
};
2.通过自定义的结构体,构建CostFunction, 并加入到problem:
CostFunction* cost_function =
new NumericDiffCostFunction<NumericDiffCostFunctor, ceres::CENTRAL, 1, 1>(
new NumericDiffCostFunctor);
problem.AddResidualBlock(cost_function, NULL, &x);
注意:这里使用的是NumericDiffCostFunction,而前面用的是AutoDiffFunction。更推荐使用AutoDiffFunction, 因为更加高效。
解析导数
在一些场合(不太懂),需要使用解析导数,这就需要自定义参差和Jacobian。可以通过构建一个CostFunction或者SizedCostFuntion的子类来实现。
但是,除非必须使用解析导数,一般更加推荐使用AutoDiffCostFuntion 或者 NumericDiffCosFunction
讨论
求导是使用Ceres中最复杂的过程,这里只是一个介绍而已。
例:鲍威尔方程(Powell’s Function)
问题
第一步:定义参差项(以f4为例):
struct F4 {
template <typename T>
bool operator()(const T* const x1, const T* const x4, T* residual) const {
residual[0] = T(sqrt(10.0)) * (x1[0] - x4[0]) * (x1[0] - x4[0]);
return true;
}
};
第二步:添加误差项
double x1 = 3.0; double x2 = -1.0; double x3 = 0.0; double x4 = 1.0;
Problem problem;
// Add residual terms to the problem using the using the autodiff
// wrapper to get the derivatives automatically.
problem.AddResidualBlock(
new AutoDiffCostFunction<F1, 1, 1, 1>(new F1), NULL, &x1, &x2);
problem.AddResidualBlock(
new AutoDiffCostFunction<F2, 1, 1, 1>(new F2), NULL, &x3, &x4);
problem.AddResidualBlock(
new AutoDiffCostFunction<F3, 1, 1, 1>(new F3), NULL, &x2, &x3)
problem.AddResidualBlock(
new AutoDiffCostFunction<F4, 1, 1, 1>(new F4), NULL, &x1, &x4);