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