trust_region_strategy.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_STRATEGY_H_
  31. #define CERES_INTERNAL_TRUST_REGION_STRATEGY_H_
  32. #include <memory>
  33. #include <string>
  34. #include "ceres/internal/disable_warnings.h"
  35. #include "ceres/internal/export.h"
  36. #include "ceres/linear_solver.h"
  37. namespace ceres::internal {
  38. class LinearSolver;
  39. class SparseMatrix;
  40. // Interface for classes implementing various trust region strategies
  41. // for nonlinear least squares problems.
  42. //
  43. // The object is expected to maintain and update a trust region
  44. // radius, which it then uses to solve for the trust region step using
  45. // the jacobian matrix and residual vector.
  46. //
  47. // Here the term trust region radius is used loosely, as the strategy
  48. // is free to treat it as guidance and violate it as need be. e.g.,
  49. // the LevenbergMarquardtStrategy uses the inverse of the trust region
  50. // radius to scale the damping term, which controls the step size, but
  51. // does not set a hard limit on its size.
  52. class CERES_NO_EXPORT TrustRegionStrategy {
  53. public:
  54. struct Options {
  55. TrustRegionStrategyType trust_region_strategy_type = LEVENBERG_MARQUARDT;
  56. // Linear solver used for actually solving the trust region step.
  57. LinearSolver* linear_solver = nullptr;
  58. double initial_radius = 1e4;
  59. double max_radius = 1e32;
  60. // Minimum and maximum values of the diagonal damping matrix used
  61. // by LevenbergMarquardtStrategy. The DoglegStrategy also uses
  62. // these bounds to construct a regularizing diagonal to ensure
  63. // that the Gauss-Newton step computation is of full rank.
  64. double min_lm_diagonal = 1e-6;
  65. double max_lm_diagonal = 1e32;
  66. // Further specify which dogleg method to use
  67. DoglegType dogleg_type = TRADITIONAL_DOGLEG;
  68. ContextImpl* context = nullptr;
  69. int num_threads = 1;
  70. };
  71. // Factory.
  72. static std::unique_ptr<TrustRegionStrategy> Create(const Options& options);
  73. virtual ~TrustRegionStrategy();
  74. // Per solve options.
  75. struct PerSolveOptions {
  76. // Forcing sequence for inexact solves.
  77. double eta = 1e-1;
  78. DumpFormatType dump_format_type = TEXTFILE;
  79. // If non-empty and dump_format_type is not CONSOLE, the trust
  80. // regions strategy will write the linear system to file(s) with
  81. // name starting with dump_filename_base. If dump_format_type is
  82. // CONSOLE then dump_filename_base will be ignored and the linear
  83. // system will be written to the standard error.
  84. std::string dump_filename_base;
  85. };
  86. struct Summary {
  87. // If the trust region problem is,
  88. //
  89. // 1/2 x'Ax + b'x + c,
  90. //
  91. // then
  92. //
  93. // residual_norm = |Ax -b|
  94. double residual_norm = -1;
  95. // Number of iterations used by the linear solver. If a linear
  96. // solver was not called (e.g., DogLegStrategy after an
  97. // unsuccessful step), then this would be zero.
  98. int num_iterations = -1;
  99. // Status of the linear solver used to solve the Newton system.
  100. LinearSolverTerminationType termination_type =
  101. LinearSolverTerminationType::FAILURE;
  102. };
  103. // Use the current radius to solve for the trust region step.
  104. virtual Summary ComputeStep(const PerSolveOptions& per_solve_options,
  105. SparseMatrix* jacobian,
  106. const double* residuals,
  107. double* step) = 0;
  108. // Inform the strategy that the current step has been accepted, and
  109. // that the ratio of the decrease in the non-linear objective to the
  110. // decrease in the trust region model is step_quality.
  111. virtual void StepAccepted(double step_quality) = 0;
  112. // Inform the strategy that the current step has been rejected, and
  113. // that the ratio of the decrease in the non-linear objective to the
  114. // decrease in the trust region model is step_quality.
  115. virtual void StepRejected(double step_quality) = 0;
  116. // Inform the strategy that the current step has been rejected
  117. // because it was found to be numerically invalid.
  118. // StepRejected/StepAccepted will not be called for this step, and
  119. // the strategy is free to do what it wants with this information.
  120. virtual void StepIsInvalid() = 0;
  121. // Current trust region radius.
  122. virtual double Radius() const = 0;
  123. };
  124. } // namespace ceres::internal
  125. #include "ceres/internal/reenable_warnings.h"
  126. #endif // CERES_INTERNAL_TRUST_REGION_STRATEGY_H_