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