1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
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.
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.
29 // Author: sameeragarwal@google.com (Sameer Agarwal)
31 #ifndef CERES_INTERNAL_TRUST_REGION_STRATEGY_H_
32 #define CERES_INTERNAL_TRUST_REGION_STRATEGY_H_
35 #include "ceres/internal/port.h"
36 #include "ceres/linear_solver.h"
44 // Interface for classes implementing various trust region strategies
45 // for nonlinear least squares problems.
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.
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 {
60 : trust_region_strategy_type(LEVENBERG_MARQUARDT),
63 min_lm_diagonal(1e-6),
64 max_lm_diagonal(1e32),
65 dogleg_type(TRADITIONAL_DOGLEG) {
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;
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;
81 // Further specify which dogleg method to use
82 DoglegType dogleg_type;
86 struct PerSolveOptions {
89 dump_format_type(TEXTFILE) {
92 // Forcing sequence for inexact solves.
95 DumpFormatType dump_format_type;
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;
107 : residual_norm(0.0),
109 termination_type(LINEAR_SOLVER_FAILURE) {
112 // If the trust region problem is,
114 // 1/2 x'Ax + b'x + c,
118 // residual_norm = |Ax -b|
119 double residual_norm;
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.
126 // Status of the linear solver used to solve the Newton system.
127 LinearSolverTerminationType termination_type;
130 virtual ~TrustRegionStrategy();
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,
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;
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;
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;
154 // Current trust region radius.
155 virtual double Radius() const = 0;
158 static TrustRegionStrategy* Create(const Options& options);
161 } // namespace internal
164 #endif // CERES_INTERNAL_TRUST_REGION_STRATEGY_H_