~ubuntu-branches/ubuntu/raring/ceres-solver/raring

« back to all changes in this revision

Viewing changes to include/ceres/iteration_callback.h

  • Committer: Package Import Robot
  • Author(s): Koichi Akabe
  • Date: 2012-06-04 07:15:43 UTC
  • Revision ID: package-import@ubuntu.com-20120604071543-zx6uthupvmtqn3k2
Tags: upstream-1.1.1
ImportĀ upstreamĀ versionĀ 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 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
//
 
31
// When an iteration callback is specified, Ceres calls the callback after each
 
32
// optimizer step and pass it an IterationSummary object, defined below.
 
33
 
 
34
#ifndef CERES_PUBLIC_ITERATION_CALLBACK_H_
 
35
#define CERES_PUBLIC_ITERATION_CALLBACK_H_
 
36
 
 
37
#include "ceres/types.h"
 
38
 
 
39
namespace ceres {
 
40
 
 
41
// This struct describes the state of the optimizer after each
 
42
// iteration of the minimization.
 
43
struct IterationSummary {
 
44
  // Current iteration number.
 
45
  int32 iteration;
 
46
 
 
47
  // Whether or not the algorithm made progress in this iteration.
 
48
  bool step_is_successful;
 
49
 
 
50
  // Value of the objective function.
 
51
  double cost;
 
52
 
 
53
  // Change in the value of the objective function in this
 
54
  // iteration. This can be positive or negative. Negative change
 
55
  // means that the step was not successful.
 
56
  double cost_change;
 
57
 
 
58
  // Infinity norm of the gradient vector.
 
59
  double gradient_max_norm;
 
60
 
 
61
  // 2-norm of the size of the step computed by the optimization
 
62
  // algorithm.
 
63
  double step_norm;
 
64
 
 
65
  // For trust region algorithms, the ratio of the actual change in
 
66
  // cost and the change in the cost of the linearized approximation.
 
67
  double relative_decrease;
 
68
 
 
69
  // Value of the regularization parameter for Levenberg-Marquardt
 
70
  // algorithm at the end of the current iteration.
 
71
  double mu;
 
72
 
 
73
  // For the inexact step Levenberg-Marquardt algorithm, this is the
 
74
  // relative accuracy with which the Newton(LM) step is solved. This
 
75
  // number affects only the iterative solvers capable of solving
 
76
  // linear systems inexactly. Factorization-based exact solvers
 
77
  // ignore it.
 
78
  double eta;
 
79
 
 
80
  // Number of iterations taken by the linear solver to solve for the
 
81
  // Newton step.
 
82
  int linear_solver_iterations;
 
83
 
 
84
  // TODO(sameeragarwal): Change to use a higher precision timer using
 
85
  // clock_gettime.
 
86
  // Time (in seconds) spent inside the linear least squares solver.
 
87
  int iteration_time_sec;
 
88
 
 
89
  // Time (in seconds) spent inside the linear least squares solver.
 
90
  int linear_solver_time_sec;
 
91
};
 
92
 
 
93
// Interface for specifying callbacks that are executed at the end of
 
94
// each iteration of the Minimizer. The solver uses the return value
 
95
// of operator() to decide whether to continue solving or to
 
96
// terminate. The user can return three values.
 
97
//
 
98
// SOLVER_ABORT indicates that the callback detected an abnormal
 
99
// situation. The solver returns without updating the parameter blocks
 
100
// (unless Solver::Options::update_state_every_iteration is set
 
101
// true). Solver returns with Solver::Summary::termination_type set to
 
102
// USER_ABORT.
 
103
//
 
104
// SOLVER_TERMINATE_SUCCESSFULLY indicates that there is no need to
 
105
// optimize anymore (some user specified termination criterion has
 
106
// been met). Solver returns with Solver::Summary::termination_type
 
107
// set to USER_SUCCESS.
 
108
//
 
109
// SOLVER_CONTINUE indicates that the solver should continue
 
110
// optimizing.
 
111
//
 
112
// For example, the following Callback is used internally by Ceres to
 
113
// log the progress of the optimization.
 
114
//
 
115
// Callback for logging the state of the minimizer to STDERR or STDOUT
 
116
// depending on the user's preferences and logging level.
 
117
//
 
118
//   class LoggingCallback : public IterationCallback {
 
119
//    public:
 
120
//     explicit LoggingCallback(bool log_to_stdout)
 
121
//         : log_to_stdout_(log_to_stdout) {}
 
122
//
 
123
//     ~LoggingCallback() {}
 
124
//
 
125
//     CallbackReturnType operator()(const IterationSummary& summary) {
 
126
//       const char* kReportRowFormat =
 
127
//           "% 4d: f:% 8e d:% 3.2e g:% 3.2e h:% 3.2e "
 
128
//           "rho:% 3.2e mu:% 3.2e eta:% 3.2e li:% 3d";
 
129
//       string output = StringPrintf(kReportRowFormat,
 
130
//                                    summary.iteration,
 
131
//                                    summary.cost,
 
132
//                                    summary.cost_change,
 
133
//                                    summary.gradient_max_norm,
 
134
//                                    summary.step_norm,
 
135
//                                    summary.relative_decrease,
 
136
//                                    summary.mu,
 
137
//                                    summary.eta,
 
138
//                                    summary.linear_solver_iterations);
 
139
//       if (log_to_stdout_) {
 
140
//         cout << output << endl;
 
141
//       } else {
 
142
//         VLOG(1) << output;
 
143
//       }
 
144
//       return SOLVER_CONTINUE;
 
145
//     }
 
146
//
 
147
//    private:
 
148
//     const bool log_to_stdout_;
 
149
//   };
 
150
//
 
151
class IterationCallback {
 
152
 public:
 
153
  virtual ~IterationCallback() {}
 
154
  virtual CallbackReturnType operator()(const IterationSummary& summary) = 0;
 
155
};
 
156
 
 
157
}  // namespace ceres
 
158
 
 
159
#endif  // CERES_PUBLIC_ITERATION_CALLBACK_H_