trust_region_minimizer.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef CERES_INTERNAL_TRUST_REGION_MINIMIZER_H_
  31. #define CERES_INTERNAL_TRUST_REGION_MINIMIZER_H_
  32. #include <memory>
  33. #include "ceres/internal/disable_warnings.h"
  34. #include "ceres/internal/eigen.h"
  35. #include "ceres/internal/export.h"
  36. #include "ceres/minimizer.h"
  37. #include "ceres/solver.h"
  38. #include "ceres/sparse_matrix.h"
  39. #include "ceres/trust_region_step_evaluator.h"
  40. #include "ceres/trust_region_strategy.h"
  41. #include "ceres/types.h"
  42. namespace ceres::internal {
  43. // Generic trust region minimization algorithm.
  44. //
  45. // For example usage, see SolverImpl::Minimize.
  46. class CERES_NO_EXPORT TrustRegionMinimizer final : public Minimizer {
  47. public:
  48. // This method is not thread safe.
  49. void Minimize(const Minimizer::Options& options,
  50. double* parameters,
  51. Solver::Summary* solver_summary) override;
  52. private:
  53. void Init(const Minimizer::Options& options,
  54. double* parameters,
  55. Solver::Summary* solver_summary);
  56. bool IterationZero();
  57. bool FinalizeIterationAndCheckIfMinimizerCanContinue();
  58. bool ComputeTrustRegionStep();
  59. bool EvaluateGradientAndJacobian(bool new_evaluation_point);
  60. void ComputeCandidatePointAndEvaluateCost();
  61. void DoLineSearch(const Vector& x,
  62. const Vector& gradient,
  63. const double cost,
  64. Vector* delta);
  65. void DoInnerIterationsIfNeeded();
  66. bool ParameterToleranceReached();
  67. bool FunctionToleranceReached();
  68. bool GradientToleranceReached();
  69. bool MaxSolverTimeReached();
  70. bool MaxSolverIterationsReached();
  71. bool MinTrustRegionRadiusReached();
  72. bool IsStepSuccessful();
  73. bool HandleSuccessfulStep();
  74. bool HandleInvalidStep();
  75. Minimizer::Options options_;
  76. // These pointers are shortcuts to objects passed to the
  77. // TrustRegionMinimizer. The TrustRegionMinimizer does not own them.
  78. double* parameters_;
  79. Solver::Summary* solver_summary_;
  80. Evaluator* evaluator_;
  81. SparseMatrix* jacobian_;
  82. TrustRegionStrategy* strategy_;
  83. std::unique_ptr<TrustRegionStepEvaluator> step_evaluator_;
  84. bool is_not_silent_;
  85. bool inner_iterations_are_enabled_;
  86. bool inner_iterations_were_useful_;
  87. // Summary of the current iteration.
  88. IterationSummary iteration_summary_;
  89. // Dimensionality of the problem in the ambient space.
  90. int num_parameters_;
  91. // Dimensionality of the problem in the tangent space. This is the
  92. // number of columns in the Jacobian.
  93. int num_effective_parameters_;
  94. // Length of the residual vector, also the number of rows in the Jacobian.
  95. int num_residuals_;
  96. // Current point.
  97. Vector x_;
  98. // Residuals at x_;
  99. Vector residuals_;
  100. // Gradient at x_.
  101. Vector gradient_;
  102. // Solution computed by the inner iterations.
  103. Vector inner_iteration_x_;
  104. // model_residuals = J * trust_region_step
  105. Vector model_residuals_;
  106. Vector negative_gradient_;
  107. // projected_gradient_step = Plus(x, -gradient), an intermediate
  108. // quantity used to compute the projected gradient norm.
  109. Vector projected_gradient_step_;
  110. // The step computed by the trust region strategy. If Jacobi scaling
  111. // is enabled, this is a vector in the scaled space.
  112. Vector trust_region_step_;
  113. // The current proposal for how far the trust region algorithm
  114. // thinks we should move. In the most basic case, it is just the
  115. // trust_region_step_ with the Jacobi scaling undone. If bounds
  116. // constraints are present, then it is the result of the projected
  117. // line search.
  118. Vector delta_;
  119. // candidate_x = Plus(x, delta)
  120. Vector candidate_x_;
  121. // Scaling vector to scale the columns of the Jacobian.
  122. Vector jacobian_scaling_;
  123. // Cost at x_.
  124. double x_cost_;
  125. // Minimum cost encountered up till now.
  126. double minimum_cost_;
  127. // How much did the trust region strategy reduce the cost of the
  128. // linearized Gauss-Newton model.
  129. double model_cost_change_;
  130. // Cost at candidate_x_.
  131. double candidate_cost_;
  132. // Time at which the minimizer was started.
  133. double start_time_in_secs_;
  134. // Time at which the current iteration was started.
  135. double iteration_start_time_in_secs_;
  136. // Number of consecutive steps where the minimizer loop computed a
  137. // numerically invalid step.
  138. int num_consecutive_invalid_steps_;
  139. };
  140. } // namespace ceres::internal
  141. #include "ceres/internal/reenable_warnings.h"
  142. #endif // CERES_INTERNAL_TRUST_REGION_MINIMIZER_H_