autodiff_cost_function.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2019 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: sameeragarwal@google.com (Sameer Agarwal)
  30. //
  31. // Create CostFunctions as needed by the least squares framework, with
  32. // Jacobians computed via automatic differentiation. For more
  33. // information on automatic differentiation, see the wikipedia article
  34. // at http://en.wikipedia.org/wiki/Automatic_differentiation
  35. //
  36. // To get an auto differentiated cost function, you must define a class with a
  37. // templated operator() (a functor) that computes the cost function in terms of
  38. // the template parameter T. The autodiff framework substitutes appropriate
  39. // "jet" objects for T in order to compute the derivative when necessary, but
  40. // this is hidden, and you should write the function as if T were a scalar type
  41. // (e.g. a double-precision floating point number).
  42. //
  43. // The function must write the computed value in the last argument
  44. // (the only non-const one) and return true to indicate
  45. // success. Please see cost_function.h for details on how the return
  46. // value maybe used to impose simple constraints on the parameter
  47. // block.
  48. //
  49. // For example, consider a scalar error e = k - x'y, where both x and y are
  50. // two-dimensional column vector parameters, the prime sign indicates
  51. // transposition, and k is a constant. The form of this error, which is the
  52. // difference between a constant and an expression, is a common pattern in least
  53. // squares problems. For example, the value x'y might be the model expectation
  54. // for a series of measurements, where there is an instance of the cost function
  55. // for each measurement k.
  56. //
  57. // The actual cost added to the total problem is e^2, or (k - x'y)^2; however,
  58. // the squaring is implicitly done by the optimization framework.
  59. //
  60. // To write an auto-differentiable cost function for the above model, first
  61. // define the object
  62. //
  63. // class MyScalarCostFunctor {
  64. // MyScalarCostFunctor(double k): k_(k) {}
  65. //
  66. // template <typename T>
  67. // bool operator()(const T* const x , const T* const y, T* e) const {
  68. // e[0] = T(k_) - x[0] * y[0] + x[1] * y[1];
  69. // return true;
  70. // }
  71. //
  72. // private:
  73. // double k_;
  74. // };
  75. //
  76. // Note that in the declaration of operator() the input parameters x and y come
  77. // first, and are passed as const pointers to arrays of T. If there were three
  78. // input parameters, then the third input parameter would come after y. The
  79. // output is always the last parameter, and is also a pointer to an array. In
  80. // the example above, e is a scalar, so only e[0] is set.
  81. //
  82. // Then given this class definition, the auto differentiated cost function for
  83. // it can be constructed as follows.
  84. //
  85. // CostFunction* cost_function
  86. // = new AutoDiffCostFunction<MyScalarCostFunctor, 1, 2, 2>(
  87. // new MyScalarCostFunctor(1.0)); ^ ^ ^
  88. // | | |
  89. // Dimension of residual -----+ | |
  90. // Dimension of x ---------------+ |
  91. // Dimension of y ------------------+
  92. //
  93. // In this example, there is usually an instance for each measurement of k.
  94. //
  95. // In the instantiation above, the template parameters following
  96. // "MyScalarCostFunctor", "1, 2, 2", describe the functor as computing a
  97. // 1-dimensional output from two arguments, both 2-dimensional.
  98. //
  99. // AutoDiffCostFunction also supports cost functions with a
  100. // runtime-determined number of residuals. For example:
  101. //
  102. // CostFunction* cost_function
  103. // = new AutoDiffCostFunction<MyScalarCostFunctor, DYNAMIC, 2, 2>(
  104. // new CostFunctorWithDynamicNumResiduals(1.0), ^ ^ ^
  105. // runtime_number_of_residuals); <----+ | | |
  106. // | | | |
  107. // | | | |
  108. // Actual number of residuals ------+ | | |
  109. // Indicate dynamic number of residuals --------+ | |
  110. // Dimension of x ------------------------------------+ |
  111. // Dimension of y ---------------------------------------+
  112. //
  113. // WARNING #1: Since the functor will get instantiated with different types for
  114. // T, you must convert from other numeric types to T before mixing
  115. // computations with other variables of type T. In the example above, this is
  116. // seen where instead of using k_ directly, k_ is wrapped with T(k_).
  117. //
  118. // WARNING #2: A common beginner's error when first using autodiff cost
  119. // functions is to get the sizing wrong. In particular, there is a tendency to
  120. // set the template parameters to (dimension of residual, number of parameters)
  121. // instead of passing a dimension parameter for *every parameter*. In the
  122. // example above, that would be <MyScalarCostFunctor, 1, 2>, which is missing
  123. // the last '2' argument. Please be careful when setting the size parameters.
  124. #ifndef CERES_PUBLIC_AUTODIFF_COST_FUNCTION_H_
  125. #define CERES_PUBLIC_AUTODIFF_COST_FUNCTION_H_
  126. #include <memory>
  127. #include "ceres/internal/autodiff.h"
  128. #include "ceres/sized_cost_function.h"
  129. #include "ceres/types.h"
  130. #include "glog/logging.h"
  131. namespace ceres {
  132. // A cost function which computes the derivative of the cost with respect to
  133. // the parameters (a.k.a. the jacobian) using an auto differentiation framework.
  134. // The first template argument is the functor object, described in the header
  135. // comment. The second argument is the dimension of the residual (or
  136. // ceres::DYNAMIC to indicate it will be set at runtime), and subsequent
  137. // arguments describe the size of the Nth parameter, one per parameter.
  138. //
  139. // The constructors take ownership of the cost functor.
  140. //
  141. // If the number of residuals (argument kNumResiduals below) is
  142. // ceres::DYNAMIC, then the two-argument constructor must be used. The
  143. // second constructor takes a number of residuals (in addition to the
  144. // templated number of residuals). This allows for varying the number
  145. // of residuals for a single autodiff cost function at runtime.
  146. template <typename CostFunctor,
  147. int kNumResiduals, // Number of residuals, or ceres::DYNAMIC.
  148. int... Ns> // Number of parameters in each parameter block.
  149. class AutoDiffCostFunction final
  150. : public SizedCostFunction<kNumResiduals, Ns...> {
  151. public:
  152. // Takes ownership of functor by default. Uses the template-provided
  153. // value for the number of residuals ("kNumResiduals").
  154. explicit AutoDiffCostFunction(CostFunctor* functor,
  155. Ownership ownership = TAKE_OWNERSHIP)
  156. : functor_(functor), ownership_(ownership) {
  157. static_assert(kNumResiduals != DYNAMIC,
  158. "Can't run the fixed-size constructor if the number of "
  159. "residuals is set to ceres::DYNAMIC.");
  160. }
  161. // Takes ownership of functor by default. Ignores the template-provided
  162. // kNumResiduals in favor of the "num_residuals" argument provided.
  163. //
  164. // This allows for having autodiff cost functions which return varying
  165. // numbers of residuals at runtime.
  166. AutoDiffCostFunction(CostFunctor* functor,
  167. int num_residuals,
  168. Ownership ownership = TAKE_OWNERSHIP)
  169. : functor_(functor), ownership_(ownership) {
  170. static_assert(kNumResiduals == DYNAMIC,
  171. "Can't run the dynamic-size constructor if the number of "
  172. "residuals is not ceres::DYNAMIC.");
  173. SizedCostFunction<kNumResiduals, Ns...>::set_num_residuals(num_residuals);
  174. }
  175. AutoDiffCostFunction(AutoDiffCostFunction&& other)
  176. : functor_(std::move(other.functor_)), ownership_(other.ownership_) {}
  177. virtual ~AutoDiffCostFunction() {
  178. // Manually release pointer if configured to not take ownership rather than
  179. // deleting only if ownership is taken.
  180. // This is to stay maximally compatible to old user code which may have
  181. // forgotten to implement a virtual destructor, from when the
  182. // AutoDiffCostFunction always took ownership.
  183. if (ownership_ == DO_NOT_TAKE_OWNERSHIP) {
  184. functor_.release();
  185. }
  186. }
  187. // Implementation details follow; clients of the autodiff cost function should
  188. // not have to examine below here.
  189. //
  190. // To handle variadic cost functions, some template magic is needed. It's
  191. // mostly hidden inside autodiff.h.
  192. bool Evaluate(double const* const* parameters,
  193. double* residuals,
  194. double** jacobians) const override {
  195. using ParameterDims =
  196. typename SizedCostFunction<kNumResiduals, Ns...>::ParameterDims;
  197. if (!jacobians) {
  198. return internal::VariadicEvaluate<ParameterDims>(
  199. *functor_, parameters, residuals);
  200. }
  201. return internal::AutoDifferentiate<kNumResiduals, ParameterDims>(
  202. *functor_,
  203. parameters,
  204. SizedCostFunction<kNumResiduals, Ns...>::num_residuals(),
  205. residuals,
  206. jacobians);
  207. };
  208. const CostFunctor& functor() const { return *functor_; }
  209. private:
  210. std::unique_ptr<CostFunctor> functor_;
  211. Ownership ownership_;
  212. };
  213. } // namespace ceres
  214. #endif // CERES_PUBLIC_AUTODIFF_COST_FUNCTION_H_