34 template<
class BAYESNET,
class GRAPH>
36 EliminationTree<BAYESNET,GRAPH>::Node::eliminate(
37 const std::shared_ptr<BayesNetType>& output,
42 assert(childrenResults.size() ==
children.size());
48 gatheredFactors.push_back(childrenResults.begin(), childrenResults.end());
52 auto eliminationResult = function(gatheredFactors, Ordering(keyAsVector));
55 output->push_back(eliminationResult.first);
58 return eliminationResult.second;
62 template<
class BAYESNET,
class GRAPH>
64 const std::string& str,
const KeyFormatter& keyFormatter)
const
66 std::cout << str <<
"(" << keyFormatter(key) <<
")\n";
71 std::cout << str <<
"null factor\n";
77 template<
class BAYESNET,
class GRAPH>
81 gttic(EliminationTree_Contructor);
85 const size_t m = graph.size();
86 const size_t n = order.size();
88 static const size_t none = std::numeric_limits<size_t>::max();
98 for (
size_t j = 0; j < n; j++)
102 const sharedNode node = std::make_shared<Node>();
103 node->
key = order[j];
106 node->
children.reserve(factors.size());
107 node->
factors.reserve(factors.size());
108 for(
const size_t i: factors) {
114 if (prevCol[i] != none) {
115 size_t k = prevCol[i];
119 while (parents[r] != none)
130 node->
factors.push_back(graph[i]);
131 factorUsed[i] =
true;
137 }
catch(std::invalid_argument& e) {
141 throw std::invalid_argument(
"EliminationTree: given ordering contains variables that are not involved in the factor graph");
147 assert(parents.empty() || parents.back() == none);
148 for(
size_t j = 0; j < n; ++j)
149 if(parents[j] == none)
150 roots_.push_back(nodes[j]);
153 for(
size_t i = 0; i < m; ++i)
154 if(!factorUsed[i] && graph[i])
155 remainingFactors_.push_back(graph[i]);
159 template<
class BAYESNET,
class GRAPH>
166 This temp(factorGraph, variableIndex, order);
171 template<
class BAYESNET,
class GRAPH>
180 remainingFactors_ = other.remainingFactors_;
192 template<
class BAYESNET,
class GRAPH>
197 for (
auto&& root :
roots_) {
198 std::queue<sharedNode> bfs_queue;
201 bfs_queue.push(std::move(root));
205 while (!bfs_queue.empty()) {
207 auto node = std::move(bfs_queue.front());
211 for (
auto&& child : node->children) {
212 bfs_queue.push(std::move(child));
222 template<
class BAYESNET,
class GRAPH>
223 std::pair<std::shared_ptr<BAYESNET>, std::shared_ptr<GRAPH> >
226 gttic(EliminationTree_eliminate);
228 auto result = std::make_shared<BayesNetType>();
234 auto allRemainingFactors = std::make_shared<FactorGraphType>();
235 allRemainingFactors->push_back(remainingFactors_.begin(), remainingFactors_.end());
239 return {result, allRemainingFactors};
243 template<
class BAYESNET,
class GRAPH>
250 template<
class BAYESNET,
class GRAPH>
254 std::stack<sharedNode, FastVector<sharedNode> > stack1, stack2;
259 for(
const sharedNode& root: this->
roots_) { keys.emplace(root->key, root); }
261 for(
const Key_Node& key_node: keys) { stack1.push(key_node.second); }
267 for(
const Key_Node& key_node: keys) { stack2.push(key_node.second); }
271 while(!stack1.empty() && !stack2.empty()) {
279 if(node1->
key != node2->
key)
284 for(
typename Node::Factors::const_iterator it1 = node1->
factors.begin(), it2 = node2->
factors.begin();
285 it1 != node1->
factors.end(); ++it1, ++it2)
288 if(!(*it1)->equals(**it2, tol))
290 }
else if((*it1 && !*it2) || (*it2 && !*it1)) {
301 for(
const Key_Node& key_node: keys) { stack1.push(key_node.second); }
307 for(
const Key_Node& key_node: keys) { stack2.push(key_node.second); }
312 if(!stack1.empty() || !stack2.empty())
319 template<
class BAYESNET,
class GRAPH>
322 remainingFactors_.swap(other.remainingFactors_);
Variable ordering for the elimination algorithm.
Contains generic inference algorithms that convert between templated graphical models,...
FastVector< typename TREE::sharedFactor > EliminateTree(RESULT &result, const TREE &tree, const typename TREE::Eliminate &function)
Eliminate an elimination tree or a Bayes tree (used internally).
Definition inference-inst.h:74
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
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< FactorIndex > FactorIndices
Define collection types:
Definition Factor.h:37
FastVector< std::shared_ptr< typename FOREST::Node > > CloneForest(const FOREST &forest)
Clone a tree, copy-constructing new nodes (calling std::make_shared) and setting up child pointers fo...
Definition treeTraversal-inst.h:246
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:276
FastMap is a thin wrapper around std::map that uses the boost fast_pool_allocator instead of the defa...
Definition FastMap.h:40
An elimination tree is a data structure used intermediately during elimination.
Definition EliminationTree.h:52
bool equals(const This &other, double tol=1e-9) const
Test whether the tree is equal to another.
Definition EliminationTree-inst.h:251
void print(const std::string &name="EliminationTree: ", const KeyFormatter &formatter=DefaultKeyFormatter) const
Print the tree to cout.
Definition EliminationTree-inst.h:244
This & operator=(const This &other)
Assignment operator - makes a deep copy of the tree structure, but only pointers to factors are copie...
Definition EliminationTree-inst.h:173
EliminationTree< BAYESNET, GRAPH > This
This class.
Definition EliminationTree.h:54
~EliminationTree()
Destructor Using default destructor causes stack overflow for large trees due to recursive destructio...
Definition EliminationTree-inst.h:193
FastVector< sharedNode > roots_
concept check
Definition EliminationTree.h:86
std::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition EliminationTree.h:60
std::shared_ptr< Node > sharedNode
Shared pointer to Node.
Definition EliminationTree.h:80
GRAPH FactorGraphType
The factor graph type.
Definition EliminationTree.h:58
void swap(This &other)
Swap the data of this tree with another one, this operation is very fast.
Definition EliminationTree-inst.h:320
std::pair< std::shared_ptr< BayesNetType >, std::shared_ptr< FactorGraphType > > eliminate(Eliminate function) const
Eliminate the factors to a Bayes net and remaining factor graph.
Definition EliminationTree-inst.h:224
EliminationTree()
Protected default constructor.
Definition EliminationTree.h:164
EliminationTree(const FactorGraphType &factorGraph, const VariableIndex &structure, const Ordering &order)
Build the elimination tree of a factor graph using pre-computed column structure.
Definition EliminationTree-inst.h:78
const FastVector< sharedFactor > & remainingFactors() const
Return the remaining factors that are not pulled into elimination.
Definition EliminationTree.h:157
Key key
key associated with root
Definition EliminationTree.h:70
Children children
sub-trees
Definition EliminationTree.h:72
Factors factors
factors associated with root
Definition EliminationTree.h:71
The VariableIndex class computes and stores the block column structure of a factor graph.
Definition VariableIndex.h:41
const FactorIndices & at(Key variable) const
Access a list of factors by variable.
Definition VariableIndex.cpp:30