cost_function.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // keir@google.m (Keir Mierle)
  31. //
  32. // This is the interface through which the least squares solver accesses the
  33. // residual and Jacobian of the least squares problem. Users are expected to
  34. // subclass CostFunction to define their own terms in the least squares problem.
  35. //
  36. // It is recommended that users define templated residual functors for use as
  37. // arguments for AutoDiffCostFunction (see autodiff_cost_function.h), instead of
  38. // directly implementing the CostFunction interface. This often results in both
  39. // shorter code and faster execution than hand-coded derivatives. However,
  40. // specialized cases may demand direct implementation of the lower-level
  41. // CostFunction interface; for example, this is true when calling legacy code
  42. // which is not templated on numeric types.
  43. #ifndef CERES_PUBLIC_COST_FUNCTION_H_
  44. #define CERES_PUBLIC_COST_FUNCTION_H_
  45. #include <cstdint>
  46. #include <vector>
  47. #include "ceres/internal/disable_warnings.h"
  48. #include "ceres/internal/export.h"
  49. namespace ceres {
  50. // This class implements the computation of the cost (a.k.a. residual) terms as
  51. // a function of the input (control) variables, and is the interface for users
  52. // to describe their least squares problem to Ceres. In other words, this is the
  53. // modeling layer between users and the Ceres optimizer. The signature of the
  54. // function (number and sizes of input parameter blocks and number of outputs)
  55. // is stored in parameter_block_sizes_ and num_residuals_ respectively. User
  56. // code inheriting from this class is expected to set these two members with the
  57. // corresponding accessors. This information will be verified by the Problem
  58. // when added with AddResidualBlock().
  59. class CERES_EXPORT CostFunction {
  60. public:
  61. CostFunction();
  62. CostFunction(const CostFunction&) = delete;
  63. void operator=(const CostFunction&) = delete;
  64. virtual ~CostFunction();
  65. // Inputs:
  66. //
  67. // parameters is an array of pointers to arrays containing the
  68. // various parameter blocks. parameters has the same number of
  69. // elements as parameter_block_sizes_. Parameter blocks are in the
  70. // same order as parameter_block_sizes_.i.e.,
  71. //
  72. // parameters_[i] = double[parameter_block_sizes_[i]]
  73. //
  74. // Outputs:
  75. //
  76. // residuals is an array of size num_residuals_.
  77. //
  78. // jacobians is an array of size parameter_block_sizes_ containing
  79. // pointers to storage for jacobian blocks corresponding to each
  80. // parameter block. Jacobian blocks are in the same order as
  81. // parameter_block_sizes, i.e. jacobians[i], is an
  82. // array that contains num_residuals_* parameter_block_sizes_[i]
  83. // elements. Each jacobian block is stored in row-major order, i.e.,
  84. //
  85. // jacobians[i][r*parameter_block_size_[i] + c] =
  86. // d residual[r] / d parameters[i][c]
  87. //
  88. // If jacobians is nullptr, then no derivatives are returned; this is
  89. // the case when computing cost only. If jacobians[i] is nullptr, then
  90. // the jacobian block corresponding to the i'th parameter block must
  91. // not to be returned.
  92. //
  93. // The return value indicates whether the computation of the
  94. // residuals and/or jacobians was successful or not.
  95. //
  96. // This can be used to communicate numerical failures in jacobian
  97. // computations for instance.
  98. //
  99. // A more interesting and common use is to impose constraints on the
  100. // parameters. If the initial values of the parameter blocks satisfy
  101. // the constraints, then returning false whenever the constraints
  102. // are not satisfied will prevent the solver from moving into the
  103. // infeasible region. This is not a very sophisticated mechanism for
  104. // enforcing constraints, but is often good enough.
  105. //
  106. // Note that it is important that the initial values of the
  107. // parameter block must be feasible, otherwise the solver will
  108. // declare a numerical problem at iteration 0.
  109. virtual bool Evaluate(double const* const* parameters,
  110. double* residuals,
  111. double** jacobians) const = 0;
  112. const std::vector<int32_t>& parameter_block_sizes() const {
  113. return parameter_block_sizes_;
  114. }
  115. int num_residuals() const { return num_residuals_; }
  116. protected:
  117. std::vector<int32_t>* mutable_parameter_block_sizes() {
  118. return &parameter_block_sizes_;
  119. }
  120. void set_num_residuals(int num_residuals) { num_residuals_ = num_residuals; }
  121. private:
  122. // Cost function signature metadata: number of inputs & their sizes,
  123. // number of outputs (residuals).
  124. std::vector<int32_t> parameter_block_sizes_;
  125. int num_residuals_;
  126. };
  127. } // namespace ceres
  128. #include "ceres/internal/reenable_warnings.h"
  129. #endif // CERES_PUBLIC_COST_FUNCTION_H_