blob: 998514fbfa560935b4c8ecd2bfecab9bb70d03e7 [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 Agarwalc4a32912013-06-13 22:00:48 -070034#include <string>
35#include "ceres/internal/port.h"
Sameer Agarwal79bde352013-11-21 21:33:51 -080036#include "ceres/linear_solver.h"
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070037
38namespace ceres {
39namespace internal {
40
Sameer Agarwal05292bf2012-08-20 07:40:45 -070041class LinearSolver;
42class SparseMatrix;
43
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070044// Interface for classes implementing various trust region strategies
45// for nonlinear least squares problems.
46//
47// The object is expected to maintain and update a trust region
48// radius, which it then uses to solve for the trust region step using
49// the jacobian matrix and residual vector.
50//
51// Here the term trust region radius is used loosely, as the strategy
52// is free to treat it as guidance and violate it as need be. e.g.,
53// the LevenbergMarquardtStrategy uses the inverse of the trust region
54// radius to scale the damping term, which controls the step size, but
55// does not set a hard limit on its size.
56class TrustRegionStrategy {
Sameer Agarwal509f68c2013-02-20 01:39:03 -080057 public:
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070058 struct Options {
59 Options()
60 : trust_region_strategy_type(LEVENBERG_MARQUARDT),
61 initial_radius(1e4),
62 max_radius(1e32),
Sameer Agarwaleeedd2e2013-07-07 23:04:31 -070063 min_lm_diagonal(1e-6),
64 max_lm_diagonal(1e32),
Markus Moll51cf7cb2012-08-20 20:10:20 +020065 dogleg_type(TRADITIONAL_DOGLEG) {
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070066 }
67
68 TrustRegionStrategyType trust_region_strategy_type;
69 // Linear solver used for actually solving the trust region step.
70 LinearSolver* linear_solver;
71 double initial_radius;
72 double max_radius;
73
74 // Minimum and maximum values of the diagonal damping matrix used
Sameer Agarwalfa015192012-06-11 14:21:42 -070075 // by LevenbergMarquardtStrategy. The DoglegStrategy also uses
76 // these bounds to construct a regularizing diagonal to ensure
77 // that the Gauss-Newton step computation is of full rank.
Sameer Agarwaleeedd2e2013-07-07 23:04:31 -070078 double min_lm_diagonal;
79 double max_lm_diagonal;
Markus Moll51cf7cb2012-08-20 20:10:20 +020080
81 // Further specify which dogleg method to use
82 DoglegType dogleg_type;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070083 };
84
85 // Per solve options.
86 struct PerSolveOptions {
Sameer Agarwalc4a32912013-06-13 22:00:48 -070087 PerSolveOptions()
88 : eta(0),
89 dump_filename_base(""),
90 dump_format_type(TEXTFILE) {
91 }
92
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070093 // Forcing sequence for inexact solves.
94 double eta;
Sameer Agarwalc4a32912013-06-13 22:00:48 -070095
96 // If non-empty and dump_format_type is not CONSOLE, the trust
97 // regions strategy will write the linear system to file(s) with
98 // name starting with dump_filename_base. If dump_format_type is
99 // CONSOLE then dump_filename_base will be ignored and the linear
100 // system will be written to the standard error.
101 string dump_filename_base;
102 DumpFormatType dump_format_type;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700103 };
104
Sameer Agarwal05292bf2012-08-20 07:40:45 -0700105 struct Summary {
106 Summary()
107 : residual_norm(0.0),
108 num_iterations(-1),
Sameer Agarwal33e01b92013-11-27 10:24:03 -0800109 termination_type(LINEAR_SOLVER_FAILURE) {
Sameer Agarwal05292bf2012-08-20 07:40:45 -0700110 }
111
112 // If the trust region problem is,
113 //
114 // 1/2 x'Ax + b'x + c,
115 //
116 // then
117 //
118 // residual_norm = |Ax -b|
119 double residual_norm;
120
121 // Number of iterations used by the linear solver. If a linear
122 // solver was not called (e.g., DogLegStrategy after an
123 // unsuccessful step), then this would be zero.
124 int num_iterations;
125
126 // Status of the linear solver used to solve the Newton system.
127 LinearSolverTerminationType termination_type;
128 };
129
Sameer Agarwal59534c12012-06-15 14:36:27 -0700130 virtual ~TrustRegionStrategy();
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700131
132 // Use the current radius to solve for the trust region step.
Sameer Agarwal05292bf2012-08-20 07:40:45 -0700133 virtual Summary ComputeStep(const PerSolveOptions& per_solve_options,
134 SparseMatrix* jacobian,
135 const double* residuals,
136 double* step) = 0;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700137
138 // Inform the strategy that the current step has been accepted, and
139 // that the ratio of the decrease in the non-linear objective to the
140 // decrease in the trust region model is step_quality.
141 virtual void StepAccepted(double step_quality) = 0;
142
143 // Inform the strategy that the current step has been rejected, and
144 // that the ratio of the decrease in the non-linear objective to the
145 // decrease in the trust region model is step_quality.
146 virtual void StepRejected(double step_quality) = 0;
147
Sameer Agarwald432f782012-06-11 11:52:32 -0700148 // Inform the strategy that the current step has been rejected
149 // because it was found to be numerically invalid.
150 // StepRejected/StepAccepted will not be called for this step, and
151 // the strategy is free to do what it wants with this information.
152 virtual void StepIsInvalid() = 0;
153
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700154 // Current trust region radius.
155 virtual double Radius() const = 0;
156
157 // Factory.
158 static TrustRegionStrategy* Create(const Options& options);
159};
160
161} // namespace internal
162} // namespace ceres
163
164#endif // CERES_INTERNAL_TRUST_REGION_STRATEGY_H_