1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2016 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_MINIMIZER_H_
32 #define CERES_INTERNAL_TRUST_REGION_MINIMIZER_H_
34 #include "ceres/internal/eigen.h"
35 #include "ceres/internal/scoped_ptr.h"
36 #include "ceres/minimizer.h"
37 #include "ceres/solver.h"
38 #include "ceres/sparse_matrix.h"
39 #include "ceres/trust_region_step_evaluator.h"
40 #include "ceres/trust_region_strategy.h"
41 #include "ceres/types.h"
46 // Generic trust region minimization algorithm.
48 // For example usage, see SolverImpl::Minimize.
49 class TrustRegionMinimizer : public Minimizer {
51 ~TrustRegionMinimizer();
53 // This method is not thread safe.
54 virtual void Minimize(const Minimizer::Options& options,
56 Solver::Summary* solver_summary);
59 void Init(const Minimizer::Options& options,
61 Solver::Summary* solver_summary);
63 bool FinalizeIterationAndCheckIfMinimizerCanContinue();
64 bool ComputeTrustRegionStep();
66 bool EvaluateGradientAndJacobian();
67 void ComputeCandidatePointAndEvaluateCost();
69 void DoLineSearch(const Vector& x,
70 const Vector& gradient,
73 void DoInnerIterationsIfNeeded();
75 bool ParameterToleranceReached();
76 bool FunctionToleranceReached();
77 bool GradientToleranceReached();
78 bool MaxSolverTimeReached();
79 bool MaxSolverIterationsReached();
80 bool MinTrustRegionRadiusReached();
82 bool IsStepSuccessful();
83 void HandleUnsuccessfulStep();
84 bool HandleSuccessfulStep();
85 bool HandleInvalidStep();
87 Minimizer::Options options_;
89 // These pointers are shortcuts to objects passed to the
90 // TrustRegionMinimizer. The TrustRegionMinimizer does not own them.
92 Solver::Summary* solver_summary_;
93 Evaluator* evaluator_;
94 SparseMatrix* jacobian_;
95 TrustRegionStrategy* strategy_;
97 scoped_ptr<TrustRegionStepEvaluator> step_evaluator_;
100 bool inner_iterations_are_enabled_;
101 bool inner_iterations_were_useful_;
103 // Summary of the current iteration.
104 IterationSummary iteration_summary_;
106 // Dimensionality of the problem in the ambient space.
108 // Dimensionality of the problem in the tangent space. This is the
109 // number of columns in the Jacobian.
110 int num_effective_parameters_;
111 // Length of the residual vector, also the number of rows in the Jacobian.
120 // Solution computed by the inner iterations.
121 Vector inner_iteration_x_;
122 // model_residuals = J * trust_region_step
123 Vector model_residuals_;
124 Vector negative_gradient_;
125 // projected_gradient_step = Plus(x, -gradient), an intermediate
126 // quantity used to compute the projected gradient norm.
127 Vector projected_gradient_step_;
128 // The step computed by the trust region strategy. If Jacobi scaling
129 // is enabled, this is a vector in the scaled space.
130 Vector trust_region_step_;
131 // The current proposal for how far the trust region algorithm
132 // thinks we should move. In the most basic case, it is just the
133 // trust_region_step_ with the Jacobi scaling undone. If bounds
134 // constraints are present, then it is the result of the projected
137 // candidate_x = Plus(x, delta)
139 // Scaling vector to scale the columns of the Jacobian.
140 Vector jacobian_scaling_;
142 // Euclidean norm of x_.
146 // Minimum cost encountered up till now.
147 double minimum_cost_;
148 // How much did the trust region strategy reduce the cost of the
149 // linearized Gauss-Newton model.
150 double model_cost_change_;
151 // Cost at candidate_x_.
152 double candidate_cost_;
154 // Time at which the minimizer was started.
155 double start_time_in_secs_;
156 // Time at which the current iteration was started.
157 double iteration_start_time_in_secs_;
158 // Number of consecutive steps where the minimizer loop computed a
159 // numerically invalid step.
160 int num_consecutive_invalid_steps_;
163 } // namespace internal
166 #endif // CERES_INTERNAL_TRUST_REGION_MINIMIZER_H_