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


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:42:26