Home | History | Annotate | Download | only in ceres
      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 (at) google.com (Sameer Agarwal)
     30 
     31 #ifndef CERES_INTERNAL_MINIMIZER_H_
     32 #define CERES_INTERNAL_MINIMIZER_H_
     33 
     34 #include <vector>
     35 #include "ceres/solver.h"
     36 #include "ceres/iteration_callback.h"
     37 
     38 namespace ceres {
     39 namespace internal {
     40 
     41 class Evaluator;
     42 class LinearSolver;
     43 class SparseMatrix;
     44 class TrustRegionStrategy;
     45 
     46 // Interface for non-linear least squares solvers.
     47 class Minimizer {
     48  public:
     49   // Options struct to control the behaviour of the Minimizer. Please
     50   // see solver.h for detailed information about the meaning and
     51   // default values of each of these parameters.
     52   struct Options {
     53     Options() {
     54       Init(Solver::Options());
     55     }
     56 
     57     explicit Options(const Solver::Options& options) {
     58       Init(options);
     59     }
     60 
     61     void Init(const Solver::Options& options) {
     62       num_threads = options.num_threads;
     63       max_num_iterations = options.max_num_iterations;
     64       max_solver_time_in_seconds = options.max_solver_time_in_seconds;
     65       max_step_solver_retries = 5;
     66       gradient_tolerance = options.gradient_tolerance;
     67       parameter_tolerance = options.parameter_tolerance;
     68       function_tolerance = options.function_tolerance;
     69       min_relative_decrease = options.min_relative_decrease;
     70       eta = options.eta;
     71       jacobi_scaling = options.jacobi_scaling;
     72       use_nonmonotonic_steps = options.use_nonmonotonic_steps;
     73       max_consecutive_nonmonotonic_steps =
     74           options.max_consecutive_nonmonotonic_steps;
     75       lsqp_dump_directory = options.lsqp_dump_directory;
     76       lsqp_iterations_to_dump = options.lsqp_iterations_to_dump;
     77       lsqp_dump_format_type = options.lsqp_dump_format_type;
     78       max_num_consecutive_invalid_steps =
     79           options.max_num_consecutive_invalid_steps;
     80       min_trust_region_radius = options.min_trust_region_radius;
     81       evaluator = NULL;
     82       trust_region_strategy = NULL;
     83       jacobian = NULL;
     84       callbacks = options.callbacks;
     85       inner_iteration_minimizer = NULL;
     86     }
     87 
     88     int max_num_iterations;
     89     double max_solver_time_in_seconds;
     90 
     91     // Number of times the linear solver should be retried in case of
     92     // numerical failure. The retries are done by exponentially scaling up
     93     // mu at each retry. This leads to stronger and stronger
     94     // regularization making the linear least squares problem better
     95     // conditioned at each retry.
     96     int num_threads;
     97     int max_step_solver_retries;
     98     double gradient_tolerance;
     99     double parameter_tolerance;
    100     double function_tolerance;
    101     double min_relative_decrease;
    102     double eta;
    103     bool jacobi_scaling;
    104     bool use_nonmonotonic_steps;
    105     int max_consecutive_nonmonotonic_steps;
    106     vector<int> lsqp_iterations_to_dump;
    107     DumpFormatType lsqp_dump_format_type;
    108     string lsqp_dump_directory;
    109     int max_num_consecutive_invalid_steps;
    110     int min_trust_region_radius;
    111 
    112     // List of callbacks that are executed by the Minimizer at the end
    113     // of each iteration.
    114     //
    115     // The Options struct does not own these pointers.
    116     vector<IterationCallback*> callbacks;
    117 
    118     // Object responsible for evaluating the cost, residuals and
    119     // Jacobian matrix. The Options struct does not own this pointer.
    120     Evaluator* evaluator;
    121 
    122     // Object responsible for actually computing the trust region
    123     // step, and sizing the trust region radius. The Options struct
    124     // does not own this pointer.
    125     TrustRegionStrategy* trust_region_strategy;
    126 
    127     // Object holding the Jacobian matrix. It is assumed that the
    128     // sparsity structure of the matrix has already been initialized
    129     // and will remain constant for the life time of the
    130     // optimization. The Options struct does not own this pointer.
    131     SparseMatrix* jacobian;
    132 
    133     Minimizer* inner_iteration_minimizer;
    134   };
    135 
    136   virtual ~Minimizer() {}
    137 
    138   // Note: The minimizer is expected to update the state of the
    139   // parameters array every iteration. This is required for the
    140   // StateUpdatingCallback to work.
    141   virtual void Minimize(const Options& options,
    142                         double* parameters,
    143                         Solver::Summary* summary) = 0;
    144 };
    145 
    146 }  // namespace internal
    147 }  // namespace ceres
    148 
    149 #endif  // CERES_INTERNAL_MINIMIZER_H_
    150