29 std::cout << s <<
" (" << problemSize_ <<
")";
34 template <
class GRAPH>
36 std::vector<size_t> nrFrontals;
37 nrFrontals.reserve(nrChildren());
38 for (
const sharedNode& child :
children)
39 nrFrontals.push_back(child->nrFrontals());
44 template <
class GRAPH>
48 cluster->orderedFrontalKeys.rend());
49 factors.push_back(cluster->factors);
50 children.insert(
children.end(), cluster->children.begin(), cluster->children.end());
52 problemSize_ = std::max(problemSize_, cluster->problemSize_);
58 const std::vector<bool>&
merge) {
59 gttic(Cluster_mergeChildren);
60 assert(merge.size() == this->
children.size());
64 size_t nrFactors =
factors.size();
65 size_t nrNewChildren = 0;
68 for(
const sharedNode& child: this->
children) {
70 nrKeys += child->orderedFrontalKeys.size();
71 nrFactors += child->factors.size();
72 nrNewChildren += child->nrChildren();
81 this->children.clear();
82 this->children.reserve(nrNewChildren);
86 for (
const sharedNode& child : oldChildren) {
98 template <
class GRAPH>
110 template <
class GRAPH>
114 for (
auto&& root :
roots_) {
115 std::queue<sharedNode> bfs_queue;
117 bfs_queue.push(root);
122 while (!bfs_queue.empty()) {
124 auto node = bfs_queue.front();
128 for (
auto child : node->children) {
129 bfs_queue.push(child);
139 template <
class GRAPH>
150 template<
class CLUSTERTREE>
153 typedef typename CLUSTERTREE::sharedFactor
sharedFactor;
154 typedef typename CLUSTERTREE::FactorType
FactorType;
156 typedef typename CLUSTERTREE::ConditionalType ConditionalType;
157 typedef typename CLUSTERTREE::BayesTreeType::Node BTNode;
160 size_t myIndexInParent;
162 std::shared_ptr<BTNode> bayesTreeNode;
164 std::shared_ptr<std::mutex> writeLock;
168 parentData(_parentData), bayesTreeNode(std::make_shared<BTNode>())
170 , writeLock(std::make_shared<std::mutex>())
175 parentData->writeLock->lock();
177 myIndexInParent = parentData->childFactors.size();
180 parentData->writeLock->unlock();
187 if (parentData->parentData)
188 bayesTreeNode->parent_ = parentData->bayesTreeNode;
189 parentData->bayesTreeNode->children.push_back(bayesTreeNode);
195 const typename CLUSTERTREE::sharedNode& node,
199 myData.bayesTreeNode->problemSize_ = node->problemSize();
206 const typename CLUSTERTREE::Eliminate& eliminationFunction_;
207 typename CLUSTERTREE::BayesTreeType::Nodes& nodesIndex_;
212 const typename CLUSTERTREE::Eliminate& eliminationFunction,
213 typename CLUSTERTREE::BayesTreeType::Nodes& nodesIndex) :
214 eliminationFunction_(eliminationFunction), nodesIndex_(nodesIndex) {
218 void operator()(
const typename CLUSTERTREE::sharedNode& node,
EliminationData& myData) {
222 FactorGraphType gatheredFactors;
223 gatheredFactors.reserve(node->factors.size() + node->nrChildren());
224 gatheredFactors.push_back(node->factors);
225 gatheredFactors.push_back(myData.childFactors);
229 for (
const sharedFactor& factor: node->factors) {
232 myData.bayesTreeNode->children.push_back(asSubtree->clique);
233 asSubtree->clique->parent_ = myData.bayesTreeNode;
238 auto eliminationResult = eliminationFunction_(gatheredFactors, node->orderedFrontalKeys);
243 myData.bayesTreeNode->setEliminationResult(eliminationResult);
248 for (
const Key& j : myData.bayesTreeNode->conditional()->frontals()) {
250 nodesIndex_.insert({j, myData.bayesTreeNode});
252 nodesIndex_.emplace(j, myData.bayesTreeNode);
256 if (!eliminationResult.second->empty()) {
258 myData.parentData->writeLock->lock();
260 myData.parentData->childFactors[myData.myIndexInParent] = eliminationResult.second;
262 myData.parentData->writeLock->unlock();
270 template<
class BAYESTREE,
class GRAPH>
277 remainingFactors_ = other.remainingFactors_;
283 template <
class BAYESTREE,
class GRAPH>
284 std::pair<std::shared_ptr<BAYESTREE>, std::shared_ptr<GRAPH> >
286 gttic(ClusterTree_eliminate);
290 std::shared_ptr<BayesTreeType> result = std::make_shared<BayesTreeType>();
293 Data rootsContainer(0, this->nrRoots());
295 typename Data::EliminationPostOrderVisitor visitorPost(
function, result->nodes_);
303 result->roots_.insert(result->roots_.end(), rootsContainer.bayesTreeNode->children.begin(),
304 rootsContainer.bayesTreeNode->children.end());
307 std::shared_ptr<FactorGraphType> remaining = std::make_shared<FactorGraphType>();
308 remaining->reserve(remainingFactors_.size() + rootsContainer.childFactors.size());
309 remaining->push_back(remainingFactors_.begin(), remainingFactors_.end());
310 for (
const sharedFactor& factor : rootsContainer.childFactors) {
312 remaining->push_back(factor);
316 return {result, remaining};
Definition: ClusterTree-inst.h:205
GTSAM_EXPORT void PrintKeyVector(const KeyVector &keys, const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter)
Utility function to print sets of keys with optional prefix.
FactorGraphType factors
Factors associated with this node.
Definition: ClusterTree.h:43
FastVector< sharedNode > roots_
Definition: ClusterTree.h:116
std::pair< std::shared_ptr< BayesTreeType >, std::shared_ptr< FactorGraphType > > eliminate(const Eliminate &function) const
Definition: ClusterTree-inst.h:285
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
void merge(const std::shared_ptr< Cluster > &cluster)
Merge in given cluster.
Definition: ClusterTree-inst.h:45
void addChild(const std::shared_ptr< Cluster > &cluster)
Add a child cluster.
Definition: ClusterTree.h:71
Variable ordering for the elimination algorithm.
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: ClusterTree-inst.h:99
GRAPH::FactorType FactorType
The type of factors.
Definition: ClusterTree.h:31
This & operator=(const This &other)
Definition: ClusterTree-inst.h:140
Definition: BayesTree.h:281
Definition: BayesTree.h:35
void DepthFirstForestParallel(FOREST &forest, DATA &rootData, VISITOR_PRE &visitorPre, VISITOR_POST &visitorPost, int problemSizeThreshold=10)
Definition: treeTraversal-inst.h:154
std::vector< size_t > nrFrontalsOfChildren() const
Return a vector with nrFrontal keys for each child.
Definition: ClusterTree-inst.h:35
void PrintForest(const FOREST &forest, std::string str, const KeyFormatter &keyFormatter)
Definition: treeTraversal-inst.h:219
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
Bayes Tree is a tree of cliques of a Bayes Chain.
FastVector< std::shared_ptr< typename FOREST::Node > > CloneForest(const FOREST &forest)
Definition: treeTraversal-inst.h:189
Keys orderedFrontalKeys
Frontal keys of this node.
Definition: ClusterTree.h:41
Definition: chartTesting.h:28
This & operator=(const This &other)
Definition: ClusterTree-inst.h:271
Children children
sub-trees
Definition: ClusterTree.h:38
std::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition: ClusterTree.h:32
Definition: ClusterTree-inst.h:151
SymbolicFactorGraph ::Eliminate Eliminate
Typedef for an eliminate subroutine.
Definition: ClusterTree.h:197
void mergeChildren(const std::vector< bool > &merge)
Merge all children for which bit is set into this node.
Definition: ClusterTree-inst.h:57
std::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition: ClusterTree.h:199
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
Collects factorgraph fragments defined on variable clusters, arranged in a tree.
Definition: ClusterTree.h:25
GRAPH FactorGraphType
The factor graph type.
Definition: ClusterTree.h:27
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition: ClusterTree-inst.h:27