Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 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 | // Generic loop for line search based optimization algorithms. |
| 32 | // |
| 33 | // This is primarily inpsired by the minFunc packaged written by Mark |
| 34 | // Schmidt. |
| 35 | // |
| 36 | // http://www.di.ens.fr/~mschmidt/Software/minFunc.html |
| 37 | // |
| 38 | // For details on the theory and implementation see "Numerical |
| 39 | // Optimization" by Nocedal & Wright. |
| 40 | |
Sameer Agarwal | 8140f0f | 2013-03-12 09:45:08 -0700 | [diff] [blame] | 41 | #ifndef CERES_NO_LINE_SEARCH_MINIMIZER |
| 42 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 43 | #include "ceres/line_search_minimizer.h" |
| 44 | |
| 45 | #include <algorithm> |
| 46 | #include <cstdlib> |
| 47 | #include <cmath> |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 48 | #include <string> |
| 49 | #include <vector> |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 50 | |
| 51 | #include "Eigen/Dense" |
| 52 | #include "ceres/array_utils.h" |
| 53 | #include "ceres/evaluator.h" |
| 54 | #include "ceres/internal/eigen.h" |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 55 | #include "ceres/internal/port.h" |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 56 | #include "ceres/internal/scoped_ptr.h" |
| 57 | #include "ceres/line_search.h" |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 58 | #include "ceres/line_search_direction.h" |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 59 | #include "ceres/stringprintf.h" |
| 60 | #include "ceres/types.h" |
| 61 | #include "ceres/wall_time.h" |
| 62 | #include "glog/logging.h" |
| 63 | |
| 64 | namespace ceres { |
| 65 | namespace internal { |
| 66 | namespace { |
| 67 | // Small constant for various floating point issues. |
Sameer Agarwal | c89ea4b | 2013-01-09 16:09:35 -0800 | [diff] [blame] | 68 | // TODO(sameeragarwal): Change to a better name if this has only one |
| 69 | // use. |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 70 | const double kEpsilon = 1e-12; |
Sameer Agarwal | c89ea4b | 2013-01-09 16:09:35 -0800 | [diff] [blame] | 71 | |
Sameer Agarwal | a8d9ca8 | 2013-09-05 22:08:57 -0700 | [diff] [blame] | 72 | // TODO(sameeragarwal): I think there is a small bug here, in that if |
| 73 | // the evaluation fails, then the state can contain garbage. Look at |
| 74 | // this more carefully. |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 75 | bool Evaluate(Evaluator* evaluator, |
| 76 | const Vector& x, |
| 77 | LineSearchMinimizer::State* state) { |
| 78 | const bool status = evaluator->Evaluate(x.data(), |
| 79 | &(state->cost), |
| 80 | NULL, |
| 81 | state->gradient.data(), |
| 82 | NULL); |
| 83 | if (status) { |
| 84 | state->gradient_squared_norm = state->gradient.squaredNorm(); |
| 85 | state->gradient_max_norm = state->gradient.lpNorm<Eigen::Infinity>(); |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 86 | } |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 87 | |
| 88 | return status; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 91 | } // namespace |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 92 | |
| 93 | void LineSearchMinimizer::Minimize(const Minimizer::Options& options, |
| 94 | double* parameters, |
| 95 | Solver::Summary* summary) { |
Sameer Agarwal | 2d785d6 | 2013-09-04 23:34:29 -0700 | [diff] [blame] | 96 | const bool is_not_silent = !options.is_silent; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 97 | double start_time = WallTimeInSeconds(); |
| 98 | double iteration_start_time = start_time; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 99 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 100 | Evaluator* evaluator = CHECK_NOTNULL(options.evaluator); |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 101 | const int num_parameters = evaluator->NumParameters(); |
| 102 | const int num_effective_parameters = evaluator->NumEffectiveParameters(); |
| 103 | |
| 104 | summary->termination_type = NO_CONVERGENCE; |
| 105 | summary->num_successful_steps = 0; |
| 106 | summary->num_unsuccessful_steps = 0; |
| 107 | |
| 108 | VectorRef x(parameters, num_parameters); |
| 109 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 110 | State current_state(num_parameters, num_effective_parameters); |
| 111 | State previous_state(num_parameters, num_effective_parameters); |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 112 | |
| 113 | Vector delta(num_effective_parameters); |
| 114 | Vector x_plus_delta(num_parameters); |
| 115 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 116 | IterationSummary iteration_summary; |
| 117 | iteration_summary.iteration = 0; |
| 118 | iteration_summary.step_is_valid = false; |
| 119 | iteration_summary.step_is_successful = false; |
| 120 | iteration_summary.cost_change = 0.0; |
| 121 | iteration_summary.gradient_max_norm = 0.0; |
Sameer Agarwal | 4d2df0c | 2013-09-13 12:54:03 -0700 | [diff] [blame] | 122 | iteration_summary.gradient_norm = 0.0; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 123 | iteration_summary.step_norm = 0.0; |
| 124 | iteration_summary.linear_solver_iterations = 0; |
| 125 | iteration_summary.step_solver_time_in_seconds = 0; |
| 126 | |
| 127 | // Do initial cost and Jacobian evaluation. |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 128 | if (!Evaluate(evaluator, x, ¤t_state)) { |
Sameer Agarwal | 2d785d6 | 2013-09-04 23:34:29 -0700 | [diff] [blame] | 129 | LOG_IF(WARNING, is_not_silent) |
| 130 | << "Terminating: Cost and gradient evaluation failed."; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 131 | summary->termination_type = NUMERICAL_FAILURE; |
| 132 | return; |
| 133 | } |
| 134 | |
Sameer Agarwal | f102a68 | 2013-02-11 15:08:40 -0800 | [diff] [blame] | 135 | summary->initial_cost = current_state.cost + summary->fixed_cost; |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 136 | iteration_summary.cost = current_state.cost + summary->fixed_cost; |
Sameer Agarwal | f102a68 | 2013-02-11 15:08:40 -0800 | [diff] [blame] | 137 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 138 | iteration_summary.gradient_max_norm = current_state.gradient_max_norm; |
Sameer Agarwal | 4d2df0c | 2013-09-13 12:54:03 -0700 | [diff] [blame] | 139 | iteration_summary.gradient_norm = sqrt(current_state.gradient_squared_norm); |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 140 | |
| 141 | // The initial gradient max_norm is bounded from below so that we do |
| 142 | // not divide by zero. |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 143 | const double initial_gradient_max_norm = |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 144 | max(iteration_summary.gradient_max_norm, kEpsilon); |
| 145 | const double absolute_gradient_tolerance = |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 146 | options.gradient_tolerance * initial_gradient_max_norm; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 147 | |
| 148 | if (iteration_summary.gradient_max_norm <= absolute_gradient_tolerance) { |
Sameer Agarwal | 2d785d6 | 2013-09-04 23:34:29 -0700 | [diff] [blame] | 149 | VLOG_IF(1, is_not_silent) |
| 150 | << "Terminating: Gradient tolerance reached." |
| 151 | << "Relative gradient max norm: " |
| 152 | << iteration_summary.gradient_max_norm / initial_gradient_max_norm |
| 153 | << " <= " << options.gradient_tolerance; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 154 | summary->termination_type = GRADIENT_TOLERANCE; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
| 158 | iteration_summary.iteration_time_in_seconds = |
| 159 | WallTimeInSeconds() - iteration_start_time; |
| 160 | iteration_summary.cumulative_time_in_seconds = |
| 161 | WallTimeInSeconds() - start_time |
| 162 | + summary->preprocessor_time_in_seconds; |
| 163 | summary->iterations.push_back(iteration_summary); |
| 164 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 165 | LineSearchDirection::Options line_search_direction_options; |
| 166 | line_search_direction_options.num_parameters = num_effective_parameters; |
| 167 | line_search_direction_options.type = options.line_search_direction_type; |
| 168 | line_search_direction_options.nonlinear_conjugate_gradient_type = |
| 169 | options.nonlinear_conjugate_gradient_type; |
| 170 | line_search_direction_options.max_lbfgs_rank = options.max_lbfgs_rank; |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 171 | line_search_direction_options.use_approximate_eigenvalue_bfgs_scaling = |
| 172 | options.use_approximate_eigenvalue_bfgs_scaling; |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 173 | scoped_ptr<LineSearchDirection> line_search_direction( |
| 174 | LineSearchDirection::Create(line_search_direction_options)); |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 175 | |
| 176 | LineSearchFunction line_search_function(evaluator); |
Sameer Agarwal | 0924401 | 2013-06-30 14:33:23 -0700 | [diff] [blame] | 177 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 178 | LineSearch::Options line_search_options; |
Sameer Agarwal | 0924401 | 2013-06-30 14:33:23 -0700 | [diff] [blame] | 179 | line_search_options.interpolation_type = |
| 180 | options.line_search_interpolation_type; |
| 181 | line_search_options.min_step_size = options.min_line_search_step_size; |
| 182 | line_search_options.sufficient_decrease = |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 183 | options.line_search_sufficient_function_decrease; |
| 184 | line_search_options.max_step_contraction = |
| 185 | options.max_line_search_step_contraction; |
| 186 | line_search_options.min_step_contraction = |
| 187 | options.min_line_search_step_contraction; |
| 188 | line_search_options.max_num_iterations = |
| 189 | options.max_num_line_search_step_size_iterations; |
| 190 | line_search_options.sufficient_curvature_decrease = |
| 191 | options.line_search_sufficient_curvature_decrease; |
| 192 | line_search_options.max_step_expansion = |
| 193 | options.max_line_search_step_expansion; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 194 | line_search_options.function = &line_search_function; |
| 195 | |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 196 | scoped_ptr<LineSearch> |
| 197 | line_search(LineSearch::Create(options.line_search_type, |
| 198 | line_search_options, |
| 199 | &summary->error)); |
| 200 | if (line_search.get() == NULL) { |
Sameer Agarwal | 2d785d6 | 2013-09-04 23:34:29 -0700 | [diff] [blame] | 201 | LOG_IF(ERROR, is_not_silent) |
| 202 | << "Ceres bug: Unable to create a LineSearch object, please " |
| 203 | << "contact the developers!, error: " << summary->error; |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 204 | summary->termination_type = DID_NOT_RUN; |
| 205 | return; |
| 206 | } |
| 207 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 208 | LineSearch::Summary line_search_summary; |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 209 | int num_line_search_direction_restarts = 0; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 210 | |
| 211 | while (true) { |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 212 | if (!RunCallbacks(options.callbacks, iteration_summary, summary)) { |
| 213 | return; |
| 214 | } |
| 215 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 216 | iteration_start_time = WallTimeInSeconds(); |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 217 | if (iteration_summary.iteration >= options.max_num_iterations) { |
Sameer Agarwal | 2d785d6 | 2013-09-04 23:34:29 -0700 | [diff] [blame] | 218 | VLOG_IF(1, is_not_silent) |
| 219 | << "Terminating: Maximum number of iterations reached."; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 220 | summary->termination_type = NO_CONVERGENCE; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 221 | break; |
| 222 | } |
| 223 | |
| 224 | const double total_solver_time = iteration_start_time - start_time + |
| 225 | summary->preprocessor_time_in_seconds; |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 226 | if (total_solver_time >= options.max_solver_time_in_seconds) { |
Sameer Agarwal | 2d785d6 | 2013-09-04 23:34:29 -0700 | [diff] [blame] | 227 | VLOG_IF(1, is_not_silent) << "Terminating: Maximum solver time reached."; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 228 | summary->termination_type = NO_CONVERGENCE; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 229 | break; |
| 230 | } |
| 231 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 232 | iteration_summary = IterationSummary(); |
| 233 | iteration_summary.iteration = summary->iterations.back().iteration + 1; |
Sameer Agarwal | 1c70ae9 | 2013-06-30 12:50:43 -0700 | [diff] [blame] | 234 | iteration_summary.step_is_valid = false; |
| 235 | iteration_summary.step_is_successful = false; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 236 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 237 | bool line_search_status = true; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 238 | if (iteration_summary.iteration == 1) { |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 239 | current_state.search_direction = -current_state.gradient; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 240 | } else { |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 241 | line_search_status = line_search_direction->NextDirection( |
| 242 | previous_state, |
| 243 | current_state, |
| 244 | ¤t_state.search_direction); |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 247 | if (!line_search_status && |
| 248 | num_line_search_direction_restarts >= |
| 249 | options.max_num_line_search_direction_restarts) { |
| 250 | // Line search direction failed to generate a new direction, and we |
| 251 | // have already reached our specified maximum number of restarts, |
| 252 | // terminate optimization. |
| 253 | summary->error = |
| 254 | StringPrintf("Line search direction failure: specified " |
| 255 | "max_num_line_search_direction_restarts: %d reached.", |
| 256 | options.max_num_line_search_direction_restarts); |
Sameer Agarwal | 2d785d6 | 2013-09-04 23:34:29 -0700 | [diff] [blame] | 257 | LOG_IF(WARNING, is_not_silent) << summary->error |
| 258 | << " terminating optimization."; |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 259 | summary->termination_type = NUMERICAL_FAILURE; |
| 260 | break; |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 261 | } else if (!line_search_status) { |
| 262 | // Restart line search direction with gradient descent on first iteration |
| 263 | // as we have not yet reached our maximum number of restarts. |
| 264 | CHECK_LT(num_line_search_direction_restarts, |
| 265 | options.max_num_line_search_direction_restarts); |
| 266 | |
| 267 | ++num_line_search_direction_restarts; |
Sameer Agarwal | 2d785d6 | 2013-09-04 23:34:29 -0700 | [diff] [blame] | 268 | LOG_IF(WARNING, is_not_silent) |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 269 | << "Line search direction algorithm: " |
Sameer Agarwal | 2d785d6 | 2013-09-04 23:34:29 -0700 | [diff] [blame] | 270 | << LineSearchDirectionTypeToString( |
| 271 | options.line_search_direction_type) |
| 272 | << ", failed to produce a valid new direction at " |
| 273 | << "iteration: " << iteration_summary.iteration |
| 274 | << ". Restarting, number of restarts: " |
| 275 | << num_line_search_direction_restarts << " / " |
| 276 | << options.max_num_line_search_direction_restarts |
| 277 | << " [max]."; |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 278 | line_search_direction.reset( |
| 279 | LineSearchDirection::Create(line_search_direction_options)); |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 280 | current_state.search_direction = -current_state.gradient; |
| 281 | } |
| 282 | |
| 283 | line_search_function.Init(x, current_state.search_direction); |
| 284 | current_state.directional_derivative = |
| 285 | current_state.gradient.dot(current_state.search_direction); |
| 286 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 287 | // TODO(sameeragarwal): Refactor this into its own object and add |
| 288 | // explanations for the various choices. |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 289 | // |
| 290 | // Note that we use !line_search_status to ensure that we treat cases when |
| 291 | // we restarted the line search direction equivalently to the first |
| 292 | // iteration. |
| 293 | const double initial_step_size = |
| 294 | (iteration_summary.iteration == 1 || !line_search_status) |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 295 | ? min(1.0, 1.0 / current_state.gradient_max_norm) |
| 296 | : min(1.0, 2.0 * (current_state.cost - previous_state.cost) / |
| 297 | current_state.directional_derivative); |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 298 | // By definition, we should only ever go forwards along the specified search |
| 299 | // direction in a line search, most likely cause for this being violated |
| 300 | // would be a numerical failure in the line search direction calculation. |
| 301 | if (initial_step_size < 0.0) { |
| 302 | summary->error = |
| 303 | StringPrintf("Numerical failure in line search, initial_step_size is " |
| 304 | "negative: %.5e, directional_derivative: %.5e, " |
| 305 | "(current_cost - previous_cost): %.5e", |
| 306 | initial_step_size, current_state.directional_derivative, |
| 307 | (current_state.cost - previous_state.cost)); |
Sameer Agarwal | 2d785d6 | 2013-09-04 23:34:29 -0700 | [diff] [blame] | 308 | LOG_IF(WARNING, is_not_silent) << summary->error; |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 309 | summary->termination_type = NUMERICAL_FAILURE; |
| 310 | break; |
| 311 | } |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 312 | |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 313 | line_search->Search(initial_step_size, |
| 314 | current_state.cost, |
| 315 | current_state.directional_derivative, |
| 316 | &line_search_summary); |
Alex Stewart | 7124c34 | 2013-11-07 16:10:02 +0000 | [diff] [blame] | 317 | if (!line_search_summary.success) { |
| 318 | summary->error = |
| 319 | StringPrintf("Numerical failure in line search, failed to find " |
| 320 | "a valid step size, (did not run out of iterations) " |
| 321 | "using initial_step_size: %.5e, initial_cost: %.5e, " |
| 322 | "initial_gradient: %.5e.", |
| 323 | initial_step_size, current_state.cost, |
| 324 | current_state.directional_derivative); |
| 325 | LOG_IF(WARNING, is_not_silent) << summary->error; |
| 326 | summary->termination_type = NUMERICAL_FAILURE; |
| 327 | break; |
| 328 | } |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 329 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 330 | current_state.step_size = line_search_summary.optimal_step_size; |
| 331 | delta = current_state.step_size * current_state.search_direction; |
| 332 | |
| 333 | previous_state = current_state; |
Sameer Agarwal | 1c70ae9 | 2013-06-30 12:50:43 -0700 | [diff] [blame] | 334 | iteration_summary.step_solver_time_in_seconds = |
| 335 | WallTimeInSeconds() - iteration_start_time; |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 336 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 337 | // TODO(sameeragarwal): Collect stats. |
Alex Stewart | 7124c34 | 2013-11-07 16:10:02 +0000 | [diff] [blame] | 338 | // |
| 339 | // TODO(sameeragarwal): This call to Plus() directly updates the parameter |
| 340 | // vector via the VectorRef x. This is incorrect as we check the |
| 341 | // gradient and cost changes to determine if the step is accepted |
| 342 | // later, as such we could mutate x with a step that is not |
| 343 | // subsequently accepted, thus it is possible that |
| 344 | // summary->iterations.end()->x != x at termination. |
Sameer Agarwal | a8d9ca8 | 2013-09-05 22:08:57 -0700 | [diff] [blame] | 345 | if (!evaluator->Plus(x.data(), delta.data(), x_plus_delta.data())) { |
Sameer Agarwal | 2d785d6 | 2013-09-04 23:34:29 -0700 | [diff] [blame] | 346 | LOG_IF(WARNING, is_not_silent) |
| 347 | << "x_plus_delta = Plus(x, delta) failed. "; |
Sameer Agarwal | a8d9ca8 | 2013-09-05 22:08:57 -0700 | [diff] [blame] | 348 | } else if (!Evaluate(evaluator, x_plus_delta, ¤t_state)) { |
Sameer Agarwal | 2d785d6 | 2013-09-04 23:34:29 -0700 | [diff] [blame] | 349 | LOG_IF(WARNING, is_not_silent) << "Step failed to evaluate. "; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 350 | } else { |
| 351 | x = x_plus_delta; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 352 | } |
| 353 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 354 | iteration_summary.gradient_max_norm = current_state.gradient_max_norm; |
Sameer Agarwal | 4d2df0c | 2013-09-13 12:54:03 -0700 | [diff] [blame] | 355 | iteration_summary.gradient_norm = sqrt(current_state.gradient_squared_norm); |
| 356 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 357 | if (iteration_summary.gradient_max_norm <= absolute_gradient_tolerance) { |
Sameer Agarwal | 2d785d6 | 2013-09-04 23:34:29 -0700 | [diff] [blame] | 358 | VLOG_IF(1, is_not_silent) |
| 359 | << "Terminating: Gradient tolerance reached." |
| 360 | << "Relative gradient max norm: " |
| 361 | << (iteration_summary.gradient_max_norm / |
| 362 | initial_gradient_max_norm) |
| 363 | << " <= " << options.gradient_tolerance; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 364 | summary->termination_type = GRADIENT_TOLERANCE; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 365 | break; |
| 366 | } |
| 367 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 368 | iteration_summary.cost_change = previous_state.cost - current_state.cost; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 369 | const double absolute_function_tolerance = |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 370 | options.function_tolerance * previous_state.cost; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 371 | if (fabs(iteration_summary.cost_change) < absolute_function_tolerance) { |
Sameer Agarwal | 2d785d6 | 2013-09-04 23:34:29 -0700 | [diff] [blame] | 372 | VLOG_IF(1, is_not_silent) |
| 373 | << "Terminating. Function tolerance reached. " |
| 374 | << "|cost_change|/cost: " |
| 375 | << fabs(iteration_summary.cost_change) / previous_state.cost |
| 376 | << " <= " << options.function_tolerance; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 377 | summary->termination_type = FUNCTION_TOLERANCE; |
| 378 | return; |
| 379 | } |
| 380 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 381 | iteration_summary.cost = current_state.cost + summary->fixed_cost; |
| 382 | iteration_summary.step_norm = delta.norm(); |
| 383 | iteration_summary.step_is_valid = true; |
| 384 | iteration_summary.step_is_successful = true; |
| 385 | iteration_summary.step_norm = delta.norm(); |
| 386 | iteration_summary.step_size = current_state.step_size; |
| 387 | iteration_summary.line_search_function_evaluations = |
Alex Stewart | 9aa0e3c | 2013-07-05 20:22:37 +0100 | [diff] [blame] | 388 | line_search_summary.num_function_evaluations; |
| 389 | iteration_summary.line_search_gradient_evaluations = |
| 390 | line_search_summary.num_gradient_evaluations; |
| 391 | iteration_summary.line_search_iterations = |
| 392 | line_search_summary.num_iterations; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 393 | iteration_summary.iteration_time_in_seconds = |
| 394 | WallTimeInSeconds() - iteration_start_time; |
| 395 | iteration_summary.cumulative_time_in_seconds = |
| 396 | WallTimeInSeconds() - start_time |
| 397 | + summary->preprocessor_time_in_seconds; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 398 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 399 | summary->iterations.push_back(iteration_summary); |
Sameer Agarwal | 1c70ae9 | 2013-06-30 12:50:43 -0700 | [diff] [blame] | 400 | ++summary->num_successful_steps; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | } // namespace internal |
| 405 | } // namespace ceres |
Sameer Agarwal | 8140f0f | 2013-03-12 09:45:08 -0700 | [diff] [blame] | 406 | |
Sameer Agarwal | 700d50d | 2013-03-12 16:12:42 -0700 | [diff] [blame] | 407 | #endif // CERES_NO_LINE_SEARCH_MINIMIZER |