power_series_expansion_preconditioner_test.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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: markshachkov@gmail.com (Mark Shachkov)
  30. #include "ceres/power_series_expansion_preconditioner.h"
  31. #include <memory>
  32. #include "Eigen/Dense"
  33. #include "ceres/linear_least_squares_problems.h"
  34. #include "gtest/gtest.h"
  35. namespace ceres::internal {
  36. const double kEpsilon = 1e-14;
  37. class PowerSeriesExpansionPreconditionerTest : public ::testing::Test {
  38. protected:
  39. void SetUp() final {
  40. problem_ = CreateLinearLeastSquaresProblemFromId(5);
  41. const auto A = down_cast<BlockSparseMatrix*>(problem_->A.get());
  42. const auto D = problem_->D.get();
  43. options_.elimination_groups.push_back(problem_->num_eliminate_blocks);
  44. options_.preconditioner_type = SCHUR_POWER_SERIES_EXPANSION;
  45. preconditioner_options_ = Preconditioner::Options(options_);
  46. isc_ = std::make_unique<ImplicitSchurComplement>(options_);
  47. isc_->Init(*A, D, problem_->b.get());
  48. num_f_cols_ = isc_->rhs().rows();
  49. const int num_rows = A->num_rows(), num_cols = A->num_cols(),
  50. num_e_cols = num_cols - num_f_cols_;
  51. // Using predefined linear operator with schur structure and block-diagonal
  52. // F'F to explicitly construct schur complement and to calculate its inverse
  53. // to be used as a reference.
  54. Matrix A_dense, E, F, DE, DF;
  55. problem_->A->ToDenseMatrix(&A_dense);
  56. E = A_dense.leftCols(num_e_cols);
  57. F = A_dense.rightCols(num_f_cols_);
  58. DE = VectorRef(D, num_e_cols).asDiagonal();
  59. DF = VectorRef(D + num_e_cols, num_f_cols_).asDiagonal();
  60. sc_inverse_expected_ =
  61. (F.transpose() *
  62. (Matrix::Identity(num_rows, num_rows) -
  63. E * (E.transpose() * E + DE).inverse() * E.transpose()) *
  64. F +
  65. DF)
  66. .inverse();
  67. }
  68. std::unique_ptr<LinearLeastSquaresProblem> problem_;
  69. std::unique_ptr<ImplicitSchurComplement> isc_;
  70. int num_f_cols_;
  71. Matrix sc_inverse_expected_;
  72. LinearSolver::Options options_;
  73. Preconditioner::Options preconditioner_options_;
  74. };
  75. TEST_F(PowerSeriesExpansionPreconditionerTest,
  76. InverseValidPreconditionerToleranceReached) {
  77. const double spse_tolerance = kEpsilon;
  78. const int max_num_iterations = 50;
  79. PowerSeriesExpansionPreconditioner preconditioner(
  80. isc_.get(), max_num_iterations, spse_tolerance, preconditioner_options_);
  81. Vector x(num_f_cols_), y(num_f_cols_);
  82. for (int i = 0; i < num_f_cols_; i++) {
  83. x.setZero();
  84. x(i) = 1.0;
  85. y.setZero();
  86. preconditioner.RightMultiplyAndAccumulate(x.data(), y.data());
  87. EXPECT_LT((y - sc_inverse_expected_.col(i)).norm(), kEpsilon)
  88. << "Reference Schur complement inverse and its estimate via "
  89. "PowerSeriesExpansionPreconditioner differs in "
  90. << i
  91. << " column.\nreference : " << sc_inverse_expected_.col(i).transpose()
  92. << "\nestimated: " << y.transpose();
  93. }
  94. }
  95. TEST_F(PowerSeriesExpansionPreconditionerTest,
  96. InverseValidPreconditionerMaxIterations) {
  97. const double spse_tolerance = 0;
  98. const int max_num_iterations = 50;
  99. PowerSeriesExpansionPreconditioner preconditioner_fixed_n_iterations(
  100. isc_.get(), max_num_iterations, spse_tolerance, preconditioner_options_);
  101. Vector x(num_f_cols_), y(num_f_cols_);
  102. for (int i = 0; i < num_f_cols_; i++) {
  103. x.setZero();
  104. x(i) = 1.0;
  105. y.setZero();
  106. preconditioner_fixed_n_iterations.RightMultiplyAndAccumulate(x.data(),
  107. y.data());
  108. EXPECT_LT((y - sc_inverse_expected_.col(i)).norm(), kEpsilon)
  109. << "Reference Schur complement inverse and its estimate via "
  110. "PowerSeriesExpansionPreconditioner differs in "
  111. << i
  112. << " column.\nreference : " << sc_inverse_expected_.col(i).transpose()
  113. << "\nestimated: " << y.transpose();
  114. }
  115. }
  116. TEST_F(PowerSeriesExpansionPreconditionerTest,
  117. InverseInvalidBadPreconditionerTolerance) {
  118. const double spse_tolerance = 1 / kEpsilon;
  119. const int max_num_iterations = 50;
  120. PowerSeriesExpansionPreconditioner preconditioner_bad_tolerance(
  121. isc_.get(), max_num_iterations, spse_tolerance, preconditioner_options_);
  122. Vector x(num_f_cols_), y(num_f_cols_);
  123. for (int i = 0; i < num_f_cols_; i++) {
  124. x.setZero();
  125. x(i) = 1.0;
  126. y.setZero();
  127. preconditioner_bad_tolerance.RightMultiplyAndAccumulate(x.data(), y.data());
  128. EXPECT_GT((y - sc_inverse_expected_.col(i)).norm(), kEpsilon)
  129. << "Reference Schur complement inverse and its estimate via "
  130. "PowerSeriesExpansionPreconditioner are too similar, tolerance "
  131. "stopping criteria failed.";
  132. }
  133. }
  134. TEST_F(PowerSeriesExpansionPreconditionerTest,
  135. InverseInvalidBadPreconditionerMaxIterations) {
  136. const double spse_tolerance = kEpsilon;
  137. const int max_num_iterations = 1;
  138. PowerSeriesExpansionPreconditioner preconditioner_bad_iterations_limit(
  139. isc_.get(), max_num_iterations, spse_tolerance, preconditioner_options_);
  140. Vector x(num_f_cols_), y(num_f_cols_);
  141. for (int i = 0; i < num_f_cols_; i++) {
  142. x.setZero();
  143. x(i) = 1.0;
  144. y.setZero();
  145. preconditioner_bad_iterations_limit.RightMultiplyAndAccumulate(x.data(),
  146. y.data());
  147. EXPECT_GT((y - sc_inverse_expected_.col(i)).norm(), kEpsilon)
  148. << "Reference Schur complement inverse and its estimate via "
  149. "PowerSeriesExpansionPreconditioner are too similar, maximum "
  150. "iterations stopping criteria failed.";
  151. }
  152. }
  153. } // namespace ceres::internal