gtsam
Loading...
Searching...
No Matches
JunctionTree-inst.h
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#pragma once
22
27
28#include <cassert>
29
30namespace gtsam {
31
32template<class BAYESTREE, class GRAPH, class ETREE_NODE>
33struct ConstructorTraversalData {
34 typedef typename JunctionTree<BAYESTREE, GRAPH>::Node Node;
35 typedef typename JunctionTree<BAYESTREE, GRAPH>::sharedNode sharedNode;
36
37 ConstructorTraversalData* const parentData;
38 sharedNode junctionTreeNode;
39 FastVector<SymbolicConditional::shared_ptr> childSymbolicConditionals;
40 FastVector<SymbolicFactor::shared_ptr> childSymbolicFactors;
41
42 // Small inner class to store symbolic factors
43 class SymbolicFactors: public FactorGraph<Factor> {
44 };
45
46 ConstructorTraversalData(ConstructorTraversalData* _parentData) :
47 parentData(_parentData) {
48 }
49
50 // Pre-order visitor function
51 static ConstructorTraversalData ConstructorTraversalVisitorPre(
52 const std::shared_ptr<ETREE_NODE>& node,
53 ConstructorTraversalData& parentData) {
54 // On the pre-order pass, before children have been visited, we just set up
55 // a traversal data structure with its own JT node, and create a child
56 // pointer in its parent.
58 myData.junctionTreeNode =
59 std::make_shared<Node>(node->key, node->factors);
60 parentData.junctionTreeNode->addChild(myData.junctionTreeNode);
61 return myData;
62 }
63
64 // Post-order visitor function
65 static void ConstructorTraversalVisitorPostAlg2(
66 const std::shared_ptr<ETREE_NODE>& ETreeNode,
67 const ConstructorTraversalData& myData) {
68 // In this post-order visitor, we combine the symbolic elimination results
69 // from the elimination tree children and symbolically eliminate the current
70 // elimination tree node. We then check whether each of our elimination
71 // tree child nodes should be merged with us. The check for this is that
72 // our number of symbolic elimination parents is exactly 1 less than
73 // our child's symbolic elimination parents - this condition indicates that
74 // eliminating the current node did not introduce any parents beyond those
75 // already in the child->
76
77 // Do symbolic elimination for this node
78 SymbolicFactors symbolicFactors;
79 symbolicFactors.reserve(
80 ETreeNode->factors.size() + myData.childSymbolicFactors.size());
81 // Add ETree node factors
82 symbolicFactors.push_back(ETreeNode->factors);
83 // Add symbolic factors passed up from children
84 symbolicFactors.push_back(myData.childSymbolicFactors);
85
86 Ordering keyAsOrdering;
87 keyAsOrdering.push_back(ETreeNode->key);
88 const auto [myConditional, mySeparatorFactor] =
89 internal::EliminateSymbolic(symbolicFactors, keyAsOrdering);
90
91 // Store symbolic elimination results in the parent
92 myData.parentData->childSymbolicConditionals.push_back(myConditional);
93 myData.parentData->childSymbolicFactors.push_back(mySeparatorFactor);
94
95 sharedNode node = myData.junctionTreeNode;
96 const FastVector<SymbolicConditional::shared_ptr>& childConditionals =
97 myData.childSymbolicConditionals;
98 node->problemSize_ = (int) (myConditional->size() * symbolicFactors.size());
99
100 // Merge our children if they are in our clique - if our conditional has
101 // exactly one fewer parent than our child's conditional.
102 const size_t myNrParents = myConditional->nrParents();
103 const size_t nrChildren = node->nrChildren();
104 assert(childConditionals.size() == nrChildren);
105
106 // decide which children to merge, as index into children
107 std::vector<size_t> nrFrontals = node->nrFrontalsOfChildren();
108 std::vector<bool> merge(nrChildren, false);
109 size_t myNrFrontals = 1;
110 for (size_t i = 0;i<nrChildren;i++){
111 // Check if we should merge the i^th child
112 if (myNrParents + myNrFrontals == childConditionals[i]->nrParents()) {
113 // Increment number of frontal variables
114 myNrFrontals += nrFrontals[i];
115 merge[i] = true;
116 }
117 }
118
119 // now really merge
120 node->mergeChildren(merge);
121 }
122};
123
124/* ************************************************************************* */
125template<class BAYESTREE, class GRAPH>
126template<class ETREE_BAYESNET, class ETREE_GRAPH>
128 const EliminationTree<ETREE_BAYESNET, ETREE_GRAPH>& eliminationTree) {
129 gttic(JunctionTree_FromEliminationTree);
130 // Here we rely on the BayesNet having been produced by this elimination tree,
131 // such that the conditionals are arranged in DFS post-order. We traverse the
132 // elimination tree, and inspect the symbolic conditional corresponding to
133 // each node. The elimination tree node is added to the same clique with its
134 // parent if it has exactly one more Bayes net conditional parent than
135 // does its elimination tree parent.
136
137 // Traverse the elimination tree, doing symbolic elimination and merging nodes
138 // as we go. Gather the created junction tree roots in a dummy Node.
141 Data rootData(0);
142 // Make a dummy node to gather the junction tree roots
143 rootData.junctionTreeNode = std::make_shared<typename Base::Node>();
144 treeTraversal::DepthFirstForest(eliminationTree, rootData,
145 Data::ConstructorTraversalVisitorPre,
146 Data::ConstructorTraversalVisitorPostAlg2);
147
148 // Assign roots from the dummy node
149 this->addChildrenAsRoots(rootData.junctionTreeNode);
150
151 // Transfer remaining factors from elimination tree
152 Base::remainingFactors_ = eliminationTree.remainingFactors();
153}
154
155} // namespace gtsam
The junction tree.
Collects factorgraph fragments defined on variable clusters, arranged in a tree.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
void DepthFirstForest(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost)
Traverse a forest depth-first with pre-order and post-order visits.
Definition treeTraversal-inst.h:78
FactorGraph()
Definition FactorGraph.h:103
An elimination tree is a data structure used intermediately during elimination.
Definition EliminationTree.h:52
const FastVector< sharedFactor > & remainingFactors() const
Return the remaining factors that are not pulled into elimination.
Definition EliminationTree.h:157
Definition EliminationTree.h:66
Definition JunctionTree-inst.h:33
Definition JunctionTree-inst.h:43
JunctionTree(const EliminationTree< ETREE_BAYESNET, ETREE_GRAPH > &eliminationTree)
Build the junction tree from an elimination tree.
Definition JunctionTree-inst.h:127