trust_region_step_evaluator.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include "ceres/trust_region_step_evaluator.h"
  31. #include <algorithm>
  32. #include <limits>
  33. #include "glog/logging.h"
  34. namespace ceres::internal {
  35. TrustRegionStepEvaluator::TrustRegionStepEvaluator(
  36. const double initial_cost, const int max_consecutive_nonmonotonic_steps)
  37. : max_consecutive_nonmonotonic_steps_(max_consecutive_nonmonotonic_steps),
  38. minimum_cost_(initial_cost),
  39. current_cost_(initial_cost),
  40. reference_cost_(initial_cost),
  41. candidate_cost_(initial_cost),
  42. accumulated_reference_model_cost_change_(0.0),
  43. accumulated_candidate_model_cost_change_(0.0),
  44. num_consecutive_nonmonotonic_steps_(0) {}
  45. double TrustRegionStepEvaluator::StepQuality(
  46. const double cost, const double model_cost_change) const {
  47. // If the function evaluation for this step was a failure, in which
  48. // case the TrustRegionMinimizer would have set the cost to
  49. // std::numeric_limits<double>::max(). In this case, the division by
  50. // model_cost_change can result in an overflow. To prevent that from
  51. // happening, we will deal with this case explicitly.
  52. if (cost >= std::numeric_limits<double>::max()) {
  53. return std::numeric_limits<double>::lowest();
  54. }
  55. const double relative_decrease = (current_cost_ - cost) / model_cost_change;
  56. const double historical_relative_decrease =
  57. (reference_cost_ - cost) /
  58. (accumulated_reference_model_cost_change_ + model_cost_change);
  59. return std::max(relative_decrease, historical_relative_decrease);
  60. }
  61. void TrustRegionStepEvaluator::StepAccepted(const double cost,
  62. const double model_cost_change) {
  63. // Algorithm 10.1.2 from Trust Region Methods by Conn, Gould &
  64. // Toint.
  65. //
  66. // Step 3a
  67. current_cost_ = cost;
  68. accumulated_candidate_model_cost_change_ += model_cost_change;
  69. accumulated_reference_model_cost_change_ += model_cost_change;
  70. // Step 3b.
  71. if (current_cost_ < minimum_cost_) {
  72. minimum_cost_ = current_cost_;
  73. num_consecutive_nonmonotonic_steps_ = 0;
  74. candidate_cost_ = current_cost_;
  75. accumulated_candidate_model_cost_change_ = 0.0;
  76. } else {
  77. // Step 3c.
  78. ++num_consecutive_nonmonotonic_steps_;
  79. if (current_cost_ > candidate_cost_) {
  80. candidate_cost_ = current_cost_;
  81. accumulated_candidate_model_cost_change_ = 0.0;
  82. }
  83. }
  84. // Step 3d.
  85. //
  86. // At this point we have made too many non-monotonic steps and
  87. // we are going to reset the value of the reference iterate so
  88. // as to force the algorithm to descend.
  89. //
  90. // Note: In the original algorithm by Toint, this step was only
  91. // executed if the step was non-monotonic, but that would not handle
  92. // the case of max_consecutive_nonmonotonic_steps = 0. The small
  93. // modification of doing this always handles that corner case
  94. // correctly.
  95. if (num_consecutive_nonmonotonic_steps_ ==
  96. max_consecutive_nonmonotonic_steps_) {
  97. reference_cost_ = candidate_cost_;
  98. accumulated_reference_model_cost_change_ =
  99. accumulated_candidate_model_cost_change_;
  100. }
  101. }
  102. } // namespace ceres::internal