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 
20 
21 #include <vector>
22 #include <boost/make_shared.hpp>
23 #include <boost/assign/list_of.hpp>
24 
27 #include <gtsam/inference/Symbol.h>
28 
29 #include "symbolicExampleGraphs.h"
30 
31 using namespace gtsam;
32 using namespace gtsam::symbol_shorthand;
33 using namespace std;
34 using boost::assign::list_of;
35 
37 public:
38  // build hardcoded tree
40 
41  SymbolicEliminationTree::sharedNode leaf0(new SymbolicEliminationTree::Node);
42  leaf0->key = 0;
43  leaf0->factors.push_back(fg[0]);
44  leaf0->factors.push_back(fg[1]);
45 
46  SymbolicEliminationTree::sharedNode node1(new SymbolicEliminationTree::Node);
47  node1->key = 1;
48  node1->factors.push_back(fg[2]);
49  node1->children.push_back(leaf0);
50 
51  SymbolicEliminationTree::sharedNode node2(new SymbolicEliminationTree::Node);
52  node2->key = 2;
53  node2->factors.push_back(fg[3]);
54  node2->children.push_back(node1);
55 
56  SymbolicEliminationTree::sharedNode leaf3(new SymbolicEliminationTree::Node);
57  leaf3->key = 3;
58  leaf3->factors.push_back(fg[4]);
59 
60  SymbolicEliminationTree::sharedNode root(new SymbolicEliminationTree::Node);
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  template<typename ROOTS>
71  static SymbolicEliminationTree MakeTree(const ROOTS& roots)
72  {
74  et.roots_.assign(roots.begin(), roots.end());
75  return et;
76  }
77 };
78 
79 template<typename FACTORS>
81 {
82  SymbolicEliminationTree::sharedNode node = boost::make_shared<SymbolicEliminationTree::Node>();
83  node->key = key;
84  SymbolicFactorGraph factorsAsGraph = factors;
85  node->factors.assign(factorsAsGraph.begin(), factorsAsGraph.end());
86  return node;
87 }
88 
89 template<typename FACTORS, typename CHILDREN>
90 static SymbolicEliminationTree::sharedNode MakeNode(Key key, const FACTORS& factors, const CHILDREN& children)
91 {
92  SymbolicEliminationTree::sharedNode node = boost::make_shared<SymbolicEliminationTree::Node>();
93  node->key = key;
94  SymbolicFactorGraph factorsAsGraph = factors;
95  node->factors.assign(factorsAsGraph.begin(), factorsAsGraph.end());
96  node->children.assign(children.begin(), children.end());
97  return node;
98 }
99 
100 
101 /* ************************************************************************* */
103 {
106 
107  // Build from factor graph
108  Ordering order;
109  order += 0,1,2,3,4;
110  SymbolicEliminationTree actual(simpleTestGraph1, order);
111 
112  CHECK(assert_equal(expected, actual));
113 }
114 
115 /* ************************************************************************* */
117 {
118  // l1 l2
119  // / | / |
120  // x1 --- x2 --- x3 --- x4 --- x5
121  // \ |
122  // l3
124  graph += SymbolicFactor(X(1), L(1));
125  graph += SymbolicFactor(X(1), X(2));
126  graph += SymbolicFactor(X(2), L(1));
127  graph += SymbolicFactor(X(2), X(3));
128  graph += SymbolicFactor(X(3), X(4));
129  graph += SymbolicFactor(X(4), L(2));
130  graph += SymbolicFactor(X(4), X(5));
131  graph += SymbolicFactor(L(2), X(5));
132  graph += SymbolicFactor(X(4), L(3));
133  graph += SymbolicFactor(X(5), L(3));
134 
136  (MakeNode(X(3), SymbolicFactorGraph(), list_of
137  (MakeNode(X(2), list_of(SymbolicFactor(X(2), X(3))), list_of
138  (MakeNode(L(1), list_of(SymbolicFactor(X(2), L(1))), list_of
139  (MakeNode(X(1), list_of(SymbolicFactor(X(1), L(1))) (SymbolicFactor(X(1), X(2)))))))))
140  (MakeNode(X(4), list_of(SymbolicFactor(X(3), X(4))), list_of
141  (MakeNode(L(2), list_of(SymbolicFactor(X(4), L(2))), list_of
142  (MakeNode(X(5), list_of(SymbolicFactor(X(4), X(5))) (SymbolicFactor(L(2), X(5))), list_of
143  (MakeNode(L(3), list_of(SymbolicFactor(X(4), L(3))) (SymbolicFactor(X(5), L(3))))))))))))));
144 
145  Ordering order = list_of(X(1)) (L(3)) (L(1)) (X(5)) (X(2)) (L(2)) (X(4)) (X(3));
146 
147  SymbolicEliminationTree actual(graph, order);
148 
149  EXPECT(assert_equal(expected, actual));
150 }
151 
152 /* ************************************************************************* */
153 int main() {
154  TestResult tr;
155  return TestRegistry::runAllTests(tr);
156 }
157 /* ************************************************************************* */
FastVector< sharedNode > roots_
Provides additional testing facilities for common data structures.
#define CHECK(condition)
Definition: Test.h:109
static SymbolicEliminationTree MakeTree(const ROOTS &roots)
static int runAllTests(TestResult &result)
Matrix expected
Definition: testMatrix.cpp:974
static SymbolicEliminationTree buildHardcodedTree(const SymbolicFactorGraph &fg)
Definition: Half.h:150
GaussianFactorGraph factors(list_of(factor1)(factor2)(factor3))
Key X(std::uint64_t j)
const mpreal root(const mpreal &x, unsigned long int k, mp_rnd_t r=mpreal::get_default_rnd())
Definition: mpreal.h:2194
const_iterator begin() const
Definition: FactorGraph.h:333
NonlinearFactorGraph graph
boost::shared_ptr< Node > sharedNode
Shared pointer to Node.
#define EXPECT(condition)
Definition: Test.h:151
const_iterator end() const
Definition: FactorGraph.h:336
traits
Definition: chartTesting.h:28
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
Key L(std::uint64_t j)
TEST(LPInitSolver, InfiniteLoopSingleVar)
static SymbolicEliminationTree::sharedNode MakeNode(Key key, const FACTORS &factors)
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:49:57