12#include <gtsam/inference/ClusterTree.h>
24 std::cout << s <<
" (" << problemSize_ <<
")";
31 std::vector<size_t> nrFrontals;
32 nrFrontals.reserve(nrChildren());
33 for (
const sharedNode& child : children)
34 nrFrontals.push_back(child->nrFrontals());
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());
110template<
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;
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;
206template<
class BAYESTREE,
class GRAPH>
213 remainingFactors_ = other.remainingFactors_;
219template <
class BAYESTREE,
class GRAPH>
220std::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);
Bayes Tree is a tree of cliques of a Bayes Chain.
Variable ordering for the elimination algorithm.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
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
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:69
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
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:189
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:219
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:161
A cluster-tree that eliminates to a Bayes tree.
Definition: ClusterTree.h:184
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
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
boost::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition: ClusterTree.h:197
GRAPH::Eliminate Eliminate
Typedef for an eliminate subroutine.
Definition: ClusterTree.h:195
Definition: BayesTree.h:270
Definition: ClusterTree-inst.h:111
Definition: ClusterTree-inst.h:152
A cluster-tree is associated with a factor graph and is defined as in Koller-Friedman: each node k re...
Definition: ClusterTree.h:25
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
FastVector< sharedNode > roots_
concept check
Definition: ClusterTree.h:116
GRAPH::FactorType FactorType
The type of factors.
Definition: ClusterTree.h:31
GRAPH FactorGraphType
The factor graph type.
Definition: ClusterTree.h:27
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Print the cluster tree.
Definition: ClusterTree-inst.h:94
boost::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition: ClusterTree.h:32
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition: ClusterTree-inst.h:22
Keys orderedFrontalKeys
Frontal keys of this node.
Definition: ClusterTree.h:41
void mergeChildren(const std::vector< bool > &merge)
Merge all children for which bit is set into this node.
Definition: ClusterTree-inst.h:52
void merge(const boost::shared_ptr< Cluster > &cluster)
Merge in given cluster.
Definition: ClusterTree-inst.h:40
std::vector< size_t > nrFrontalsOfChildren() const
Return a vector with nrFrontal keys for each child.
Definition: ClusterTree-inst.h:30