Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 1 | // 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> |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 38 | #include <map> |
Sameer Agarwal | a427c87 | 2013-06-24 17:50:56 -0700 | [diff] [blame] | 39 | #include <string> |
Sameer Agarwal | 0c52f1e | 2012-09-17 11:30:14 -0700 | [diff] [blame] | 40 | #include <vector> |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 41 | #include "ceres/block_sparse_matrix.h" |
| 42 | #include "ceres/casts.h" |
| 43 | #include "ceres/compressed_row_sparse_matrix.h" |
| 44 | #include "ceres/dense_sparse_matrix.h" |
Sameer Agarwal | bdd87c0 | 2013-01-29 16:24:31 -0800 | [diff] [blame] | 45 | #include "ceres/execution_summary.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 46 | #include "ceres/triplet_sparse_matrix.h" |
| 47 | #include "ceres/types.h" |
Sameer Agarwal | 509f68c | 2013-02-20 01:39:03 -0800 | [diff] [blame] | 48 | #include "glog/logging.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 49 | |
| 50 | namespace ceres { |
| 51 | namespace internal { |
| 52 | |
Sameer Agarwal | 79bde35 | 2013-11-21 21:33:51 -0800 | [diff] [blame] | 53 | enum LinearSolverTerminationType { |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 54 | // Termination criterion was met. |
| 55 | LINEAR_SOLVER_SUCCESS, |
Sameer Agarwal | 79bde35 | 2013-11-21 21:33:51 -0800 | [diff] [blame] | 56 | |
| 57 | // Solver ran for max_num_iterations and terminated before the |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 58 | // termination tolerance could be satisfied. |
| 59 | LINEAR_SOLVER_NO_CONVERGENCE, |
Sameer Agarwal | 79bde35 | 2013-11-21 21:33:51 -0800 | [diff] [blame] | 60 | |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 61 | // Solver was terminated due to numerical problems, generally due to |
| 62 | // the linear system being poorly conditioned. |
| 63 | LINEAR_SOLVER_FAILURE, |
Sameer Agarwal | 79bde35 | 2013-11-21 21:33:51 -0800 | [diff] [blame] | 64 | |
Sameer Agarwal | 5fd4806 | 2013-12-02 12:16:53 -0800 | [diff] [blame] | 65 | // Solver failed with a fatal error that cannot be recovered from, |
| 66 | // e.g. CHOLMOD ran out of memory when computing the symbolic or |
| 67 | // numeric factorization or an underlying library was called with |
| 68 | // the wrong arguments. |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 69 | LINEAR_SOLVER_FATAL_ERROR |
Sameer Agarwal | 79bde35 | 2013-11-21 21:33:51 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 73 | class LinearOperator; |
| 74 | |
| 75 | // Abstract base class for objects that implement algorithms for |
| 76 | // solving linear systems |
| 77 | // |
| 78 | // Ax = b |
| 79 | // |
| 80 | // It is expected that a single instance of a LinearSolver object |
Sameer Agarwal | a9d8ef8 | 2012-05-14 02:28:05 -0700 | [diff] [blame] | 81 | // maybe used multiple times for solving multiple linear systems with |
| 82 | // the same sparsity structure. This allows them to cache and reuse |
| 83 | // information across solves. This means that calling Solve on the |
| 84 | // same LinearSolver instance with two different linear systems will |
| 85 | // result in undefined behaviour. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 86 | // |
| 87 | // Subclasses of LinearSolver use two structs to configure themselves. |
| 88 | // The Options struct configures the LinearSolver object for its |
| 89 | // lifetime. The PerSolveOptions struct is used to specify options for |
| 90 | // a particular Solve call. |
| 91 | class LinearSolver { |
| 92 | public: |
| 93 | struct Options { |
| 94 | Options() |
| 95 | : type(SPARSE_NORMAL_CHOLESKY), |
| 96 | preconditioner_type(JACOBI), |
Sameer Agarwal | f06b9fa | 2013-10-27 21:38:13 -0700 | [diff] [blame] | 97 | visibility_clustering_type(CANONICAL_VIEWS), |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 98 | dense_linear_algebra_library_type(EIGEN), |
| 99 | sparse_linear_algebra_library_type(SUITE_SPARSE), |
Sameer Agarwal | 9189f4e | 2013-04-19 17:09:49 -0700 | [diff] [blame] | 100 | use_postordering(false), |
Richard Stebbing | 3253078 | 2014-04-26 07:42:23 +0100 | [diff] [blame] | 101 | dynamic_sparsity(false), |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 102 | min_num_iterations(1), |
| 103 | max_num_iterations(1), |
| 104 | num_threads(1), |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 105 | residual_reset_period(10), |
Sameer Agarwal | 31730ef | 2013-02-28 11:20:28 -0800 | [diff] [blame] | 106 | row_block_size(Eigen::Dynamic), |
| 107 | e_block_size(Eigen::Dynamic), |
| 108 | f_block_size(Eigen::Dynamic) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | LinearSolverType type; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 112 | PreconditionerType preconditioner_type; |
Sameer Agarwal | f06b9fa | 2013-10-27 21:38:13 -0700 | [diff] [blame] | 113 | VisibilityClusteringType visibility_clustering_type; |
Sameer Agarwal | 367b65e | 2013-08-09 10:35:37 -0700 | [diff] [blame] | 114 | DenseLinearAlgebraLibraryType dense_linear_algebra_library_type; |
| 115 | SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type; |
Sameer Agarwal | b051873 | 2012-05-29 00:27:57 -0700 | [diff] [blame] | 116 | |
Sameer Agarwal | 9189f4e | 2013-04-19 17:09:49 -0700 | [diff] [blame] | 117 | // See solver.h for information about this flag. |
| 118 | bool use_postordering; |
Richard Stebbing | 3253078 | 2014-04-26 07:42:23 +0100 | [diff] [blame] | 119 | bool dynamic_sparsity; |
Sameer Agarwal | 7a3c43b | 2012-06-05 23:10:59 -0700 | [diff] [blame] | 120 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 121 | // Number of internal iterations that the solver uses. This |
| 122 | // parameter only makes sense for iterative solvers like CG. |
| 123 | int min_num_iterations; |
| 124 | int max_num_iterations; |
| 125 | |
| 126 | // If possible, how many threads can the solver use. |
| 127 | int num_threads; |
| 128 | |
Sameer Agarwal | 0c52f1e | 2012-09-17 11:30:14 -0700 | [diff] [blame] | 129 | // Hints about the order in which the parameter blocks should be |
| 130 | // eliminated by the linear solver. |
| 131 | // |
| 132 | // For example if elimination_groups is a vector of size k, then |
| 133 | // the linear solver is informed that it should eliminate the |
Sameer Agarwal | 931c309 | 2013-02-25 09:46:21 -0800 | [diff] [blame] | 134 | // parameter blocks 0 ... elimination_groups[0] - 1 first, and |
| 135 | // then elimination_groups[0] ... elimination_groups[1] - 1 and so |
| 136 | // on. Within each elimination group, the linear solver is free to |
| 137 | // choose how the parameter blocks are ordered. Different linear |
| 138 | // solvers have differing requirements on elimination_groups. |
Sameer Agarwal | 0c52f1e | 2012-09-17 11:30:14 -0700 | [diff] [blame] | 139 | // |
| 140 | // The most common use is for Schur type solvers, where there |
| 141 | // should be at least two elimination groups and the first |
| 142 | // elimination group must form an independent set in the normal |
| 143 | // equations. The first elimination group corresponds to the |
| 144 | // num_eliminate_blocks in the Schur type solvers. |
| 145 | vector<int> elimination_groups; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 146 | |
| 147 | // Iterative solvers, e.g. Preconditioned Conjugate Gradients |
| 148 | // maintain a cheap estimate of the residual which may become |
| 149 | // inaccurate over time. Thus for non-zero values of this |
| 150 | // parameter, the solver can be told to recalculate the value of |
| 151 | // the residual using a |b - Ax| evaluation. |
| 152 | int residual_reset_period; |
| 153 | |
| 154 | // If the block sizes in a BlockSparseMatrix are fixed, then in |
| 155 | // some cases the Schur complement based solvers can detect and |
| 156 | // specialize on them. |
| 157 | // |
| 158 | // It is expected that these parameters are set programmatically |
| 159 | // rather than manually. |
| 160 | // |
Sameer Agarwal | a9d8ef8 | 2012-05-14 02:28:05 -0700 | [diff] [blame] | 161 | // Please see schur_complement_solver.h and schur_eliminator.h for |
| 162 | // more details. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 163 | int row_block_size; |
| 164 | int e_block_size; |
| 165 | int f_block_size; |
| 166 | }; |
| 167 | |
| 168 | // Options for the Solve method. |
| 169 | struct PerSolveOptions { |
| 170 | PerSolveOptions() |
| 171 | : D(NULL), |
| 172 | preconditioner(NULL), |
| 173 | r_tolerance(0.0), |
| 174 | q_tolerance(0.0) { |
| 175 | } |
| 176 | |
| 177 | // This option only makes sense for unsymmetric linear solvers |
| 178 | // that can solve rectangular linear systems. |
| 179 | // |
| 180 | // Given a matrix A, an optional diagonal matrix D as a vector, |
| 181 | // and a vector b, the linear solver will solve for |
| 182 | // |
| 183 | // | A | x = | b | |
| 184 | // | D | | 0 | |
| 185 | // |
| 186 | // If D is null, then it is treated as zero, and the solver returns |
| 187 | // the solution to |
| 188 | // |
| 189 | // A x = b |
| 190 | // |
| 191 | // In either case, x is the vector that solves the following |
| 192 | // optimization problem. |
| 193 | // |
Keir Mierle | f7898fb | 2012-05-05 20:55:08 -0700 | [diff] [blame] | 194 | // arg min_x ||Ax - b||^2 + ||Dx||^2 |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 195 | // |
| 196 | // Here A is a matrix of size m x n, with full column rank. If A |
| 197 | // does not have full column rank, the results returned by the |
| 198 | // solver cannot be relied on. D, if it is not null is an array of |
| 199 | // size n. b is an array of size m and x is an array of size n. |
| 200 | double * D; |
| 201 | |
| 202 | // This option only makes sense for iterative solvers. |
| 203 | // |
| 204 | // In general the performance of an iterative linear solver |
| 205 | // depends on the condition number of the matrix A. For example |
| 206 | // the convergence rate of the conjugate gradients algorithm |
| 207 | // is proportional to the square root of the condition number. |
| 208 | // |
| 209 | // One particularly useful technique for improving the |
| 210 | // conditioning of a linear system is to precondition it. In its |
| 211 | // simplest form a preconditioner is a matrix M such that instead |
| 212 | // of solving Ax = b, we solve the linear system AM^{-1} y = b |
| 213 | // instead, where M is such that the condition number k(AM^{-1}) |
| 214 | // is smaller than the conditioner k(A). Given the solution to |
| 215 | // this system, x = M^{-1} y. The iterative solver takes care of |
| 216 | // the mechanics of solving the preconditioned system and |
| 217 | // returning the corrected solution x. The user only needs to |
| 218 | // supply a linear operator. |
| 219 | // |
| 220 | // A null preconditioner is equivalent to an identity matrix being |
| 221 | // used a preconditioner. |
| 222 | LinearOperator* preconditioner; |
| 223 | |
| 224 | |
| 225 | // The following tolerance related options only makes sense for |
| 226 | // iterative solvers. Direct solvers ignore them. |
| 227 | |
| 228 | // Solver terminates when |
| 229 | // |
| 230 | // |Ax - b| <= r_tolerance * |b|. |
| 231 | // |
| 232 | // This is the most commonly used termination criterion for |
| 233 | // iterative solvers. |
| 234 | double r_tolerance; |
| 235 | |
| 236 | // For PSD matrices A, let |
| 237 | // |
| 238 | // Q(x) = x'Ax - 2b'x |
| 239 | // |
| 240 | // be the cost of the quadratic function defined by A and b. Then, |
| 241 | // the solver terminates at iteration i if |
| 242 | // |
| 243 | // i * (Q(x_i) - Q(x_i-1)) / Q(x_i) < q_tolerance. |
| 244 | // |
| 245 | // This termination criterion is more useful when using CG to |
| 246 | // solve the Newton step. This particular convergence test comes |
| 247 | // from Stephen Nash's work on truncated Newton |
| 248 | // methods. References: |
| 249 | // |
| 250 | // 1. Stephen G. Nash & Ariela Sofer, Assessing A Search |
| 251 | // Direction Within A Truncated Newton Method, Operation |
| 252 | // Research Letters 9(1990) 219-221. |
| 253 | // |
| 254 | // 2. Stephen G. Nash, A Survey of Truncated Newton Methods, |
| 255 | // Journal of Computational and Applied Mathematics, |
| 256 | // 124(1-2), 45-59, 2000. |
| 257 | // |
| 258 | double q_tolerance; |
| 259 | }; |
| 260 | |
| 261 | // Summary of a call to the Solve method. We should move away from |
| 262 | // the true/false method for determining solver success. We should |
| 263 | // let the summary object do the talking. |
| 264 | struct Summary { |
| 265 | Summary() |
| 266 | : residual_norm(0.0), |
| 267 | num_iterations(-1), |
Sameer Agarwal | 33e01b9 | 2013-11-27 10:24:03 -0800 | [diff] [blame] | 268 | termination_type(LINEAR_SOLVER_FAILURE) { |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | double residual_norm; |
| 272 | int num_iterations; |
| 273 | LinearSolverTerminationType termination_type; |
Sameer Agarwal | 89a592f | 2013-11-26 11:35:49 -0800 | [diff] [blame] | 274 | string message; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | virtual ~LinearSolver(); |
| 278 | |
| 279 | // Solve Ax = b. |
| 280 | virtual Summary Solve(LinearOperator* A, |
| 281 | const double* b, |
| 282 | const PerSolveOptions& per_solve_options, |
| 283 | double* x) = 0; |
| 284 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 285 | // The following two methods return copies instead of references so |
| 286 | // that the base class implementation does not have to worry about |
| 287 | // life time issues. Further, these calls are not expected to be |
| 288 | // frequent or performance sensitive. |
| 289 | virtual map<string, int> CallStatistics() const { |
| 290 | return map<string, int>(); |
| 291 | } |
| 292 | |
| 293 | virtual map<string, double> TimeStatistics() const { |
| 294 | return map<string, double>(); |
| 295 | } |
| 296 | |
Sameer Agarwal | a9d8ef8 | 2012-05-14 02:28:05 -0700 | [diff] [blame] | 297 | // Factory |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 298 | static LinearSolver* Create(const Options& options); |
| 299 | }; |
| 300 | |
| 301 | // This templated subclass of LinearSolver serves as a base class for |
| 302 | // other linear solvers that depend on the particular matrix layout of |
| 303 | // the underlying linear operator. For example some linear solvers |
| 304 | // need low level access to the TripletSparseMatrix implementing the |
| 305 | // LinearOperator interface. This class hides those implementation |
| 306 | // details behind a private virtual method, and has the Solve method |
| 307 | // perform the necessary upcasting. |
| 308 | template <typename MatrixType> |
| 309 | class TypedLinearSolver : public LinearSolver { |
| 310 | public: |
| 311 | virtual ~TypedLinearSolver() {} |
| 312 | virtual LinearSolver::Summary Solve( |
| 313 | LinearOperator* A, |
| 314 | const double* b, |
| 315 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 316 | double* x) { |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 317 | ScopedExecutionTimer total_time("LinearSolver::Solve", &execution_summary_); |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 318 | CHECK_NOTNULL(A); |
| 319 | CHECK_NOTNULL(b); |
| 320 | CHECK_NOTNULL(x); |
| 321 | return SolveImpl(down_cast<MatrixType*>(A), b, per_solve_options, x); |
| 322 | } |
| 323 | |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 324 | virtual map<string, int> CallStatistics() const { |
| 325 | return execution_summary_.calls(); |
| 326 | } |
| 327 | |
| 328 | virtual map<string, double> TimeStatistics() const { |
| 329 | return execution_summary_.times(); |
| 330 | } |
| 331 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 332 | private: |
| 333 | virtual LinearSolver::Summary SolveImpl( |
| 334 | MatrixType* A, |
| 335 | const double* b, |
| 336 | const LinearSolver::PerSolveOptions& per_solve_options, |
| 337 | double* x) = 0; |
Sameer Agarwal | 42a84b8 | 2013-02-01 12:22:53 -0800 | [diff] [blame] | 338 | |
| 339 | ExecutionSummary execution_summary_; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 340 | }; |
| 341 | |
| 342 | // Linear solvers that depend on acccess to the low level structure of |
| 343 | // a SparseMatrix. |
| 344 | typedef TypedLinearSolver<BlockSparseMatrix> BlockSparseMatrixSolver; // NOLINT |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 345 | typedef TypedLinearSolver<CompressedRowSparseMatrix> CompressedRowSparseMatrixSolver; // NOLINT |
| 346 | typedef TypedLinearSolver<DenseSparseMatrix> DenseSparseMatrixSolver; // NOLINT |
| 347 | typedef TypedLinearSolver<TripletSparseMatrix> TripletSparseMatrixSolver; // NOLINT |
| 348 | |
| 349 | } // namespace internal |
| 350 | } // namespace ceres |
| 351 | |
| 352 | #endif // CERES_INTERNAL_LINEAR_SOLVER_H_ |