blob: 246550de7cd8d8ff5d5d6e3bb346515dece4027a [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
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 <memory>
35#include <string>
36#include <vector>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080037
Austin Schuh70cc9552019-01-21 19:46:48 -080038#include "ceres/internal/port.h"
39#include "ceres/iteration_callback.h"
40#include "ceres/solver.h"
41
42namespace ceres {
43namespace internal {
44
45class Evaluator;
46class SparseMatrix;
47class TrustRegionStrategy;
48class CoordinateDescentMinimizer;
49class LinearSolver;
50
51// Interface for non-linear least squares solvers.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080052class CERES_EXPORT_INTERNAL Minimizer {
Austin Schuh70cc9552019-01-21 19:46:48 -080053 public:
54 // Options struct to control the behaviour of the Minimizer. Please
55 // see solver.h for detailed information about the meaning and
56 // default values of each of these parameters.
57 struct Options {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080058 Options() { Init(Solver::Options()); }
Austin Schuh70cc9552019-01-21 19:46:48 -080059
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080060 explicit Options(const Solver::Options& options) { Init(options); }
Austin Schuh70cc9552019-01-21 19:46:48 -080061
62 void Init(const Solver::Options& options) {
63 num_threads = options.num_threads;
64 max_num_iterations = options.max_num_iterations;
65 max_solver_time_in_seconds = options.max_solver_time_in_seconds;
66 max_step_solver_retries = 5;
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;
72 jacobi_scaling = options.jacobi_scaling;
73 use_nonmonotonic_steps = options.use_nonmonotonic_steps;
74 max_consecutive_nonmonotonic_steps =
75 options.max_consecutive_nonmonotonic_steps;
76 trust_region_problem_dump_directory =
77 options.trust_region_problem_dump_directory;
78 trust_region_minimizer_iterations_to_dump =
79 options.trust_region_minimizer_iterations_to_dump;
80 trust_region_problem_dump_format_type =
81 options.trust_region_problem_dump_format_type;
82 max_num_consecutive_invalid_steps =
83 options.max_num_consecutive_invalid_steps;
84 min_trust_region_radius = options.min_trust_region_radius;
85 line_search_direction_type = options.line_search_direction_type;
86 line_search_type = options.line_search_type;
87 nonlinear_conjugate_gradient_type =
88 options.nonlinear_conjugate_gradient_type;
89 max_lbfgs_rank = options.max_lbfgs_rank;
90 use_approximate_eigenvalue_bfgs_scaling =
91 options.use_approximate_eigenvalue_bfgs_scaling;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080092 line_search_interpolation_type = options.line_search_interpolation_type;
Austin Schuh70cc9552019-01-21 19:46:48 -080093 min_line_search_step_size = options.min_line_search_step_size;
94 line_search_sufficient_function_decrease =
95 options.line_search_sufficient_function_decrease;
96 max_line_search_step_contraction =
97 options.max_line_search_step_contraction;
98 min_line_search_step_contraction =
99 options.min_line_search_step_contraction;
100 max_num_line_search_step_size_iterations =
101 options.max_num_line_search_step_size_iterations;
102 max_num_line_search_direction_restarts =
103 options.max_num_line_search_direction_restarts;
104 line_search_sufficient_curvature_decrease =
105 options.line_search_sufficient_curvature_decrease;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800106 max_line_search_step_expansion = options.max_line_search_step_expansion;
Austin Schuh70cc9552019-01-21 19:46:48 -0800107 inner_iteration_tolerance = options.inner_iteration_tolerance;
108 is_silent = (options.logging_type == SILENT);
109 is_constrained = false;
110 callbacks = options.callbacks;
111 }
112
113 int max_num_iterations;
114 double max_solver_time_in_seconds;
115 int num_threads;
116
117 // Number of times the linear solver should be retried in case of
118 // numerical failure. The retries are done by exponentially scaling up
119 // mu at each retry. This leads to stronger and stronger
120 // regularization making the linear least squares problem better
121 // conditioned at each retry.
122 int max_step_solver_retries;
123 double gradient_tolerance;
124 double parameter_tolerance;
125 double function_tolerance;
126 double min_relative_decrease;
127 double eta;
128 bool jacobi_scaling;
129 bool use_nonmonotonic_steps;
130 int max_consecutive_nonmonotonic_steps;
131 std::vector<int> trust_region_minimizer_iterations_to_dump;
132 DumpFormatType trust_region_problem_dump_format_type;
133 std::string trust_region_problem_dump_directory;
134 int max_num_consecutive_invalid_steps;
135 double min_trust_region_radius;
136 LineSearchDirectionType line_search_direction_type;
137 LineSearchType line_search_type;
138 NonlinearConjugateGradientType nonlinear_conjugate_gradient_type;
139 int max_lbfgs_rank;
140 bool use_approximate_eigenvalue_bfgs_scaling;
141 LineSearchInterpolationType line_search_interpolation_type;
142 double min_line_search_step_size;
143 double line_search_sufficient_function_decrease;
144 double max_line_search_step_contraction;
145 double min_line_search_step_contraction;
146 int max_num_line_search_step_size_iterations;
147 int max_num_line_search_direction_restarts;
148 double line_search_sufficient_curvature_decrease;
149 double max_line_search_step_expansion;
150 double inner_iteration_tolerance;
151
152 // If true, then all logging is disabled.
153 bool is_silent;
154
155 // Use a bounds constrained optimization algorithm.
156 bool is_constrained;
157
158 // List of callbacks that are executed by the Minimizer at the end
159 // of each iteration.
160 //
161 // The Options struct does not own these pointers.
162 std::vector<IterationCallback*> callbacks;
163
164 // Object responsible for evaluating the cost, residuals and
165 // Jacobian matrix.
166 std::shared_ptr<Evaluator> evaluator;
167
168 // Object responsible for actually computing the trust region
169 // step, and sizing the trust region radius.
170 std::shared_ptr<TrustRegionStrategy> trust_region_strategy;
171
172 // Object holding the Jacobian matrix. It is assumed that the
173 // sparsity structure of the matrix has already been initialized
174 // and will remain constant for the life time of the
175 // optimization.
176 std::shared_ptr<SparseMatrix> jacobian;
177
178 std::shared_ptr<CoordinateDescentMinimizer> inner_iteration_minimizer;
179 };
180
181 static Minimizer* Create(MinimizerType minimizer_type);
182 static bool RunCallbacks(const Options& options,
183 const IterationSummary& iteration_summary,
184 Solver::Summary* summary);
185
186 virtual ~Minimizer();
187 // Note: The minimizer is expected to update the state of the
188 // parameters array every iteration. This is required for the
189 // StateUpdatingCallback to work.
190 virtual void Minimize(const Options& options,
191 double* parameters,
192 Solver::Summary* summary) = 0;
193};
194
195} // namespace internal
196} // namespace ceres
197
198#endif // CERES_INTERNAL_MINIMIZER_H_