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 | #ifndef CERES_INTERNAL_MINIMIZER_H_ |
| 32 | #define CERES_INTERNAL_MINIMIZER_H_ |
| 33 | |
| 34 | #include <vector> |
Sameer Agarwal | aed9961 | 2012-11-29 10:33:19 -0800 | [diff] [blame] | 35 | #include "ceres/internal/port.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 36 | #include "ceres/iteration_callback.h" |
Sameer Agarwal | aed9961 | 2012-11-29 10:33:19 -0800 | [diff] [blame] | 37 | #include "ceres/solver.h" |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 38 | |
| 39 | namespace ceres { |
| 40 | namespace internal { |
| 41 | |
| 42 | class Evaluator; |
| 43 | class LinearSolver; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 44 | class SparseMatrix; |
| 45 | class TrustRegionStrategy; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 46 | |
| 47 | // Interface for non-linear least squares solvers. |
| 48 | class Minimizer { |
| 49 | public: |
| 50 | // Options struct to control the behaviour of the Minimizer. Please |
| 51 | // see solver.h for detailed information about the meaning and |
| 52 | // default values of each of these parameters. |
| 53 | struct Options { |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 54 | Options() { |
| 55 | Init(Solver::Options()); |
| 56 | } |
| 57 | |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 58 | explicit Options(const Solver::Options& options) { |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 59 | Init(options); |
| 60 | } |
| 61 | |
| 62 | void Init(const Solver::Options& options) { |
Sameer Agarwal | 9123e2f | 2012-09-18 21:49:06 -0700 | [diff] [blame] | 63 | num_threads = options.num_threads; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 64 | max_num_iterations = options.max_num_iterations; |
Sameer Agarwal | fa01519 | 2012-06-11 14:21:42 -0700 | [diff] [blame] | 65 | max_solver_time_in_seconds = options.max_solver_time_in_seconds; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 66 | max_step_solver_retries = 5; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 67 | gradient_tolerance = options.gradient_tolerance; |
| 68 | parameter_tolerance = options.parameter_tolerance; |
| 69 | function_tolerance = options.function_tolerance; |
| 70 | min_relative_decrease = options.min_relative_decrease; |
| 71 | eta = options.eta; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 72 | jacobi_scaling = options.jacobi_scaling; |
Sameer Agarwal | a8f87d7 | 2012-08-08 10:38:31 -0700 | [diff] [blame] | 73 | use_nonmonotonic_steps = options.use_nonmonotonic_steps; |
| 74 | max_consecutive_nonmonotonic_steps = |
| 75 | options.max_consecutive_nonmonotonic_steps; |
Sameer Agarwal | 82f4b88 | 2012-05-06 21:05:28 -0700 | [diff] [blame] | 76 | lsqp_dump_directory = options.lsqp_dump_directory; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 77 | lsqp_iterations_to_dump = options.lsqp_iterations_to_dump; |
Sameer Agarwal | 82f4b88 | 2012-05-06 21:05:28 -0700 | [diff] [blame] | 78 | lsqp_dump_format_type = options.lsqp_dump_format_type; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 79 | max_num_consecutive_invalid_steps = |
| 80 | options.max_num_consecutive_invalid_steps; |
| 81 | min_trust_region_radius = options.min_trust_region_radius; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 82 | line_search_direction_type = options.line_search_direction_type; |
| 83 | line_search_type = options.line_search_type; |
Sameer Agarwal | aed9961 | 2012-11-29 10:33:19 -0800 | [diff] [blame] | 84 | nonlinear_conjugate_gradient_type = |
| 85 | options.nonlinear_conjugate_gradient_type; |
| 86 | max_lbfgs_rank = options.max_lbfgs_rank; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 87 | evaluator = NULL; |
| 88 | trust_region_strategy = NULL; |
| 89 | jacobian = NULL; |
Keir Mierle | f747183 | 2012-06-14 11:31:53 -0700 | [diff] [blame] | 90 | callbacks = options.callbacks; |
Sameer Agarwal | 9123e2f | 2012-09-18 21:49:06 -0700 | [diff] [blame] | 91 | inner_iteration_minimizer = NULL; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | int max_num_iterations; |
Sameer Agarwal | fa01519 | 2012-06-11 14:21:42 -0700 | [diff] [blame] | 95 | double max_solver_time_in_seconds; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 96 | int num_threads; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 97 | |
| 98 | // Number of times the linear solver should be retried in case of |
| 99 | // numerical failure. The retries are done by exponentially scaling up |
| 100 | // mu at each retry. This leads to stronger and stronger |
| 101 | // regularization making the linear least squares problem better |
| 102 | // conditioned at each retry. |
| 103 | int max_step_solver_retries; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 104 | double gradient_tolerance; |
| 105 | double parameter_tolerance; |
| 106 | double function_tolerance; |
| 107 | double min_relative_decrease; |
| 108 | double eta; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 109 | bool jacobi_scaling; |
Sameer Agarwal | a8f87d7 | 2012-08-08 10:38:31 -0700 | [diff] [blame] | 110 | bool use_nonmonotonic_steps; |
Petter Strandmark | 2d7176a | 2012-08-30 19:51:24 -0700 | [diff] [blame] | 111 | int max_consecutive_nonmonotonic_steps; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 112 | vector<int> lsqp_iterations_to_dump; |
Sameer Agarwal | 82f4b88 | 2012-05-06 21:05:28 -0700 | [diff] [blame] | 113 | DumpFormatType lsqp_dump_format_type; |
| 114 | string lsqp_dump_directory; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 115 | int max_num_consecutive_invalid_steps; |
Sameer Agarwal | 69ebad4 | 2013-04-17 15:38:00 -0700 | [diff] [blame] | 116 | double min_trust_region_radius; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 117 | LineSearchDirectionType line_search_direction_type; |
| 118 | LineSearchType line_search_type; |
| 119 | NonlinearConjugateGradientType nonlinear_conjugate_gradient_type; |
Sameer Agarwal | aed9961 | 2012-11-29 10:33:19 -0800 | [diff] [blame] | 120 | int max_lbfgs_rank; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 121 | |
| 122 | // List of callbacks that are executed by the Minimizer at the end |
| 123 | // of each iteration. |
| 124 | // |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 125 | // The Options struct does not own these pointers. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 126 | vector<IterationCallback*> callbacks; |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 127 | |
| 128 | // Object responsible for evaluating the cost, residuals and |
| 129 | // Jacobian matrix. The Options struct does not own this pointer. |
| 130 | Evaluator* evaluator; |
| 131 | |
| 132 | // Object responsible for actually computing the trust region |
| 133 | // step, and sizing the trust region radius. The Options struct |
| 134 | // does not own this pointer. |
| 135 | TrustRegionStrategy* trust_region_strategy; |
| 136 | |
| 137 | // Object holding the Jacobian matrix. It is assumed that the |
| 138 | // sparsity structure of the matrix has already been initialized |
| 139 | // and will remain constant for the life time of the |
| 140 | // optimization. The Options struct does not own this pointer. |
| 141 | SparseMatrix* jacobian; |
Sameer Agarwal | 9123e2f | 2012-09-18 21:49:06 -0700 | [diff] [blame] | 142 | |
| 143 | Minimizer* inner_iteration_minimizer; |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 146 | static bool RunCallbacks(const vector<IterationCallback*> callbacks, |
| 147 | const IterationSummary& iteration_summary, |
| 148 | Solver::Summary* summary); |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 149 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 150 | virtual ~Minimizer(); |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 151 | // Note: The minimizer is expected to update the state of the |
| 152 | // parameters array every iteration. This is required for the |
| 153 | // StateUpdatingCallback to work. |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 154 | virtual void Minimize(const Options& options, |
Sameer Agarwal | aa9a83c | 2012-05-29 17:40:17 -0700 | [diff] [blame] | 155 | double* parameters, |
Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 156 | Solver::Summary* summary) = 0; |
| 157 | }; |
| 158 | |
| 159 | } // namespace internal |
| 160 | } // namespace ceres |
| 161 | |
| 162 | #endif // CERES_INTERNAL_MINIMIZER_H_ |