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