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
113  graph.emplace_shared<SymbolicFactor>(X(1), L(1));
114  graph.emplace_shared<SymbolicFactor>(X(1), X(2));
115  graph.emplace_shared<SymbolicFactor>(X(2), L(1));
116  graph.emplace_shared<SymbolicFactor>(X(2), X(3));
117  graph.emplace_shared<SymbolicFactor>(X(3), X(4));
118  graph.emplace_shared<SymbolicFactor>(X(4), L(2));
119  graph.emplace_shared<SymbolicFactor>(X(4), X(5));
120  graph.emplace_shared<SymbolicFactor>(L(2), X(5));
121  graph.emplace_shared<SymbolicFactor>(X(4), L(3));
122  graph.emplace_shared<SymbolicFactor>(X(5), L(3));
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),
144  SymbolicFactorGraph(binary(
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 /* ************************************************************************* */
FastVector< sharedNode > roots_
const gtsam::Symbol key('X', 0)
SymbolicEliminationTree::sharedNode sharedNode
Provides additional testing facilities for common data structures.
#define CHECK(condition)
Definition: Test.h:108
static SymbolicEliminationTree MakeTree(const ChildNodes::Result &roots)
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:196
static int runAllTests(TestResult &result)
Matrix expected
Definition: testMatrix.cpp:971
ChainedVector< sharedNode > ChildNodes
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
const GaussianFactorGraph factors
static SymbolicEliminationTree buildHardcodedTree(const SymbolicFactorGraph &fg)
Definition: BFloat16.h:88
Key X(std::uint64_t j)
NonlinearFactorGraph graph
const_iterator begin() const
Definition: FactorGraph.h:361
#define EXPECT(condition)
Definition: Test.h:150
static sharedNode Node(Key key, const SymbolicFactorGraph &factors, const ChildNodes::Result &children)
static sharedNode Leaf(Key key, const SymbolicFactorGraph &factors)
const_iterator end() const
Definition: FactorGraph.h:364
traits
Definition: chartTesting.h:28
Key L(std::uint64_t j)
TEST(SmartFactorBase, Pinhole)
std::shared_ptr< Node > sharedNode
Shared pointer to Node.
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:39:43