blob: 3d9da997d243d23d666756033864544e87894e39 [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#ifndef CERES_INTERNAL_MINIMIZER_H_
32#define CERES_INTERNAL_MINIMIZER_H_
33
Sameer Agarwala427c872013-06-24 17:50:56 -070034#include <string>
Keir Mierle8ebb0732012-04-30 23:09:08 -070035#include <vector>
Sameer Agarwalaed99612012-11-29 10:33:19 -080036#include "ceres/internal/port.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070037#include "ceres/iteration_callback.h"
Sameer Agarwalaed99612012-11-29 10:33:19 -080038#include "ceres/solver.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070039
40namespace ceres {
41namespace internal {
42
43class Evaluator;
44class LinearSolver;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070045class SparseMatrix;
46class TrustRegionStrategy;
Keir Mierle8ebb0732012-04-30 23:09:08 -070047
48// Interface for non-linear least squares solvers.
49class Minimizer {
50 public:
51 // Options struct to control the behaviour of the Minimizer. Please
52 // see solver.h for detailed information about the meaning and
53 // default values of each of these parameters.
54 struct Options {
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070055 Options() {
56 Init(Solver::Options());
57 }
58
Keir Mierle8ebb0732012-04-30 23:09:08 -070059 explicit Options(const Solver::Options& options) {
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070060 Init(options);
61 }
62
63 void Init(const Solver::Options& options) {
Sameer Agarwal9123e2f2012-09-18 21:49:06 -070064 num_threads = options.num_threads;
Keir Mierle8ebb0732012-04-30 23:09:08 -070065 max_num_iterations = options.max_num_iterations;
Sameer Agarwalfa015192012-06-11 14:21:42 -070066 max_solver_time_in_seconds = options.max_solver_time_in_seconds;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070067 max_step_solver_retries = 5;
Keir Mierle8ebb0732012-04-30 23:09:08 -070068 gradient_tolerance = options.gradient_tolerance;
69 parameter_tolerance = options.parameter_tolerance;
70 function_tolerance = options.function_tolerance;
71 min_relative_decrease = options.min_relative_decrease;
72 eta = options.eta;
Keir Mierle8ebb0732012-04-30 23:09:08 -070073 jacobi_scaling = options.jacobi_scaling;
Sameer Agarwala8f87d72012-08-08 10:38:31 -070074 use_nonmonotonic_steps = options.use_nonmonotonic_steps;
75 max_consecutive_nonmonotonic_steps =
76 options.max_consecutive_nonmonotonic_steps;
Sameer Agarwalc4a32912013-06-13 22:00:48 -070077 trust_region_problem_dump_directory =
78 options.trust_region_problem_dump_directory;
79 trust_region_minimizer_iterations_to_dump =
80 options.trust_region_minimizer_iterations_to_dump;
81 trust_region_problem_dump_format_type =
82 options.trust_region_problem_dump_format_type;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070083 max_num_consecutive_invalid_steps =
84 options.max_num_consecutive_invalid_steps;
85 min_trust_region_radius = options.min_trust_region_radius;
Sameer Agarwalf4d01642012-11-26 12:55:58 -080086 line_search_direction_type = options.line_search_direction_type;
87 line_search_type = options.line_search_type;
Sameer Agarwalaed99612012-11-29 10:33:19 -080088 nonlinear_conjugate_gradient_type =
89 options.nonlinear_conjugate_gradient_type;
90 max_lbfgs_rank = options.max_lbfgs_rank;
Alex Stewart9aa0e3c2013-07-05 20:22:37 +010091 use_approximate_eigenvalue_bfgs_scaling =
92 options.use_approximate_eigenvalue_bfgs_scaling;
Sameer Agarwal09244012013-06-30 14:33:23 -070093 line_search_interpolation_type =
94 options.line_search_interpolation_type;
95 min_line_search_step_size = options.min_line_search_step_size;
Alex Stewart9aa0e3c2013-07-05 20:22:37 +010096 line_search_sufficient_function_decrease =
97 options.line_search_sufficient_function_decrease;
98 max_line_search_step_contraction =
99 options.max_line_search_step_contraction;
100 min_line_search_step_contraction =
101 options.min_line_search_step_contraction;
102 max_num_line_search_step_size_iterations =
103 options.max_num_line_search_step_size_iterations;
104 max_num_line_search_direction_restarts =
105 options.max_num_line_search_direction_restarts;
106 line_search_sufficient_curvature_decrease =
107 options.line_search_sufficient_curvature_decrease;
108 max_line_search_step_expansion =
109 options.max_line_search_step_expansion;
Sameer Agarwal2d785d62013-09-04 23:34:29 -0700110 is_silent = false;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700111 evaluator = NULL;
112 trust_region_strategy = NULL;
113 jacobian = NULL;
Keir Mierlef7471832012-06-14 11:31:53 -0700114 callbacks = options.callbacks;
Sameer Agarwal9123e2f2012-09-18 21:49:06 -0700115 inner_iteration_minimizer = NULL;
Sameer Agarwald4cb94b2013-05-22 09:13:27 -0700116 inner_iteration_tolerance = options.inner_iteration_tolerance;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700117 }
118
119 int max_num_iterations;
Sameer Agarwalfa015192012-06-11 14:21:42 -0700120 double max_solver_time_in_seconds;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800121 int num_threads;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700122
123 // Number of times the linear solver should be retried in case of
124 // numerical failure. The retries are done by exponentially scaling up
125 // mu at each retry. This leads to stronger and stronger
126 // regularization making the linear least squares problem better
127 // conditioned at each retry.
128 int max_step_solver_retries;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700129 double gradient_tolerance;
130 double parameter_tolerance;
131 double function_tolerance;
132 double min_relative_decrease;
133 double eta;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700134 bool jacobi_scaling;
Sameer Agarwala8f87d72012-08-08 10:38:31 -0700135 bool use_nonmonotonic_steps;
Petter Strandmark2d7176a2012-08-30 19:51:24 -0700136 int max_consecutive_nonmonotonic_steps;
Sameer Agarwalc4a32912013-06-13 22:00:48 -0700137 vector<int> trust_region_minimizer_iterations_to_dump;
138 DumpFormatType trust_region_problem_dump_format_type;
139 string trust_region_problem_dump_directory;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700140 int max_num_consecutive_invalid_steps;
Sameer Agarwal69ebad42013-04-17 15:38:00 -0700141 double min_trust_region_radius;
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800142 LineSearchDirectionType line_search_direction_type;
143 LineSearchType line_search_type;
144 NonlinearConjugateGradientType nonlinear_conjugate_gradient_type;
Sameer Agarwalaed99612012-11-29 10:33:19 -0800145 int max_lbfgs_rank;
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100146 bool use_approximate_eigenvalue_bfgs_scaling;
Sameer Agarwal09244012013-06-30 14:33:23 -0700147 LineSearchInterpolationType line_search_interpolation_type;
148 double min_line_search_step_size;
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100149 double line_search_sufficient_function_decrease;
150 double max_line_search_step_contraction;
151 double min_line_search_step_contraction;
152 int max_num_line_search_step_size_iterations;
153 int max_num_line_search_direction_restarts;
154 double line_search_sufficient_curvature_decrease;
155 double max_line_search_step_expansion;
Sameer Agarwal09244012013-06-30 14:33:23 -0700156
Sameer Agarwal2d785d62013-09-04 23:34:29 -0700157 // If true, then all logging is disabled.
158 bool is_silent;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700159
160 // List of callbacks that are executed by the Minimizer at the end
161 // of each iteration.
162 //
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700163 // The Options struct does not own these pointers.
Keir Mierle8ebb0732012-04-30 23:09:08 -0700164 vector<IterationCallback*> callbacks;
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700165
166 // Object responsible for evaluating the cost, residuals and
167 // Jacobian matrix. The Options struct does not own this pointer.
168 Evaluator* evaluator;
169
170 // Object responsible for actually computing the trust region
171 // step, and sizing the trust region radius. The Options struct
172 // does not own this pointer.
173 TrustRegionStrategy* trust_region_strategy;
174
175 // Object holding the Jacobian matrix. It is assumed that the
176 // sparsity structure of the matrix has already been initialized
177 // and will remain constant for the life time of the
178 // optimization. The Options struct does not own this pointer.
179 SparseMatrix* jacobian;
Sameer Agarwal9123e2f2012-09-18 21:49:06 -0700180
181 Minimizer* inner_iteration_minimizer;
Sameer Agarwald4cb94b2013-05-22 09:13:27 -0700182 double inner_iteration_tolerance;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700183 };
184
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800185 static bool RunCallbacks(const vector<IterationCallback*> callbacks,
186 const IterationSummary& iteration_summary,
187 Solver::Summary* summary);
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700188
Sameer Agarwal9883fc32012-11-30 12:32:43 -0800189 virtual ~Minimizer();
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700190 // Note: The minimizer is expected to update the state of the
191 // parameters array every iteration. This is required for the
192 // StateUpdatingCallback to work.
Keir Mierle8ebb0732012-04-30 23:09:08 -0700193 virtual void Minimize(const Options& options,
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700194 double* parameters,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700195 Solver::Summary* summary) = 0;
196};
197
198} // namespace internal
199} // namespace ceres
200
201#endif // CERES_INTERNAL_MINIMIZER_H_