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