Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / internal / ceres / trust_region_strategy.h
1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 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_STRATEGY_H_
32 #define CERES_INTERNAL_TRUST_REGION_STRATEGY_H_
33
34 #include <string>
35 #include "ceres/internal/port.h"
36 #include "ceres/linear_solver.h"
37
38 namespace ceres {
39 namespace internal {
40
41 class LinearSolver;
42 class SparseMatrix;
43
44 // Interface for classes implementing various trust region strategies
45 // for nonlinear least squares problems.
46 //
47 // The object is expected to maintain and update a trust region
48 // radius, which it then uses to solve for the trust region step using
49 // the jacobian matrix and residual vector.
50 //
51 // Here the term trust region radius is used loosely, as the strategy
52 // is free to treat it as guidance and violate it as need be. e.g.,
53 // the LevenbergMarquardtStrategy uses the inverse of the trust region
54 // radius to scale the damping term, which controls the step size, but
55 // does not set a hard limit on its size.
56 class TrustRegionStrategy {
57  public:
58   struct Options {
59     Options()
60         : trust_region_strategy_type(LEVENBERG_MARQUARDT),
61           initial_radius(1e4),
62           max_radius(1e32),
63           min_lm_diagonal(1e-6),
64           max_lm_diagonal(1e32),
65           dogleg_type(TRADITIONAL_DOGLEG) {
66     }
67
68     TrustRegionStrategyType trust_region_strategy_type;
69     // Linear solver used for actually solving the trust region step.
70     LinearSolver* linear_solver;
71     double initial_radius;
72     double max_radius;
73
74     // Minimum and maximum values of the diagonal damping matrix used
75     // by LevenbergMarquardtStrategy. The DoglegStrategy also uses
76     // these bounds to construct a regularizing diagonal to ensure
77     // that the Gauss-Newton step computation is of full rank.
78     double min_lm_diagonal;
79     double max_lm_diagonal;
80
81     // Further specify which dogleg method to use
82     DoglegType dogleg_type;
83   };
84
85   // Per solve options.
86   struct PerSolveOptions {
87     PerSolveOptions()
88         : eta(0),
89           dump_format_type(TEXTFILE) {
90     }
91
92     // Forcing sequence for inexact solves.
93     double eta;
94
95     DumpFormatType dump_format_type;
96
97     // If non-empty and dump_format_type is not CONSOLE, the trust
98     // regions strategy will write the linear system to file(s) with
99     // name starting with dump_filename_base.  If dump_format_type is
100     // CONSOLE then dump_filename_base will be ignored and the linear
101     // system will be written to the standard error.
102     std::string dump_filename_base;
103   };
104
105   struct Summary {
106     Summary()
107         : residual_norm(0.0),
108           num_iterations(-1),
109           termination_type(LINEAR_SOLVER_FAILURE) {
110     }
111
112     // If the trust region problem is,
113     //
114     //   1/2 x'Ax + b'x + c,
115     //
116     // then
117     //
118     //   residual_norm = |Ax -b|
119     double residual_norm;
120
121     // Number of iterations used by the linear solver. If a linear
122     // solver was not called (e.g., DogLegStrategy after an
123     // unsuccessful step), then this would be zero.
124     int num_iterations;
125
126     // Status of the linear solver used to solve the Newton system.
127     LinearSolverTerminationType termination_type;
128   };
129
130   virtual ~TrustRegionStrategy();
131
132   // Use the current radius to solve for the trust region step.
133   virtual Summary ComputeStep(const PerSolveOptions& per_solve_options,
134                               SparseMatrix* jacobian,
135                               const double* residuals,
136                               double* step) = 0;
137
138   // Inform the strategy that the current step has been accepted, and
139   // that the ratio of the decrease in the non-linear objective to the
140   // decrease in the trust region model is step_quality.
141   virtual void StepAccepted(double step_quality) = 0;
142
143   // Inform the strategy that the current step has been rejected, and
144   // that the ratio of the decrease in the non-linear objective to the
145   // decrease in the trust region model is step_quality.
146   virtual void StepRejected(double step_quality) = 0;
147
148   // Inform the strategy that the current step has been rejected
149   // because it was found to be numerically invalid.
150   // StepRejected/StepAccepted will not be called for this step, and
151   // the strategy is free to do what it wants with this information.
152   virtual void StepIsInvalid() = 0;
153
154   // Current trust region radius.
155   virtual double Radius() const = 0;
156
157   // Factory.
158   static TrustRegionStrategy* Create(const Options& options);
159 };
160
161 }  // namespace internal
162 }  // namespace ceres
163
164 #endif  // CERES_INTERNAL_TRUST_REGION_STRATEGY_H_