16 #include <boost/foreach.hpp>
17 #include <boost/bind.hpp>
27 template<
class CLUSTERTREE>
28 struct EliminationData {
29 EliminationData*
const parentData;
30 size_t myIndexInParent;
31 FastVector<typename CLUSTERTREE::sharedFactor> childFactors;
32 boost::shared_ptr<typename CLUSTERTREE::BayesTreeType::Node> bayesTreeNode;
33 EliminationData(EliminationData* _parentData,
size_t nChildren) :
34 parentData(_parentData),
35 bayesTreeNode(boost::make_shared<typename CLUSTERTREE::BayesTreeType::Node>())
38 myIndexInParent = parentData->childFactors.size();
39 parentData->childFactors.push_back(
typename CLUSTERTREE::sharedFactor());
45 if(parentData->parentData)
46 bayesTreeNode->parent_ = parentData->bayesTreeNode;
47 parentData->bayesTreeNode->children.push_back(bayesTreeNode);
55 template<
class CLUSTERTREE>
56 EliminationData<CLUSTERTREE> eliminationPreOrderVisitor(
57 const typename CLUSTERTREE::sharedNode& node, EliminationData<CLUSTERTREE>& parentData)
59 EliminationData<CLUSTERTREE> myData(&parentData, node->children.size());
60 myData.bayesTreeNode->problemSize_ = node->problemSize();
67 template<
class CLUSTERTREE>
68 struct EliminationPostOrderVisitor
70 const typename CLUSTERTREE::Eliminate& eliminationFunction;
71 typename CLUSTERTREE::BayesTreeType::Nodes& nodesIndex;
72 EliminationPostOrderVisitor(
const typename CLUSTERTREE::Eliminate& eliminationFunction,
73 typename CLUSTERTREE::BayesTreeType::Nodes& nodesIndex) :
74 eliminationFunction(eliminationFunction), nodesIndex(nodesIndex) {}
75 void operator()(
const typename CLUSTERTREE::sharedNode& node, EliminationData<CLUSTERTREE>& myData)
78 typedef typename CLUSTERTREE::sharedFactor sharedFactor;
79 typedef typename CLUSTERTREE::FactorType FactorType;
80 typedef typename CLUSTERTREE::FactorGraphType FactorGraphType;
81 typedef typename CLUSTERTREE::ConditionalType ConditionalType;
82 typedef typename CLUSTERTREE::BayesTreeType::Node BTNode;
85 FactorGraphType gatheredFactors;
86 gatheredFactors.reserve(node->factors.size() + node->children.size());
87 gatheredFactors += node->factors;
88 gatheredFactors += myData.childFactors;
91 BOOST_FOREACH(
const sharedFactor& f, node->factors)
93 if(
const BayesTreeOrphanWrapper<BTNode>* asSubtree =
dynamic_cast<const BayesTreeOrphanWrapper<BTNode>*
>(f.get()))
95 myData.bayesTreeNode->children.push_back(asSubtree->clique);
96 asSubtree->clique->parent_ = myData.bayesTreeNode;
101 std::pair<boost::shared_ptr<ConditionalType>, boost::shared_ptr<FactorType> > eliminationResult =
102 eliminationFunction(gatheredFactors, Ordering(node->keys));
105 myData.bayesTreeNode->setEliminationResult(eliminationResult);
110 BOOST_FOREACH(
const Key& j, myData.bayesTreeNode->conditional()->frontals())
111 nodesIndex.insert(std::make_pair(j, myData.bayesTreeNode));
114 if(!eliminationResult.second->empty())
115 myData.parentData->childFactors[myData.myIndexInParent] = eliminationResult.second;
121 template<
class BAYESTREE,
class GRAPH>
123 const std::string& s,
const KeyFormatter& keyFormatter)
const
127 std::cout << j <<
" ";
128 std::cout <<
"problemSize = " << problemSize_ << std::endl;
132 template<
class BAYESTREE,
class GRAPH>
134 const std::string& s,
const KeyFormatter& keyFormatter)
const
140 template<
class BAYESTREE,
class GRAPH>
148 remainingFactors_ = other.remainingFactors_;
154 template<
class BAYESTREE,
class GRAPH>
155 std::pair<boost::shared_ptr<BAYESTREE>, boost::shared_ptr<GRAPH> >
158 gttic(ClusterTree_eliminate);
162 boost::shared_ptr<BayesTreeType> result = boost::make_shared<BayesTreeType>();
163 EliminationData<This> rootsContainer(0, roots_.size());
164 EliminationPostOrderVisitor<This> visitorPost(
function, result->nodes_);
168 eliminationPreOrderVisitor<This>, visitorPost, 10);
172 result->roots_.insert(result->roots_.end(), rootsContainer.bayesTreeNode->children.begin(), rootsContainer.bayesTreeNode->children.end());
175 boost::shared_ptr<FactorGraphType> allRemainingFactors = boost::make_shared<FactorGraphType>();
176 allRemainingFactors->reserve(remainingFactors_.size() + rootsContainer.childFactors.size());
177 allRemainingFactors->push_back(remainingFactors_.begin(), remainingFactors_.end());
178 BOOST_FOREACH(
const sharedFactor& factor, rootsContainer.childFactors)
180 allRemainingFactors->push_back(factor);
183 return std::make_pair(result, allRemainingFactors);
Bayes Tree is a tree of cliques of a Bayes Chain.
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
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:156
FactorGraphType::Eliminate Eliminate
Typedef for an eliminate subroutine.
Definition: ClusterTree.h:37
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:152
size_t Key
Integer nonlinear key type.
Definition: types.h:59
Keys keys
Frontal keys of this node.
Definition: ClusterTree.h:44
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:141
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Print the cluster tree.
Definition: ClusterTree-inst.h:133
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print this node
Definition: ClusterTree-inst.h:122
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:218
Collects factorgraph fragments defined on variable clusters, arranged in a tree.
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62
An object whose scope defines a block where TBB and OpenMP parallelism are mixed. ...
Definition: types.h:258
A cluster-tree is associated with a factor graph and is defined as in Koller-Friedman: each node k re...
Definition: BayesTree.h:33