block_jacobi_preconditioner_test.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/block_jacobi_preconditioner.h"
  31. #include <memory>
  32. #include <random>
  33. #include <vector>
  34. #include "Eigen/Dense"
  35. #include "ceres/block_random_access_diagonal_matrix.h"
  36. #include "ceres/block_sparse_matrix.h"
  37. #include "ceres/linear_least_squares_problems.h"
  38. #include "gtest/gtest.h"
  39. namespace ceres::internal {
  40. TEST(BlockSparseJacobiPreconditioner, _) {
  41. constexpr int kNumtrials = 10;
  42. BlockSparseMatrix::RandomMatrixOptions options;
  43. options.num_col_blocks = 3;
  44. options.min_col_block_size = 1;
  45. options.max_col_block_size = 3;
  46. options.num_row_blocks = 5;
  47. options.min_row_block_size = 1;
  48. options.max_row_block_size = 4;
  49. options.block_density = 0.25;
  50. std::mt19937 prng;
  51. Preconditioner::Options preconditioner_options;
  52. ContextImpl context;
  53. preconditioner_options.context = &context;
  54. for (int trial = 0; trial < kNumtrials; ++trial) {
  55. auto jacobian = BlockSparseMatrix::CreateRandomMatrix(options, prng);
  56. Vector diagonal = Vector::Ones(jacobian->num_cols());
  57. Matrix dense_jacobian;
  58. jacobian->ToDenseMatrix(&dense_jacobian);
  59. Matrix hessian = dense_jacobian.transpose() * dense_jacobian;
  60. hessian.diagonal() += diagonal.array().square().matrix();
  61. BlockSparseJacobiPreconditioner pre(preconditioner_options, *jacobian);
  62. pre.Update(*jacobian, diagonal.data());
  63. // The const_cast is needed to be able to call GetCell.
  64. auto* m = const_cast<BlockRandomAccessDiagonalMatrix*>(&pre.matrix());
  65. EXPECT_EQ(m->num_rows(), jacobian->num_cols());
  66. EXPECT_EQ(m->num_cols(), jacobian->num_cols());
  67. const CompressedRowBlockStructure* bs = jacobian->block_structure();
  68. for (int i = 0; i < bs->cols.size(); ++i) {
  69. const int block_size = bs->cols[i].size;
  70. int r, c, row_stride, col_stride;
  71. CellInfo* cell_info = m->GetCell(i, i, &r, &c, &row_stride, &col_stride);
  72. Matrix actual_block_inverse =
  73. MatrixRef(cell_info->values, row_stride, col_stride)
  74. .block(r, c, block_size, block_size);
  75. Matrix expected_block = hessian.block(
  76. bs->cols[i].position, bs->cols[i].position, block_size, block_size);
  77. const double residual = (actual_block_inverse * expected_block -
  78. Matrix::Identity(block_size, block_size))
  79. .norm();
  80. EXPECT_NEAR(residual, 0.0, 1e-12) << "Block: " << i;
  81. }
  82. options.num_col_blocks++;
  83. options.num_row_blocks++;
  84. }
  85. }
  86. TEST(CompressedRowSparseJacobiPreconditioner, _) {
  87. constexpr int kNumtrials = 10;
  88. CompressedRowSparseMatrix::RandomMatrixOptions options;
  89. options.num_col_blocks = 3;
  90. options.min_col_block_size = 1;
  91. options.max_col_block_size = 3;
  92. options.num_row_blocks = 5;
  93. options.min_row_block_size = 1;
  94. options.max_row_block_size = 4;
  95. options.block_density = 0.25;
  96. std::mt19937 prng;
  97. Preconditioner::Options preconditioner_options;
  98. ContextImpl context;
  99. preconditioner_options.context = &context;
  100. for (int trial = 0; trial < kNumtrials; ++trial) {
  101. auto jacobian =
  102. CompressedRowSparseMatrix::CreateRandomMatrix(options, prng);
  103. Vector diagonal = Vector::Ones(jacobian->num_cols());
  104. Matrix dense_jacobian;
  105. jacobian->ToDenseMatrix(&dense_jacobian);
  106. Matrix hessian = dense_jacobian.transpose() * dense_jacobian;
  107. hessian.diagonal() += diagonal.array().square().matrix();
  108. BlockCRSJacobiPreconditioner pre(preconditioner_options, *jacobian);
  109. pre.Update(*jacobian, diagonal.data());
  110. auto& m = pre.matrix();
  111. EXPECT_EQ(m.num_rows(), jacobian->num_cols());
  112. EXPECT_EQ(m.num_cols(), jacobian->num_cols());
  113. const auto& col_blocks = jacobian->col_blocks();
  114. for (int i = 0, col = 0; i < col_blocks.size(); ++i) {
  115. const int block_size = col_blocks[i].size;
  116. int idx = m.rows()[col];
  117. for (int j = 0; j < block_size; ++j) {
  118. EXPECT_EQ(m.rows()[col + j + 1] - m.rows()[col + j], block_size);
  119. for (int k = 0; k < block_size; ++k, ++idx) {
  120. EXPECT_EQ(m.cols()[idx], col + k);
  121. }
  122. }
  123. ConstMatrixRef actual_block_inverse(
  124. m.values() + m.rows()[col], block_size, block_size);
  125. Matrix expected_block = hessian.block(col, col, block_size, block_size);
  126. const double residual = (actual_block_inverse * expected_block -
  127. Matrix::Identity(block_size, block_size))
  128. .norm();
  129. EXPECT_NEAR(residual, 0.0, 1e-12) << "Block: " << i;
  130. col += block_size;
  131. }
  132. options.num_col_blocks++;
  133. options.num_row_blocks++;
  134. }
  135. }
  136. } // namespace ceres::internal