Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame^] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2015 Google Inc. All rights reserved. |
| 3 | // http://ceres-solver.org/ |
| 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 | #include <iostream> // NO LINT |
| 32 | #include "ceres/callbacks.h" |
| 33 | #include "ceres/program.h" |
| 34 | #include "ceres/stringprintf.h" |
| 35 | #include "glog/logging.h" |
| 36 | |
| 37 | namespace ceres { |
| 38 | namespace internal { |
| 39 | |
| 40 | using std::string; |
| 41 | |
| 42 | StateUpdatingCallback::StateUpdatingCallback(Program* program, |
| 43 | double* parameters) |
| 44 | : program_(program), parameters_(parameters) {} |
| 45 | |
| 46 | StateUpdatingCallback::~StateUpdatingCallback() {} |
| 47 | |
| 48 | CallbackReturnType StateUpdatingCallback::operator()( |
| 49 | const IterationSummary& summary) { |
| 50 | program_->StateVectorToParameterBlocks(parameters_); |
| 51 | program_->CopyParameterBlockStateToUserState(); |
| 52 | return SOLVER_CONTINUE; |
| 53 | } |
| 54 | |
| 55 | GradientProblemSolverStateUpdatingCallback:: |
| 56 | GradientProblemSolverStateUpdatingCallback( |
| 57 | int num_parameters, |
| 58 | const double* internal_parameters, |
| 59 | double* user_parameters) |
| 60 | : num_parameters_(num_parameters), |
| 61 | internal_parameters_(internal_parameters), |
| 62 | user_parameters_(user_parameters) {} |
| 63 | |
| 64 | GradientProblemSolverStateUpdatingCallback:: |
| 65 | ~GradientProblemSolverStateUpdatingCallback() {} |
| 66 | |
| 67 | CallbackReturnType GradientProblemSolverStateUpdatingCallback::operator()( |
| 68 | const IterationSummary& summary) { |
| 69 | if (summary.step_is_successful) { |
| 70 | std::copy(internal_parameters_, |
| 71 | internal_parameters_ + num_parameters_, |
| 72 | user_parameters_); |
| 73 | } |
| 74 | return SOLVER_CONTINUE; |
| 75 | } |
| 76 | |
| 77 | LoggingCallback::LoggingCallback(const MinimizerType minimizer_type, |
| 78 | const bool log_to_stdout) |
| 79 | : minimizer_type(minimizer_type), |
| 80 | log_to_stdout_(log_to_stdout) {} |
| 81 | |
| 82 | LoggingCallback::~LoggingCallback() {} |
| 83 | |
| 84 | CallbackReturnType LoggingCallback::operator()( |
| 85 | const IterationSummary& summary) { |
| 86 | string output; |
| 87 | if (minimizer_type == LINE_SEARCH) { |
| 88 | const char* kReportRowFormat = |
| 89 | "% 4d: f:% 8e d:% 3.2e g:% 3.2e h:% 3.2e " |
| 90 | "s:% 3.2e e:% 3d it:% 3.2e tt:% 3.2e"; |
| 91 | output = StringPrintf(kReportRowFormat, |
| 92 | summary.iteration, |
| 93 | summary.cost, |
| 94 | summary.cost_change, |
| 95 | summary.gradient_max_norm, |
| 96 | summary.step_norm, |
| 97 | summary.step_size, |
| 98 | summary.line_search_function_evaluations, |
| 99 | summary.iteration_time_in_seconds, |
| 100 | summary.cumulative_time_in_seconds); |
| 101 | } else if (minimizer_type == TRUST_REGION) { |
| 102 | if (summary.iteration == 0) { |
| 103 | output = "iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time\n"; // NOLINT |
| 104 | } |
| 105 | const char* kReportRowFormat = |
| 106 | "% 4d % 8e % 3.2e % 3.2e % 3.2e % 3.2e % 3.2e % 4d % 3.2e % 3.2e"; // NOLINT |
| 107 | output += StringPrintf(kReportRowFormat, |
| 108 | summary.iteration, |
| 109 | summary.cost, |
| 110 | summary.cost_change, |
| 111 | summary.gradient_max_norm, |
| 112 | summary.step_norm, |
| 113 | summary.relative_decrease, |
| 114 | summary.trust_region_radius, |
| 115 | summary.linear_solver_iterations, |
| 116 | summary.iteration_time_in_seconds, |
| 117 | summary.cumulative_time_in_seconds); |
| 118 | } else { |
| 119 | LOG(FATAL) << "Unknown minimizer type."; |
| 120 | } |
| 121 | |
| 122 | if (log_to_stdout_) { |
| 123 | std::cout << output << std::endl; |
| 124 | } else { |
| 125 | VLOG(1) << output; |
| 126 | } |
| 127 | return SOLVER_CONTINUE; |
| 128 | } |
| 129 | |
| 130 | } // namespace internal |
| 131 | } // namespace ceres |