Sameer Agarwal | f5e81b6 | 2014-05-29 14:04:59 -0700 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Keir Mierle | 7492b0d | 2015-03-17 22:30:16 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google Inc. All rights reserved. |
| 3 | // http://ceres-solver.org/ |
Sameer Agarwal | f5e81b6 | 2014-05-29 14:04:59 -0700 | [diff] [blame] | 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 | |
Sameer Agarwal | 05a07ec | 2015-01-07 15:10:46 -0800 | [diff] [blame] | 40 | using std::string; |
| 41 | |
Sameer Agarwal | f5e81b6 | 2014-05-29 14:04:59 -0700 | [diff] [blame] | 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 | if (summary.step_is_successful) { |
| 51 | program_->StateVectorToParameterBlocks(parameters_); |
| 52 | program_->CopyParameterBlockStateToUserState(); |
| 53 | } |
| 54 | return SOLVER_CONTINUE; |
| 55 | } |
| 56 | |
| 57 | LoggingCallback::LoggingCallback(const MinimizerType minimizer_type, |
| 58 | const bool log_to_stdout) |
| 59 | : minimizer_type(minimizer_type), |
| 60 | log_to_stdout_(log_to_stdout) {} |
| 61 | |
| 62 | LoggingCallback::~LoggingCallback() {} |
| 63 | |
| 64 | CallbackReturnType LoggingCallback::operator()( |
| 65 | const IterationSummary& summary) { |
| 66 | string output; |
| 67 | if (minimizer_type == LINE_SEARCH) { |
| 68 | const char* kReportRowFormat = |
| 69 | "% 4d: f:% 8e d:% 3.2e g:% 3.2e h:% 3.2e " |
| 70 | "s:% 3.2e e:% 3d it:% 3.2e tt:% 3.2e"; |
| 71 | output = StringPrintf(kReportRowFormat, |
| 72 | summary.iteration, |
| 73 | summary.cost, |
| 74 | summary.cost_change, |
| 75 | summary.gradient_max_norm, |
| 76 | summary.step_norm, |
| 77 | summary.step_size, |
| 78 | summary.line_search_function_evaluations, |
| 79 | summary.iteration_time_in_seconds, |
| 80 | summary.cumulative_time_in_seconds); |
| 81 | } else if (minimizer_type == TRUST_REGION) { |
Sameer Agarwal | 14ec849 | 2014-06-04 17:29:03 -0700 | [diff] [blame] | 82 | if (summary.iteration == 0) { |
Sameer Agarwal | bcc865f | 2014-12-17 07:35:09 -0800 | [diff] [blame] | 83 | output = "iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time\n"; // NOLINT |
Sameer Agarwal | 14ec849 | 2014-06-04 17:29:03 -0700 | [diff] [blame] | 84 | } |
Sameer Agarwal | f5e81b6 | 2014-05-29 14:04:59 -0700 | [diff] [blame] | 85 | const char* kReportRowFormat = |
Sameer Agarwal | bcc865f | 2014-12-17 07:35:09 -0800 | [diff] [blame] | 86 | "% 4d % 8e % 3.2e % 3.2e % 3.2e % 3.2e % 3.2e % 4d % 3.2e % 3.2e"; // NOLINT |
Sameer Agarwal | 14ec849 | 2014-06-04 17:29:03 -0700 | [diff] [blame] | 87 | output += StringPrintf(kReportRowFormat, |
Sameer Agarwal | bcc865f | 2014-12-17 07:35:09 -0800 | [diff] [blame] | 88 | summary.iteration, |
| 89 | summary.cost, |
| 90 | summary.cost_change, |
| 91 | summary.gradient_max_norm, |
| 92 | summary.step_norm, |
| 93 | summary.relative_decrease, |
| 94 | summary.trust_region_radius, |
| 95 | summary.linear_solver_iterations, |
| 96 | summary.iteration_time_in_seconds, |
| 97 | summary.cumulative_time_in_seconds); |
Sameer Agarwal | f5e81b6 | 2014-05-29 14:04:59 -0700 | [diff] [blame] | 98 | } else { |
| 99 | LOG(FATAL) << "Unknown minimizer type."; |
| 100 | } |
| 101 | |
| 102 | if (log_to_stdout_) { |
Sameer Agarwal | bcc865f | 2014-12-17 07:35:09 -0800 | [diff] [blame] | 103 | std::cout << output << std::endl; |
Sameer Agarwal | f5e81b6 | 2014-05-29 14:04:59 -0700 | [diff] [blame] | 104 | } else { |
| 105 | VLOG(1) << output; |
| 106 | } |
| 107 | return SOLVER_CONTINUE; |
| 108 | } |
| 109 | |
| 110 | } // namespace internal |
| 111 | } // namespace ceres |