blob: 955e2addb7bd3e40cbc94bb0a52d9288f18af9d7 [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// When an iteration callback is specified, Ceres calls the callback
32// after each minimizer step (if the minimizer has not converged) and
33// passes it an IterationSummary object, defined below.
34
35#ifndef CERES_PUBLIC_ITERATION_CALLBACK_H_
36#define CERES_PUBLIC_ITERATION_CALLBACK_H_
37
Austin Schuh70cc9552019-01-21 19:46:48 -080038#include "ceres/internal/disable_warnings.h"
Austin Schuh3de38b02024-06-25 18:25:10 -070039#include "ceres/internal/export.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080040#include "ceres/types.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080041
42namespace ceres {
43
44// This struct describes the state of the optimizer after each
45// iteration of the minimization.
46struct CERES_EXPORT IterationSummary {
Austin Schuh70cc9552019-01-21 19:46:48 -080047 // Current iteration number.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080048 int iteration = 0;
Austin Schuh70cc9552019-01-21 19:46:48 -080049
50 // Step was numerically valid, i.e., all values are finite and the
51 // step reduces the value of the linearized model.
52 //
53 // Note: step_is_valid is always true when iteration = 0.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080054 bool step_is_valid = false;
Austin Schuh70cc9552019-01-21 19:46:48 -080055
56 // Step did not reduce the value of the objective function
57 // sufficiently, but it was accepted because of the relaxed
58 // acceptance criterion used by the non-monotonic trust region
59 // algorithm.
60 //
61 // Note: step_is_nonmonotonic is always false when iteration = 0;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080062 bool step_is_nonmonotonic = false;
Austin Schuh70cc9552019-01-21 19:46:48 -080063
64 // Whether or not the minimizer accepted this step or not. If the
65 // ordinary trust region algorithm is used, this means that the
66 // relative reduction in the objective function value was greater
67 // than Solver::Options::min_relative_decrease. However, if the
68 // non-monotonic trust region algorithm is used
69 // (Solver::Options:use_nonmonotonic_steps = true), then even if the
70 // relative decrease is not sufficient, the algorithm may accept the
71 // step and the step is declared successful.
72 //
73 // Note: step_is_successful is always true when iteration = 0.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080074 bool step_is_successful = false;
Austin Schuh70cc9552019-01-21 19:46:48 -080075
76 // Value of the objective function.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080077 double cost = 0.0;
Austin Schuh70cc9552019-01-21 19:46:48 -080078
79 // Change in the value of the objective function in this
80 // iteration. This can be positive or negative.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080081 double cost_change = 0.0;
Austin Schuh70cc9552019-01-21 19:46:48 -080082
83 // Infinity norm of the gradient vector.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080084 double gradient_max_norm = 0.0;
Austin Schuh70cc9552019-01-21 19:46:48 -080085
86 // 2-norm of the gradient vector.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080087 double gradient_norm = 0.0;
Austin Schuh70cc9552019-01-21 19:46:48 -080088
89 // 2-norm of the size of the step computed by the optimization
90 // algorithm.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080091 double step_norm = 0.0;
Austin Schuh70cc9552019-01-21 19:46:48 -080092
93 // For trust region algorithms, the ratio of the actual change in
94 // cost and the change in the cost of the linearized approximation.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080095 double relative_decrease = 0.0;
Austin Schuh70cc9552019-01-21 19:46:48 -080096
97 // Size of the trust region at the end of the current iteration. For
98 // the Levenberg-Marquardt algorithm, the regularization parameter
99 // mu = 1.0 / trust_region_radius.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800100 double trust_region_radius = 0.0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800101
102 // For the inexact step Levenberg-Marquardt algorithm, this is the
103 // relative accuracy with which the Newton(LM) step is solved. This
104 // number affects only the iterative solvers capable of solving
105 // linear systems inexactly. Factorization-based exact solvers
106 // ignore it.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800107 double eta = 0.0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800108
109 // Step sized computed by the line search algorithm.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800110 double step_size = 0.0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800111
112 // Number of function value evaluations used by the line search algorithm.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800113 int line_search_function_evaluations = 0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800114
115 // Number of function gradient evaluations used by the line search algorithm.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800116 int line_search_gradient_evaluations = 0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800117
118 // Number of iterations taken by the line search algorithm.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800119 int line_search_iterations = 0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800120
121 // Number of iterations taken by the linear solver to solve for the
122 // Newton step.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800123 int linear_solver_iterations = 0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800124
125 // All times reported below are wall times.
126
127 // Time (in seconds) spent inside the minimizer loop in the current
128 // iteration.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800129 double iteration_time_in_seconds = 0.0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800130
131 // Time (in seconds) spent inside the trust region step solver.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800132 double step_solver_time_in_seconds = 0.0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800133
134 // Time (in seconds) since the user called Solve().
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800135 double cumulative_time_in_seconds = 0.0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800136};
137
138// Interface for specifying callbacks that are executed at the end of
139// each iteration of the Minimizer. The solver uses the return value
140// of operator() to decide whether to continue solving or to
141// terminate. The user can return three values.
142//
143// SOLVER_ABORT indicates that the callback detected an abnormal
144// situation. The solver returns without updating the parameter blocks
145// (unless Solver::Options::update_state_every_iteration is set
146// true). Solver returns with Solver::Summary::termination_type set to
147// USER_ABORT.
148//
149// SOLVER_TERMINATE_SUCCESSFULLY indicates that there is no need to
150// optimize anymore (some user specified termination criterion has
151// been met). Solver returns with Solver::Summary::termination_type
152// set to USER_SUCCESS.
153//
154// SOLVER_CONTINUE indicates that the solver should continue
155// optimizing.
156//
157// For example, the following Callback is used internally by Ceres to
158// log the progress of the optimization.
159//
160// Callback for logging the state of the minimizer to STDERR or STDOUT
161// depending on the user's preferences and logging level.
162//
163// class LoggingCallback : public IterationCallback {
164// public:
165// explicit LoggingCallback(bool log_to_stdout)
166// : log_to_stdout_(log_to_stdout) {}
167//
Austin Schuh70cc9552019-01-21 19:46:48 -0800168// CallbackReturnType operator()(const IterationSummary& summary) {
169// const char* kReportRowFormat =
170// "% 4d: f:% 8e d:% 3.2e g:% 3.2e h:% 3.2e "
171// "rho:% 3.2e mu:% 3.2e eta:% 3.2e li:% 3d";
172// string output = StringPrintf(kReportRowFormat,
173// summary.iteration,
174// summary.cost,
175// summary.cost_change,
176// summary.gradient_max_norm,
177// summary.step_norm,
178// summary.relative_decrease,
179// summary.trust_region_radius,
180// summary.eta,
181// summary.linear_solver_iterations);
182// if (log_to_stdout_) {
183// cout << output << endl;
184// } else {
185// VLOG(1) << output;
186// }
187// return SOLVER_CONTINUE;
188// }
189//
190// private:
191// const bool log_to_stdout_;
192// };
193//
194class CERES_EXPORT IterationCallback {
195 public:
Austin Schuh3de38b02024-06-25 18:25:10 -0700196 virtual ~IterationCallback();
Austin Schuh70cc9552019-01-21 19:46:48 -0800197 virtual CallbackReturnType operator()(const IterationSummary& summary) = 0;
198};
199
200} // namespace ceres
201
202#include "ceres/internal/reenable_warnings.h"
203
204#endif // CERES_PUBLIC_ITERATION_CALLBACK_H_