blob: 5eca392da36aba7cc7859d82d0e84a9e89ede0ba [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//
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070031// 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.
Keir Mierle8ebb0732012-04-30 23:09:08 -070034
35#ifndef CERES_PUBLIC_ITERATION_CALLBACK_H_
36#define CERES_PUBLIC_ITERATION_CALLBACK_H_
37
38#include "ceres/types.h"
39
40namespace ceres {
41
42// This struct describes the state of the optimizer after each
43// iteration of the minimization.
Björn Piltz5d7eed82014-04-23 22:13:37 +020044struct CERES_EXPORT IterationSummary {
Sameer Agarwalb23fd4e2012-09-25 09:04:41 -070045 IterationSummary()
46 : iteration(0),
47 step_is_valid(false),
48 step_is_nonmonotonic(false),
49 step_is_successful(false),
50 cost(0.0),
51 cost_change(0.0),
52 gradient_max_norm(0.0),
Sameer Agarwal4d2df0c2013-09-13 12:54:03 -070053 gradient_norm(0.0),
Sameer Agarwalb23fd4e2012-09-25 09:04:41 -070054 step_norm(0.0),
55 eta(0.0),
Sameer Agarwalf4d01642012-11-26 12:55:58 -080056 step_size(0.0),
57 line_search_function_evaluations(0),
Alex Stewart9aa0e3c2013-07-05 20:22:37 +010058 line_search_gradient_evaluations(0),
59 line_search_iterations(0),
Sameer Agarwalb23fd4e2012-09-25 09:04:41 -070060 linear_solver_iterations(0),
61 iteration_time_in_seconds(0.0),
62 step_solver_time_in_seconds(0.0),
63 cumulative_time_in_seconds(0.0) {}
64
Keir Mierle8ebb0732012-04-30 23:09:08 -070065 // Current iteration number.
66 int32 iteration;
67
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070068 // Step was numerically valid, i.e., all values are finite and the
69 // step reduces the value of the linearized model.
70 //
71 // Note: step_is_valid is false when iteration = 0.
72 bool step_is_valid;
73
Sameer Agarwalb23fd4e2012-09-25 09:04:41 -070074 // Step did not reduce the value of the objective function
75 // sufficiently, but it was accepted because of the relaxed
76 // acceptance criterion used by the non-monotonic trust region
77 // algorithm.
78 //
79 // Note: step_is_nonmonotonic is false when iteration = 0;
80 bool step_is_nonmonotonic;
81
82 // Whether or not the minimizer accepted this step or not. If the
83 // ordinary trust region algorithm is used, this means that the
84 // relative reduction in the objective function value was greater
85 // than Solver::Options::min_relative_decrease. However, if the
86 // non-monotonic trust region algorithm is used
87 // (Solver::Options:use_nonmonotonic_steps = true), then even if the
88 // relative decrease is not sufficient, the algorithm may accept the
89 // step and the step is declared successful.
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070090 //
91 // Note: step_is_successful is false when iteration = 0.
Keir Mierle8ebb0732012-04-30 23:09:08 -070092 bool step_is_successful;
93
94 // Value of the objective function.
95 double cost;
96
97 // Change in the value of the objective function in this
Sameer Agarwalb23fd4e2012-09-25 09:04:41 -070098 // iteration. This can be positive or negative.
Keir Mierle8ebb0732012-04-30 23:09:08 -070099 double cost_change;
100
101 // Infinity norm of the gradient vector.
102 double gradient_max_norm;
103
Sameer Agarwal4d2df0c2013-09-13 12:54:03 -0700104 // 2-norm of the gradient vector.
105 double gradient_norm;
106
Keir Mierle8ebb0732012-04-30 23:09:08 -0700107 // 2-norm of the size of the step computed by the optimization
108 // algorithm.
109 double step_norm;
110
111 // For trust region algorithms, the ratio of the actual change in
112 // cost and the change in the cost of the linearized approximation.
113 double relative_decrease;
114
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700115 // Size of the trust region at the end of the current iteration. For
116 // the Levenberg-Marquardt algorithm, the regularization parameter
117 // mu = 1.0 / trust_region_radius.
118 double trust_region_radius;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700119
120 // For the inexact step Levenberg-Marquardt algorithm, this is the
121 // relative accuracy with which the Newton(LM) step is solved. This
122 // number affects only the iterative solvers capable of solving
123 // linear systems inexactly. Factorization-based exact solvers
124 // ignore it.
125 double eta;
126
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800127 // Step sized computed by the line search algorithm.
128 double step_size;
129
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100130 // Number of function value evaluations used by the line search algorithm.
Sameer Agarwalf4d01642012-11-26 12:55:58 -0800131 int line_search_function_evaluations;
132
Alex Stewart9aa0e3c2013-07-05 20:22:37 +0100133 // Number of function gradient evaluations used by the line search algorithm.
134 int line_search_gradient_evaluations;
135
136 // Number of iterations taken by the line search algorithm.
137 int line_search_iterations;
138
Keir Mierle8ebb0732012-04-30 23:09:08 -0700139 // Number of iterations taken by the linear solver to solve for the
140 // Newton step.
141 int linear_solver_iterations;
142
Sameer Agarwalf0b071b2013-05-31 13:22:51 -0700143 // All times reported below are wall times.
144
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700145 // Time (in seconds) spent inside the minimizer loop in the current
146 // iteration.
Sameer Agarwalfa015192012-06-11 14:21:42 -0700147 double iteration_time_in_seconds;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700148
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700149 // Time (in seconds) spent inside the trust region step solver.
Sameer Agarwalfa015192012-06-11 14:21:42 -0700150 double step_solver_time_in_seconds;
151
152 // Time (in seconds) since the user called Solve().
153 double cumulative_time_in_seconds;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700154};
155
156// Interface for specifying callbacks that are executed at the end of
157// each iteration of the Minimizer. The solver uses the return value
158// of operator() to decide whether to continue solving or to
159// terminate. The user can return three values.
160//
161// SOLVER_ABORT indicates that the callback detected an abnormal
162// situation. The solver returns without updating the parameter blocks
163// (unless Solver::Options::update_state_every_iteration is set
164// true). Solver returns with Solver::Summary::termination_type set to
165// USER_ABORT.
166//
167// SOLVER_TERMINATE_SUCCESSFULLY indicates that there is no need to
168// optimize anymore (some user specified termination criterion has
169// been met). Solver returns with Solver::Summary::termination_type
170// set to USER_SUCCESS.
171//
172// SOLVER_CONTINUE indicates that the solver should continue
173// optimizing.
174//
175// For example, the following Callback is used internally by Ceres to
176// log the progress of the optimization.
177//
178// Callback for logging the state of the minimizer to STDERR or STDOUT
179// depending on the user's preferences and logging level.
180//
181// class LoggingCallback : public IterationCallback {
182// public:
183// explicit LoggingCallback(bool log_to_stdout)
184// : log_to_stdout_(log_to_stdout) {}
185//
186// ~LoggingCallback() {}
187//
188// CallbackReturnType operator()(const IterationSummary& summary) {
189// const char* kReportRowFormat =
190// "% 4d: f:% 8e d:% 3.2e g:% 3.2e h:% 3.2e "
191// "rho:% 3.2e mu:% 3.2e eta:% 3.2e li:% 3d";
192// string output = StringPrintf(kReportRowFormat,
193// summary.iteration,
194// summary.cost,
195// summary.cost_change,
196// summary.gradient_max_norm,
197// summary.step_norm,
198// summary.relative_decrease,
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700199// summary.trust_region_radius,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700200// summary.eta,
201// summary.linear_solver_iterations);
202// if (log_to_stdout_) {
203// cout << output << endl;
204// } else {
205// VLOG(1) << output;
206// }
207// return SOLVER_CONTINUE;
208// }
209//
210// private:
211// const bool log_to_stdout_;
212// };
213//
Björn Piltz5d7eed82014-04-23 22:13:37 +0200214class CERES_EXPORT IterationCallback {
Keir Mierle8ebb0732012-04-30 23:09:08 -0700215 public:
216 virtual ~IterationCallback() {}
217 virtual CallbackReturnType operator()(const IterationSummary& summary) = 0;
218};
219
220} // namespace ceres
221
222#endif // CERES_PUBLIC_ITERATION_CALLBACK_H_