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