blob: 7d94ca28373a3c54b64ea42edb97aec086ac3adf [file] [log] [blame]
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
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_STRATEGY_H_
32#define CERES_INTERNAL_TRUST_REGION_STRATEGY_H_
33
Sameer Agarwal05292bf2012-08-20 07:40:45 -070034#include "ceres/types.h"
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070035
36namespace ceres {
37namespace internal {
38
Sameer Agarwal05292bf2012-08-20 07:40:45 -070039class LinearSolver;
40class SparseMatrix;
41
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070042// Interface for classes implementing various trust region strategies
43// for nonlinear least squares problems.
44//
45// The object is expected to maintain and update a trust region
46// radius, which it then uses to solve for the trust region step using
47// the jacobian matrix and residual vector.
48//
49// Here the term trust region radius is used loosely, as the strategy
50// is free to treat it as guidance and violate it as need be. e.g.,
51// the LevenbergMarquardtStrategy uses the inverse of the trust region
52// radius to scale the damping term, which controls the step size, but
53// does not set a hard limit on its size.
54class TrustRegionStrategy {
55public:
56 struct Options {
57 Options()
58 : trust_region_strategy_type(LEVENBERG_MARQUARDT),
59 initial_radius(1e4),
60 max_radius(1e32),
61 lm_min_diagonal(1e-6),
62 lm_max_diagonal(1e32) {
63 }
64
65 TrustRegionStrategyType trust_region_strategy_type;
66 // Linear solver used for actually solving the trust region step.
67 LinearSolver* linear_solver;
68 double initial_radius;
69 double max_radius;
70
71 // Minimum and maximum values of the diagonal damping matrix used
Sameer Agarwalfa015192012-06-11 14:21:42 -070072 // by LevenbergMarquardtStrategy. The DoglegStrategy also uses
73 // these bounds to construct a regularizing diagonal to ensure
74 // that the Gauss-Newton step computation is of full rank.
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070075 double lm_min_diagonal;
76 double lm_max_diagonal;
77 };
78
79 // Per solve options.
80 struct PerSolveOptions {
81 // Forcing sequence for inexact solves.
82 double eta;
83 };
84
Sameer Agarwal05292bf2012-08-20 07:40:45 -070085 struct Summary {
86 Summary()
87 : residual_norm(0.0),
88 num_iterations(-1),
89 termination_type(FAILURE) {
90 }
91
92 // If the trust region problem is,
93 //
94 // 1/2 x'Ax + b'x + c,
95 //
96 // then
97 //
98 // residual_norm = |Ax -b|
99 double residual_norm;
100
101 // Number of iterations used by the linear solver. If a linear
102 // solver was not called (e.g., DogLegStrategy after an
103 // unsuccessful step), then this would be zero.
104 int num_iterations;
105
106 // Status of the linear solver used to solve the Newton system.
107 LinearSolverTerminationType termination_type;
108 };
109
Sameer Agarwal59534c12012-06-15 14:36:27 -0700110 virtual ~TrustRegionStrategy();
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700111
112 // Use the current radius to solve for the trust region step.
Sameer Agarwal05292bf2012-08-20 07:40:45 -0700113 virtual Summary ComputeStep(const PerSolveOptions& per_solve_options,
114 SparseMatrix* jacobian,
115 const double* residuals,
116 double* step) = 0;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700117
118 // Inform the strategy that the current step has been accepted, and
119 // that the ratio of the decrease in the non-linear objective to the
120 // decrease in the trust region model is step_quality.
121 virtual void StepAccepted(double step_quality) = 0;
122
123 // Inform the strategy that the current step has been rejected, and
124 // that the ratio of the decrease in the non-linear objective to the
125 // decrease in the trust region model is step_quality.
126 virtual void StepRejected(double step_quality) = 0;
127
Sameer Agarwald432f782012-06-11 11:52:32 -0700128 // Inform the strategy that the current step has been rejected
129 // because it was found to be numerically invalid.
130 // StepRejected/StepAccepted will not be called for this step, and
131 // the strategy is free to do what it wants with this information.
132 virtual void StepIsInvalid() = 0;
133
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700134 // Current trust region radius.
135 virtual double Radius() const = 0;
136
137 // Factory.
138 static TrustRegionStrategy* Create(const Options& options);
139};
140
141} // namespace internal
142} // namespace ceres
143
144#endif // CERES_INTERNAL_TRUST_REGION_STRATEGY_H_