minimizer.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_MINIMIZER_H_
  31. #define CERES_INTERNAL_MINIMIZER_H_
  32. #include <memory>
  33. #include <string>
  34. #include <vector>
  35. #include "ceres/internal/disable_warnings.h"
  36. #include "ceres/internal/export.h"
  37. #include "ceres/iteration_callback.h"
  38. #include "ceres/solver.h"
  39. namespace ceres::internal {
  40. class Evaluator;
  41. class SparseMatrix;
  42. class TrustRegionStrategy;
  43. class CoordinateDescentMinimizer;
  44. class LinearSolver;
  45. class ContextImpl;
  46. // Interface for non-linear least squares solvers.
  47. class CERES_NO_EXPORT Minimizer {
  48. public:
  49. // Options struct to control the behaviour of the Minimizer. Please
  50. // see solver.h for detailed information about the meaning and
  51. // default values of each of these parameters.
  52. struct Options {
  53. Options() { Init(Solver::Options()); }
  54. explicit Options(const Solver::Options& options) { Init(options); }
  55. void Init(const Solver::Options& options) {
  56. num_threads = options.num_threads;
  57. max_num_iterations = options.max_num_iterations;
  58. max_solver_time_in_seconds = options.max_solver_time_in_seconds;
  59. max_step_solver_retries = 5;
  60. gradient_tolerance = options.gradient_tolerance;
  61. parameter_tolerance = options.parameter_tolerance;
  62. function_tolerance = options.function_tolerance;
  63. min_relative_decrease = options.min_relative_decrease;
  64. eta = options.eta;
  65. jacobi_scaling = options.jacobi_scaling;
  66. use_nonmonotonic_steps = options.use_nonmonotonic_steps;
  67. max_consecutive_nonmonotonic_steps =
  68. options.max_consecutive_nonmonotonic_steps;
  69. trust_region_problem_dump_directory =
  70. options.trust_region_problem_dump_directory;
  71. trust_region_minimizer_iterations_to_dump =
  72. options.trust_region_minimizer_iterations_to_dump;
  73. trust_region_problem_dump_format_type =
  74. options.trust_region_problem_dump_format_type;
  75. max_num_consecutive_invalid_steps =
  76. options.max_num_consecutive_invalid_steps;
  77. min_trust_region_radius = options.min_trust_region_radius;
  78. line_search_direction_type = options.line_search_direction_type;
  79. line_search_type = options.line_search_type;
  80. nonlinear_conjugate_gradient_type =
  81. options.nonlinear_conjugate_gradient_type;
  82. max_lbfgs_rank = options.max_lbfgs_rank;
  83. use_approximate_eigenvalue_bfgs_scaling =
  84. options.use_approximate_eigenvalue_bfgs_scaling;
  85. line_search_interpolation_type = options.line_search_interpolation_type;
  86. min_line_search_step_size = options.min_line_search_step_size;
  87. line_search_sufficient_function_decrease =
  88. options.line_search_sufficient_function_decrease;
  89. max_line_search_step_contraction =
  90. options.max_line_search_step_contraction;
  91. min_line_search_step_contraction =
  92. options.min_line_search_step_contraction;
  93. max_num_line_search_step_size_iterations =
  94. options.max_num_line_search_step_size_iterations;
  95. max_num_line_search_direction_restarts =
  96. options.max_num_line_search_direction_restarts;
  97. line_search_sufficient_curvature_decrease =
  98. options.line_search_sufficient_curvature_decrease;
  99. max_line_search_step_expansion = options.max_line_search_step_expansion;
  100. inner_iteration_tolerance = options.inner_iteration_tolerance;
  101. is_silent = (options.logging_type == SILENT);
  102. is_constrained = false;
  103. callbacks = options.callbacks;
  104. }
  105. int max_num_iterations;
  106. double max_solver_time_in_seconds;
  107. int num_threads;
  108. ContextImpl* context = nullptr;
  109. // Number of times the linear solver should be retried in case of
  110. // numerical failure. The retries are done by exponentially scaling up
  111. // mu at each retry. This leads to stronger and stronger
  112. // regularization making the linear least squares problem better
  113. // conditioned at each retry.
  114. int max_step_solver_retries;
  115. double gradient_tolerance;
  116. double parameter_tolerance;
  117. double function_tolerance;
  118. double min_relative_decrease;
  119. double eta;
  120. bool jacobi_scaling;
  121. bool use_nonmonotonic_steps;
  122. int max_consecutive_nonmonotonic_steps;
  123. std::vector<int> trust_region_minimizer_iterations_to_dump;
  124. DumpFormatType trust_region_problem_dump_format_type;
  125. std::string trust_region_problem_dump_directory;
  126. int max_num_consecutive_invalid_steps;
  127. double min_trust_region_radius;
  128. LineSearchDirectionType line_search_direction_type;
  129. LineSearchType line_search_type;
  130. NonlinearConjugateGradientType nonlinear_conjugate_gradient_type;
  131. int max_lbfgs_rank;
  132. bool use_approximate_eigenvalue_bfgs_scaling;
  133. LineSearchInterpolationType line_search_interpolation_type;
  134. double min_line_search_step_size;
  135. double line_search_sufficient_function_decrease;
  136. double max_line_search_step_contraction;
  137. double min_line_search_step_contraction;
  138. int max_num_line_search_step_size_iterations;
  139. int max_num_line_search_direction_restarts;
  140. double line_search_sufficient_curvature_decrease;
  141. double max_line_search_step_expansion;
  142. double inner_iteration_tolerance;
  143. // If true, then all logging is disabled.
  144. bool is_silent;
  145. // Use a bounds constrained optimization algorithm.
  146. bool is_constrained;
  147. // List of callbacks that are executed by the Minimizer at the end
  148. // of each iteration.
  149. //
  150. // The Options struct does not own these pointers.
  151. std::vector<IterationCallback*> callbacks;
  152. // Object responsible for evaluating the cost, residuals and
  153. // Jacobian matrix.
  154. std::shared_ptr<Evaluator> evaluator;
  155. // Object responsible for actually computing the trust region
  156. // step, and sizing the trust region radius.
  157. std::shared_ptr<TrustRegionStrategy> trust_region_strategy;
  158. // Object holding the Jacobian matrix. It is assumed that the
  159. // sparsity structure of the matrix has already been initialized
  160. // and will remain constant for the life time of the
  161. // optimization.
  162. std::shared_ptr<SparseMatrix> jacobian;
  163. std::shared_ptr<CoordinateDescentMinimizer> inner_iteration_minimizer;
  164. };
  165. static std::unique_ptr<Minimizer> Create(MinimizerType minimizer_type);
  166. static bool RunCallbacks(const Options& options,
  167. const IterationSummary& iteration_summary,
  168. Solver::Summary* summary);
  169. virtual ~Minimizer();
  170. // Note: The minimizer is expected to update the state of the
  171. // parameters array every iteration. This is required for the
  172. // StateUpdatingCallback to work.
  173. virtual void Minimize(const Options& options,
  174. double* parameters,
  175. Solver::Summary* summary) = 0;
  176. };
  177. } // namespace ceres::internal
  178. #include "ceres/internal/reenable_warnings.h"
  179. #endif // CERES_INTERNAL_MINIMIZER_H_