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