blob: 29157d380f25feccdbef536a636f1e5fe31c8b27 [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.
44struct IterationSummary {
45 // Current iteration number.
46 int32 iteration;
47
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070048 // Step was numerically valid, i.e., all values are finite and the
49 // step reduces the value of the linearized model.
50 //
51 // Note: step_is_valid is false when iteration = 0.
52 bool step_is_valid;
53
Keir Mierle8ebb0732012-04-30 23:09:08 -070054 // Whether or not the algorithm made progress in this iteration.
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070055 //
56 // Note: step_is_successful is false when iteration = 0.
Keir Mierle8ebb0732012-04-30 23:09:08 -070057 bool step_is_successful;
58
59 // Value of the objective function.
60 double cost;
61
62 // Change in the value of the objective function in this
63 // iteration. This can be positive or negative. Negative change
64 // means that the step was not successful.
65 double cost_change;
66
67 // Infinity norm of the gradient vector.
68 double gradient_max_norm;
69
70 // 2-norm of the size of the step computed by the optimization
71 // algorithm.
72 double step_norm;
73
74 // For trust region algorithms, the ratio of the actual change in
75 // cost and the change in the cost of the linearized approximation.
76 double relative_decrease;
77
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070078 // Size of the trust region at the end of the current iteration. For
79 // the Levenberg-Marquardt algorithm, the regularization parameter
80 // mu = 1.0 / trust_region_radius.
81 double trust_region_radius;
Keir Mierle8ebb0732012-04-30 23:09:08 -070082
83 // For the inexact step Levenberg-Marquardt algorithm, this is the
84 // relative accuracy with which the Newton(LM) step is solved. This
85 // number affects only the iterative solvers capable of solving
86 // linear systems inexactly. Factorization-based exact solvers
87 // ignore it.
88 double eta;
89
90 // Number of iterations taken by the linear solver to solve for the
91 // Newton step.
92 int linear_solver_iterations;
93
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070094 // Time (in seconds) spent inside the minimizer loop in the current
95 // iteration.
Sameer Agarwalfa015192012-06-11 14:21:42 -070096 double iteration_time_in_seconds;
Keir Mierle8ebb0732012-04-30 23:09:08 -070097
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -070098 // Time (in seconds) spent inside the trust region step solver.
Sameer Agarwalfa015192012-06-11 14:21:42 -070099 double step_solver_time_in_seconds;
100
101 // Time (in seconds) since the user called Solve().
102 double cumulative_time_in_seconds;
Keir Mierle8ebb0732012-04-30 23:09:08 -0700103};
104
105// Interface for specifying callbacks that are executed at the end of
106// each iteration of the Minimizer. The solver uses the return value
107// of operator() to decide whether to continue solving or to
108// terminate. The user can return three values.
109//
110// SOLVER_ABORT indicates that the callback detected an abnormal
111// situation. The solver returns without updating the parameter blocks
112// (unless Solver::Options::update_state_every_iteration is set
113// true). Solver returns with Solver::Summary::termination_type set to
114// USER_ABORT.
115//
116// SOLVER_TERMINATE_SUCCESSFULLY indicates that there is no need to
117// optimize anymore (some user specified termination criterion has
118// been met). Solver returns with Solver::Summary::termination_type
119// set to USER_SUCCESS.
120//
121// SOLVER_CONTINUE indicates that the solver should continue
122// optimizing.
123//
124// For example, the following Callback is used internally by Ceres to
125// log the progress of the optimization.
126//
127// Callback for logging the state of the minimizer to STDERR or STDOUT
128// depending on the user's preferences and logging level.
129//
130// class LoggingCallback : public IterationCallback {
131// public:
132// explicit LoggingCallback(bool log_to_stdout)
133// : log_to_stdout_(log_to_stdout) {}
134//
135// ~LoggingCallback() {}
136//
137// CallbackReturnType operator()(const IterationSummary& summary) {
138// const char* kReportRowFormat =
139// "% 4d: f:% 8e d:% 3.2e g:% 3.2e h:% 3.2e "
140// "rho:% 3.2e mu:% 3.2e eta:% 3.2e li:% 3d";
141// string output = StringPrintf(kReportRowFormat,
142// summary.iteration,
143// summary.cost,
144// summary.cost_change,
145// summary.gradient_max_norm,
146// summary.step_norm,
147// summary.relative_decrease,
Sameer Agarwalaa9a83c2012-05-29 17:40:17 -0700148// summary.trust_region_radius,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700149// summary.eta,
150// summary.linear_solver_iterations);
151// if (log_to_stdout_) {
152// cout << output << endl;
153// } else {
154// VLOG(1) << output;
155// }
156// return SOLVER_CONTINUE;
157// }
158//
159// private:
160// const bool log_to_stdout_;
161// };
162//
163class IterationCallback {
164 public:
165 virtual ~IterationCallback() {}
166 virtual CallbackReturnType operator()(const IterationSummary& summary) = 0;
167};
168
169} // namespace ceres
170
171#endif // CERES_PUBLIC_ITERATION_CALLBACK_H_