testSubgraphSolver.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 
20 #include <tests/smallExample.h>
22 #include <gtsam/linear/iterative.h>
25 #include <gtsam/inference/Symbol.h>
28 
30 
31 using namespace std;
32 using namespace gtsam;
33 
34 static size_t N = 3;
37 
38 /* ************************************************************************* */
40 static double error(const GaussianFactorGraph& fg, const VectorValues& x) {
41  double total_error = 0.;
42  for(const GaussianFactor::shared_ptr& factor: fg)
43  total_error += factor->error(x);
44  return total_error;
45 }
46 
47 /* ************************************************************************* */
48 TEST( SubgraphSolver, Parameters )
49 {
50  LONGS_EQUAL(SubgraphSolverParameters::SILENT, kParameters.verbosity());
52 }
53 
54 /* ************************************************************************* */
56 {
57  // Build a planar graph
58  const auto [Ab, xtrue] = example::planarGraph(N); // A*x-b
59 
61  params.augmentationFactor = 0.0;
62  SubgraphBuilder builder(params);
63  auto subgraph = builder(Ab);
64  EXPECT_LONGS_EQUAL(9, subgraph.size());
65 
66  const auto [Ab1, Ab2] = splitFactorGraph(Ab, subgraph);
67  EXPECT_LONGS_EQUAL(9, Ab1.size());
68  EXPECT_LONGS_EQUAL(13, Ab2.size());
69 }
70 
71 /* ************************************************************************* */
72 TEST( SubgraphSolver, constructor1 )
73 {
74  // Build a planar graph
75  const auto [Ab, xtrue] = example::planarGraph(N); // A*x-b
76 
77  // The first constructor just takes a factor graph (and kParameters)
78  // and it will split the graph into A1 and A2, where A1 is a spanning tree
80  VectorValues optimized = solver.optimize(); // does PCG optimization
81  DOUBLES_EQUAL(0.0, error(Ab, optimized), 1e-5);
82 }
83 
84 /* ************************************************************************* */
85 TEST( SubgraphSolver, constructor2 )
86 {
87  // Build a planar graph
88  size_t N = 3;
89  const auto [Ab, xtrue] = example::planarGraph(N); // A*x-b
90 
91  // Get the spanning tree, A1*x-b1 and A2*x-b2
92  const auto [Ab1, Ab2] = example::splitOffPlanarTree(N, Ab);
93 
94  // The second constructor takes two factor graphs, so the caller can specify
95  // the preconditioner (Ab1) and the constraints that are left out (Ab2)
97  VectorValues optimized = solver.optimize();
98  DOUBLES_EQUAL(0.0, error(Ab, optimized), 1e-5);
99 }
100 
101 /* ************************************************************************* */
102 TEST( SubgraphSolver, constructor3 )
103 {
104  // Build a planar graph
105  size_t N = 3;
106  const auto [Ab, xtrue] = example::planarGraph(N); // A*x-b
107 
108  // Get the spanning tree and corresponding kOrdering
109  // A1*x-b1 and A2*x-b2
110  const auto [Ab1, Ab2] = example::splitOffPlanarTree(N, Ab);
111 
112  // The caller solves |A1*x-b1|^2 == |R1*x-c1|^2, where R1 is square UT
113  auto Rc1 = *Ab1.eliminateSequential();
114 
115  // The third constructor allows the caller to pass an already solved preconditioner Rc1_
116  // as a Bayes net, in addition to the "loop closing constraints" Ab2, as before
117  SubgraphSolver solver(Rc1, Ab2, kParameters);
118  VectorValues optimized = solver.optimize();
119  DOUBLES_EQUAL(0.0, error(Ab, optimized), 1e-5);
120 }
121 
122 /* ************************************************************************* */
123 int main() { TestResult tr; return TestRegistry::runAllTests(tr); }
124 /* ************************************************************************* */
gtsam::SubgraphBuilderParameters
Definition: SubgraphBuilder.h:102
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
error
static double error(const GaussianFactorGraph &fg, const VectorValues &x)
Definition: testSubgraphSolver.cpp:40
kOrdering
static auto kOrdering
Definition: testSubgraphSolver.cpp:36
GaussianFactorGraph.h
Linear Factor Graph where all factors are Gaussians.
SubgraphSolver.h
Subgraph Solver from IROS 2010.
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
EXPECT_LONGS_EQUAL
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:154
TestHarness.h
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
gtsam::DecisionTreeFactor::error
double error(const DiscreteValues &values) const override
Calculate error for DiscreteValues x, is -log(probability).
Definition: DecisionTreeFactor.cpp:56
gtsam::IterativeOptimizationParameters::verbosity
Verbosity verbosity() const
Definition: IterativeSolver.h:61
Ordering.h
Variable ordering for the elimination algorithm.
GaussianBayesNet.h
Chordal Bayes Net, the result of eliminating a factor graph.
gtsam::example::splitOffPlanarTree
std::pair< GaussianFactorGraph, GaussianFactorGraph > splitOffPlanarTree(size_t N, const GaussianFactorGraph &original)
Definition: smallExample.h:677
vanilla::params
static const SmartProjectionParams params
Definition: smartFactorScenarios.h:69
N
static size_t N
Definition: testSubgraphSolver.cpp:34
solver
BiCGSTAB< SparseMatrix< double > > solver
Definition: BiCGSTAB_simple.cpp:5
pruning_fixture::factor
DecisionTreeFactor factor(D &C &B &A, "0.0 0.0 0.0 0.60658897 0.61241912 0.61241969 0.61247685 0.61247742 0.0 " "0.0 0.0 0.99995287 1.0 1.0 1.0 1.0")
gtsam::GaussianFactorGraph
Definition: GaussianFactorGraph.h:73
numericalDerivative.h
Some functions to compute numerical derivatives.
main
int main()
Definition: testSubgraphSolver.cpp:123
gtsam::VectorValues
Definition: VectorValues.h:74
gtsam::splitFactorGraph
std::pair< GaussianFactorGraph, GaussianFactorGraph > splitFactorGraph(const GaussianFactorGraph &factorGraph, const Subgraph &subgraph)
Definition: SubgraphBuilder.cpp:420
DOUBLES_EQUAL
#define DOUBLES_EQUAL(expected, actual, threshold)
Definition: Test.h:141
gtsam::GaussianFactor::shared_ptr
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: GaussianFactor.h:42
gtsam::SubgraphBuilder
Definition: SubgraphBuilder.h:153
Symbol.h
iterative.h
Iterative methods, implementation.
gtsam::example::planarOrdering
Ordering planarOrdering(size_t N)
Definition: smallExample.h:668
TestResult
Definition: TestResult.h:26
gtsam::SubgraphSolverParameters
Definition: SubgraphSolver.h:35
gtsam::example::planarGraph
std::pair< GaussianFactorGraph, VectorValues > planarGraph(size_t N)
Definition: smallExample.h:626
gtsam
traits
Definition: SFMdata.h:40
gtsam::ConjugateGradientParameters::maxIterations
size_t maxIterations
maximum number of cg iterations
Definition: ConjugateGradientSolver.h:35
std
Definition: BFloat16.h:88
TEST
TEST(SubgraphSolver, Parameters)
Definition: testSubgraphSolver.cpp:48
exampleQR::Ab
Matrix Ab
Definition: testNoiseModel.cpp:207
SubgraphBuilder.h
gtsam::SubgraphSolver
Definition: SubgraphSolver.h:76
smallExample.h
Create small example with two poses and one landmark.
LONGS_EQUAL
#define LONGS_EQUAL(expected, actual)
Definition: Test.h:134
kParameters
static SubgraphSolverParameters kParameters
Definition: testSubgraphSolver.cpp:35


gtsam
Author(s):
autogenerated on Thu Dec 19 2024 04:07:34