2012-04-30 23:09:08 -07:00
|
|
|
// Ceres Solver - A fast non-linear least squares minimizer
|
|
|
|
|
// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
|
|
|
|
|
// http://code.google.com/p/ceres-solver/
|
|
|
|
|
//
|
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
|
//
|
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
|
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
|
|
|
// used to endorse or promote products derived from this software without
|
|
|
|
|
// specific prior written permission.
|
|
|
|
|
//
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
//
|
|
|
|
|
// Author: sameeragarwal@google.com (Sameer Agarwal)
|
|
|
|
|
|
|
|
|
|
#ifndef CERES_PUBLIC_SOLVER_H_
|
|
|
|
|
#define CERES_PUBLIC_SOLVER_H_
|
|
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2012-07-02 12:44:34 -07:00
|
|
|
#include "ceres/crs_matrix.h"
|
2012-04-30 23:09:08 -07:00
|
|
|
#include "ceres/internal/macros.h"
|
|
|
|
|
#include "ceres/internal/port.h"
|
2012-07-02 12:44:34 -07:00
|
|
|
#include "ceres/iteration_callback.h"
|
2012-04-30 23:09:08 -07:00
|
|
|
#include "ceres/types.h"
|
|
|
|
|
|
|
|
|
|
namespace ceres {
|
|
|
|
|
|
2012-09-10 17:41:38 -07:00
|
|
|
class Ordering;
|
2012-04-30 23:09:08 -07:00
|
|
|
class Problem;
|
|
|
|
|
|
|
|
|
|
// Interface for non-linear least squares solvers.
|
|
|
|
|
class Solver {
|
|
|
|
|
public:
|
|
|
|
|
virtual ~Solver();
|
|
|
|
|
|
|
|
|
|
// The options structure contains, not surprisingly, options that control how
|
|
|
|
|
// the solver operates. The defaults should be suitable for a wide range of
|
|
|
|
|
// problems; however, better performance is often obtainable with tweaking.
|
|
|
|
|
//
|
|
|
|
|
// The constants are defined inside types.h
|
|
|
|
|
struct Options {
|
|
|
|
|
// Default constructor that sets up a generic sparse problem.
|
|
|
|
|
Options() {
|
2012-05-29 17:40:17 -07:00
|
|
|
trust_region_strategy_type = LEVENBERG_MARQUARDT;
|
2012-08-20 20:10:20 +02:00
|
|
|
dogleg_type = TRADITIONAL_DOGLEG;
|
2012-08-08 10:38:31 -07:00
|
|
|
use_nonmonotonic_steps = false;
|
|
|
|
|
max_consecutive_nonmonotonic_steps = 5;
|
2012-04-30 23:09:08 -07:00
|
|
|
max_num_iterations = 50;
|
2012-06-11 14:21:42 -07:00
|
|
|
max_solver_time_in_seconds = 1e9;
|
2012-04-30 23:09:08 -07:00
|
|
|
num_threads = 1;
|
2012-05-29 17:40:17 -07:00
|
|
|
initial_trust_region_radius = 1e4;
|
|
|
|
|
max_trust_region_radius = 1e16;
|
|
|
|
|
min_trust_region_radius = 1e-32;
|
2012-04-30 23:09:08 -07:00
|
|
|
min_relative_decrease = 1e-3;
|
2012-05-29 17:40:17 -07:00
|
|
|
lm_min_diagonal = 1e-6;
|
|
|
|
|
lm_max_diagonal = 1e32;
|
|
|
|
|
max_num_consecutive_invalid_steps = 5;
|
2012-04-30 23:09:08 -07:00
|
|
|
function_tolerance = 1e-6;
|
|
|
|
|
gradient_tolerance = 1e-10;
|
|
|
|
|
parameter_tolerance = 1e-8;
|
2012-05-29 00:27:57 -07:00
|
|
|
|
|
|
|
|
#if defined(CERES_NO_SUITESPARSE) && defined(CERES_NO_CXSPARSE)
|
2012-04-30 23:09:08 -07:00
|
|
|
linear_solver_type = DENSE_QR;
|
2012-05-29 00:27:57 -07:00
|
|
|
#else
|
|
|
|
|
linear_solver_type = SPARSE_NORMAL_CHOLESKY;
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-06-17 10:08:19 -07:00
|
|
|
preconditioner_type = JACOBI;
|
|
|
|
|
|
2012-05-29 00:27:57 -07:00
|
|
|
sparse_linear_algebra_library = SUITE_SPARSE;
|
|
|
|
|
#if defined(CERES_NO_SUITESPARSE) && !defined(CERES_NO_CXSPARSE)
|
|
|
|
|
sparse_linear_algebra_library = CX_SPARSE;
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-06-17 10:08:19 -07:00
|
|
|
num_linear_solver_threads = 1;
|
|
|
|
|
|
2012-06-05 23:10:59 -07:00
|
|
|
#if defined(CERES_NO_SUITESPARSE)
|
|
|
|
|
use_block_amd = false;
|
|
|
|
|
#else
|
|
|
|
|
use_block_amd = true;
|
|
|
|
|
#endif
|
2012-09-17 12:06:57 -07:00
|
|
|
ordering = NULL;
|
2012-09-18 21:49:06 -07:00
|
|
|
use_inner_iterations = false;
|
2012-04-30 23:09:08 -07:00
|
|
|
linear_solver_min_num_iterations = 1;
|
|
|
|
|
linear_solver_max_num_iterations = 500;
|
|
|
|
|
eta = 1e-1;
|
|
|
|
|
jacobi_scaling = true;
|
|
|
|
|
logging_type = PER_MINIMIZER_ITERATION;
|
|
|
|
|
minimizer_progress_to_stdout = false;
|
|
|
|
|
return_initial_residuals = false;
|
2012-07-02 12:44:34 -07:00
|
|
|
return_initial_gradient = false;
|
|
|
|
|
return_initial_jacobian = false;
|
2012-04-30 23:09:08 -07:00
|
|
|
return_final_residuals = false;
|
2012-07-02 12:44:34 -07:00
|
|
|
return_final_gradient = false;
|
|
|
|
|
return_final_jacobian = false;
|
2012-05-06 21:05:28 -07:00
|
|
|
lsqp_dump_directory = "/tmp";
|
|
|
|
|
lsqp_dump_format_type = TEXTFILE;
|
2012-04-30 23:09:08 -07:00
|
|
|
check_gradients = false;
|
|
|
|
|
gradient_check_relative_precision = 1e-8;
|
|
|
|
|
numeric_derivative_relative_step_size = 1e-6;
|
|
|
|
|
update_state_every_iteration = false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-17 12:06:57 -07:00
|
|
|
~Options();
|
2012-04-30 23:09:08 -07:00
|
|
|
// Minimizer options ----------------------------------------
|
|
|
|
|
|
2012-05-29 17:40:17 -07:00
|
|
|
TrustRegionStrategyType trust_region_strategy_type;
|
2012-04-30 23:09:08 -07:00
|
|
|
|
2012-08-20 20:10:20 +02:00
|
|
|
// Type of dogleg strategy to use.
|
|
|
|
|
DoglegType dogleg_type;
|
|
|
|
|
|
2012-08-08 10:38:31 -07:00
|
|
|
// The classical trust region methods are descent methods, in that
|
|
|
|
|
// they only accept a point if it strictly reduces the value of
|
|
|
|
|
// the objective function.
|
|
|
|
|
//
|
|
|
|
|
// Relaxing this requirement allows the algorithm to be more
|
|
|
|
|
// efficient in the long term at the cost of some local increase
|
|
|
|
|
// in the value of the objective function.
|
|
|
|
|
//
|
|
|
|
|
// This is because allowing for non-decreasing objective function
|
|
|
|
|
// values in a princpled manner allows the algorithm to "jump over
|
|
|
|
|
// boulders" as the method is not restricted to move into narrow
|
|
|
|
|
// valleys while preserving its convergence properties.
|
|
|
|
|
//
|
|
|
|
|
// Setting use_nonmonotonic_steps to true enables the
|
|
|
|
|
// non-monotonic trust region algorithm as described by Conn,
|
|
|
|
|
// Gould & Toint in "Trust Region Methods", Section 10.1.
|
|
|
|
|
//
|
|
|
|
|
// The parameter max_consecutive_nonmonotonic_steps controls the
|
|
|
|
|
// window size used by the step selection algorithm to accept
|
|
|
|
|
// non-monotonic steps.
|
|
|
|
|
//
|
|
|
|
|
// Even though the value of the objective function may be larger
|
|
|
|
|
// than the minimum value encountered over the course of the
|
|
|
|
|
// optimization, the final parameters returned to the user are the
|
|
|
|
|
// ones corresponding to the minimum cost over all iterations.
|
|
|
|
|
bool use_nonmonotonic_steps;
|
|
|
|
|
int max_consecutive_nonmonotonic_steps;
|
|
|
|
|
|
2012-04-30 23:09:08 -07:00
|
|
|
// Maximum number of iterations for the minimizer to run for.
|
|
|
|
|
int max_num_iterations;
|
|
|
|
|
|
|
|
|
|
// Maximum time for which the minimizer should run for.
|
2012-06-11 14:21:42 -07:00
|
|
|
double max_solver_time_in_seconds;
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
// Number of threads used by Ceres for evaluating the cost and
|
|
|
|
|
// jacobians.
|
|
|
|
|
int num_threads;
|
|
|
|
|
|
2012-05-29 17:40:17 -07:00
|
|
|
// Trust region minimizer settings.
|
|
|
|
|
double initial_trust_region_radius;
|
|
|
|
|
double max_trust_region_radius;
|
|
|
|
|
|
|
|
|
|
// Minimizer terminates when the trust region radius becomes
|
|
|
|
|
// smaller than this value.
|
|
|
|
|
double min_trust_region_radius;
|
2012-05-14 12:41:10 -07:00
|
|
|
|
2012-05-29 17:40:17 -07:00
|
|
|
// Lower bound for the relative decrease before a step is
|
|
|
|
|
// accepted.
|
2012-04-30 23:09:08 -07:00
|
|
|
double min_relative_decrease;
|
|
|
|
|
|
2012-05-29 17:40:17 -07:00
|
|
|
// For the Levenberg-Marquadt algorithm, the scaled diagonal of
|
|
|
|
|
// the normal equations J'J is used to control the size of the
|
|
|
|
|
// trust region. Extremely small and large values along the
|
|
|
|
|
// diagonal can make this regularization scheme
|
|
|
|
|
// fail. lm_max_diagonal and lm_min_diagonal, clamp the values of
|
|
|
|
|
// diag(J'J) from above and below. In the normal course of
|
|
|
|
|
// operation, the user should not have to modify these parameters.
|
|
|
|
|
double lm_min_diagonal;
|
|
|
|
|
double lm_max_diagonal;
|
|
|
|
|
|
|
|
|
|
// Sometimes due to numerical conditioning problems or linear
|
|
|
|
|
// solver flakiness, the trust region strategy may return a
|
|
|
|
|
// numerically invalid step that can be fixed by reducing the
|
|
|
|
|
// trust region size. So the TrustRegionMinimizer allows for a few
|
|
|
|
|
// successive invalid steps before it declares NUMERICAL_FAILURE.
|
|
|
|
|
int max_num_consecutive_invalid_steps;
|
|
|
|
|
|
2012-04-30 23:09:08 -07:00
|
|
|
// Minimizer terminates when
|
|
|
|
|
//
|
|
|
|
|
// (new_cost - old_cost) < function_tolerance * old_cost;
|
|
|
|
|
//
|
|
|
|
|
double function_tolerance;
|
|
|
|
|
|
|
|
|
|
// Minimizer terminates when
|
|
|
|
|
//
|
|
|
|
|
// max_i |gradient_i| < gradient_tolerance * max_i|initial_gradient_i|
|
|
|
|
|
//
|
|
|
|
|
// This value should typically be 1e-4 * function_tolerance.
|
|
|
|
|
double gradient_tolerance;
|
|
|
|
|
|
|
|
|
|
// Minimizer terminates when
|
|
|
|
|
//
|
|
|
|
|
// |step|_2 <= parameter_tolerance * ( |x|_2 + parameter_tolerance)
|
|
|
|
|
//
|
|
|
|
|
double parameter_tolerance;
|
|
|
|
|
|
|
|
|
|
// Linear least squares solver options -------------------------------------
|
|
|
|
|
|
|
|
|
|
LinearSolverType linear_solver_type;
|
|
|
|
|
|
|
|
|
|
// Type of preconditioner to use with the iterative linear solvers.
|
|
|
|
|
PreconditionerType preconditioner_type;
|
|
|
|
|
|
2012-05-29 00:27:57 -07:00
|
|
|
// Ceres supports using multiple sparse linear algebra libraries
|
|
|
|
|
// for sparse matrix ordering and factorizations. Currently,
|
|
|
|
|
// SUITE_SPARSE and CX_SPARSE are the valid choices, depending on
|
|
|
|
|
// whether they are linked into Ceres at build time.
|
|
|
|
|
SparseLinearAlgebraLibraryType sparse_linear_algebra_library;
|
|
|
|
|
|
2012-04-30 23:09:08 -07:00
|
|
|
// Number of threads used by Ceres to solve the Newton
|
|
|
|
|
// step. Currently only the SPARSE_SCHUR solver is capable of
|
|
|
|
|
// using this setting.
|
|
|
|
|
int num_linear_solver_threads;
|
|
|
|
|
|
2012-09-18 21:49:06 -07:00
|
|
|
// The order in which variables are eliminated in a linear solver
|
|
|
|
|
// can have a significant of impact on the efficiency and accuracy
|
|
|
|
|
// of the method. e.g., when doing sparse Cholesky factorization,
|
|
|
|
|
// there are matrices for which a good ordering will give a
|
|
|
|
|
// Cholesky factor with O(n) storage, where as a bad ordering will
|
|
|
|
|
// result in an completely dense factor.
|
|
|
|
|
//
|
|
|
|
|
// Ceres allows the user to provide varying amounts of hints to
|
|
|
|
|
// the solver about the variable elimination ordering to use. This
|
|
|
|
|
// can range from no hints, where the solver is free to decide the
|
|
|
|
|
// best possible ordering based on the user's choices like the
|
|
|
|
|
// linear solver being used, to an exact order in which the
|
|
|
|
|
// variables should be eliminated, and a variety of possibilities
|
|
|
|
|
// in between.
|
|
|
|
|
//
|
|
|
|
|
// Instances of the Ordering class are used to communicate this
|
|
|
|
|
// infornation to Ceres.
|
|
|
|
|
//
|
|
|
|
|
// Formally an ordering is an ordered partitioning of the parameter
|
|
|
|
|
// blocks, i.e, each parameter block belongs to exactly one group, and
|
|
|
|
|
// each group has a unique integer associated with it, that determines
|
|
|
|
|
// its order in the set of groups.
|
|
|
|
|
//
|
|
|
|
|
// Given such an ordering, Ceres ensures that the parameter blocks in
|
|
|
|
|
// the lowest numbered group are eliminated first, and then the
|
|
|
|
|
// parmeter blocks in the next lowest numbered group and so on. Within
|
|
|
|
|
// each group, Ceres is free to order the parameter blocks as it
|
|
|
|
|
// chooses.
|
|
|
|
|
//
|
2012-09-17 12:06:57 -07:00
|
|
|
// If NULL, then all parameter blocks are assumed to be in the
|
|
|
|
|
// same group and the solver is free to decide the best
|
|
|
|
|
// ordering. (See ordering.h for more details).
|
|
|
|
|
Ordering* ordering;
|
2012-04-30 23:09:08 -07:00
|
|
|
|
2012-09-18 21:49:06 -07:00
|
|
|
// Some non-linear least squares problems have additional
|
|
|
|
|
// structure in the way the parameter blocks interact that it is
|
|
|
|
|
// beneficial to modify the way the trust region step is computed.
|
|
|
|
|
//
|
|
|
|
|
// e.g., consider the following regression problem
|
|
|
|
|
//
|
|
|
|
|
// y = a_1 exp(b_1 x) + a_2 exp(b_3 x^2 + c_1)
|
|
|
|
|
//
|
|
|
|
|
// Given a set of pairs{(x_i, y_i)}, the user wishes to estimate
|
|
|
|
|
// a_1, a_2, b_1, b_2, and c_1.
|
|
|
|
|
//
|
|
|
|
|
// Notice here that the expression on the left is linear in a_1
|
|
|
|
|
// and a_2, and given any value for b_1, b_2 and c_1, it is
|
|
|
|
|
// possible to use linear regression to estimate the optimal
|
|
|
|
|
// values of a_1 and a_2. Indeed, its possible to analytically
|
|
|
|
|
// eliminate the variables a_1 and a_2 from the problem all
|
|
|
|
|
// together. Problems like these are known as separable least
|
|
|
|
|
// squares problem and the most famous algorithm for solving them
|
|
|
|
|
// is the Variable Projection algorithm invented by Golub &
|
|
|
|
|
// Pereyra.
|
|
|
|
|
//
|
|
|
|
|
// Similar structure can be found in the matrix factorization with
|
|
|
|
|
// missing data problem. There the corresponding algorithm is
|
|
|
|
|
// known as Wiberg's algorithm.
|
|
|
|
|
//
|
|
|
|
|
// Ruhe & Wedin (Algorithms for Separable Nonlinear Least Squares
|
|
|
|
|
// Problems, SIAM Reviews, 22(3), 1980) present an analyis of
|
|
|
|
|
// various algorithms for solving separable non-linear least
|
|
|
|
|
// squares problems and refer to "Variable Projection" as
|
|
|
|
|
// Algorithm I in their paper.
|
|
|
|
|
//
|
|
|
|
|
// Implementing Variable Projection is tedious and expensive, and
|
|
|
|
|
// they present a simpler algorithm, which they refer to as
|
|
|
|
|
// Algorithm II, where once the Newton/Trust Region step has been
|
|
|
|
|
// computed for the whole problem (a_1, a_2, b_1, b_2, c_1) and
|
|
|
|
|
// additional optimization step is performed to estimate a_1 and
|
|
|
|
|
// a_2 exactly.
|
|
|
|
|
//
|
|
|
|
|
// This idea can be generalized to cases where the residual is not
|
|
|
|
|
// linear in a_1 and a_2, i.e., Solve for the trust region step
|
|
|
|
|
// for the full problem, and then use it as the starting point to
|
|
|
|
|
// further optimize just a_1 and a_2. For the linear case, this
|
|
|
|
|
// amounts to doing a single linear least squares solve. For
|
|
|
|
|
// non-linear problems, any method for solving the a_1 and a_2
|
|
|
|
|
// optimization problems will do. The only constraint on a_1 and
|
|
|
|
|
// a_2 is that they do not co-occur in any residual block.
|
|
|
|
|
//
|
|
|
|
|
// Setting "use_inner_iterations" to true enables the use of this
|
|
|
|
|
// non-linear generalization of Ruhe & Wedin's Algorithm II. This
|
|
|
|
|
// version of Ceres has a higher iteration complexity, but also
|
|
|
|
|
// displays better convergence behaviour per iteration.
|
|
|
|
|
bool use_inner_iterations;
|
|
|
|
|
|
|
|
|
|
// If inner_iterations is true, then the user has two choices.
|
|
|
|
|
//
|
|
|
|
|
// 1. Provide a list of parameter blocks, which should be subject
|
|
|
|
|
// to inner iterations. The only requirement on the set of
|
|
|
|
|
// parameter blocks is that they form an independent set in the
|
|
|
|
|
// Hessian matrix, much like the first elimination group in
|
|
|
|
|
// Solver::Options::ordering.
|
|
|
|
|
//
|
|
|
|
|
// 2. The second is to leave it empty, in which case, Ceres will
|
|
|
|
|
// use a heuristic to automatically choose a set of parameter
|
|
|
|
|
// blocks.
|
|
|
|
|
vector<double*> parameter_blocks_for_inner_iterations;
|
|
|
|
|
|
2012-06-05 23:10:59 -07:00
|
|
|
// By virtue of the modeling layer in Ceres being block oriented,
|
|
|
|
|
// all the matrices used by Ceres are also block oriented. When
|
|
|
|
|
// doing sparse direct factorization of these matrices (for
|
|
|
|
|
// SPARSE_NORMAL_CHOLESKY, SPARSE_SCHUR and ITERATIVE in
|
|
|
|
|
// conjunction with CLUSTER_TRIDIAGONAL AND CLUSTER_JACOBI
|
|
|
|
|
// preconditioners), the fill-reducing ordering algorithms can
|
|
|
|
|
// either be run on the block or the scalar form of these matrices.
|
|
|
|
|
// Running it on the block form exposes more of the super-nodal
|
|
|
|
|
// structure of the matrix to the factorization routines. Setting
|
|
|
|
|
// this parameter to true runs the ordering algorithms in block
|
|
|
|
|
// form. Currently this option only makes sense with
|
|
|
|
|
// sparse_linear_algebra_library = SUITE_SPARSE.
|
|
|
|
|
bool use_block_amd;
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
// Minimum number of iterations for which the linear solver should
|
|
|
|
|
// run, even if the convergence criterion is satisfied.
|
|
|
|
|
int linear_solver_min_num_iterations;
|
|
|
|
|
|
|
|
|
|
// Maximum number of iterations for which the linear solver should
|
|
|
|
|
// run. If the solver does not converge in less than
|
|
|
|
|
// linear_solver_max_num_iterations, then it returns
|
|
|
|
|
// MAX_ITERATIONS, as its termination type.
|
|
|
|
|
int linear_solver_max_num_iterations;
|
|
|
|
|
|
|
|
|
|
// Forcing sequence parameter. The truncated Newton solver uses
|
|
|
|
|
// this number to control the relative accuracy with which the
|
|
|
|
|
// Newton step is computed.
|
|
|
|
|
//
|
|
|
|
|
// This constant is passed to ConjugateGradientsSolver which uses
|
|
|
|
|
// it to terminate the iterations when
|
|
|
|
|
//
|
|
|
|
|
// (Q_i - Q_{i-1})/Q_i < eta/i
|
|
|
|
|
double eta;
|
|
|
|
|
|
|
|
|
|
// Normalize the jacobian using Jacobi scaling before calling
|
|
|
|
|
// the linear least squares solver.
|
|
|
|
|
bool jacobi_scaling;
|
|
|
|
|
|
|
|
|
|
// Logging options ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
LoggingType logging_type;
|
|
|
|
|
|
|
|
|
|
// By default the Minimizer progress is logged to VLOG(1), which
|
|
|
|
|
// is sent to STDERR depending on the vlog level. If this flag is
|
|
|
|
|
// set to true, and logging_type is not SILENT, the logging output
|
|
|
|
|
// is sent to STDOUT.
|
|
|
|
|
bool minimizer_progress_to_stdout;
|
|
|
|
|
|
|
|
|
|
bool return_initial_residuals;
|
2012-07-02 12:44:34 -07:00
|
|
|
bool return_initial_gradient;
|
|
|
|
|
bool return_initial_jacobian;
|
|
|
|
|
|
2012-04-30 23:09:08 -07:00
|
|
|
bool return_final_residuals;
|
2012-07-02 12:44:34 -07:00
|
|
|
bool return_final_gradient;
|
|
|
|
|
bool return_final_jacobian;
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
// List of iterations at which the optimizer should dump the
|
|
|
|
|
// linear least squares problem to disk. Useful for testing and
|
|
|
|
|
// benchmarking. If empty (default), no problems are dumped.
|
|
|
|
|
//
|
|
|
|
|
// This is ignored if protocol buffers are disabled.
|
|
|
|
|
vector<int> lsqp_iterations_to_dump;
|
2012-05-06 21:05:28 -07:00
|
|
|
string lsqp_dump_directory;
|
|
|
|
|
DumpFormatType lsqp_dump_format_type;
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
// Finite differences options ----------------------------------------------
|
|
|
|
|
|
|
|
|
|
// Check all jacobians computed by each residual block with finite
|
|
|
|
|
// differences. This is expensive since it involves computing the
|
|
|
|
|
// derivative by normal means (e.g. user specified, autodiff,
|
|
|
|
|
// etc), then also computing it using finite differences. The
|
|
|
|
|
// results are compared, and if they differ substantially, details
|
|
|
|
|
// are printed to the log.
|
|
|
|
|
bool check_gradients;
|
|
|
|
|
|
|
|
|
|
// Relative precision to check for in the gradient checker. If the
|
|
|
|
|
// relative difference between an element in a jacobian exceeds
|
|
|
|
|
// this number, then the jacobian for that cost term is dumped.
|
|
|
|
|
double gradient_check_relative_precision;
|
|
|
|
|
|
|
|
|
|
// Relative shift used for taking numeric derivatives. For finite
|
|
|
|
|
// differencing, each dimension is evaluated at slightly shifted
|
|
|
|
|
// values; for the case of central difference, this is what gets
|
|
|
|
|
// evaluated:
|
|
|
|
|
//
|
|
|
|
|
// delta = numeric_derivative_relative_step_size;
|
|
|
|
|
// f_initial = f(x)
|
|
|
|
|
// f_forward = f((1 + delta) * x)
|
|
|
|
|
// f_backward = f((1 - delta) * x)
|
|
|
|
|
//
|
|
|
|
|
// The finite differencing is done along each dimension. The
|
|
|
|
|
// reason to use a relative (rather than absolute) step size is
|
|
|
|
|
// that this way, numeric differentation works for functions where
|
|
|
|
|
// the arguments are typically large (e.g. 1e9) and when the
|
|
|
|
|
// values are small (e.g. 1e-5). It is possible to construct
|
|
|
|
|
// "torture cases" which break this finite difference heuristic,
|
|
|
|
|
// but they do not come up often in practice.
|
|
|
|
|
//
|
|
|
|
|
// TODO(keir): Pick a smarter number than the default above! In
|
|
|
|
|
// theory a good choice is sqrt(eps) * x, which for doubles means
|
|
|
|
|
// about 1e-8 * x. However, I have found this number too
|
|
|
|
|
// optimistic. This number should be exposed for users to change.
|
|
|
|
|
double numeric_derivative_relative_step_size;
|
|
|
|
|
|
|
|
|
|
// If true, the user's parameter blocks are updated at the end of
|
|
|
|
|
// every Minimizer iteration, otherwise they are updated when the
|
|
|
|
|
// Minimizer terminates. This is useful if, for example, the user
|
|
|
|
|
// wishes to visualize the state of the optimization every
|
|
|
|
|
// iteration.
|
|
|
|
|
bool update_state_every_iteration;
|
|
|
|
|
|
|
|
|
|
// Callbacks that are executed at the end of each iteration of the
|
2012-05-29 17:40:17 -07:00
|
|
|
// Minimizer. An iteration may terminate midway, either due to
|
|
|
|
|
// numerical failures or because one of the convergence tests has
|
|
|
|
|
// been satisfied. In this case none of the callbacks are
|
|
|
|
|
// executed.
|
|
|
|
|
|
|
|
|
|
// Callbacks are executed in the order that they are specified in
|
|
|
|
|
// this vector. By default, parameter blocks are updated only at
|
|
|
|
|
// the end of the optimization, i.e when the Minimizer
|
|
|
|
|
// terminates. This behaviour is controlled by
|
2012-04-30 23:09:08 -07:00
|
|
|
// update_state_every_variable. If the user wishes to have access
|
|
|
|
|
// to the update parameter blocks when his/her callbacks are
|
|
|
|
|
// executed, then set update_state_every_iteration to true.
|
|
|
|
|
//
|
|
|
|
|
// The solver does NOT take ownership of these pointers.
|
|
|
|
|
vector<IterationCallback*> callbacks;
|
2012-08-09 21:46:19 -07:00
|
|
|
|
|
|
|
|
// If non-empty, a summary of the execution of the solver is
|
|
|
|
|
// recorded to this file.
|
|
|
|
|
string solver_log;
|
2012-04-30 23:09:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Summary {
|
|
|
|
|
Summary();
|
|
|
|
|
|
|
|
|
|
// A brief one line description of the state of the solver after
|
|
|
|
|
// termination.
|
|
|
|
|
string BriefReport() const;
|
|
|
|
|
|
|
|
|
|
// A full multiline description of the state of the solver after
|
|
|
|
|
// termination.
|
|
|
|
|
string FullReport() const;
|
|
|
|
|
|
|
|
|
|
// Minimizer summary -------------------------------------------------
|
|
|
|
|
SolverTerminationType termination_type;
|
|
|
|
|
|
|
|
|
|
// If the solver did not run, or there was a failure, a
|
|
|
|
|
// description of the error.
|
|
|
|
|
string error;
|
|
|
|
|
|
|
|
|
|
// Cost of the problem before and after the optimization. See
|
|
|
|
|
// problem.h for definition of the cost of a problem.
|
|
|
|
|
double initial_cost;
|
|
|
|
|
double final_cost;
|
|
|
|
|
|
|
|
|
|
// The part of the total cost that comes from residual blocks that
|
|
|
|
|
// were held fixed by the preprocessor because all the parameter
|
|
|
|
|
// blocks that they depend on were fixed.
|
|
|
|
|
double fixed_cost;
|
|
|
|
|
|
2012-07-02 12:44:34 -07:00
|
|
|
// Vectors of residuals before and after the optimization. The
|
|
|
|
|
// entries of these vectors are in the order in which
|
|
|
|
|
// ResidualBlocks were added to the Problem object.
|
|
|
|
|
//
|
|
|
|
|
// Whether the residual vectors are populated with values is
|
|
|
|
|
// controlled by Solver::Options::return_initial_residuals and
|
|
|
|
|
// Solver::Options::return_final_residuals respectively.
|
2012-04-30 23:09:08 -07:00
|
|
|
vector<double> initial_residuals;
|
|
|
|
|
vector<double> final_residuals;
|
|
|
|
|
|
2012-07-02 12:44:34 -07:00
|
|
|
// Gradient vectors, before and after the optimization. The rows
|
|
|
|
|
// are in the same order in which the ParameterBlocks were added
|
|
|
|
|
// to the Problem object.
|
|
|
|
|
//
|
|
|
|
|
// NOTE: Since AddResidualBlock adds ParameterBlocks to the
|
|
|
|
|
// Problem automatically if they do not already exist, if you wish
|
|
|
|
|
// to have explicit control over the ordering of the vectors, then
|
|
|
|
|
// use Problem::AddParameterBlock to explicitly add the
|
|
|
|
|
// ParameterBlocks in the order desired.
|
|
|
|
|
//
|
|
|
|
|
// Whether the vectors are populated with values is controlled by
|
|
|
|
|
// Solver::Options::return_initial_gradient and
|
|
|
|
|
// Solver::Options::return_final_gradient respectively.
|
|
|
|
|
vector<double> initial_gradient;
|
|
|
|
|
vector<double> final_gradient;
|
|
|
|
|
|
|
|
|
|
// Jacobian matrices before and after the optimization. The rows
|
|
|
|
|
// of these matrices are in the same order in which the
|
|
|
|
|
// ResidualBlocks were added to the Problem object. The columns
|
|
|
|
|
// are in the same order in which the ParameterBlocks were added
|
|
|
|
|
// to the Problem object.
|
|
|
|
|
//
|
|
|
|
|
// NOTE: Since AddResidualBlock adds ParameterBlocks to the
|
|
|
|
|
// Problem automatically if they do not already exist, if you wish
|
|
|
|
|
// to have explicit control over the column ordering of the
|
|
|
|
|
// matrix, then use Problem::AddParameterBlock to explicitly add
|
|
|
|
|
// the ParameterBlocks in the order desired.
|
|
|
|
|
//
|
|
|
|
|
// The Jacobian matrices are stored as compressed row sparse
|
|
|
|
|
// matrices. Please see ceres/crs_matrix.h for more details of the
|
|
|
|
|
// format.
|
|
|
|
|
//
|
|
|
|
|
// Whether the Jacboan matrices are populated with values is
|
|
|
|
|
// controlled by Solver::Options::return_initial_jacobian and
|
|
|
|
|
// Solver::Options::return_final_jacobian respectively.
|
|
|
|
|
CRSMatrix initial_jacobian;
|
|
|
|
|
CRSMatrix final_jacobian;
|
|
|
|
|
|
2012-04-30 23:09:08 -07:00
|
|
|
vector<IterationSummary> iterations;
|
|
|
|
|
|
|
|
|
|
int num_successful_steps;
|
|
|
|
|
int num_unsuccessful_steps;
|
|
|
|
|
|
2012-06-11 14:21:42 -07:00
|
|
|
// When the user calls Solve, before the actual optimization
|
|
|
|
|
// occurs, Ceres performs a number of preprocessing steps. These
|
|
|
|
|
// include error checks, memory allocations, and reorderings. This
|
|
|
|
|
// time is accounted for as preprocessing time.
|
2012-04-30 23:09:08 -07:00
|
|
|
double preprocessor_time_in_seconds;
|
2012-06-11 14:21:42 -07:00
|
|
|
|
|
|
|
|
// Time spent in the TrustRegionMinimizer.
|
2012-04-30 23:09:08 -07:00
|
|
|
double minimizer_time_in_seconds;
|
2012-06-11 14:21:42 -07:00
|
|
|
|
|
|
|
|
// After the Minimizer is finished, some time is spent in
|
|
|
|
|
// re-evaluating residuals etc. This time is accounted for in the
|
|
|
|
|
// postprocessor time.
|
|
|
|
|
double postprocessor_time_in_seconds;
|
|
|
|
|
|
|
|
|
|
// Some total of all time spent inside Ceres when Solve is called.
|
2012-04-30 23:09:08 -07:00
|
|
|
double total_time_in_seconds;
|
|
|
|
|
|
|
|
|
|
// Preprocessor summary.
|
|
|
|
|
int num_parameter_blocks;
|
|
|
|
|
int num_parameters;
|
|
|
|
|
int num_residual_blocks;
|
|
|
|
|
int num_residuals;
|
|
|
|
|
|
|
|
|
|
int num_parameter_blocks_reduced;
|
|
|
|
|
int num_parameters_reduced;
|
|
|
|
|
int num_residual_blocks_reduced;
|
|
|
|
|
int num_residuals_reduced;
|
|
|
|
|
|
|
|
|
|
int num_eliminate_blocks_given;
|
|
|
|
|
int num_eliminate_blocks_used;
|
|
|
|
|
|
|
|
|
|
int num_threads_given;
|
|
|
|
|
int num_threads_used;
|
|
|
|
|
|
|
|
|
|
int num_linear_solver_threads_given;
|
|
|
|
|
int num_linear_solver_threads_used;
|
|
|
|
|
|
|
|
|
|
LinearSolverType linear_solver_type_given;
|
|
|
|
|
LinearSolverType linear_solver_type_used;
|
|
|
|
|
|
|
|
|
|
PreconditionerType preconditioner_type;
|
2012-06-17 10:08:19 -07:00
|
|
|
|
|
|
|
|
TrustRegionStrategyType trust_region_strategy_type;
|
2012-08-21 18:00:54 -07:00
|
|
|
DoglegType dogleg_type;
|
2012-06-17 10:08:19 -07:00
|
|
|
SparseLinearAlgebraLibraryType sparse_linear_algebra_library;
|
2012-04-30 23:09:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Once a least squares problem has been built, this function takes
|
|
|
|
|
// the problem and optimizes it based on the values of the options
|
|
|
|
|
// parameters. Upon return, a detailed summary of the work performed
|
|
|
|
|
// by the preprocessor, the non-linear minmizer and the linear
|
|
|
|
|
// solver are reported in the summary object.
|
|
|
|
|
virtual void Solve(const Options& options,
|
|
|
|
|
Problem* problem,
|
|
|
|
|
Solver::Summary* summary);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Helper function which avoids going through the interface.
|
|
|
|
|
void Solve(const Solver::Options& options,
|
|
|
|
|
Problem* problem,
|
|
|
|
|
Solver::Summary* summary);
|
|
|
|
|
|
|
|
|
|
} // namespace ceres
|
|
|
|
|
|
|
|
|
|
#endif // CERES_PUBLIC_SOLVER_H_
|