Sameer Agarwal | 46ad469 | 2016-01-01 21:36:30 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Sameer Agarwal | 5a30cae | 2023-09-19 15:29:34 -0700 | [diff] [blame] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Sameer Agarwal | 46ad469 | 2016-01-01 21:36:30 -0800 | [diff] [blame] | 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 | |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 31 | #include "ceres/trust_region_step_evaluator.h" |
| 32 | |
Sameer Agarwal | 46ad469 | 2016-01-01 21:36:30 -0800 | [diff] [blame] | 33 | #include <algorithm> |
Sameer Agarwal | f27082a | 2018-04-06 20:13:16 -0700 | [diff] [blame] | 34 | #include <limits> |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 35 | |
Sameer Agarwal | caf614a | 2022-04-21 17:41:10 -0700 | [diff] [blame] | 36 | namespace ceres::internal { |
Sameer Agarwal | 46ad469 | 2016-01-01 21:36:30 -0800 | [diff] [blame] | 37 | |
| 38 | TrustRegionStepEvaluator::TrustRegionStepEvaluator( |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 39 | const double initial_cost, const int max_consecutive_nonmonotonic_steps) |
Sameer Agarwal | 46ad469 | 2016-01-01 21:36:30 -0800 | [diff] [blame] | 40 | : max_consecutive_nonmonotonic_steps_(max_consecutive_nonmonotonic_steps), |
| 41 | minimum_cost_(initial_cost), |
| 42 | current_cost_(initial_cost), |
| 43 | reference_cost_(initial_cost), |
| 44 | candidate_cost_(initial_cost), |
| 45 | accumulated_reference_model_cost_change_(0.0), |
| 46 | accumulated_candidate_model_cost_change_(0.0), |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 47 | num_consecutive_nonmonotonic_steps_(0) {} |
Sameer Agarwal | 46ad469 | 2016-01-01 21:36:30 -0800 | [diff] [blame] | 48 | |
| 49 | double TrustRegionStepEvaluator::StepQuality( |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 50 | const double cost, const double model_cost_change) const { |
Sameer Agarwal | f27082a | 2018-04-06 20:13:16 -0700 | [diff] [blame] | 51 | // If the function evaluation for this step was a failure, in which |
| 52 | // case the TrustRegionMinimizer would have set the cost to |
| 53 | // std::numeric_limits<double>::max(). In this case, the division by |
| 54 | // model_cost_change can result in an overflow. To prevent that from |
| 55 | // happening, we will deal with this case explicitly. |
| 56 | if (cost >= std::numeric_limits<double>::max()) { |
| 57 | return std::numeric_limits<double>::lowest(); |
| 58 | } |
| 59 | |
Sameer Agarwal | 46ad469 | 2016-01-01 21:36:30 -0800 | [diff] [blame] | 60 | const double relative_decrease = (current_cost_ - cost) / model_cost_change; |
| 61 | const double historical_relative_decrease = |
| 62 | (reference_cost_ - cost) / |
| 63 | (accumulated_reference_model_cost_change_ + model_cost_change); |
| 64 | return std::max(relative_decrease, historical_relative_decrease); |
| 65 | } |
| 66 | |
Nikolaus Demmel | 7b8f675 | 2020-09-20 21:45:24 +0200 | [diff] [blame] | 67 | void TrustRegionStepEvaluator::StepAccepted(const double cost, |
| 68 | const double model_cost_change) { |
Sameer Agarwal | 46ad469 | 2016-01-01 21:36:30 -0800 | [diff] [blame] | 69 | // Algorithm 10.1.2 from Trust Region Methods by Conn, Gould & |
| 70 | // Toint. |
| 71 | // |
| 72 | // Step 3a |
| 73 | current_cost_ = cost; |
| 74 | accumulated_candidate_model_cost_change_ += model_cost_change; |
| 75 | accumulated_reference_model_cost_change_ += model_cost_change; |
| 76 | |
| 77 | // Step 3b. |
| 78 | if (current_cost_ < minimum_cost_) { |
| 79 | minimum_cost_ = current_cost_; |
| 80 | num_consecutive_nonmonotonic_steps_ = 0; |
| 81 | candidate_cost_ = current_cost_; |
| 82 | accumulated_candidate_model_cost_change_ = 0.0; |
| 83 | } else { |
| 84 | // Step 3c. |
| 85 | ++num_consecutive_nonmonotonic_steps_; |
| 86 | if (current_cost_ > candidate_cost_) { |
| 87 | candidate_cost_ = current_cost_; |
| 88 | accumulated_candidate_model_cost_change_ = 0.0; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Step 3d. |
| 93 | // |
| 94 | // At this point we have made too many non-monotonic steps and |
| 95 | // we are going to reset the value of the reference iterate so |
| 96 | // as to force the algorithm to descend. |
| 97 | // |
| 98 | // Note: In the original algorithm by Toint, this step was only |
| 99 | // executed if the step was non-monotonic, but that would not handle |
| 100 | // the case of max_consecutive_nonmonotonic_steps = 0. The small |
| 101 | // modification of doing this always handles that corner case |
| 102 | // correctly. |
| 103 | if (num_consecutive_nonmonotonic_steps_ == |
| 104 | max_consecutive_nonmonotonic_steps_) { |
| 105 | reference_cost_ = candidate_cost_; |
| 106 | accumulated_reference_model_cost_change_ = |
| 107 | accumulated_candidate_model_cost_change_; |
| 108 | } |
| 109 | } |
| 110 | |
Sameer Agarwal | caf614a | 2022-04-21 17:41:10 -0700 | [diff] [blame] | 111 | } // namespace ceres::internal |