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 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 38 #include "ceres/types.h" 39 40 namespace ceres { 41 42 // This struct describes the state of the optimizer after each 43 // iteration of the minimization. 44 struct IterationSummary { IterationSummaryIterationSummary45 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), 53 step_norm(0.0), 54 eta(0.0), 55 linear_solver_iterations(0), 56 iteration_time_in_seconds(0.0), 57 step_solver_time_in_seconds(0.0), 58 cumulative_time_in_seconds(0.0) {} 59 60 // Current iteration number. 61 int32 iteration; 62 63 // Step was numerically valid, i.e., all values are finite and the 64 // step reduces the value of the linearized model. 65 // 66 // Note: step_is_valid is false when iteration = 0. 67 bool step_is_valid; 68 69 // Step did not reduce the value of the objective function 70 // sufficiently, but it was accepted because of the relaxed 71 // acceptance criterion used by the non-monotonic trust region 72 // algorithm. 73 // 74 // Note: step_is_nonmonotonic is false when iteration = 0; 75 bool step_is_nonmonotonic; 76 77 // Whether or not the minimizer accepted this step or not. If the 78 // ordinary trust region algorithm is used, this means that the 79 // relative reduction in the objective function value was greater 80 // than Solver::Options::min_relative_decrease. However, if the 81 // non-monotonic trust region algorithm is used 82 // (Solver::Options:use_nonmonotonic_steps = true), then even if the 83 // relative decrease is not sufficient, the algorithm may accept the 84 // step and the step is declared successful. 85 // 86 // Note: step_is_successful is false when iteration = 0. 87 bool step_is_successful; 88 89 // Value of the objective function. 90 double cost; 91 92 // Change in the value of the objective function in this 93 // iteration. This can be positive or negative. 94 double cost_change; 95 96 // Infinity norm of the gradient vector. 97 double gradient_max_norm; 98 99 // 2-norm of the size of the step computed by the optimization 100 // algorithm. 101 double step_norm; 102 103 // For trust region algorithms, the ratio of the actual change in 104 // cost and the change in the cost of the linearized approximation. 105 double relative_decrease; 106 107 // Size of the trust region at the end of the current iteration. For 108 // the Levenberg-Marquardt algorithm, the regularization parameter 109 // mu = 1.0 / trust_region_radius. 110 double trust_region_radius; 111 112 // For the inexact step Levenberg-Marquardt algorithm, this is the 113 // relative accuracy with which the Newton(LM) step is solved. This 114 // number affects only the iterative solvers capable of solving 115 // linear systems inexactly. Factorization-based exact solvers 116 // ignore it. 117 double eta; 118 119 // Number of iterations taken by the linear solver to solve for the 120 // Newton step. 121 int linear_solver_iterations; 122 123 // Time (in seconds) spent inside the minimizer loop in the current 124 // iteration. 125 double iteration_time_in_seconds; 126 127 // Time (in seconds) spent inside the trust region step solver. 128 double step_solver_time_in_seconds; 129 130 // Time (in seconds) since the user called Solve(). 131 double cumulative_time_in_seconds; 132 }; 133 134 // Interface for specifying callbacks that are executed at the end of 135 // each iteration of the Minimizer. The solver uses the return value 136 // of operator() to decide whether to continue solving or to 137 // terminate. The user can return three values. 138 // 139 // SOLVER_ABORT indicates that the callback detected an abnormal 140 // situation. The solver returns without updating the parameter blocks 141 // (unless Solver::Options::update_state_every_iteration is set 142 // true). Solver returns with Solver::Summary::termination_type set to 143 // USER_ABORT. 144 // 145 // SOLVER_TERMINATE_SUCCESSFULLY indicates that there is no need to 146 // optimize anymore (some user specified termination criterion has 147 // been met). Solver returns with Solver::Summary::termination_type 148 // set to USER_SUCCESS. 149 // 150 // SOLVER_CONTINUE indicates that the solver should continue 151 // optimizing. 152 // 153 // For example, the following Callback is used internally by Ceres to 154 // log the progress of the optimization. 155 // 156 // Callback for logging the state of the minimizer to STDERR or STDOUT 157 // depending on the user's preferences and logging level. 158 // 159 // class LoggingCallback : public IterationCallback { 160 // public: 161 // explicit LoggingCallback(bool log_to_stdout) 162 // : log_to_stdout_(log_to_stdout) {} 163 // 164 // ~LoggingCallback() {} 165 // 166 // CallbackReturnType operator()(const IterationSummary& summary) { 167 // const char* kReportRowFormat = 168 // "% 4d: f:% 8e d:% 3.2e g:% 3.2e h:% 3.2e " 169 // "rho:% 3.2e mu:% 3.2e eta:% 3.2e li:% 3d"; 170 // string output = StringPrintf(kReportRowFormat, 171 // summary.iteration, 172 // summary.cost, 173 // summary.cost_change, 174 // summary.gradient_max_norm, 175 // summary.step_norm, 176 // summary.relative_decrease, 177 // summary.trust_region_radius, 178 // summary.eta, 179 // summary.linear_solver_iterations); 180 // if (log_to_stdout_) { 181 // cout << output << endl; 182 // } else { 183 // VLOG(1) << output; 184 // } 185 // return SOLVER_CONTINUE; 186 // } 187 // 188 // private: 189 // const bool log_to_stdout_; 190 // }; 191 // 192 class IterationCallback { 193 public: ~IterationCallback()194 virtual ~IterationCallback() {} 195 virtual CallbackReturnType operator()(const IterationSummary& summary) = 0; 196 }; 197 198 } // namespace ceres 199 200 #endif // CERES_PUBLIC_ITERATION_CALLBACK_H_ 201