blob: 5c10c05c86bffa29127fecb811cb5a4beee4951b [file] [log] [blame]
Sameer Agarwal46ad4692016-01-01 21:36:30 -08001// Ceres Solver - A fast non-linear least squares minimizer
Sameer Agarwal5a30cae2023-09-19 15:29:34 -07002// Copyright 2023 Google Inc. All rights reserved.
Sameer Agarwal46ad4692016-01-01 21:36:30 -08003// 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 Demmel7b8f6752020-09-20 21:45:24 +020031#include "ceres/trust_region_step_evaluator.h"
32
Sameer Agarwal46ad4692016-01-01 21:36:30 -080033#include <algorithm>
Sameer Agarwalf27082a2018-04-06 20:13:16 -070034#include <limits>
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +020035
Sameer Agarwalcaf614a2022-04-21 17:41:10 -070036namespace ceres::internal {
Sameer Agarwal46ad4692016-01-01 21:36:30 -080037
38TrustRegionStepEvaluator::TrustRegionStepEvaluator(
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +020039 const double initial_cost, const int max_consecutive_nonmonotonic_steps)
Sameer Agarwal46ad4692016-01-01 21:36:30 -080040 : 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 Demmel7b8f6752020-09-20 21:45:24 +020047 num_consecutive_nonmonotonic_steps_(0) {}
Sameer Agarwal46ad4692016-01-01 21:36:30 -080048
49double TrustRegionStepEvaluator::StepQuality(
Nikolaus Demmel7b8f6752020-09-20 21:45:24 +020050 const double cost, const double model_cost_change) const {
Sameer Agarwalf27082a2018-04-06 20:13:16 -070051 // 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 Agarwal46ad4692016-01-01 21:36:30 -080060 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 Demmel7b8f6752020-09-20 21:45:24 +020067void TrustRegionStepEvaluator::StepAccepted(const double cost,
68 const double model_cost_change) {
Sameer Agarwal46ad4692016-01-01 21:36:30 -080069 // 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 Agarwalcaf614a2022-04-21 17:41:10 -0700111} // namespace ceres::internal