12 #include <gtsam/inference/ClusterTree.h> 24 std::cout << s <<
" (" << problemSize_ <<
")";
29 template <
class GRAPH>
31 std::vector<size_t> nrFrontals;
32 nrFrontals.reserve(nrChildren());
33 for (
const sharedNode& child : children)
34 nrFrontals.push_back(child->nrFrontals());
39 template <
class GRAPH>
42 orderedFrontalKeys.insert(orderedFrontalKeys.end(), cluster->orderedFrontalKeys.rbegin(),
43 cluster->orderedFrontalKeys.rend());
44 factors.push_back(cluster->factors);
45 children.insert(children.end(), cluster->children.begin(), cluster->children.end());
47 problemSize_ = std::max(problemSize_, cluster->problemSize_);
53 const std::vector<bool>& merge) {
54 gttic(Cluster_mergeChildren);
55 assert(merge.size() == this->children.size());
58 size_t nrKeys = orderedFrontalKeys.size();
59 size_t nrFactors = factors.size();
60 size_t nrNewChildren = 0;
63 for(
const sharedNode& child: this->children) {
65 nrKeys += child->orderedFrontalKeys.size();
66 nrFactors += child->factors.size();
67 nrNewChildren += child->nrChildren();
75 auto oldChildren = this->children;
76 this->children.clear();
77 this->children.reserve(nrNewChildren);
78 orderedFrontalKeys.reserve(nrKeys);
79 factors.reserve(nrFactors);
81 for (
const sharedNode& child : oldChildren) {
85 this->addChild(child);
89 std::reverse(orderedFrontalKeys.begin(), orderedFrontalKeys.end());
93 template <
class GRAPH>
99 template <
class GRAPH>
110 template<
class CLUSTERTREE>
113 typedef typename CLUSTERTREE::sharedFactor
sharedFactor;
114 typedef typename CLUSTERTREE::FactorType
FactorType;
116 typedef typename CLUSTERTREE::ConditionalType ConditionalType;
117 typedef typename CLUSTERTREE::BayesTreeType::Node BTNode;
120 size_t myIndexInParent;
121 FastVector<sharedFactor> childFactors;
122 boost::shared_ptr<BTNode> bayesTreeNode;
125 parentData(_parentData), bayesTreeNode(boost::make_shared<BTNode>()) {
127 myIndexInParent = parentData->childFactors.size();
134 if (parentData->parentData)
135 bayesTreeNode->parent_ = parentData->bayesTreeNode;
136 parentData->bayesTreeNode->children.push_back(bayesTreeNode);
142 const typename CLUSTERTREE::sharedNode& node,
146 myData.bayesTreeNode->problemSize_ = node->problemSize();
153 const typename CLUSTERTREE::Eliminate& eliminationFunction_;
154 typename CLUSTERTREE::BayesTreeType::Nodes& nodesIndex_;
159 const typename CLUSTERTREE::Eliminate& eliminationFunction,
160 typename CLUSTERTREE::BayesTreeType::Nodes& nodesIndex) :
161 eliminationFunction_(eliminationFunction), nodesIndex_(nodesIndex) {
165 void operator()(
const typename CLUSTERTREE::sharedNode& node,
EliminationData& myData) {
169 FactorGraphType gatheredFactors;
170 gatheredFactors.reserve(node->factors.size() + node->nrChildren());
171 gatheredFactors += node->factors;
172 gatheredFactors += myData.childFactors;
176 for (
const sharedFactor& factor: node->factors) {
179 myData.bayesTreeNode->children.push_back(asSubtree->clique);
180 asSubtree->clique->parent_ = myData.bayesTreeNode;
185 auto eliminationResult = eliminationFunction_(gatheredFactors, node->orderedFrontalKeys);
190 myData.bayesTreeNode->setEliminationResult(eliminationResult);
195 for (
const Key& j: myData.bayesTreeNode->conditional()->frontals())
196 nodesIndex_.insert(std::make_pair(j, myData.bayesTreeNode));
199 if (!eliminationResult.second->empty())
200 myData.parentData->childFactors[myData.myIndexInParent] = eliminationResult.second;
206 template<
class BAYESTREE,
class GRAPH>
213 remainingFactors_ = other.remainingFactors_;
219 template <
class BAYESTREE,
class GRAPH>
220 std::pair<boost::shared_ptr<BAYESTREE>, boost::shared_ptr<GRAPH> >
222 gttic(ClusterTree_eliminate);
226 boost::shared_ptr<BayesTreeType> result = boost::make_shared<BayesTreeType>();
229 Data rootsContainer(0, this->nrRoots());
231 typename Data::EliminationPostOrderVisitor visitorPost(
function, result->nodes_);
239 result->roots_.insert(result->roots_.end(), rootsContainer.bayesTreeNode->children.begin(),
240 rootsContainer.bayesTreeNode->children.end());
243 boost::shared_ptr<FactorGraphType> remaining = boost::make_shared<FactorGraphType>();
244 remaining->reserve(remainingFactors_.size() + rootsContainer.childFactors.size());
245 remaining->push_back(remainingFactors_.begin(), remainingFactors_.end());
246 for (
const sharedFactor& factor : rootsContainer.childFactors) {
248 remaining->push_back(factor);
252 return std::make_pair(result, remaining);
boost::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition: ClusterTree.h:197
This & operator=(const This &other)
Assignment operator - makes a deep copy of the tree structure, but only pointers to factors are copie...
Definition: ClusterTree-inst.h:100
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Print the cluster tree.
Definition: ClusterTree-inst.h:94
SymbolicFactorGraph ::Eliminate Eliminate
Typedef for an eliminate subroutine.
Definition: ClusterTree.h:195
Definition: ClusterTree-inst.h:111
GRAPH::FactorType FactorType
The type of factors.
Definition: ClusterTree.h:31
Definition: BayesTree.h:267
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
boost::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition: ClusterTree.h:32
A cluster-tree that eliminates to a Bayes tree.
Definition: BayesTree.h:33
std::pair< boost::shared_ptr< BayesTreeType >, boost::shared_ptr< FactorGraphType > > eliminate(const Eliminate &function) const
Eliminate the factors to a Bayes tree and remaining factor graph.
Definition: ClusterTree-inst.h:221
A cluster-tree is associated with a factor graph and is defined as in Koller-Friedman: each node k re...
Definition: ClusterTree.h:25
Variable ordering for the elimination algorithm.
This & operator=(const This &other)
Assignment operator - makes a deep copy of the tree structure, but only pointers to factors are copie...
Definition: ClusterTree-inst.h:207
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:33
Definition: ClusterTree-inst.h:152
Keys orderedFrontalKeys
Frontal keys of this node.
Definition: ClusterTree.h:41
void DepthFirstForestParallel(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost, int problemSizeThreshold=10)
Traverse a forest depth-first with pre-order and post-order visits.
Definition: treeTraversal-inst.h:154
An object whose scope defines a block where TBB and OpenMP parallelism are mixed.
Definition: types.h:148
void PrintKeyVector(const KeyVector &keys, const string &s, const KeyFormatter &keyFormatter)
Utility function to print sets of keys with optional prefix.
Definition: Key.cpp:77
void PrintForest(const FOREST &forest, std::string str, const KeyFormatter &keyFormatter)
Print a tree, prefixing each line with str, and formatting keys using keyFormatter.
Definition: treeTraversal-inst.h:220
GRAPH FactorGraphType
The factor graph type.
Definition: ClusterTree.h:27
std::vector< size_t > nrFrontalsOfChildren() const
Return a vector with nrFrontal keys for each child.
Definition: ClusterTree-inst.h:30
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
Bayes Tree is a tree of cliques of a Bayes Chain.
void mergeChildren(const std::vector< bool > &merge)
Merge all children for which bit is set into this node.
Definition: ClusterTree-inst.h:52
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition: ClusterTree-inst.h:22
FastVector< boost::shared_ptr< typename FOREST::Node > > CloneForest(const FOREST &forest)
Clone a tree, copy-constructing new nodes (calling boost::make_shared) and setting up child pointers ...
Definition: treeTraversal-inst.h:190
void merge(const boost::shared_ptr< Cluster > &cluster)
Merge in given cluster.
Definition: ClusterTree-inst.h:40