parameter_block_ordering_test.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/parameter_block_ordering.h"
  31. #include <cstddef>
  32. #include <memory>
  33. #include <unordered_set>
  34. #include <vector>
  35. #include "ceres/cost_function.h"
  36. #include "ceres/graph.h"
  37. #include "ceres/problem_impl.h"
  38. #include "ceres/program.h"
  39. #include "ceres/sized_cost_function.h"
  40. #include "ceres/stl_util.h"
  41. #include "gtest/gtest.h"
  42. namespace ceres::internal {
  43. using VertexSet = std::unordered_set<ParameterBlock*>;
  44. template <int M, int... Ns>
  45. class DummyCostFunction : public SizedCostFunction<M, Ns...> {
  46. bool Evaluate(double const* const* parameters,
  47. double* residuals,
  48. double** jacobians) const final {
  49. return true;
  50. }
  51. };
  52. class SchurOrderingTest : public ::testing::Test {
  53. protected:
  54. void SetUp() final {
  55. // The explicit calls to AddParameterBlock are necessary because
  56. // the below tests depend on the specific numbering of the
  57. // parameter blocks.
  58. problem_.AddParameterBlock(x_, 3);
  59. problem_.AddParameterBlock(y_, 4);
  60. problem_.AddParameterBlock(z_, 5);
  61. problem_.AddParameterBlock(w_, 6);
  62. problem_.AddResidualBlock(new DummyCostFunction<2, 3>, nullptr, x_);
  63. problem_.AddResidualBlock(new DummyCostFunction<6, 5, 4>, nullptr, z_, y_);
  64. problem_.AddResidualBlock(new DummyCostFunction<3, 3, 5>, nullptr, x_, z_);
  65. problem_.AddResidualBlock(new DummyCostFunction<7, 5, 3>, nullptr, z_, x_);
  66. problem_.AddResidualBlock(
  67. new DummyCostFunction<1, 5, 3, 6>, nullptr, z_, x_, w_);
  68. }
  69. ProblemImpl problem_;
  70. double x_[3], y_[4], z_[5], w_[6];
  71. };
  72. TEST_F(SchurOrderingTest, NoFixed) {
  73. const Program& program = problem_.program();
  74. const std::vector<ParameterBlock*>& parameter_blocks =
  75. program.parameter_blocks();
  76. auto graph = CreateHessianGraph(program);
  77. const VertexSet& vertices = graph->vertices();
  78. EXPECT_EQ(vertices.size(), 4);
  79. for (int i = 0; i < 4; ++i) {
  80. EXPECT_TRUE(vertices.find(parameter_blocks[i]) != vertices.end());
  81. }
  82. {
  83. const VertexSet& neighbors = graph->Neighbors(parameter_blocks[0]);
  84. EXPECT_EQ(neighbors.size(), 2);
  85. EXPECT_TRUE(neighbors.find(parameter_blocks[2]) != neighbors.end());
  86. EXPECT_TRUE(neighbors.find(parameter_blocks[3]) != neighbors.end());
  87. }
  88. {
  89. const VertexSet& neighbors = graph->Neighbors(parameter_blocks[1]);
  90. EXPECT_EQ(neighbors.size(), 1);
  91. EXPECT_TRUE(neighbors.find(parameter_blocks[2]) != neighbors.end());
  92. }
  93. {
  94. const VertexSet& neighbors = graph->Neighbors(parameter_blocks[2]);
  95. EXPECT_EQ(neighbors.size(), 3);
  96. EXPECT_TRUE(neighbors.find(parameter_blocks[0]) != neighbors.end());
  97. EXPECT_TRUE(neighbors.find(parameter_blocks[1]) != neighbors.end());
  98. EXPECT_TRUE(neighbors.find(parameter_blocks[3]) != neighbors.end());
  99. }
  100. {
  101. const VertexSet& neighbors = graph->Neighbors(parameter_blocks[3]);
  102. EXPECT_EQ(neighbors.size(), 2);
  103. EXPECT_TRUE(neighbors.find(parameter_blocks[0]) != neighbors.end());
  104. EXPECT_TRUE(neighbors.find(parameter_blocks[2]) != neighbors.end());
  105. }
  106. }
  107. TEST_F(SchurOrderingTest, AllFixed) {
  108. problem_.SetParameterBlockConstant(x_);
  109. problem_.SetParameterBlockConstant(y_);
  110. problem_.SetParameterBlockConstant(z_);
  111. problem_.SetParameterBlockConstant(w_);
  112. const Program& program = problem_.program();
  113. auto graph = CreateHessianGraph(program);
  114. EXPECT_EQ(graph->vertices().size(), 0);
  115. }
  116. TEST_F(SchurOrderingTest, OneFixed) {
  117. problem_.SetParameterBlockConstant(x_);
  118. const Program& program = problem_.program();
  119. const std::vector<ParameterBlock*>& parameter_blocks =
  120. program.parameter_blocks();
  121. auto graph = CreateHessianGraph(program);
  122. const VertexSet& vertices = graph->vertices();
  123. EXPECT_EQ(vertices.size(), 3);
  124. EXPECT_TRUE(vertices.find(parameter_blocks[0]) == vertices.end());
  125. for (int i = 1; i < 3; ++i) {
  126. EXPECT_TRUE(vertices.find(parameter_blocks[i]) != vertices.end());
  127. }
  128. {
  129. const VertexSet& neighbors = graph->Neighbors(parameter_blocks[1]);
  130. EXPECT_EQ(neighbors.size(), 1);
  131. EXPECT_TRUE(neighbors.find(parameter_blocks[2]) != neighbors.end());
  132. }
  133. {
  134. const VertexSet& neighbors = graph->Neighbors(parameter_blocks[2]);
  135. EXPECT_EQ(neighbors.size(), 2);
  136. EXPECT_TRUE(neighbors.find(parameter_blocks[1]) != neighbors.end());
  137. EXPECT_TRUE(neighbors.find(parameter_blocks[3]) != neighbors.end());
  138. }
  139. {
  140. const VertexSet& neighbors = graph->Neighbors(parameter_blocks[3]);
  141. EXPECT_EQ(neighbors.size(), 1);
  142. EXPECT_TRUE(neighbors.find(parameter_blocks[2]) != neighbors.end());
  143. }
  144. // The constant parameter block is at the end.
  145. std::vector<ParameterBlock*> ordering;
  146. ComputeSchurOrdering(program, &ordering);
  147. EXPECT_EQ(ordering.back(), parameter_blocks[0]);
  148. }
  149. } // namespace ceres::internal