blob: 2c0f0e5a836c631add52d9b14ab5146de30e55c3 [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// Enums and other top level class definitions.
32//
33// Note: internal/types.cc defines stringification routines for some
34// of these enums. Please update those routines if you extend or
35// remove enums from here.
36
37#ifndef CERES_PUBLIC_TYPES_H_
38#define CERES_PUBLIC_TYPES_H_
39
Sameer Agarwalcbae8562012-09-02 13:50:43 -070040#include "ceres/internal/port.h"
41
Keir Mierle8ebb0732012-04-30 23:09:08 -070042namespace ceres {
43
44// Basic integer types. These typedefs are in the Ceres namespace to avoid
45// conflicts with other packages having similar typedefs.
46typedef short int16;
47typedef int int32;
48
49// Argument type used in interfaces that can optionally take ownership
50// of a passed in argument. If TAKE_OWNERSHIP is passed, the called
51// object takes ownership of the pointer argument, and will call
52// delete on it upon completion.
53enum Ownership {
54 DO_NOT_TAKE_OWNERSHIP,
55 TAKE_OWNERSHIP
56};
57
58// TODO(keir): Considerably expand the explanations of each solver type.
59enum LinearSolverType {
60 // These solvers are for general rectangular systems formed from the
61 // normal equations A'A x = A'b. They are direct solvers and do not
62 // assume any special problem structure.
63
Sameer Agarwalb9f15a52012-08-18 13:06:19 -070064 // Solve the normal equations using a dense Cholesky solver; based
65 // on Eigen.
66 DENSE_NORMAL_CHOLESKY,
Keir Mierle8ebb0732012-04-30 23:09:08 -070067
68 // Solve the normal equations using a dense QR solver; based on
69 // Eigen.
70 DENSE_QR,
71
Sameer Agarwalb9f15a52012-08-18 13:06:19 -070072 // Solve the normal equations using a sparse cholesky solver; requires
73 // SuiteSparse or CXSparse.
74 SPARSE_NORMAL_CHOLESKY,
75
Keir Mierle8ebb0732012-04-30 23:09:08 -070076 // Specialized solvers, specific to problems with a generalized
77 // bi-partitite structure.
78
79 // Solves the reduced linear system using a dense Cholesky solver;
80 // based on Eigen.
81 DENSE_SCHUR,
82
83 // Solves the reduced linear system using a sparse Cholesky solver;
84 // based on CHOLMOD.
85 SPARSE_SCHUR,
86
87 // Solves the reduced linear system using Conjugate Gradients, based
88 // on a new Ceres implementation. Suitable for large scale
89 // problems.
90 ITERATIVE_SCHUR,
91
Keir Mierlee2a6cdc2012-05-07 06:39:56 -070092 // Conjugate gradients on the normal equations.
93 CGNR
Keir Mierle8ebb0732012-04-30 23:09:08 -070094};
95
96enum PreconditionerType {
97 // Trivial preconditioner - the identity matrix.
98 IDENTITY,
99
100 // Block diagonal of the Gauss-Newton Hessian.
101 JACOBI,
102
103 // Block diagonal of the Schur complement. This preconditioner may
104 // only be used with the ITERATIVE_SCHUR solver. Requires
105 // SuiteSparse/CHOLMOD.
106 SCHUR_JACOBI,
107
108 // Visibility clustering based preconditioners.
109 //
110 // These preconditioners are well suited for Structure from Motion
111 // problems, particularly problems arising from community photo
112 // collections. These preconditioners use the visibility structure
113 // of the scene to determine the sparsity structure of the
114 // preconditioner. Requires SuiteSparse/CHOLMOD.
115 CLUSTER_JACOBI,
116 CLUSTER_TRIDIAGONAL
117};
118
Sameer Agarwalb0518732012-05-29 00:27:57 -0700119enum SparseLinearAlgebraLibraryType {
120 // High performance sparse Cholesky factorization and approximate
121 // minimum degree ordering.
122 SUITE_SPARSE,
123
124 // A lightweight replacment for SuiteSparse.
125 CX_SPARSE
126};
127
Keir Mierle8ebb0732012-04-30 23:09:08 -0700128enum LinearSolverTerminationType {
129 // Termination criterion was met. For factorization based solvers
130 // the tolerance is assumed to be zero. Any user provided values are
131 // ignored.
132 TOLERANCE,
133
134 // Solver ran for max_num_iterations and terminated before the
135 // termination tolerance could be satified.
136 MAX_ITERATIONS,
137
138 // Solver is stuck and further iterations will not result in any
139 // measurable progress.
140 STAGNATION,
141
142 // Solver failed. Solver was terminated due to numerical errors. The
143 // exact cause of failure depends on the particular solver being
144 // used.
145 FAILURE
146};
147
Keir Mierle8ebb0732012-04-30 23:09:08 -0700148// Logging options
149// The options get progressively noisier.
150enum LoggingType {
151 SILENT,
Sameer Agarwal64472192012-05-03 21:53:07 -0700152 PER_MINIMIZER_ITERATION
Keir Mierle8ebb0732012-04-30 23:09:08 -0700153};
154
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800155enum MinimizerType {
156 LINE_SEARCH,
157 TRUST_REGION
158};
159
160enum LineSearchDirectionType {
161 // Negative of the gradient.
162 STEEPEST_DESCENT,
163
164 // A generalization of the Conjugate Gradient method to non-linear
165 // functions. The generalization can be performed in a number of
166 // different ways, resulting in a variety of search directions. The
167 // precise choice of the non-linear conjugate gradient algorithm
Sameer Agarwalc89ea4b2013-01-09 16:09:35 -0800168 // used is determined by NonlinerConjuateGradientType.
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800169 NONLINEAR_CONJUGATE_GRADIENT,
Sameer Agarwal3e8d1922012-11-28 17:20:22 -0800170
171 // A limited memory approximation to the inverse Hessian is
172 // maintained and used to compute a quasi-Newton step.
173 //
174 // For more details see
175 //
176 // Nocedal, J. (1980). "Updating Quasi-Newton Matrices with Limited
177 // Storage". Mathematics of Computation 35 (151): 773–782.
178 //
179 // Byrd, R. H.; Nocedal, J.; Schnabel, R. B. (1994).
180 // "Representations of Quasi-Newton Matrices and their use in
181 // Limited Memory Methods". Mathematical Programming 63 (4):
182 // 129–156.
183 LBFGS,
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800184};
185
186// Nonliner conjugate gradient methods are a generalization of the
187// method of Conjugate Gradients for linear systems. The
188// generalization can be carried out in a number of different ways
189// leading to number of different rules for computing the search
190// direction. Ceres provides a number of different variants. For more
191// details see Numerical Optimization by Nocedal & Wright.
192enum NonlinearConjugateGradientType {
193 FLETCHER_REEVES,
194 POLAK_RIBIRERE,
Sameer Agarwal1afd4982012-11-29 10:28:11 -0800195 HESTENES_STIEFEL,
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800196};
197
198enum LineSearchType {
199 // Backtracking line search with polynomial interpolation or
200 // bisection.
Sameer Agarwal1afd4982012-11-29 10:28:11 -0800201 ARMIJO,
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800202};
203
Sameer Agarwalfa015192012-06-11 14:21:42 -0700204// Ceres supports different strategies for computing the trust region
205// step.
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700206enum TrustRegionStrategyType {
Sameer Agarwalfa015192012-06-11 14:21:42 -0700207 // The default trust region strategy is to use the step computation
208 // used in the Levenberg-Marquardt algorithm. For more details see
209 // levenberg_marquardt_strategy.h
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700210 LEVENBERG_MARQUARDT,
Sameer Agarwalfa015192012-06-11 14:21:42 -0700211
212 // Powell's dogleg algorithm interpolates between the Cauchy point
213 // and the Gauss-Newton step. It is particularly useful if the
214 // LEVENBERG_MARQUARDT algorithm is making a large number of
215 // unsuccessful steps. For more details see dogleg_strategy.h.
216 //
217 // NOTES:
218 //
219 // 1. This strategy has not been experimented with or tested as
220 // extensively as LEVENBERG_MARQUARDT, and therefore it should be
221 // considered EXPERIMENTAL for now.
222 //
223 // 2. For now this strategy should only be used with exact
224 // factorization based linear solvers, i.e., SPARSE_SCHUR,
225 // DENSE_SCHUR, DENSE_QR and SPARSE_NORMAL_CHOLESKY.
226 DOGLEG
Keir Mierle8ebb0732012-04-30 23:09:08 -0700227};
228
Markus Moll51cf7cb2012-08-20 20:10:20 +0200229// Ceres supports two different dogleg strategies.
230// The "traditional" dogleg method by Powell and the
231// "subspace" method described in
232// R. H. Byrd, R. B. Schnabel, and G. A. Shultz,
233// "Approximate solution of the trust region problem by minimization
234// over two-dimensional subspaces", Mathematical Programming,
235// 40 (1988), pp. 247--263
236enum DoglegType {
237 // The traditional approach constructs a dogleg path
238 // consisting of two line segments and finds the furthest
239 // point on that path that is still inside the trust region.
240 TRADITIONAL_DOGLEG,
241
242 // The subspace approach finds the exact minimum of the model
243 // constrained to the subspace spanned by the dogleg path.
244 SUBSPACE_DOGLEG
245};
246
Keir Mierle8ebb0732012-04-30 23:09:08 -0700247enum SolverTerminationType {
248 // The minimizer did not run at all; usually due to errors in the user's
249 // Problem or the solver options.
250 DID_NOT_RUN,
251
252 // The solver ran for maximum number of iterations specified by the
253 // user, but none of the convergence criterion specified by the user
254 // were met.
255 NO_CONVERGENCE,
256
257 // Minimizer terminated because
258 // (new_cost - old_cost) < function_tolerance * old_cost;
259 FUNCTION_TOLERANCE,
260
261 // Minimizer terminated because
262 // max_i |gradient_i| < gradient_tolerance * max_i|initial_gradient_i|
263 GRADIENT_TOLERANCE,
264
265 // Minimized terminated because
266 // |step|_2 <= parameter_tolerance * ( |x|_2 + parameter_tolerance)
267 PARAMETER_TOLERANCE,
268
269 // The minimizer terminated because it encountered a numerical error
270 // that it could not recover from.
271 NUMERICAL_FAILURE,
272
273 // Using an IterationCallback object, user code can control the
274 // minimizer. The following enums indicate that the user code was
275 // responsible for termination.
276
277 // User's IterationCallback returned SOLVER_ABORT.
278 USER_ABORT,
279
280 // User's IterationCallback returned SOLVER_TERMINATE_SUCCESSFULLY
Sameer Agarwal64472192012-05-03 21:53:07 -0700281 USER_SUCCESS
Keir Mierle8ebb0732012-04-30 23:09:08 -0700282};
283
284// Enums used by the IterationCallback instances to indicate to the
285// solver whether it should continue solving, the user detected an
286// error or the solution is good enough and the solver should
287// terminate.
288enum CallbackReturnType {
289 // Continue solving to next iteration.
290 SOLVER_CONTINUE,
291
292 // Terminate solver, and do not update the parameter blocks upon
293 // return. Unless the user has set
294 // Solver:Options:::update_state_every_iteration, in which case the
295 // state would have been updated every iteration
296 // anyways. Solver::Summary::termination_type is set to USER_ABORT.
297 SOLVER_ABORT,
298
299 // Terminate solver, update state and
300 // return. Solver::Summary::termination_type is set to USER_SUCCESS.
301 SOLVER_TERMINATE_SUCCESSFULLY
302};
303
Sameer Agarwal82f4b882012-05-06 21:05:28 -0700304// The format in which linear least squares problems should be logged
305// when Solver::Options::lsqp_iterations_to_dump is non-empty.
306enum DumpFormatType {
307 // Print the linear least squares problem in a human readable format
308 // to stderr. The Jacobian is printed as a dense matrix. The vectors
309 // D, x and f are printed as dense vectors. This should only be used
310 // for small problems.
311 CONSOLE,
312
313 // Write out the linear least squares problem to the directory
314 // pointed to by Solver::Options::lsqp_dump_directory as a protocol
315 // buffer. linear_least_squares_problems.h/cc contains routines for
316 // loading these problems. For details on the on disk format used,
317 // see matrix.proto. The files are named lm_iteration_???.lsqp.
318 PROTOBUF,
319
320 // Write out the linear least squares problem to the directory
321 // pointed to by Solver::Options::lsqp_dump_directory as text files
322 // which can be read into MATLAB/Octave. The Jacobian is dumped as a
323 // text file containing (i,j,s) triplets, the vectors D, x and f are
324 // dumped as text files containing a list of their values.
325 //
326 // A MATLAB/octave script called lm_iteration_???.m is also output,
327 // which can be used to parse and load the problem into memory.
328 TEXTFILE
329};
330
Keir Mierlefdeb5772012-05-09 07:38:07 -0700331// For SizedCostFunction and AutoDiffCostFunction, DYNAMIC can be specified for
332// the number of residuals. If specified, then the number of residuas for that
333// cost function can vary at runtime.
334enum DimensionType {
335 DYNAMIC = -1
336};
337
Sameer Agarwal2fc0ed62013-01-15 11:34:10 -0800338enum NumericDiffMethod {
339 CENTRAL,
340 FORWARD
341};
342
Keir Mierle8ebb0732012-04-30 23:09:08 -0700343const char* LinearSolverTypeToString(LinearSolverType type);
Sameer Agarwalcbae8562012-09-02 13:50:43 -0700344bool StringToLinearSolverType(string value, LinearSolverType* type);
345
Keir Mierle8ebb0732012-04-30 23:09:08 -0700346const char* PreconditionerTypeToString(PreconditionerType type);
Sameer Agarwalcbae8562012-09-02 13:50:43 -0700347bool StringToPreconditionerType(string value, PreconditionerType* type);
348
Sameer Agarwalb0518732012-05-29 00:27:57 -0700349const char* SparseLinearAlgebraLibraryTypeToString(
350 SparseLinearAlgebraLibraryType type);
Sameer Agarwalcbae8562012-09-02 13:50:43 -0700351bool StringToSparseLinearAlgebraLibraryType(
352 string value,
353 SparseLinearAlgebraLibraryType* type);
354
Sameer Agarwalcbae8562012-09-02 13:50:43 -0700355const char* TrustRegionStrategyTypeToString(TrustRegionStrategyType type);
356bool StringToTrustRegionStrategyType(string value,
357 TrustRegionStrategyType* type);
358
359const char* DoglegTypeToString(DoglegType type);
360bool StringToDoglegType(string value, DoglegType* type);
361
Sameer Agarwal1afd4982012-11-29 10:28:11 -0800362const char* MinimizerTypeToString(MinimizerType type);
363bool StringToMinimizerType(string value, MinimizerType* type);
364
365const char* LineSearchDirectionTypeToString(LineSearchDirectionType type);
366bool StringToLineSearchDirectionType(string value,
367 LineSearchDirectionType* type);
368
369const char* LineSearchTypeToString(LineSearchType type);
370bool StringToLineSearchType(string value, LineSearchType* type);
371
372const char* NonlinearConjugateGradientTypeToString(
373 NonlinearConjugateGradientType type);
374bool StringToNonlinearConjugateGradientType(
375 string value, NonlinearConjugateGradientType* type);
376
Keir Mierle8ebb0732012-04-30 23:09:08 -0700377const char* LinearSolverTerminationTypeToString(
378 LinearSolverTerminationType type);
Sameer Agarwalcbae8562012-09-02 13:50:43 -0700379
Keir Mierle8ebb0732012-04-30 23:09:08 -0700380const char* SolverTerminationTypeToString(SolverTerminationType type);
Sameer Agarwal14ee7952012-09-06 11:05:32 -0700381
Keir Mierle8ebb0732012-04-30 23:09:08 -0700382bool IsSchurType(LinearSolverType type);
Sameer Agarwal14ee7952012-09-06 11:05:32 -0700383bool IsSparseLinearAlgebraLibraryTypeAvailable(
384 SparseLinearAlgebraLibraryType type);
385
Keir Mierle8ebb0732012-04-30 23:09:08 -0700386
387} // namespace ceres
388
389#endif // CERES_PUBLIC_TYPES_H_