callbacks.cc 4.8 KB

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