Blame include/ceres/gradient_problem_solver.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
Packit ea1746
#ifndef CERES_PUBLIC_GRADIENT_PROBLEM_SOLVER_H_
Packit ea1746
#define CERES_PUBLIC_GRADIENT_PROBLEM_SOLVER_H_
Packit ea1746
Packit ea1746
#include <cmath>
Packit ea1746
#include <string>
Packit ea1746
#include <vector>
Packit ea1746
#include "ceres/internal/macros.h"
Packit ea1746
#include "ceres/internal/port.h"
Packit ea1746
#include "ceres/iteration_callback.h"
Packit ea1746
#include "ceres/types.h"
Packit ea1746
#include "ceres/internal/disable_warnings.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
Packit ea1746
class GradientProblem;
Packit ea1746
Packit ea1746
class CERES_EXPORT GradientProblemSolver {
Packit ea1746
 public:
Packit ea1746
  virtual ~GradientProblemSolver();
Packit ea1746
Packit ea1746
  // The options structure contains, not surprisingly, options that control how
Packit ea1746
  // the solver operates. The defaults should be suitable for a wide range of
Packit ea1746
  // problems; however, better performance is often obtainable with tweaking.
Packit ea1746
  //
Packit ea1746
  // The constants are defined inside types.h
Packit ea1746
  struct CERES_EXPORT Options {
Packit ea1746
    // Default constructor that sets up a generic sparse problem.
Packit ea1746
    Options() {
Packit ea1746
      line_search_direction_type = LBFGS;
Packit ea1746
      line_search_type = WOLFE;
Packit ea1746
      nonlinear_conjugate_gradient_type = FLETCHER_REEVES;
Packit ea1746
      max_lbfgs_rank = 20;
Packit ea1746
      use_approximate_eigenvalue_bfgs_scaling = false;
Packit ea1746
      line_search_interpolation_type = CUBIC;
Packit ea1746
      min_line_search_step_size = 1e-9;
Packit ea1746
      line_search_sufficient_function_decrease = 1e-4;
Packit ea1746
      max_line_search_step_contraction = 1e-3;
Packit ea1746
      min_line_search_step_contraction = 0.6;
Packit ea1746
      max_num_line_search_step_size_iterations = 20;
Packit ea1746
      max_num_line_search_direction_restarts = 5;
Packit ea1746
      line_search_sufficient_curvature_decrease = 0.9;
Packit ea1746
      max_line_search_step_expansion = 10.0;
Packit ea1746
      max_num_iterations = 50;
Packit ea1746
      max_solver_time_in_seconds = 1e9;
Packit ea1746
      function_tolerance = 1e-6;
Packit ea1746
      gradient_tolerance = 1e-10;
Packit ea1746
      parameter_tolerance = 1e-8;
Packit ea1746
      logging_type = PER_MINIMIZER_ITERATION;
Packit ea1746
      minimizer_progress_to_stdout = false;
Packit ea1746
    }
Packit ea1746
Packit ea1746
    // Returns true if the options struct has a valid
Packit ea1746
    // configuration. Returns false otherwise, and fills in *error
Packit ea1746
    // with a message describing the problem.
Packit ea1746
    bool IsValid(std::string* error) const;
Packit ea1746
Packit ea1746
    // Minimizer options ----------------------------------------
Packit ea1746
    LineSearchDirectionType line_search_direction_type;
Packit ea1746
    LineSearchType line_search_type;
Packit ea1746
    NonlinearConjugateGradientType nonlinear_conjugate_gradient_type;
Packit ea1746
Packit ea1746
    // The LBFGS hessian approximation is a low rank approximation to
Packit ea1746
    // the inverse of the Hessian matrix. The rank of the
Packit ea1746
    // approximation determines (linearly) the space and time
Packit ea1746
    // complexity of using the approximation. Higher the rank, the
Packit ea1746
    // better is the quality of the approximation. The increase in
Packit ea1746
    // quality is however is bounded for a number of reasons.
Packit ea1746
    //
Packit ea1746
    // 1. The method only uses secant information and not actual
Packit ea1746
    // derivatives.
Packit ea1746
    //
Packit ea1746
    // 2. The Hessian approximation is constrained to be positive
Packit ea1746
    // definite.
Packit ea1746
    //
Packit ea1746
    // So increasing this rank to a large number will cost time and
Packit ea1746
    // space complexity without the corresponding increase in solution
Packit ea1746
    // quality. There are no hard and fast rules for choosing the
Packit ea1746
    // maximum rank. The best choice usually requires some problem
Packit ea1746
    // specific experimentation.
Packit ea1746
    //
Packit ea1746
    // For more theoretical and implementation details of the LBFGS
Packit ea1746
    // method, please see:
Packit ea1746
    //
Packit ea1746
    // Nocedal, J. (1980). "Updating Quasi-Newton Matrices with
Packit ea1746
    // Limited Storage". Mathematics of Computation 35 (151): 773–782.
Packit ea1746
    int max_lbfgs_rank;
Packit ea1746
Packit ea1746
    // As part of the (L)BFGS update step (BFGS) / right-multiply step (L-BFGS),
Packit ea1746
    // the initial inverse Hessian approximation is taken to be the Identity.
Packit ea1746
    // However, Oren showed that using instead I * \gamma, where \gamma is
Packit ea1746
    // chosen to approximate an eigenvalue of the true inverse Hessian can
Packit ea1746
    // result in improved convergence in a wide variety of cases. Setting
Packit ea1746
    // use_approximate_eigenvalue_bfgs_scaling to true enables this scaling.
Packit ea1746
    //
Packit ea1746
    // It is important to note that approximate eigenvalue scaling does not
Packit ea1746
    // always improve convergence, and that it can in fact significantly degrade
Packit ea1746
    // performance for certain classes of problem, which is why it is disabled
Packit ea1746
    // by default.  In particular it can degrade performance when the
Packit ea1746
    // sensitivity of the problem to different parameters varies significantly,
Packit ea1746
    // as in this case a single scalar factor fails to capture this variation
Packit ea1746
    // and detrimentally downscales parts of the jacobian approximation which
Packit ea1746
    // correspond to low-sensitivity parameters. It can also reduce the
Packit ea1746
    // robustness of the solution to errors in the jacobians.
Packit ea1746
    //
Packit ea1746
    // Oren S.S., Self-scaling variable metric (SSVM) algorithms
Packit ea1746
    // Part II: Implementation and experiments, Management Science,
Packit ea1746
    // 20(5), 863-874, 1974.
Packit ea1746
    bool use_approximate_eigenvalue_bfgs_scaling;
Packit ea1746
Packit ea1746
    // Degree of the polynomial used to approximate the objective
Packit ea1746
    // function. Valid values are BISECTION, QUADRATIC and CUBIC.
Packit ea1746
    //
Packit ea1746
    // BISECTION corresponds to pure backtracking search with no
Packit ea1746
    // interpolation.
Packit ea1746
    LineSearchInterpolationType line_search_interpolation_type;
Packit ea1746
Packit ea1746
    // If during the line search, the step_size falls below this
Packit ea1746
    // value, it is truncated to zero.
Packit ea1746
    double min_line_search_step_size;
Packit ea1746
Packit ea1746
    // Line search parameters.
Packit ea1746
Packit ea1746
    // Solving the line search problem exactly is computationally
Packit ea1746
    // prohibitive. Fortunately, line search based optimization
Packit ea1746
    // algorithms can still guarantee convergence if instead of an
Packit ea1746
    // exact solution, the line search algorithm returns a solution
Packit ea1746
    // which decreases the value of the objective function
Packit ea1746
    // sufficiently. More precisely, we are looking for a step_size
Packit ea1746
    // s.t.
Packit ea1746
    //
Packit ea1746
    //   f(step_size) <= f(0) + sufficient_decrease * f'(0) * step_size
Packit ea1746
    //
Packit ea1746
    double line_search_sufficient_function_decrease;
Packit ea1746
Packit ea1746
    // In each iteration of the line search,
Packit ea1746
    //
Packit ea1746
    //  new_step_size >= max_line_search_step_contraction * step_size
Packit ea1746
    //
Packit ea1746
    // Note that by definition, for contraction:
Packit ea1746
    //
Packit ea1746
    //  0 < max_step_contraction < min_step_contraction < 1
Packit ea1746
    //
Packit ea1746
    double max_line_search_step_contraction;
Packit ea1746
Packit ea1746
    // In each iteration of the line search,
Packit ea1746
    //
Packit ea1746
    //  new_step_size <= min_line_search_step_contraction * step_size
Packit ea1746
    //
Packit ea1746
    // Note that by definition, for contraction:
Packit ea1746
    //
Packit ea1746
    //  0 < max_step_contraction < min_step_contraction < 1
Packit ea1746
    //
Packit ea1746
    double min_line_search_step_contraction;
Packit ea1746
Packit ea1746
    // Maximum number of trial step size iterations during each line search,
Packit ea1746
    // if a step size satisfying the search conditions cannot be found within
Packit ea1746
    // this number of trials, the line search will terminate.
Packit ea1746
    int max_num_line_search_step_size_iterations;
Packit ea1746
Packit ea1746
    // Maximum number of restarts of the line search direction algorithm before
Packit ea1746
    // terminating the optimization. Restarts of the line search direction
Packit ea1746
    // algorithm occur when the current algorithm fails to produce a new descent
Packit ea1746
    // direction. This typically indicates a numerical failure, or a breakdown
Packit ea1746
    // in the validity of the approximations used.
Packit ea1746
    int max_num_line_search_direction_restarts;
Packit ea1746
Packit ea1746
    // The strong Wolfe conditions consist of the Armijo sufficient
Packit ea1746
    // decrease condition, and an additional requirement that the
Packit ea1746
    // step-size be chosen s.t. the _magnitude_ ('strong' Wolfe
Packit ea1746
    // conditions) of the gradient along the search direction
Packit ea1746
    // decreases sufficiently. Precisely, this second condition
Packit ea1746
    // is that we seek a step_size s.t.
Packit ea1746
    //
Packit ea1746
    //   |f'(step_size)| <= sufficient_curvature_decrease * |f'(0)|
Packit ea1746
    //
Packit ea1746
    // Where f() is the line search objective and f'() is the derivative
Packit ea1746
    // of f w.r.t step_size (d f / d step_size).
Packit ea1746
    double line_search_sufficient_curvature_decrease;
Packit ea1746
Packit ea1746
    // During the bracketing phase of the Wolfe search, the step size is
Packit ea1746
    // increased until either a point satisfying the Wolfe conditions is
Packit ea1746
    // found, or an upper bound for a bracket containing a point satisfying
Packit ea1746
    // the conditions is found.  Precisely, at each iteration of the
Packit ea1746
    // expansion:
Packit ea1746
    //
Packit ea1746
    //   new_step_size <= max_step_expansion * step_size.
Packit ea1746
    //
Packit ea1746
    // By definition for expansion, max_step_expansion > 1.0.
Packit ea1746
    double max_line_search_step_expansion;
Packit ea1746
Packit ea1746
    // Maximum number of iterations for the minimizer to run for.
Packit ea1746
    int max_num_iterations;
Packit ea1746
Packit ea1746
    // Maximum time for which the minimizer should run for.
Packit ea1746
    double max_solver_time_in_seconds;
Packit ea1746
Packit ea1746
    // Minimizer terminates when
Packit ea1746
    //
Packit ea1746
    //   (new_cost - old_cost) < function_tolerance * old_cost;
Packit ea1746
    //
Packit ea1746
    double function_tolerance;
Packit ea1746
Packit ea1746
    // Minimizer terminates when
Packit ea1746
    //
Packit ea1746
    //   max_i |x - Project(Plus(x, -g(x))| < gradient_tolerance
Packit ea1746
    //
Packit ea1746
    // This value should typically be 1e-4 * function_tolerance.
Packit ea1746
    double gradient_tolerance;
Packit ea1746
Packit ea1746
    // Minimizer terminates when
Packit ea1746
    //
Packit ea1746
    //   |step|_2 <= parameter_tolerance * ( |x|_2 +  parameter_tolerance)
Packit ea1746
    //
Packit ea1746
    double parameter_tolerance;
Packit ea1746
Packit ea1746
    // Logging options ---------------------------------------------------------
Packit ea1746
Packit ea1746
    LoggingType logging_type;
Packit ea1746
Packit ea1746
    // By default the Minimizer progress is logged to VLOG(1), which
Packit ea1746
    // is sent to STDERR depending on the vlog level. If this flag is
Packit ea1746
    // set to true, and logging_type is not SILENT, the logging output
Packit ea1746
    // is sent to STDOUT.
Packit ea1746
    bool minimizer_progress_to_stdout;
Packit ea1746
Packit ea1746
    // Callbacks that are executed at the end of each iteration of the
Packit ea1746
    // Minimizer. An iteration may terminate midway, either due to
Packit ea1746
    // numerical failures or because one of the convergence tests has
Packit ea1746
    // been satisfied. In this case none of the callbacks are
Packit ea1746
    // executed.
Packit ea1746
Packit ea1746
    // Callbacks are executed in the order that they are specified in
Packit ea1746
    // this vector. By default, parameter blocks are updated only at
Packit ea1746
    // the end of the optimization, i.e when the Minimizer
Packit ea1746
    // terminates. This behaviour is controlled by
Packit ea1746
    // update_state_every_variable. If the user wishes to have access
Packit ea1746
    // to the update parameter blocks when his/her callbacks are
Packit ea1746
    // executed, then set update_state_every_iteration to true.
Packit ea1746
    //
Packit ea1746
    // The solver does NOT take ownership of these pointers.
Packit ea1746
    std::vector<IterationCallback*> callbacks;
Packit ea1746
  };
Packit ea1746
Packit ea1746
  struct CERES_EXPORT Summary {
Packit ea1746
    Summary();
Packit ea1746
Packit ea1746
    // A brief one line description of the state of the solver after
Packit ea1746
    // termination.
Packit ea1746
    std::string BriefReport() const;
Packit ea1746
Packit ea1746
    // A full multiline description of the state of the solver after
Packit ea1746
    // termination.
Packit ea1746
    std::string FullReport() const;
Packit ea1746
Packit ea1746
    bool IsSolutionUsable() const;
Packit ea1746
Packit ea1746
    // Minimizer summary -------------------------------------------------
Packit ea1746
    TerminationType termination_type;
Packit ea1746
Packit ea1746
    // Reason why the solver terminated.
Packit ea1746
    std::string message;
Packit ea1746
Packit ea1746
    // Cost of the problem (value of the objective function) before
Packit ea1746
    // the optimization.
Packit ea1746
    double initial_cost;
Packit ea1746
Packit ea1746
    // Cost of the problem (value of the objective function) after the
Packit ea1746
    // optimization.
Packit ea1746
    double final_cost;
Packit ea1746
Packit ea1746
    // IterationSummary for each minimizer iteration in order.
Packit ea1746
    std::vector<IterationSummary> iterations;
Packit ea1746
Packit ea1746
    // Number of times the cost (and not the gradient) was evaluated.
Packit ea1746
    int num_cost_evaluations;
Packit ea1746
Packit ea1746
    // Number of times the gradient (and the cost) were evaluated.
Packit ea1746
    int num_gradient_evaluations;
Packit ea1746
Packit ea1746
    // Sum total of all time spent inside Ceres when Solve is called.
Packit ea1746
    double total_time_in_seconds;
Packit ea1746
Packit ea1746
    // Time (in seconds) spent evaluating the cost.
Packit ea1746
    double cost_evaluation_time_in_seconds;
Packit ea1746
Packit ea1746
    // Time (in seconds) spent evaluating the gradient.
Packit ea1746
    double gradient_evaluation_time_in_seconds;
Packit ea1746
Packit ea1746
    // Time (in seconds) spent minimizing the interpolating polynomial
Packit ea1746
    // to compute the next candidate step size as part of a line search.
Packit ea1746
    double line_search_polynomial_minimization_time_in_seconds;
Packit ea1746
Packit ea1746
    // Number of parameters in the probem.
Packit ea1746
    int num_parameters;
Packit ea1746
Packit ea1746
    // Dimension of the tangent space of the problem.
Packit ea1746
    int num_local_parameters;
Packit ea1746
Packit ea1746
    // Type of line search direction used.
Packit ea1746
    LineSearchDirectionType line_search_direction_type;
Packit ea1746
Packit ea1746
    // Type of the line search algorithm used.
Packit ea1746
    LineSearchType line_search_type;
Packit ea1746
Packit ea1746
    //  When performing line search, the degree of the polynomial used
Packit ea1746
    //  to approximate the objective function.
Packit ea1746
    LineSearchInterpolationType line_search_interpolation_type;
Packit ea1746
Packit ea1746
    // If the line search direction is NONLINEAR_CONJUGATE_GRADIENT,
Packit ea1746
    // then this indicates the particular variant of non-linear
Packit ea1746
    // conjugate gradient used.
Packit ea1746
    NonlinearConjugateGradientType nonlinear_conjugate_gradient_type;
Packit ea1746
Packit ea1746
    // If the type of the line search direction is LBFGS, then this
Packit ea1746
    // indicates the rank of the Hessian approximation.
Packit ea1746
    int max_lbfgs_rank;
Packit ea1746
  };
Packit ea1746
Packit ea1746
  // Once a least squares problem has been built, this function takes
Packit ea1746
  // the problem and optimizes it based on the values of the options
Packit ea1746
  // parameters. Upon return, a detailed summary of the work performed
Packit ea1746
  // by the preprocessor, the non-linear minmizer and the linear
Packit ea1746
  // solver are reported in the summary object.
Packit ea1746
  virtual void Solve(const GradientProblemSolver::Options& options,
Packit ea1746
                     const GradientProblem& problem,
Packit ea1746
                     double* parameters,
Packit ea1746
                     GradientProblemSolver::Summary* summary);
Packit ea1746
};
Packit ea1746
Packit ea1746
// Helper function which avoids going through the interface.
Packit ea1746
CERES_EXPORT void Solve(const GradientProblemSolver::Options& options,
Packit ea1746
                        const GradientProblem& problem,
Packit ea1746
                        double* parameters,
Packit ea1746
                        GradientProblemSolver::Summary* summary);
Packit ea1746
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#include "ceres/internal/reenable_warnings.h"
Packit ea1746
Packit ea1746
#endif  // CERES_PUBLIC_GRADIENT_PROBLEM_SOLVER_H_