blob: be4d40653c4bf186e8fb60f9b42d9eaffce81ab9 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// 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_MINIMIZER_H_
32#define CERES_INTERNAL_TRUST_REGION_MINIMIZER_H_
33
34#include <memory>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080035
Austin Schuh70cc9552019-01-21 19:46:48 -080036#include "ceres/internal/eigen.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080037#include "ceres/internal/port.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080038#include "ceres/minimizer.h"
39#include "ceres/solver.h"
40#include "ceres/sparse_matrix.h"
41#include "ceres/trust_region_step_evaluator.h"
42#include "ceres/trust_region_strategy.h"
43#include "ceres/types.h"
44
45namespace ceres {
46namespace internal {
47
48// Generic trust region minimization algorithm.
49//
50// For example usage, see SolverImpl::Minimize.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080051class CERES_EXPORT_INTERNAL TrustRegionMinimizer : public Minimizer {
Austin Schuh70cc9552019-01-21 19:46:48 -080052 public:
53 ~TrustRegionMinimizer();
54
55 // This method is not thread safe.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080056 void Minimize(const Minimizer::Options& options,
57 double* parameters,
58 Solver::Summary* solver_summary) override;
Austin Schuh70cc9552019-01-21 19:46:48 -080059
60 private:
61 void Init(const Minimizer::Options& options,
62 double* parameters,
63 Solver::Summary* solver_summary);
64 bool IterationZero();
65 bool FinalizeIterationAndCheckIfMinimizerCanContinue();
66 bool ComputeTrustRegionStep();
67
68 bool EvaluateGradientAndJacobian(bool new_evaluation_point);
69 void ComputeCandidatePointAndEvaluateCost();
70
71 void DoLineSearch(const Vector& x,
72 const Vector& gradient,
73 const double cost,
74 Vector* delta);
75 void DoInnerIterationsIfNeeded();
76
77 bool ParameterToleranceReached();
78 bool FunctionToleranceReached();
79 bool GradientToleranceReached();
80 bool MaxSolverTimeReached();
81 bool MaxSolverIterationsReached();
82 bool MinTrustRegionRadiusReached();
83
84 bool IsStepSuccessful();
Austin Schuh70cc9552019-01-21 19:46:48 -080085 bool HandleSuccessfulStep();
86 bool HandleInvalidStep();
87
88 Minimizer::Options options_;
89
90 // These pointers are shortcuts to objects passed to the
91 // TrustRegionMinimizer. The TrustRegionMinimizer does not own them.
92 double* parameters_;
93 Solver::Summary* solver_summary_;
94 Evaluator* evaluator_;
95 SparseMatrix* jacobian_;
96 TrustRegionStrategy* strategy_;
97
98 std::unique_ptr<TrustRegionStepEvaluator> step_evaluator_;
99
100 bool is_not_silent_;
101 bool inner_iterations_are_enabled_;
102 bool inner_iterations_were_useful_;
103
104 // Summary of the current iteration.
105 IterationSummary iteration_summary_;
106
107 // Dimensionality of the problem in the ambient space.
108 int num_parameters_;
109 // Dimensionality of the problem in the tangent space. This is the
110 // number of columns in the Jacobian.
111 int num_effective_parameters_;
112 // Length of the residual vector, also the number of rows in the Jacobian.
113 int num_residuals_;
114
115 // Current point.
116 Vector x_;
117 // Residuals at x_;
118 Vector residuals_;
119 // Gradient at x_.
120 Vector gradient_;
121 // Solution computed by the inner iterations.
122 Vector inner_iteration_x_;
123 // model_residuals = J * trust_region_step
124 Vector model_residuals_;
125 Vector negative_gradient_;
126 // projected_gradient_step = Plus(x, -gradient), an intermediate
127 // quantity used to compute the projected gradient norm.
128 Vector projected_gradient_step_;
129 // The step computed by the trust region strategy. If Jacobi scaling
130 // is enabled, this is a vector in the scaled space.
131 Vector trust_region_step_;
132 // The current proposal for how far the trust region algorithm
133 // thinks we should move. In the most basic case, it is just the
134 // trust_region_step_ with the Jacobi scaling undone. If bounds
135 // constraints are present, then it is the result of the projected
136 // line search.
137 Vector delta_;
138 // candidate_x = Plus(x, delta)
139 Vector candidate_x_;
140 // Scaling vector to scale the columns of the Jacobian.
141 Vector jacobian_scaling_;
142
143 // Euclidean norm of x_.
144 double x_norm_;
145 // Cost at x_.
146 double x_cost_;
147 // Minimum cost encountered up till now.
148 double minimum_cost_;
149 // How much did the trust region strategy reduce the cost of the
150 // linearized Gauss-Newton model.
151 double model_cost_change_;
152 // Cost at candidate_x_.
153 double candidate_cost_;
154
155 // Time at which the minimizer was started.
156 double start_time_in_secs_;
157 // Time at which the current iteration was started.
158 double iteration_start_time_in_secs_;
159 // Number of consecutive steps where the minimizer loop computed a
160 // numerically invalid step.
161 int num_consecutive_invalid_steps_;
162};
163
164} // namespace internal
165} // namespace ceres
166
167#endif // CERES_INTERNAL_TRUST_REGION_MINIMIZER_H_