Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / internal / ceres / trust_region_step_evaluator.cc
1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2016 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 #include <algorithm>
32 #include "ceres/trust_region_step_evaluator.h"
33 #include "glog/logging.h"
34
35 namespace ceres {
36 namespace internal {
37
38 TrustRegionStepEvaluator::TrustRegionStepEvaluator(
39     const double initial_cost,
40     const int max_consecutive_nonmonotonic_steps)
41     : max_consecutive_nonmonotonic_steps_(max_consecutive_nonmonotonic_steps),
42       minimum_cost_(initial_cost),
43       current_cost_(initial_cost),
44       reference_cost_(initial_cost),
45       candidate_cost_(initial_cost),
46       accumulated_reference_model_cost_change_(0.0),
47       accumulated_candidate_model_cost_change_(0.0),
48       num_consecutive_nonmonotonic_steps_(0){
49 }
50
51 double TrustRegionStepEvaluator::StepQuality(
52     const double cost,
53     const double model_cost_change) const {
54   const double relative_decrease = (current_cost_ - cost) / model_cost_change;
55   const double historical_relative_decrease =
56       (reference_cost_ - cost) /
57       (accumulated_reference_model_cost_change_ + model_cost_change);
58   return std::max(relative_decrease, historical_relative_decrease);
59 }
60
61 void TrustRegionStepEvaluator::StepAccepted(
62     const double cost,
63     const double model_cost_change) {
64   // Algorithm 10.1.2 from Trust Region Methods by Conn, Gould &
65   // Toint.
66   //
67   // Step 3a
68   current_cost_ = cost;
69   accumulated_candidate_model_cost_change_ += model_cost_change;
70   accumulated_reference_model_cost_change_ += model_cost_change;
71
72   // Step 3b.
73   if (current_cost_ < minimum_cost_) {
74     minimum_cost_ = current_cost_;
75     num_consecutive_nonmonotonic_steps_ = 0;
76     candidate_cost_ = current_cost_;
77     accumulated_candidate_model_cost_change_ = 0.0;
78   } else {
79     // Step 3c.
80     ++num_consecutive_nonmonotonic_steps_;
81     if (current_cost_ > candidate_cost_) {
82       candidate_cost_ = current_cost_;
83       accumulated_candidate_model_cost_change_ = 0.0;
84     }
85   }
86
87   // Step 3d.
88   //
89   // At this point we have made too many non-monotonic steps and
90   // we are going to reset the value of the reference iterate so
91   // as to force the algorithm to descend.
92   //
93   // Note: In the original algorithm by Toint, this step was only
94   // executed if the step was non-monotonic, but that would not handle
95   // the case of max_consecutive_nonmonotonic_steps = 0. The small
96   // modification of doing this always handles that corner case
97   // correctly.
98   if (num_consecutive_nonmonotonic_steps_ ==
99       max_consecutive_nonmonotonic_steps_) {
100     reference_cost_ = candidate_cost_;
101     accumulated_reference_model_cost_change_ =
102         accumulated_candidate_model_cost_change_;
103   }
104 }
105
106 }  // namespace internal
107 }  // namespace ceres