iteration_callback.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. //
  31. // When an iteration callback is specified, Ceres calls the callback
  32. // after each minimizer step (if the minimizer has not converged) and
  33. // passes it an IterationSummary object, defined below.
  34. #ifndef CERES_PUBLIC_ITERATION_CALLBACK_H_
  35. #define CERES_PUBLIC_ITERATION_CALLBACK_H_
  36. #include "ceres/internal/disable_warnings.h"
  37. #include "ceres/internal/export.h"
  38. #include "ceres/types.h"
  39. namespace ceres {
  40. // This struct describes the state of the optimizer after each
  41. // iteration of the minimization.
  42. struct CERES_EXPORT IterationSummary {
  43. // Current iteration number.
  44. int iteration = 0;
  45. // Step was numerically valid, i.e., all values are finite and the
  46. // step reduces the value of the linearized model.
  47. //
  48. // Note: step_is_valid is always true when iteration = 0.
  49. bool step_is_valid = false;
  50. // Step did not reduce the value of the objective function
  51. // sufficiently, but it was accepted because of the relaxed
  52. // acceptance criterion used by the non-monotonic trust region
  53. // algorithm.
  54. //
  55. // Note: step_is_nonmonotonic is always false when iteration = 0;
  56. bool step_is_nonmonotonic = false;
  57. // Whether or not the minimizer accepted this step or not. If the
  58. // ordinary trust region algorithm is used, this means that the
  59. // relative reduction in the objective function value was greater
  60. // than Solver::Options::min_relative_decrease. However, if the
  61. // non-monotonic trust region algorithm is used
  62. // (Solver::Options:use_nonmonotonic_steps = true), then even if the
  63. // relative decrease is not sufficient, the algorithm may accept the
  64. // step and the step is declared successful.
  65. //
  66. // Note: step_is_successful is always true when iteration = 0.
  67. bool step_is_successful = false;
  68. // Value of the objective function.
  69. double cost = 0.0;
  70. // Change in the value of the objective function in this
  71. // iteration. This can be positive or negative.
  72. double cost_change = 0.0;
  73. // Infinity norm of the gradient vector.
  74. double gradient_max_norm = 0.0;
  75. // 2-norm of the gradient vector.
  76. double gradient_norm = 0.0;
  77. // 2-norm of the size of the step computed by the optimization
  78. // algorithm.
  79. double step_norm = 0.0;
  80. // For trust region algorithms, the ratio of the actual change in
  81. // cost and the change in the cost of the linearized approximation.
  82. double relative_decrease = 0.0;
  83. // Size of the trust region at the end of the current iteration. For
  84. // the Levenberg-Marquardt algorithm, the regularization parameter
  85. // mu = 1.0 / trust_region_radius.
  86. double trust_region_radius = 0.0;
  87. // For the inexact step Levenberg-Marquardt algorithm, this is the
  88. // relative accuracy with which the Newton(LM) step is solved. This
  89. // number affects only the iterative solvers capable of solving
  90. // linear systems inexactly. Factorization-based exact solvers
  91. // ignore it.
  92. double eta = 0.0;
  93. // Step sized computed by the line search algorithm.
  94. double step_size = 0.0;
  95. // Number of function value evaluations used by the line search algorithm.
  96. int line_search_function_evaluations = 0;
  97. // Number of function gradient evaluations used by the line search algorithm.
  98. int line_search_gradient_evaluations = 0;
  99. // Number of iterations taken by the line search algorithm.
  100. int line_search_iterations = 0;
  101. // Number of iterations taken by the linear solver to solve for the
  102. // Newton step.
  103. int linear_solver_iterations = 0;
  104. // All times reported below are wall times.
  105. // Time (in seconds) spent inside the minimizer loop in the current
  106. // iteration.
  107. double iteration_time_in_seconds = 0.0;
  108. // Time (in seconds) spent inside the trust region step solver.
  109. double step_solver_time_in_seconds = 0.0;
  110. // Time (in seconds) since the user called Solve().
  111. double cumulative_time_in_seconds = 0.0;
  112. };
  113. // Interface for specifying callbacks that are executed at the end of
  114. // each iteration of the Minimizer. The solver uses the return value
  115. // of operator() to decide whether to continue solving or to
  116. // terminate. The user can return three values.
  117. //
  118. // SOLVER_ABORT indicates that the callback detected an abnormal
  119. // situation. The solver returns without updating the parameter blocks
  120. // (unless Solver::Options::update_state_every_iteration is set
  121. // true). Solver returns with Solver::Summary::termination_type set to
  122. // USER_ABORT.
  123. //
  124. // SOLVER_TERMINATE_SUCCESSFULLY indicates that there is no need to
  125. // optimize anymore (some user specified termination criterion has
  126. // been met). Solver returns with Solver::Summary::termination_type
  127. // set to USER_SUCCESS.
  128. //
  129. // SOLVER_CONTINUE indicates that the solver should continue
  130. // optimizing.
  131. //
  132. // For example, the following Callback is used internally by Ceres to
  133. // log the progress of the optimization.
  134. //
  135. // Callback for logging the state of the minimizer to STDERR or STDOUT
  136. // depending on the user's preferences and logging level.
  137. //
  138. // class LoggingCallback : public IterationCallback {
  139. // public:
  140. // explicit LoggingCallback(bool log_to_stdout)
  141. // : log_to_stdout_(log_to_stdout) {}
  142. //
  143. // CallbackReturnType operator()(const IterationSummary& summary) {
  144. // const char* kReportRowFormat =
  145. // "% 4d: f:% 8e d:% 3.2e g:% 3.2e h:% 3.2e "
  146. // "rho:% 3.2e mu:% 3.2e eta:% 3.2e li:% 3d";
  147. // string output = StringPrintf(kReportRowFormat,
  148. // summary.iteration,
  149. // summary.cost,
  150. // summary.cost_change,
  151. // summary.gradient_max_norm,
  152. // summary.step_norm,
  153. // summary.relative_decrease,
  154. // summary.trust_region_radius,
  155. // summary.eta,
  156. // summary.linear_solver_iterations);
  157. // if (log_to_stdout_) {
  158. // cout << output << endl;
  159. // } else {
  160. // VLOG(1) << output;
  161. // }
  162. // return SOLVER_CONTINUE;
  163. // }
  164. //
  165. // private:
  166. // const bool log_to_stdout_;
  167. // };
  168. //
  169. class CERES_EXPORT IterationCallback {
  170. public:
  171. virtual ~IterationCallback();
  172. virtual CallbackReturnType operator()(const IterationSummary& summary) = 0;
  173. };
  174. } // namespace ceres
  175. #include "ceres/internal/reenable_warnings.h"
  176. #endif // CERES_PUBLIC_ITERATION_CALLBACK_H_