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 | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 72 | bool Evaluate(Evaluator* evaluator, |
| 73 | const Vector& x, |
| 74 | LineSearchMinimizer::State* state) { |
| 75 | const bool status = evaluator->Evaluate(x.data(), |
| 76 | &(state->cost), |
| 77 | NULL, |
| 78 | state->gradient.data(), |
| 79 | NULL); |
| 80 | if (status) { |
| 81 | state->gradient_squared_norm = state->gradient.squaredNorm(); |
| 82 | state->gradient_max_norm = state->gradient.lpNorm<Eigen::Infinity>(); |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 83 | } |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 84 | |
| 85 | return status; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 88 | } // namespace |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 89 | |
| 90 | void LineSearchMinimizer::Minimize(const Minimizer::Options& options, |
| 91 | double* parameters, |
| 92 | Solver::Summary* summary) { |
| 93 | double start_time = WallTimeInSeconds(); |
| 94 | double iteration_start_time = start_time; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 95 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 96 | Evaluator* evaluator = CHECK_NOTNULL(options.evaluator); |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 97 | const int num_parameters = evaluator->NumParameters(); |
| 98 | const int num_effective_parameters = evaluator->NumEffectiveParameters(); |
| 99 | |
| 100 | summary->termination_type = NO_CONVERGENCE; |
| 101 | summary->num_successful_steps = 0; |
| 102 | summary->num_unsuccessful_steps = 0; |
| 103 | |
| 104 | VectorRef x(parameters, num_parameters); |
| 105 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 106 | State current_state(num_parameters, num_effective_parameters); |
| 107 | State previous_state(num_parameters, num_effective_parameters); |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 108 | |
| 109 | Vector delta(num_effective_parameters); |
| 110 | Vector x_plus_delta(num_parameters); |
| 111 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 112 | IterationSummary iteration_summary; |
| 113 | iteration_summary.iteration = 0; |
| 114 | iteration_summary.step_is_valid = false; |
| 115 | iteration_summary.step_is_successful = false; |
| 116 | iteration_summary.cost_change = 0.0; |
| 117 | iteration_summary.gradient_max_norm = 0.0; |
| 118 | iteration_summary.step_norm = 0.0; |
| 119 | iteration_summary.linear_solver_iterations = 0; |
| 120 | iteration_summary.step_solver_time_in_seconds = 0; |
| 121 | |
| 122 | // Do initial cost and Jacobian evaluation. |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 123 | if (!Evaluate(evaluator, x, ¤t_state)) { |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 124 | LOG(WARNING) << "Terminating: Cost and gradient evaluation failed."; |
| 125 | summary->termination_type = NUMERICAL_FAILURE; |
| 126 | return; |
| 127 | } |
| 128 | |
Sameer Agarwal | f102a68 | 2013-02-11 15:08:40 -0800 | [diff] [blame] | 129 | summary->initial_cost = current_state.cost + summary->fixed_cost; |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 130 | iteration_summary.cost = current_state.cost + summary->fixed_cost; |
Sameer Agarwal | f102a68 | 2013-02-11 15:08:40 -0800 | [diff] [blame] | 131 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 132 | iteration_summary.gradient_max_norm = current_state.gradient_max_norm; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 133 | |
| 134 | // The initial gradient max_norm is bounded from below so that we do |
| 135 | // not divide by zero. |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 136 | const double initial_gradient_max_norm = |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 137 | max(iteration_summary.gradient_max_norm, kEpsilon); |
| 138 | const double absolute_gradient_tolerance = |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 139 | options.gradient_tolerance * initial_gradient_max_norm; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 140 | |
| 141 | if (iteration_summary.gradient_max_norm <= absolute_gradient_tolerance) { |
| 142 | summary->termination_type = GRADIENT_TOLERANCE; |
| 143 | VLOG(1) << "Terminating: Gradient tolerance reached." |
| 144 | << "Relative gradient max norm: " |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 145 | << iteration_summary.gradient_max_norm / initial_gradient_max_norm |
| 146 | << " <= " << options.gradient_tolerance; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | |
| 150 | iteration_summary.iteration_time_in_seconds = |
| 151 | WallTimeInSeconds() - iteration_start_time; |
| 152 | iteration_summary.cumulative_time_in_seconds = |
| 153 | WallTimeInSeconds() - start_time |
| 154 | + summary->preprocessor_time_in_seconds; |
| 155 | summary->iterations.push_back(iteration_summary); |
| 156 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 157 | LineSearchDirection::Options line_search_direction_options; |
| 158 | line_search_direction_options.num_parameters = num_effective_parameters; |
| 159 | line_search_direction_options.type = options.line_search_direction_type; |
| 160 | line_search_direction_options.nonlinear_conjugate_gradient_type = |
| 161 | options.nonlinear_conjugate_gradient_type; |
| 162 | line_search_direction_options.max_lbfgs_rank = options.max_lbfgs_rank; |
| 163 | scoped_ptr<LineSearchDirection> line_search_direction( |
| 164 | LineSearchDirection::Create(line_search_direction_options)); |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 165 | |
| 166 | LineSearchFunction line_search_function(evaluator); |
| 167 | LineSearch::Options line_search_options; |
| 168 | line_search_options.function = &line_search_function; |
| 169 | |
| 170 | // TODO(sameeragarwal): Make this parameterizable over different |
| 171 | // line searches. |
| 172 | ArmijoLineSearch line_search; |
| 173 | LineSearch::Summary line_search_summary; |
| 174 | |
| 175 | while (true) { |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 176 | if (!RunCallbacks(options.callbacks, iteration_summary, summary)) { |
| 177 | return; |
| 178 | } |
| 179 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 180 | iteration_start_time = WallTimeInSeconds(); |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 181 | if (iteration_summary.iteration >= options.max_num_iterations) { |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 182 | summary->termination_type = NO_CONVERGENCE; |
| 183 | VLOG(1) << "Terminating: Maximum number of iterations reached."; |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | const double total_solver_time = iteration_start_time - start_time + |
| 188 | summary->preprocessor_time_in_seconds; |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 189 | if (total_solver_time >= options.max_solver_time_in_seconds) { |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 190 | summary->termination_type = NO_CONVERGENCE; |
| 191 | VLOG(1) << "Terminating: Maximum solver time reached."; |
| 192 | break; |
| 193 | } |
| 194 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 195 | iteration_summary = IterationSummary(); |
| 196 | iteration_summary.iteration = summary->iterations.back().iteration + 1; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 197 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 198 | bool line_search_status = true; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 199 | if (iteration_summary.iteration == 1) { |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 200 | current_state.search_direction = -current_state.gradient; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 201 | } else { |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 202 | line_search_status = line_search_direction->NextDirection( |
| 203 | previous_state, |
| 204 | current_state, |
| 205 | ¤t_state.search_direction); |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 208 | if (!line_search_status) { |
| 209 | LOG(WARNING) << "Line search direction computation failed. " |
| 210 | "Resorting to steepest descent."; |
| 211 | current_state.search_direction = -current_state.gradient; |
| 212 | } |
| 213 | |
| 214 | line_search_function.Init(x, current_state.search_direction); |
| 215 | current_state.directional_derivative = |
| 216 | current_state.gradient.dot(current_state.search_direction); |
| 217 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 218 | // TODO(sameeragarwal): Refactor this into its own object and add |
| 219 | // explanations for the various choices. |
| 220 | const double initial_step_size = (iteration_summary.iteration == 1) |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 221 | ? min(1.0, 1.0 / current_state.gradient_max_norm) |
| 222 | : min(1.0, 2.0 * (current_state.cost - previous_state.cost) / |
| 223 | current_state.directional_derivative); |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 224 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 225 | line_search.Search(line_search_options, |
| 226 | initial_step_size, |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 227 | current_state.cost, |
| 228 | current_state.directional_derivative, |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 229 | &line_search_summary); |
| 230 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 231 | current_state.step_size = line_search_summary.optimal_step_size; |
| 232 | delta = current_state.step_size * current_state.search_direction; |
| 233 | |
| 234 | previous_state = current_state; |
| 235 | |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 236 | // TODO(sameeragarwal): Collect stats. |
| 237 | if (!evaluator->Plus(x.data(), delta.data(), x_plus_delta.data()) || |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 238 | !Evaluate(evaluator, x_plus_delta, ¤t_state)) { |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 239 | LOG(WARNING) << "Evaluation failed."; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 240 | } else { |
| 241 | x = x_plus_delta; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 244 | iteration_summary.gradient_max_norm = current_state.gradient_max_norm; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 245 | if (iteration_summary.gradient_max_norm <= absolute_gradient_tolerance) { |
| 246 | summary->termination_type = GRADIENT_TOLERANCE; |
| 247 | VLOG(1) << "Terminating: Gradient tolerance reached." |
| 248 | << "Relative gradient max norm: " |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 249 | << iteration_summary.gradient_max_norm / initial_gradient_max_norm |
| 250 | << " <= " << options.gradient_tolerance; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 251 | break; |
| 252 | } |
| 253 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 254 | iteration_summary.cost_change = previous_state.cost - current_state.cost; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 255 | const double absolute_function_tolerance = |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 256 | options.function_tolerance * previous_state.cost; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 257 | if (fabs(iteration_summary.cost_change) < absolute_function_tolerance) { |
| 258 | VLOG(1) << "Terminating. Function tolerance reached. " |
| 259 | << "|cost_change|/cost: " |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 260 | << fabs(iteration_summary.cost_change) / previous_state.cost |
| 261 | << " <= " << options.function_tolerance; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 262 | summary->termination_type = FUNCTION_TOLERANCE; |
| 263 | return; |
| 264 | } |
| 265 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 266 | iteration_summary.cost = current_state.cost + summary->fixed_cost; |
| 267 | iteration_summary.step_norm = delta.norm(); |
| 268 | iteration_summary.step_is_valid = true; |
| 269 | iteration_summary.step_is_successful = true; |
| 270 | iteration_summary.step_norm = delta.norm(); |
| 271 | iteration_summary.step_size = current_state.step_size; |
| 272 | iteration_summary.line_search_function_evaluations = |
| 273 | line_search_summary.num_evaluations; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 274 | iteration_summary.iteration_time_in_seconds = |
| 275 | WallTimeInSeconds() - iteration_start_time; |
| 276 | iteration_summary.cumulative_time_in_seconds = |
| 277 | WallTimeInSeconds() - start_time |
| 278 | + summary->preprocessor_time_in_seconds; |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 279 | |
Sameer Agarwal | 9883fc3 | 2012-11-30 12:32:43 -0800 | [diff] [blame] | 280 | summary->iterations.push_back(iteration_summary); |
Sameer Agarwal | f4d0164 | 2012-11-26 12:55:58 -0800 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | } // namespace internal |
| 285 | } // namespace ceres |
Sameer Agarwal | 8140f0f | 2013-03-12 09:45:08 -0700 | [diff] [blame] | 286 | |
Sameer Agarwal | 700d50d | 2013-03-12 16:12:42 -0700 | [diff] [blame] | 287 | #endif // CERES_NO_LINE_SEARCH_MINIMIZER |