ceres使用记录之autodiff_cost_function.h

Posted geooeg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ceres使用记录之autodiff_cost_function.h相关的知识,希望对你有一定的参考价值。

 1 // For example, consider a scalar error e = k - x‘y, where both x and y are
 2 // two-dimensional column vector parameters, the prime sign indicates
 3 // transposition, and k is a constant. The form of this error, which is the
 4 // difference between a constant and an expression, is a common pattern in least
 5 // squares problems. For example, the value x‘y might be the model expectation
 6 // for a series of measurements, where there is an instance of the cost function
 7 // for each measurement k.
 8 //
 9 // The actual cost added to the total problem is e^2, or (k - x‘k)^2; however,
10 // the squaring is implicitly done by the optimization framework.
11 // 上面的几句话的意思是在构建cost Function的时候,不需要考虑平方,平方操作在优化框架里已经实现
12 // To write an auto-differentiable cost function for the above model, first
13 // define the object
14 //  首先一个问题,用ceres规范怎么定义一个cost Function呢?如下:
15 //   class MyScalarCostFunctor {
16 //     MyScalarCostFunctor(double k): k_(k) {}
17 //     
18 //     template <typename T>
19 //     bool operator()(const T* const x , const T* const y, T* e) const {
20 //       e[0] = T(k_) - x[0] * y[0] + x[1] * y[1];
21 //       return true;
22 //     }
23 //
24 //    private:
25 //     double k_;
26 //   };
27 // 上面代码中的x和y都是状态变量,切每个状态变量都是二维的,e就是误差的定义方法,记住最终的误差是e^2,但是这个操作是在库里做好的
28 // Note that in the declaration of operator() the input parameters x and y come
29 // first, and are passed as const pointers to arrays of T. If there were three
30 // input parameters, then the third input parameter would come after y. The
31 // output is always the last parameter, and is also a pointer to an array. In
32 // the example above, e is a scalar, so only e[0] is set.
33 //
34 // Then given this class definition, the auto differentiated cost function for
35 // it can be constructed as follows.
36 // 下面详细解释了,如何新建一个自动求导的对象,以及各个参数的意义
37 //   CostFunction* cost_function
38 //       = new AutoDiffCostFunction<MyScalarCostFunctor, 1, 2, 2>(
39 //            new MyScalarCostFunctor(1.0));             ^  ^  ^
40 //                                                       |  |  |
41 //                            Dimension of residual -----+  |  |
42 //                            Dimension of x ---------------+  |
43 //                            Dimension of y ------------------+
44 //
45 // In this example, there is usually an instance for each measumerent of k.
46 //
47 // In the instantiation above, the template parameters following
48 // "MyScalarCostFunctor", "1, 2, 2", describe the functor as computing a
49 // 1-dimensional output from two arguments, both 2-dimensional.
50 // 当误差的个数不确定时,该库还支持动态个数的误差
51 // AutoDiffCostFunction also supports cost functions with a
52 // runtime-determined number of residuals. For example:
53 //
54 //   CostFunction* cost_function
55 //       = new AutoDiffCostFunction<MyScalarCostFunctor, DYNAMIC, 2, 2>(
56 //           new CostFunctorWithDynamicNumResiduals(1.0),   ^     ^  ^
57 //           runtime_number_of_residuals); <----+           |     |  |
58 //                                              |           |     |  |
59 //                                              |           |     |  |
60 //             Actual number of residuals ------+           |     |  |
61 //             Indicate dynamic number of residuals --------+     |  |
62 //             Dimension of x ------------------------------------+  |
63 //             Dimension of y ---------------------------------------+
64 //
65 // The framework can currently accommodate cost functions of up to 10
66 // independent variables, and there is no limit on the dimensionality
67 // of each of them.
68 // 
69 // WARNING #1: Since the functor will get instantiated with different types for
70 // T, you must to convert from other numeric types to T before mixing
71 // computations with other variables of type T. In the example above, this is
72 // seen where instead of using k_ directly, k_ is wrapped with T(k_).
73 //
74 // WARNING #2: A common beginner‘s error when first using autodiff cost
75 // functions is to get the sizing wrong. In particular, there is a tendency to
76 // set the template parameters to (dimension of residual, number of parameters)
77 // instead of passing a dimension parameter for *every parameter*. In the
78 // example above, that would be <MyScalarCostFunctor, 1, 2>, which is missing
79 // the last ‘2‘ argument. Please be careful when setting the size parameters.

 



以上是关于ceres使用记录之autodiff_cost_function.h的主要内容,如果未能解决你的问题,请参考以下文章

ROS实验笔记之——ceres跟eigen不匹配

ROS实验笔记之——ceres跟eigen不匹配

ceres 点云平面拟合 实例

vs2015+64位win10系统ceres-solver编译

Ceres学习

cerely 的使用