trust_region_step_evaluator.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_STEP_EVALUATOR_H_
  31. #define CERES_INTERNAL_TRUST_REGION_STEP_EVALUATOR_H_
  32. #include "ceres/internal/export.h"
  33. namespace ceres::internal {
  34. // The job of the TrustRegionStepEvaluator is to evaluate the quality
  35. // of a step, i.e., how the cost of a step compares with the reduction
  36. // in the objective of the trust region problem.
  37. //
  38. // Classic trust region methods are descent methods, in that they only
  39. // accept a point if it strictly reduces the value of the objective
  40. // function. They do this by measuring the quality of a step as
  41. //
  42. // cost_change / model_cost_change.
  43. //
  44. // Relaxing the monotonic descent requirement allows the algorithm to
  45. // be more efficient in the long term at the cost of some local
  46. // increase in the value of the objective function.
  47. //
  48. // This is because allowing for non-decreasing objective function
  49. // values in a principled manner allows the algorithm to "jump over
  50. // boulders" as the method is not restricted to move into narrow
  51. // valleys while preserving its convergence properties.
  52. //
  53. // The parameter max_consecutive_nonmonotonic_steps controls the
  54. // window size used by the step selection algorithm to accept
  55. // non-monotonic steps. Setting this parameter to zero, recovers the
  56. // classic monotonic descent algorithm.
  57. //
  58. // Based on algorithm 10.1.2 (page 357) of "Trust Region
  59. // Methods" by Conn Gould & Toint, or equations 33-40 of
  60. // "Non-monotone trust-region algorithms for nonlinear
  61. // optimization subject to convex constraints" by Phil Toint,
  62. // Mathematical Programming, 77, 1997.
  63. //
  64. // Example usage:
  65. //
  66. // TrustRegionStepEvaluator* step_evaluator = ...
  67. //
  68. // cost = ... // Compute the non-linear objective function value.
  69. // model_cost_change = ... // Change in the value of the trust region objective.
  70. // if (step_evaluator->StepQuality(cost, model_cost_change) > threshold) {
  71. // x = x + delta;
  72. // step_evaluator->StepAccepted(cost, model_cost_change);
  73. // }
  74. class CERES_NO_EXPORT TrustRegionStepEvaluator {
  75. public:
  76. // initial_cost is as the name implies the cost of the starting
  77. // state of the trust region minimizer.
  78. //
  79. // max_consecutive_nonmonotonic_steps controls the window size used
  80. // by the step selection algorithm to accept non-monotonic
  81. // steps. Setting this parameter to zero, recovers the classic
  82. // monotonic descent algorithm.
  83. TrustRegionStepEvaluator(double initial_cost,
  84. int max_consecutive_nonmonotonic_steps);
  85. // Return the quality of the step given its cost and the decrease in
  86. // the cost of the model. model_cost_change has to be positive.
  87. double StepQuality(double cost, double model_cost_change) const;
  88. // Inform the step evaluator that a step with the given cost and
  89. // model_cost_change has been accepted by the trust region
  90. // minimizer.
  91. void StepAccepted(double cost, double model_cost_change);
  92. private:
  93. const int max_consecutive_nonmonotonic_steps_;
  94. // The minimum cost encountered up till now.
  95. double minimum_cost_;
  96. // The current cost of the trust region minimizer as informed by the
  97. // last call to StepAccepted.
  98. double current_cost_;
  99. double reference_cost_;
  100. double candidate_cost_;
  101. // Accumulated model cost since the last time the reference model
  102. // cost was updated, i.e., when a step with cost less than the
  103. // current known minimum cost is accepted.
  104. double accumulated_reference_model_cost_change_;
  105. // Accumulated model cost since the last time the candidate model
  106. // cost was updated, i.e., a non-monotonic step was taken with a
  107. // cost that was greater than the current candidate cost.
  108. double accumulated_candidate_model_cost_change_;
  109. // Number of steps taken since the last time minimum_cost was updated.
  110. int num_consecutive_nonmonotonic_steps_;
  111. };
  112. } // namespace ceres::internal
  113. #endif // CERES_INTERNAL_TRUST_REGION_STEP_EVALUATOR_H_