blob: 43141da58a1ed4c75680a9c6ce6385defe44c287 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
Sameer Agarwal46ad4692016-01-01 21:36:30 -08002// Copyright 2016 Google Inc. All rights reserved.
Keir Mierle7492b0d2015-03-17 22:30:16 -07003// http://ceres-solver.org/
Keir Mierle8ebb0732012-04-30 23:09:08 -07004//
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)
Keir Mierle8ebb0732012-04-30 23:09:08 -070030
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070031#ifndef CERES_INTERNAL_TRUST_REGION_MINIMIZER_H_
32#define CERES_INTERNAL_TRUST_REGION_MINIMIZER_H_
Keir Mierle8ebb0732012-04-30 23:09:08 -070033
Sameer Agarwal46ad4692016-01-01 21:36:30 -080034#include "ceres/internal/eigen.h"
35#include "ceres/internal/scoped_ptr.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070036#include "ceres/minimizer.h"
37#include "ceres/solver.h"
Sameer Agarwal46ad4692016-01-01 21:36:30 -080038#include "ceres/sparse_matrix.h"
39#include "ceres/trust_region_step_evaluator.h"
40#include "ceres/trust_region_strategy.h"
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070041#include "ceres/types.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070042
43namespace ceres {
44namespace internal {
45
Sameer Agarwal46ad4692016-01-01 21:36:30 -080046// Generic trust region minimization algorithm.
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070047//
48// For example usage, see SolverImpl::Minimize.
49class TrustRegionMinimizer : public Minimizer {
Keir Mierle8ebb0732012-04-30 23:09:08 -070050 public:
Sameer Agarwal46ad4692016-01-01 21:36:30 -080051 ~TrustRegionMinimizer();
52
53 // This method is not thread safe.
Keir Mierle8ebb0732012-04-30 23:09:08 -070054 virtual void Minimize(const Minimizer::Options& options,
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070055 double* parameters,
Sameer Agarwal46ad4692016-01-01 21:36:30 -080056 Solver::Summary* solver_summary);
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070057
58 private:
Sameer Agarwal46ad4692016-01-01 21:36:30 -080059 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();
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();
83 void HandleUnsuccessfulStep();
84 bool HandleSuccessfulStep();
85 bool HandleInvalidStep();
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070086
87 Minimizer::Options options_;
Sameer Agarwal46ad4692016-01-01 21:36:30 -080088
89 // These pointers are shortcuts to objects passed to the
90 // TrustRegionMinimizer. The TrustRegionMinimizer does not own them.
91 double* parameters_;
92 Solver::Summary* solver_summary_;
93 Evaluator* evaluator_;
94 SparseMatrix* jacobian_;
95 TrustRegionStrategy* strategy_;
96
97 scoped_ptr<TrustRegionStepEvaluator> step_evaluator_;
98
99 bool is_not_silent_;
100 bool inner_iterations_are_enabled_;
101 bool inner_iterations_were_useful_;
102
103 // Summary of the current iteration.
104 IterationSummary iteration_summary_;
105
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800106 // Dimensionality of the problem in the ambient space.
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800107 int num_parameters_;
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800108 // Dimensionality of the problem in the tangent space. This is the
109 // number of columns in the Jacobian.
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800110 int num_effective_parameters_;
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800111 // Length of the residual vector, also the number of rows in the Jacobian.
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800112 int num_residuals_;
113
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800114 // Current point.
115 Vector x_;
116 // Residuals at x_;
117 Vector residuals_;
118 // Gradient at x_.
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800119 Vector gradient_;
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800120 // Solution computed by the inner iterations.
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800121 Vector inner_iteration_x_;
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800122 // model_residuals = J * trust_region_step
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800123 Vector model_residuals_;
124 Vector negative_gradient_;
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800125 // projected_gradient_step = Plus(x, -gradient), an intermediate
126 // quantity used to compute the projected gradient norm.
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800127 Vector projected_gradient_step_;
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800128 // The step computed by the trust region strategy. If Jacobi scaling
129 // is enabled, this is a vector in the scaled space.
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800130 Vector trust_region_step_;
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800131 // 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
135 // line search.
136 Vector delta_;
137 // candidate_x = Plus(x, delta)
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800138 Vector candidate_x_;
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800139 // Scaling vector to scale the columns of the Jacobian.
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800140 Vector jacobian_scaling_;
141
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800142 // Euclidean norm of x_.
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800143 double x_norm_;
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800144 // Cost at x_.
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800145 double x_cost_;
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800146 // Minimum cost encountered up till now.
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800147 double minimum_cost_;
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800148 // How much did the trust region strategy reduce the cost of the
149 // linearized Gauss-Newton model.
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800150 double model_cost_change_;
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800151 // Cost at candidate_x_.
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800152 double candidate_cost_;
153
Sameer Agarwal2c178ec2016-02-20 02:18:38 -0800154 // 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.
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800160 int num_consecutive_invalid_steps_;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700161};
162
163} // namespace internal
164} // namespace ceres
Sameer Agarwal46ad4692016-01-01 21:36:30 -0800165
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700166#endif // CERES_INTERNAL_TRUST_REGION_MINIMIZER_H_