testSymbolicEliminationTree.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 
21 #include <gtsam/inference/Symbol.h>
23 
24 #include <vector>
25 
26 #include "symbolicExampleGraphs.h"
27 
28 using namespace gtsam;
29 using namespace gtsam::symbol_shorthand;
30 using namespace std;
32 
33 // Use list_of replacement defined in symbolicExampleGraphs.h
34 using ChildNodes = ChainedVector<sharedNode>;
35 
37  public:
38  // build hardcoded tree
40  const SymbolicFactorGraph& fg) {
42  leaf0->key = 0;
43  leaf0->factors.push_back(fg[0]);
44  leaf0->factors.push_back(fg[1]);
45 
47  node1->key = 1;
48  node1->factors.push_back(fg[2]);
49  node1->children.push_back(leaf0);
50 
52  node2->key = 2;
53  node2->factors.push_back(fg[3]);
54  node2->children.push_back(node1);
55 
57  leaf3->key = 3;
58  leaf3->factors.push_back(fg[4]);
59 
61  root->key = 4;
62  root->children.push_back(leaf3);
63  root->children.push_back(node2);
64 
66  tree.roots_.push_back(root);
67  return tree;
68  }
69 
70  static SymbolicEliminationTree MakeTree(const ChildNodes::Result& roots) {
72  et.roots_.assign(roots.begin(), roots.end());
73  return et;
74  }
75 };
76 
77 // Create a leaf node.
80  node->key = key;
81  node->factors.assign(factors.begin(), factors.end());
82  return node;
83 }
84 
85 // Create a node with children.
87  const ChildNodes::Result& children) {
88  sharedNode node = Leaf(key, factors);
89  node->children.assign(children.begin(), children.end());
90  return node;
91 }
92 
93 /* ************************************************************************* */
97 
98  // Build from factor graph
99  const Ordering order{0, 1, 2, 3, 4};
100  SymbolicEliminationTree actual(simpleTestGraph1, order);
101 
102  CHECK(assert_equal(expected, actual));
103 }
104 
105 /* ************************************************************************* */
107  // l1 l2
108  // / | / |
109  // x1 --- x2 --- x3 --- x4 --- x5
110  // \ |
111  // l3
123 
124  auto binary = [](Key j1, Key j2) -> SymbolicFactor {
125  return SymbolicFactor(j1, j2);
126  };
127 
129  ChildNodes( //
130  Node(X(3), SymbolicFactorGraph(),
131  ChildNodes( //
132  Node(X(2), SymbolicFactorGraph(binary(X(2), X(3))),
133  ChildNodes( //
134  Node(L(1), SymbolicFactorGraph(binary(X(2), L(1))),
135  ChildNodes( //
137  binary(X(1), L(1)))(
138  binary(X(1), X(2)))))))))(
139  Node(X(4), SymbolicFactorGraph(binary(X(3), X(4))),
140  ChildNodes( //
141  Node(L(2), SymbolicFactorGraph(binary(X(4), L(2))),
142  ChildNodes( //
143  Node(X(5),
145  X(4), X(5)))(binary(L(2), X(5))),
146  ChildNodes( //
147  Leaf(L(3),
149  binary(X(4), L(3)))(
150  binary(X(5), L(3))) //
151  ) //
152  ) //
153  ) //
154  ) //
155  ) //
156  ) //
157  ) //
158  ) //
159  ) //
160  ) //
161  );
162 
163  const Ordering order{X(1), L(3), L(1), X(5), X(2), L(2), X(4), X(3)};
164  SymbolicEliminationTree actual(graph, order);
165  EXPECT(assert_equal(expected, actual));
166 }
167 
168 /* ************************************************************************* */
169 int main() {
170  TestResult tr;
171  return TestRegistry::runAllTests(tr);
172 }
173 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
SymbolicEliminationTree.h
gtsam::EliminationTree::roots_
FastVector< sharedNode > roots_
Definition: EliminationTree.h:86
Leaf
static sharedNode Leaf(Key key, const SymbolicFactorGraph &factors)
Definition: testSymbolicEliminationTree.cpp:78
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
EliminationTreeTester
Definition: testSymbolicEliminationTree.cpp:36
tree
Definition: testExpression.cpp:212
simple_graph::factors
const GaussianFactorGraph factors
Definition: testJacobianFactor.cpp:213
gtsam::symbol_shorthand
Definition: inference/Symbol.h:147
gtsam::EliminationTree
Definition: EliminationTree.h:51
EliminationTreeTester::buildHardcodedTree
static SymbolicEliminationTree buildHardcodedTree(const SymbolicFactorGraph &fg)
Definition: testSymbolicEliminationTree.cpp:39
TestableAssertions.h
Provides additional testing facilities for common data structures.
gtsam::symbol_shorthand::X
Key X(std::uint64_t j)
Definition: inference/Symbol.h:171
gtsam::SymbolicFactorGraph
Definition: SymbolicFactorGraph.h:61
main
int main()
Definition: testSymbolicEliminationTree.cpp:169
cholesky::expected
Matrix expected
Definition: testMatrix.cpp:971
Symbol.h
gtsam::symbol_shorthand::L
Key L(std::uint64_t j)
Definition: inference/Symbol.h:159
gtsam::EliminationTree< SymbolicBayesNet, SymbolicFactorGraph >::sharedNode
std::shared_ptr< Node > sharedNode
Shared pointer to Node.
Definition: EliminationTree.h:80
TestResult
Definition: TestResult.h:26
key
const gtsam::Symbol key('X', 0)
gtsam
traits
Definition: chartTesting.h:28
gtsam::TEST
TEST(SmartFactorBase, Pinhole)
Definition: testSmartFactorBase.cpp:38
symbolicExampleGraphs.h
gtsam::SymbolicFactor
Definition: SymbolicFactor.h:38
CHECK
#define CHECK(condition)
Definition: Test.h:108
std
Definition: BFloat16.h:88
gtsam::FactorGraph::end
const_iterator end() const
Definition: FactorGraph.h:342
EliminationTreeTester::MakeTree
static SymbolicEliminationTree MakeTree(const ChildNodes::Result &roots)
Definition: testSymbolicEliminationTree.cpp:70
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
gtsam::FactorGraph::begin
const_iterator begin() const
Definition: FactorGraph.h:339
graph
NonlinearFactorGraph graph
Definition: doc/Code/OdometryExample.cpp:2
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
Node
static sharedNode Node(Key key, const SymbolicFactorGraph &factors, const ChildNodes::Result &children)
Definition: testSymbolicEliminationTree.cpp:86
gtsam::Ordering
Definition: inference/Ordering.h:33
j1
double j1(double x)
Definition: j1.c:174
ChildNodes
ChainedVector< sharedNode > ChildNodes
Definition: testSymbolicEliminationTree.cpp:34
sharedNode
SymbolicEliminationTree::sharedNode sharedNode
Definition: testSymbolicEliminationTree.cpp:31
binary
Definition: testExpression.cpp:168
gtsam::FactorGraph::emplace_shared
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:153
gtsam::SymbolicEliminationTree
Definition: SymbolicEliminationTree.h:27


gtsam
Author(s):
autogenerated on Thu Jun 13 2024 03:10:34