evaluator.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 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.com (Keir Mierle)
  31. #ifndef CERES_INTERNAL_EVALUATOR_H_
  32. #define CERES_INTERNAL_EVALUATOR_H_
  33. #include <map>
  34. #include <memory>
  35. #include <string>
  36. #include <vector>
  37. #include "ceres/context_impl.h"
  38. #include "ceres/execution_summary.h"
  39. #include "ceres/internal/disable_warnings.h"
  40. #include "ceres/internal/export.h"
  41. #include "ceres/types.h"
  42. namespace ceres {
  43. struct CRSMatrix;
  44. class EvaluationCallback;
  45. namespace internal {
  46. class Program;
  47. class SparseMatrix;
  48. // The Evaluator interface offers a way to interact with a least squares cost
  49. // function that is useful for an optimizer that wants to minimize the least
  50. // squares objective. This insulates the optimizer from issues like Jacobian
  51. // storage, manifolds, etc.
  52. class CERES_NO_EXPORT Evaluator {
  53. public:
  54. virtual ~Evaluator();
  55. struct Options {
  56. int num_threads = 1;
  57. int num_eliminate_blocks = -1;
  58. LinearSolverType linear_solver_type = DENSE_QR;
  59. SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type =
  60. NO_SPARSE;
  61. bool dynamic_sparsity = false;
  62. ContextImpl* context = nullptr;
  63. EvaluationCallback* evaluation_callback = nullptr;
  64. };
  65. static std::unique_ptr<Evaluator> Create(const Options& options,
  66. Program* program,
  67. std::string* error);
  68. // Build and return a sparse matrix for storing and working with the Jacobian
  69. // of the objective function. The jacobian has dimensions
  70. // NumEffectiveParameters() by NumParameters(), and is typically extremely
  71. // sparse. Since the sparsity pattern of the Jacobian remains constant over
  72. // the lifetime of the optimization problem, this method is used to
  73. // instantiate a SparseMatrix object with the appropriate sparsity structure
  74. // (which can be an expensive operation) and then reused by the optimization
  75. // algorithm and the various linear solvers.
  76. //
  77. // It is expected that the classes implementing this interface will be aware
  78. // of their client's requirements for the kind of sparse matrix storage and
  79. // layout that is needed for an efficient implementation. For example
  80. // CompressedRowOptimizationProblem creates a compressed row representation of
  81. // the jacobian for use with CHOLMOD, where as BlockOptimizationProblem
  82. // creates a BlockSparseMatrix representation of the jacobian for use in the
  83. // Schur complement based methods.
  84. virtual std::unique_ptr<SparseMatrix> CreateJacobian() const = 0;
  85. // Options struct to control Evaluator::Evaluate;
  86. struct EvaluateOptions {
  87. // If false, the loss function correction is not applied to the
  88. // residual blocks.
  89. bool apply_loss_function = true;
  90. // If false, this evaluation point is the same as the last one.
  91. bool new_evaluation_point = true;
  92. };
  93. // Evaluate the cost function for the given state. Returns the cost,
  94. // residuals, and jacobian in the corresponding arguments. Both residuals and
  95. // jacobian are optional; to avoid computing them, pass nullptr.
  96. //
  97. // If non-nullptr, the Jacobian must have a suitable sparsity pattern; only
  98. // the values array of the jacobian is modified.
  99. //
  100. // state is an array of size NumParameters(), cost is a pointer to a single
  101. // double, and residuals is an array of doubles of size NumResiduals().
  102. virtual bool Evaluate(const EvaluateOptions& evaluate_options,
  103. const double* state,
  104. double* cost,
  105. double* residuals,
  106. double* gradient,
  107. SparseMatrix* jacobian) = 0;
  108. // Variant of Evaluator::Evaluate where the user wishes to use the
  109. // default EvaluateOptions struct. This is mostly here as a
  110. // convenience method.
  111. bool Evaluate(const double* state,
  112. double* cost,
  113. double* residuals,
  114. double* gradient,
  115. SparseMatrix* jacobian) {
  116. return Evaluate(
  117. EvaluateOptions(), state, cost, residuals, gradient, jacobian);
  118. }
  119. // Make a change delta (of size NumEffectiveParameters()) to state (of size
  120. // NumParameters()) and store the result in state_plus_delta.
  121. //
  122. // In the case that there are no manifolds used, this is equivalent to
  123. //
  124. // state_plus_delta[i] = state[i] + delta[i] ;
  125. //
  126. // however, the mapping is more complicated in the case of manifolds
  127. // like quaternions. This is the same as the "Plus()" operation in
  128. // manifold.h, but operating over the entire state vector for a
  129. // problem.
  130. virtual bool Plus(const double* state,
  131. const double* delta,
  132. double* state_plus_delta) const = 0;
  133. // The number of parameters in the optimization problem.
  134. virtual int NumParameters() const = 0;
  135. // This is the effective number of parameters that the optimizer may adjust.
  136. // This applies when there are manifolds on some of the parameters.
  137. virtual int NumEffectiveParameters() const = 0;
  138. // The number of residuals in the optimization problem.
  139. virtual int NumResiduals() const = 0;
  140. // The following two methods return copies instead of references so
  141. // that the base class implementation does not have to worry about
  142. // life time issues. Further, these calls are not expected to be
  143. // frequent or performance sensitive.
  144. virtual std::map<std::string, CallStatistics> Statistics() const {
  145. return {};
  146. }
  147. };
  148. } // namespace internal
  149. } // namespace ceres
  150. #include "ceres/internal/reenable_warnings.h"
  151. #endif // CERES_INTERNAL_EVALUATOR_H_