testSubgraphSolver.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* ----------------------------------------------------------------------------
  2. * GTSAM Copyright 2010, Georgia Tech Research Corporation,
  3. * Atlanta, Georgia 30332-0415
  4. * All Rights Reserved
  5. * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
  6. * See LICENSE for the license information
  7. * -------------------------------------------------------------------------- */
  8. /**
  9. * @file testSubgraphSolver.cpp
  10. * @brief Unit tests for SubgraphSolver
  11. * @author Yong-Dian Jian
  12. **/
  13. #include <gtsam/linear/SubgraphSolver.h>
  14. #include <tests/smallExample.h>
  15. #include <gtsam/linear/GaussianBayesNet.h>
  16. #include <gtsam/linear/iterative.h>
  17. #include <gtsam/linear/GaussianFactorGraph.h>
  18. #include <gtsam/linear/SubgraphBuilder.h>
  19. #include <gtsam/inference/Symbol.h>
  20. #include <gtsam/inference/Ordering.h>
  21. #include <gtsam/base/numericalDerivative.h>
  22. #include <CppUnitLite/TestHarness.h>
  23. #include <boost/assign/std/list.hpp>
  24. using namespace boost::assign;
  25. using namespace std;
  26. using namespace gtsam;
  27. static size_t N = 3;
  28. static SubgraphSolverParameters kParameters;
  29. static auto kOrdering = example::planarOrdering(N);
  30. /* ************************************************************************* */
  31. /** unnormalized error */
  32. static double error(const GaussianFactorGraph& fg, const VectorValues& x) {
  33. double total_error = 0.;
  34. for(const GaussianFactor::shared_ptr& factor: fg)
  35. total_error += factor->error(x);
  36. return total_error;
  37. }
  38. /* ************************************************************************* */
  39. TEST( SubgraphSolver, Parameters )
  40. {
  41. LONGS_EQUAL(SubgraphSolverParameters::SILENT, kParameters.verbosity());
  42. LONGS_EQUAL(500, kParameters.maxIterations());
  43. }
  44. /* ************************************************************************* */
  45. TEST( SubgraphSolver, splitFactorGraph )
  46. {
  47. // Build a planar graph
  48. GaussianFactorGraph Ab;
  49. VectorValues xtrue;
  50. std::tie(Ab, xtrue) = example::planarGraph(N); // A*x-b
  51. SubgraphBuilderParameters params;
  52. params.augmentationFactor = 0.0;
  53. SubgraphBuilder builder(params);
  54. auto subgraph = builder(Ab);
  55. EXPECT_LONGS_EQUAL(9, subgraph.size());
  56. GaussianFactorGraph::shared_ptr Ab1, Ab2;
  57. std::tie(Ab1, Ab2) = splitFactorGraph(Ab, subgraph);
  58. EXPECT_LONGS_EQUAL(9, Ab1->size());
  59. EXPECT_LONGS_EQUAL(13, Ab2->size());
  60. }
  61. /* ************************************************************************* */
  62. TEST( SubgraphSolver, constructor1 )
  63. {
  64. // Build a planar graph
  65. GaussianFactorGraph Ab;
  66. VectorValues xtrue;
  67. std::tie(Ab, xtrue) = example::planarGraph(N); // A*x-b
  68. // The first constructor just takes a factor graph (and kParameters)
  69. // and it will split the graph into A1 and A2, where A1 is a spanning tree
  70. SubgraphSolver solver(Ab, kParameters, kOrdering);
  71. VectorValues optimized = solver.optimize(); // does PCG optimization
  72. DOUBLES_EQUAL(0.0, error(Ab, optimized), 1e-5);
  73. }
  74. /* ************************************************************************* */
  75. TEST( SubgraphSolver, constructor2 )
  76. {
  77. // Build a planar graph
  78. GaussianFactorGraph Ab;
  79. VectorValues xtrue;
  80. size_t N = 3;
  81. std::tie(Ab, xtrue) = example::planarGraph(N); // A*x-b
  82. // Get the spanning tree
  83. GaussianFactorGraph::shared_ptr Ab1, Ab2; // A1*x-b1 and A2*x-b2
  84. std::tie(Ab1, Ab2) = example::splitOffPlanarTree(N, Ab);
  85. // The second constructor takes two factor graphs, so the caller can specify
  86. // the preconditioner (Ab1) and the constraints that are left out (Ab2)
  87. SubgraphSolver solver(*Ab1, Ab2, kParameters, kOrdering);
  88. VectorValues optimized = solver.optimize();
  89. DOUBLES_EQUAL(0.0, error(Ab, optimized), 1e-5);
  90. }
  91. /* ************************************************************************* */
  92. TEST( SubgraphSolver, constructor3 )
  93. {
  94. // Build a planar graph
  95. GaussianFactorGraph Ab;
  96. VectorValues xtrue;
  97. size_t N = 3;
  98. std::tie(Ab, xtrue) = example::planarGraph(N); // A*x-b
  99. // Get the spanning tree and corresponding kOrdering
  100. GaussianFactorGraph::shared_ptr Ab1, Ab2; // A1*x-b1 and A2*x-b2
  101. std::tie(Ab1, Ab2) = example::splitOffPlanarTree(N, Ab);
  102. // The caller solves |A1*x-b1|^2 == |R1*x-c1|^2, where R1 is square UT
  103. auto Rc1 = Ab1->eliminateSequential();
  104. // The third constructor allows the caller to pass an already solved preconditioner Rc1_
  105. // as a Bayes net, in addition to the "loop closing constraints" Ab2, as before
  106. SubgraphSolver solver(Rc1, Ab2, kParameters);
  107. VectorValues optimized = solver.optimize();
  108. DOUBLES_EQUAL(0.0, error(Ab, optimized), 1e-5);
  109. }
  110. /* ************************************************************************* */
  111. int main() { TestResult tr; return TestRegistry::runAllTests(tr); }
  112. /* ************************************************************************* */