levenberg_marquardt_strategy_test.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/levenberg_marquardt_strategy.h"
  31. #include <memory>
  32. #include "ceres/internal/eigen.h"
  33. #include "ceres/linear_solver.h"
  34. #include "ceres/trust_region_strategy.h"
  35. #include "glog/logging.h"
  36. #include "gmock/gmock.h"
  37. #include "gmock/mock-log.h"
  38. #include "gtest/gtest.h"
  39. using testing::_;
  40. using testing::AllOf;
  41. using testing::AnyNumber;
  42. using testing::HasSubstr;
  43. using testing::ScopedMockLog;
  44. namespace ceres {
  45. namespace internal {
  46. const double kTolerance = 1e-16;
  47. // Linear solver that takes as input a vector and checks that the
  48. // caller passes the same vector as LinearSolver::PerSolveOptions.D.
  49. class RegularizationCheckingLinearSolver : public DenseSparseMatrixSolver {
  50. public:
  51. RegularizationCheckingLinearSolver(const int num_cols, const double* diagonal)
  52. : num_cols_(num_cols), diagonal_(diagonal) {}
  53. private:
  54. LinearSolver::Summary SolveImpl(
  55. DenseSparseMatrix* A,
  56. const double* b,
  57. const LinearSolver::PerSolveOptions& per_solve_options,
  58. double* x) final {
  59. CHECK(per_solve_options.D != nullptr);
  60. for (int i = 0; i < num_cols_; ++i) {
  61. EXPECT_NEAR(per_solve_options.D[i], diagonal_[i], kTolerance)
  62. << i << " " << per_solve_options.D[i] << " " << diagonal_[i];
  63. }
  64. return {};
  65. }
  66. const int num_cols_;
  67. const double* diagonal_;
  68. };
  69. TEST(LevenbergMarquardtStrategy, AcceptRejectStepRadiusScaling) {
  70. TrustRegionStrategy::Options options;
  71. options.initial_radius = 2.0;
  72. options.max_radius = 20.0;
  73. options.min_lm_diagonal = 1e-8;
  74. options.max_lm_diagonal = 1e8;
  75. // We need a non-null pointer here, so anything should do.
  76. std::unique_ptr<LinearSolver> linear_solver(
  77. new RegularizationCheckingLinearSolver(0, nullptr));
  78. options.linear_solver = linear_solver.get();
  79. LevenbergMarquardtStrategy lms(options);
  80. EXPECT_EQ(lms.Radius(), options.initial_radius);
  81. lms.StepRejected(0.0);
  82. EXPECT_EQ(lms.Radius(), 1.0);
  83. lms.StepRejected(-1.0);
  84. EXPECT_EQ(lms.Radius(), 0.25);
  85. lms.StepAccepted(1.0);
  86. EXPECT_EQ(lms.Radius(), 0.25 * 3.0);
  87. lms.StepAccepted(1.0);
  88. EXPECT_EQ(lms.Radius(), 0.25 * 3.0 * 3.0);
  89. lms.StepAccepted(0.25);
  90. EXPECT_EQ(lms.Radius(), 0.25 * 3.0 * 3.0 / 1.125);
  91. lms.StepAccepted(1.0);
  92. EXPECT_EQ(lms.Radius(), 0.25 * 3.0 * 3.0 / 1.125 * 3.0);
  93. lms.StepAccepted(1.0);
  94. EXPECT_EQ(lms.Radius(), 0.25 * 3.0 * 3.0 / 1.125 * 3.0 * 3.0);
  95. lms.StepAccepted(1.0);
  96. EXPECT_EQ(lms.Radius(), options.max_radius);
  97. }
  98. TEST(LevenbergMarquardtStrategy, CorrectDiagonalToLinearSolver) {
  99. Matrix jacobian(2, 3);
  100. jacobian.setZero();
  101. jacobian(0, 0) = 0.0;
  102. jacobian(0, 1) = 1.0;
  103. jacobian(1, 1) = 1.0;
  104. jacobian(0, 2) = 100.0;
  105. double residual = 1.0;
  106. double x[3];
  107. DenseSparseMatrix dsm(jacobian);
  108. TrustRegionStrategy::Options options;
  109. options.initial_radius = 2.0;
  110. options.max_radius = 20.0;
  111. options.min_lm_diagonal = 1e-2;
  112. options.max_lm_diagonal = 1e2;
  113. double diagonal[3];
  114. diagonal[0] = options.min_lm_diagonal;
  115. diagonal[1] = 2.0;
  116. diagonal[2] = options.max_lm_diagonal;
  117. for (double& diagonal_entry : diagonal) {
  118. diagonal_entry = sqrt(diagonal_entry / options.initial_radius);
  119. }
  120. RegularizationCheckingLinearSolver linear_solver(3, diagonal);
  121. options.linear_solver = &linear_solver;
  122. LevenbergMarquardtStrategy lms(options);
  123. TrustRegionStrategy::PerSolveOptions pso;
  124. {
  125. ScopedMockLog log;
  126. EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
  127. // This using directive is needed get around the fact that there
  128. // are versions of glog which are not in the google namespace.
  129. using namespace google;
  130. #if defined(GLOG_NO_ABBREVIATED_SEVERITIES)
  131. // Use GLOG_WARNING to support MSVC if GLOG_NO_ABBREVIATED_SEVERITIES
  132. // is defined.
  133. EXPECT_CALL(log,
  134. Log(GLOG_WARNING, _, HasSubstr("Failed to compute a step")));
  135. #else
  136. EXPECT_CALL(log,
  137. Log(google::WARNING, _, HasSubstr("Failed to compute a step")));
  138. #endif
  139. TrustRegionStrategy::Summary summary =
  140. lms.ComputeStep(pso, &dsm, &residual, x);
  141. EXPECT_EQ(summary.termination_type, LinearSolverTerminationType::FAILURE);
  142. }
  143. }
  144. } // namespace internal
  145. } // namespace ceres