blob: 31f88740b9fe0b4b18aa0912d2ab2592d07ffc5e [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 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// Abstract interface for objects solving linear systems of various
32// kinds.
33
34#ifndef CERES_INTERNAL_LINEAR_SOLVER_H_
35#define CERES_INTERNAL_LINEAR_SOLVER_H_
36
37#include <cstddef>
38
39#include <glog/logging.h>
40#include "ceres/block_sparse_matrix.h"
41#include "ceres/casts.h"
42#include "ceres/compressed_row_sparse_matrix.h"
43#include "ceres/dense_sparse_matrix.h"
44#include "ceres/triplet_sparse_matrix.h"
45#include "ceres/types.h"
46
47namespace ceres {
48namespace internal {
49
50class LinearOperator;
51
52// Abstract base class for objects that implement algorithms for
53// solving linear systems
54//
55// Ax = b
56//
57// It is expected that a single instance of a LinearSolver object
Sameer Agarwala9d8ef82012-05-14 02:28:05 -070058// maybe used multiple times for solving multiple linear systems with
59// the same sparsity structure. This allows them to cache and reuse
60// information across solves. This means that calling Solve on the
61// same LinearSolver instance with two different linear systems will
62// result in undefined behaviour.
Keir Mierle8ebb0732012-04-30 23:09:08 -070063//
64// Subclasses of LinearSolver use two structs to configure themselves.
65// The Options struct configures the LinearSolver object for its
66// lifetime. The PerSolveOptions struct is used to specify options for
67// a particular Solve call.
68class LinearSolver {
69 public:
70 struct Options {
71 Options()
72 : type(SPARSE_NORMAL_CHOLESKY),
73 preconditioner_type(JACOBI),
Sameer Agarwalb0518732012-05-29 00:27:57 -070074 sparse_linear_algebra_library(SUITE_SPARSE),
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -070075 use_block_amd(true),
Keir Mierle8ebb0732012-04-30 23:09:08 -070076 min_num_iterations(1),
77 max_num_iterations(1),
78 num_threads(1),
Keir Mierle8ebb0732012-04-30 23:09:08 -070079 num_eliminate_blocks(0),
80 residual_reset_period(10),
81 row_block_size(Dynamic),
82 e_block_size(Dynamic),
83 f_block_size(Dynamic) {
84 }
85
86 LinearSolverType type;
87
88 PreconditionerType preconditioner_type;
89
Sameer Agarwalb0518732012-05-29 00:27:57 -070090 SparseLinearAlgebraLibraryType sparse_linear_algebra_library;
91
Sameer Agarwal7a3c43b2012-06-05 23:10:59 -070092 // See solver.h for explanation of this option.
93 bool use_block_amd;
94
Keir Mierle8ebb0732012-04-30 23:09:08 -070095 // Number of internal iterations that the solver uses. This
96 // parameter only makes sense for iterative solvers like CG.
97 int min_num_iterations;
98 int max_num_iterations;
99
100 // If possible, how many threads can the solver use.
101 int num_threads;
102
Keir Mierle8ebb0732012-04-30 23:09:08 -0700103 // Eliminate 0 to num_eliminate_blocks - 1 from the Normal
104 // equations to form a schur complement. Only used by the Schur
105 // complement based solver. The most common use for this parameter
106 // is in the case of structure from motion problems where we have
107 // camera blocks and point blocks. Then setting the
108 // num_eliminate_blocks to the number of points allows the solver
109 // to use the Schur complement trick. For more details see the
110 // description of this parameter in solver.h.
111 int num_eliminate_blocks;
112
113 // Iterative solvers, e.g. Preconditioned Conjugate Gradients
114 // maintain a cheap estimate of the residual which may become
115 // inaccurate over time. Thus for non-zero values of this
116 // parameter, the solver can be told to recalculate the value of
117 // the residual using a |b - Ax| evaluation.
118 int residual_reset_period;
119
120 // If the block sizes in a BlockSparseMatrix are fixed, then in
121 // some cases the Schur complement based solvers can detect and
122 // specialize on them.
123 //
124 // It is expected that these parameters are set programmatically
125 // rather than manually.
126 //
Sameer Agarwala9d8ef82012-05-14 02:28:05 -0700127 // Please see schur_complement_solver.h and schur_eliminator.h for
128 // more details.
Keir Mierle8ebb0732012-04-30 23:09:08 -0700129 int row_block_size;
130 int e_block_size;
131 int f_block_size;
132 };
133
134 // Options for the Solve method.
135 struct PerSolveOptions {
136 PerSolveOptions()
137 : D(NULL),
138 preconditioner(NULL),
139 r_tolerance(0.0),
140 q_tolerance(0.0) {
141 }
142
143 // This option only makes sense for unsymmetric linear solvers
144 // that can solve rectangular linear systems.
145 //
146 // Given a matrix A, an optional diagonal matrix D as a vector,
147 // and a vector b, the linear solver will solve for
148 //
149 // | A | x = | b |
150 // | D | | 0 |
151 //
152 // If D is null, then it is treated as zero, and the solver returns
153 // the solution to
154 //
155 // A x = b
156 //
157 // In either case, x is the vector that solves the following
158 // optimization problem.
159 //
Keir Mierlef7898fb2012-05-05 20:55:08 -0700160 // arg min_x ||Ax - b||^2 + ||Dx||^2
Keir Mierle8ebb0732012-04-30 23:09:08 -0700161 //
162 // Here A is a matrix of size m x n, with full column rank. If A
163 // does not have full column rank, the results returned by the
164 // solver cannot be relied on. D, if it is not null is an array of
165 // size n. b is an array of size m and x is an array of size n.
166 double * D;
167
168 // This option only makes sense for iterative solvers.
169 //
170 // In general the performance of an iterative linear solver
171 // depends on the condition number of the matrix A. For example
172 // the convergence rate of the conjugate gradients algorithm
173 // is proportional to the square root of the condition number.
174 //
175 // One particularly useful technique for improving the
176 // conditioning of a linear system is to precondition it. In its
177 // simplest form a preconditioner is a matrix M such that instead
178 // of solving Ax = b, we solve the linear system AM^{-1} y = b
179 // instead, where M is such that the condition number k(AM^{-1})
180 // is smaller than the conditioner k(A). Given the solution to
181 // this system, x = M^{-1} y. The iterative solver takes care of
182 // the mechanics of solving the preconditioned system and
183 // returning the corrected solution x. The user only needs to
184 // supply a linear operator.
185 //
186 // A null preconditioner is equivalent to an identity matrix being
187 // used a preconditioner.
188 LinearOperator* preconditioner;
189
190
191 // The following tolerance related options only makes sense for
192 // iterative solvers. Direct solvers ignore them.
193
194 // Solver terminates when
195 //
196 // |Ax - b| <= r_tolerance * |b|.
197 //
198 // This is the most commonly used termination criterion for
199 // iterative solvers.
200 double r_tolerance;
201
202 // For PSD matrices A, let
203 //
204 // Q(x) = x'Ax - 2b'x
205 //
206 // be the cost of the quadratic function defined by A and b. Then,
207 // the solver terminates at iteration i if
208 //
209 // i * (Q(x_i) - Q(x_i-1)) / Q(x_i) < q_tolerance.
210 //
211 // This termination criterion is more useful when using CG to
212 // solve the Newton step. This particular convergence test comes
213 // from Stephen Nash's work on truncated Newton
214 // methods. References:
215 //
216 // 1. Stephen G. Nash & Ariela Sofer, Assessing A Search
217 // Direction Within A Truncated Newton Method, Operation
218 // Research Letters 9(1990) 219-221.
219 //
220 // 2. Stephen G. Nash, A Survey of Truncated Newton Methods,
221 // Journal of Computational and Applied Mathematics,
222 // 124(1-2), 45-59, 2000.
223 //
224 double q_tolerance;
225 };
226
227 // Summary of a call to the Solve method. We should move away from
228 // the true/false method for determining solver success. We should
229 // let the summary object do the talking.
230 struct Summary {
231 Summary()
232 : residual_norm(0.0),
233 num_iterations(-1),
234 termination_type(FAILURE) {
235 }
236
237 double residual_norm;
238 int num_iterations;
239 LinearSolverTerminationType termination_type;
240 };
241
242 virtual ~LinearSolver();
243
244 // Solve Ax = b.
245 virtual Summary Solve(LinearOperator* A,
246 const double* b,
247 const PerSolveOptions& per_solve_options,
248 double* x) = 0;
249
Sameer Agarwala9d8ef82012-05-14 02:28:05 -0700250 // Factory
Keir Mierle8ebb0732012-04-30 23:09:08 -0700251 static LinearSolver* Create(const Options& options);
252};
253
254// This templated subclass of LinearSolver serves as a base class for
255// other linear solvers that depend on the particular matrix layout of
256// the underlying linear operator. For example some linear solvers
257// need low level access to the TripletSparseMatrix implementing the
258// LinearOperator interface. This class hides those implementation
259// details behind a private virtual method, and has the Solve method
260// perform the necessary upcasting.
261template <typename MatrixType>
262class TypedLinearSolver : public LinearSolver {
263 public:
264 virtual ~TypedLinearSolver() {}
265 virtual LinearSolver::Summary Solve(
266 LinearOperator* A,
267 const double* b,
268 const LinearSolver::PerSolveOptions& per_solve_options,
269 double* x) {
270 CHECK_NOTNULL(A);
271 CHECK_NOTNULL(b);
272 CHECK_NOTNULL(x);
273 return SolveImpl(down_cast<MatrixType*>(A), b, per_solve_options, x);
274 }
275
276 private:
277 virtual LinearSolver::Summary SolveImpl(
278 MatrixType* A,
279 const double* b,
280 const LinearSolver::PerSolveOptions& per_solve_options,
281 double* x) = 0;
282};
283
284// Linear solvers that depend on acccess to the low level structure of
285// a SparseMatrix.
286typedef TypedLinearSolver<BlockSparseMatrix> BlockSparseMatrixSolver; // NOLINT
287typedef TypedLinearSolver<BlockSparseMatrixBase> BlockSparseMatrixBaseSolver; // NOLINT
288typedef TypedLinearSolver<CompressedRowSparseMatrix> CompressedRowSparseMatrixSolver; // NOLINT
289typedef TypedLinearSolver<DenseSparseMatrix> DenseSparseMatrixSolver; // NOLINT
290typedef TypedLinearSolver<TripletSparseMatrix> TripletSparseMatrixSolver; // NOLINT
291
292} // namespace internal
293} // namespace ceres
294
295#endif // CERES_INTERNAL_LINEAR_SOLVER_H_