sparse_normal_cholesky_solver_test.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2017 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 <memory>
  31. #include "Eigen/Cholesky"
  32. #include "ceres/block_sparse_matrix.h"
  33. #include "ceres/casts.h"
  34. #include "ceres/context_impl.h"
  35. #include "ceres/linear_least_squares_problems.h"
  36. #include "ceres/linear_solver.h"
  37. #include "ceres/triplet_sparse_matrix.h"
  38. #include "ceres/types.h"
  39. #include "glog/logging.h"
  40. #include "gtest/gtest.h"
  41. namespace ceres::internal {
  42. // TODO(sameeragarwal): These tests needs to be re-written, since
  43. // SparseNormalCholeskySolver is a composition of two classes now,
  44. // InnerProductComputer and SparseCholesky.
  45. //
  46. // So the test should exercise the composition, rather than the
  47. // numerics of the solver, which are well covered by tests for those
  48. // classes.
  49. class SparseNormalCholeskySolverTest : public ::testing::Test {
  50. protected:
  51. void SetUp() final {
  52. std::unique_ptr<LinearLeastSquaresProblem> problem =
  53. CreateLinearLeastSquaresProblemFromId(2);
  54. CHECK(problem != nullptr);
  55. A_.reset(down_cast<BlockSparseMatrix*>(problem->A.release()));
  56. b_ = std::move(problem->b);
  57. D_ = std::move(problem->D);
  58. }
  59. void TestSolver(const LinearSolver::Options& options, double* D) {
  60. Matrix dense_A;
  61. A_->ToDenseMatrix(&dense_A);
  62. Matrix lhs = dense_A.transpose() * dense_A;
  63. if (D != nullptr) {
  64. lhs += (ConstVectorRef(D, A_->num_cols()).array() *
  65. ConstVectorRef(D, A_->num_cols()).array())
  66. .matrix()
  67. .asDiagonal();
  68. }
  69. Vector rhs(A_->num_cols());
  70. rhs.setZero();
  71. A_->LeftMultiplyAndAccumulate(b_.get(), rhs.data());
  72. Vector expected_solution = lhs.llt().solve(rhs);
  73. std::unique_ptr<LinearSolver> solver(LinearSolver::Create(options));
  74. LinearSolver::PerSolveOptions per_solve_options;
  75. per_solve_options.D = D;
  76. Vector actual_solution(A_->num_cols());
  77. LinearSolver::Summary summary;
  78. summary = solver->Solve(
  79. A_.get(), b_.get(), per_solve_options, actual_solution.data());
  80. EXPECT_EQ(summary.termination_type, LinearSolverTerminationType::SUCCESS);
  81. for (int i = 0; i < A_->num_cols(); ++i) {
  82. EXPECT_NEAR(expected_solution(i), actual_solution(i), 1e-8)
  83. << "\nExpected: " << expected_solution.transpose()
  84. << "\nActual: " << actual_solution.transpose();
  85. }
  86. }
  87. void TestSolver(const LinearSolver::Options& options) {
  88. TestSolver(options, nullptr);
  89. TestSolver(options, D_.get());
  90. }
  91. std::unique_ptr<BlockSparseMatrix> A_;
  92. std::unique_ptr<double[]> b_;
  93. std::unique_ptr<double[]> D_;
  94. };
  95. #ifndef CERES_NO_SUITESPARSE
  96. TEST_F(SparseNormalCholeskySolverTest,
  97. SparseNormalCholeskyUsingSuiteSparsePreOrdering) {
  98. LinearSolver::Options options;
  99. options.sparse_linear_algebra_library_type = SUITE_SPARSE;
  100. options.type = SPARSE_NORMAL_CHOLESKY;
  101. options.ordering_type = OrderingType::NATURAL;
  102. ContextImpl context;
  103. options.context = &context;
  104. TestSolver(options);
  105. }
  106. TEST_F(SparseNormalCholeskySolverTest,
  107. SparseNormalCholeskyUsingSuiteSparsePostOrdering) {
  108. LinearSolver::Options options;
  109. options.sparse_linear_algebra_library_type = SUITE_SPARSE;
  110. options.type = SPARSE_NORMAL_CHOLESKY;
  111. options.ordering_type = OrderingType::AMD;
  112. ContextImpl context;
  113. options.context = &context;
  114. TestSolver(options);
  115. }
  116. #endif
  117. #ifndef CERES_NO_ACCELERATE_SPARSE
  118. TEST_F(SparseNormalCholeskySolverTest,
  119. SparseNormalCholeskyUsingAccelerateSparsePreOrdering) {
  120. LinearSolver::Options options;
  121. options.sparse_linear_algebra_library_type = ACCELERATE_SPARSE;
  122. options.type = SPARSE_NORMAL_CHOLESKY;
  123. options.ordering_type = OrderingType::NATURAL;
  124. ContextImpl context;
  125. options.context = &context;
  126. TestSolver(options);
  127. }
  128. TEST_F(SparseNormalCholeskySolverTest,
  129. SparseNormalCholeskyUsingAcceleratePostOrdering) {
  130. LinearSolver::Options options;
  131. options.sparse_linear_algebra_library_type = ACCELERATE_SPARSE;
  132. options.type = SPARSE_NORMAL_CHOLESKY;
  133. options.ordering_type = OrderingType::AMD;
  134. ContextImpl context;
  135. options.context = &context;
  136. TestSolver(options);
  137. }
  138. #endif
  139. #ifdef CERES_USE_EIGEN_SPARSE
  140. TEST_F(SparseNormalCholeskySolverTest,
  141. SparseNormalCholeskyUsingEigenPreOrdering) {
  142. LinearSolver::Options options;
  143. options.sparse_linear_algebra_library_type = EIGEN_SPARSE;
  144. options.type = SPARSE_NORMAL_CHOLESKY;
  145. options.ordering_type = OrderingType::NATURAL;
  146. ContextImpl context;
  147. options.context = &context;
  148. TestSolver(options);
  149. }
  150. TEST_F(SparseNormalCholeskySolverTest,
  151. SparseNormalCholeskyUsingEigenPostOrdering) {
  152. LinearSolver::Options options;
  153. options.sparse_linear_algebra_library_type = EIGEN_SPARSE;
  154. options.type = SPARSE_NORMAL_CHOLESKY;
  155. options.ordering_type = OrderingType::AMD;
  156. ContextImpl context;
  157. options.context = &context;
  158. TestSolver(options);
  159. }
  160. #endif // CERES_USE_EIGEN_SPARSE
  161. } // namespace ceres::internal