iterative_schur_complement_solver_test.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2015 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. //
  31. // TODO(sameeragarwal): Add support for larger, more complicated and
  32. // poorly conditioned problems both for correctness testing as well as
  33. // benchmarking.
  34. #include "ceres/iterative_schur_complement_solver.h"
  35. #include <cstddef>
  36. #include <memory>
  37. #include "Eigen/Dense"
  38. #include "ceres/block_random_access_dense_matrix.h"
  39. #include "ceres/block_sparse_matrix.h"
  40. #include "ceres/casts.h"
  41. #include "ceres/context_impl.h"
  42. #include "ceres/internal/eigen.h"
  43. #include "ceres/linear_least_squares_problems.h"
  44. #include "ceres/linear_solver.h"
  45. #include "ceres/schur_eliminator.h"
  46. #include "ceres/triplet_sparse_matrix.h"
  47. #include "ceres/types.h"
  48. #include "glog/logging.h"
  49. #include "gtest/gtest.h"
  50. namespace ceres {
  51. namespace internal {
  52. using testing::AssertionResult;
  53. const double kEpsilon = 1e-14;
  54. class IterativeSchurComplementSolverTest : public ::testing::Test {
  55. protected:
  56. void SetUpProblem(int problem_id) {
  57. std::unique_ptr<LinearLeastSquaresProblem> problem =
  58. CreateLinearLeastSquaresProblemFromId(problem_id);
  59. CHECK(problem != nullptr);
  60. A_.reset(down_cast<BlockSparseMatrix*>(problem->A.release()));
  61. b_ = std::move(problem->b);
  62. D_ = std::move(problem->D);
  63. num_cols_ = A_->num_cols();
  64. num_rows_ = A_->num_rows();
  65. num_eliminate_blocks_ = problem->num_eliminate_blocks;
  66. }
  67. AssertionResult TestSolver(double* D,
  68. PreconditionerType preconditioner_type,
  69. bool use_spse_initialization) {
  70. TripletSparseMatrix triplet_A(
  71. A_->num_rows(), A_->num_cols(), A_->num_nonzeros());
  72. A_->ToTripletSparseMatrix(&triplet_A);
  73. DenseSparseMatrix dense_A(triplet_A);
  74. LinearSolver::Options options;
  75. options.type = DENSE_QR;
  76. ContextImpl context;
  77. options.context = &context;
  78. std::unique_ptr<LinearSolver> qr(LinearSolver::Create(options));
  79. LinearSolver::PerSolveOptions per_solve_options;
  80. per_solve_options.D = D;
  81. Vector reference_solution(num_cols_);
  82. qr->Solve(&dense_A, b_.get(), per_solve_options, reference_solution.data());
  83. options.elimination_groups.push_back(num_eliminate_blocks_);
  84. options.elimination_groups.push_back(0);
  85. options.max_num_iterations = num_cols_;
  86. options.max_num_spse_iterations = 1;
  87. options.use_spse_initialization = use_spse_initialization;
  88. options.preconditioner_type = preconditioner_type;
  89. IterativeSchurComplementSolver isc(options);
  90. Vector isc_sol(num_cols_);
  91. per_solve_options.r_tolerance = 1e-12;
  92. isc.Solve(A_.get(), b_.get(), per_solve_options, isc_sol.data());
  93. double diff = (isc_sol - reference_solution).norm();
  94. if (diff < kEpsilon) {
  95. return testing::AssertionSuccess();
  96. } else {
  97. return testing::AssertionFailure()
  98. << "The reference solution differs from the ITERATIVE_SCHUR"
  99. << " solution by " << diff << " which is more than " << kEpsilon;
  100. }
  101. }
  102. int num_rows_;
  103. int num_cols_;
  104. int num_eliminate_blocks_;
  105. std::unique_ptr<BlockSparseMatrix> A_;
  106. std::unique_ptr<double[]> b_;
  107. std::unique_ptr<double[]> D_;
  108. };
  109. TEST_F(IterativeSchurComplementSolverTest, NormalProblemSchurJacobi) {
  110. SetUpProblem(2);
  111. EXPECT_TRUE(TestSolver(nullptr, SCHUR_JACOBI, false));
  112. EXPECT_TRUE(TestSolver(D_.get(), SCHUR_JACOBI, false));
  113. }
  114. TEST_F(IterativeSchurComplementSolverTest,
  115. NormalProblemSchurJacobiWithPowerSeriesExpansionInitialization) {
  116. SetUpProblem(2);
  117. EXPECT_TRUE(TestSolver(nullptr, SCHUR_JACOBI, true));
  118. EXPECT_TRUE(TestSolver(D_.get(), SCHUR_JACOBI, true));
  119. }
  120. TEST_F(IterativeSchurComplementSolverTest,
  121. NormalProblemPowerSeriesExpansionPreconditioner) {
  122. SetUpProblem(5);
  123. EXPECT_TRUE(TestSolver(nullptr, SCHUR_POWER_SERIES_EXPANSION, false));
  124. EXPECT_TRUE(TestSolver(D_.get(), SCHUR_POWER_SERIES_EXPANSION, false));
  125. }
  126. TEST_F(IterativeSchurComplementSolverTest, ProblemWithNoFBlocks) {
  127. SetUpProblem(3);
  128. EXPECT_TRUE(TestSolver(nullptr, SCHUR_JACOBI, false));
  129. EXPECT_TRUE(TestSolver(D_.get(), SCHUR_JACOBI, false));
  130. }
  131. } // namespace internal
  132. } // namespace ceres