20 #include <boost/foreach.hpp>
21 #include <boost/make_shared.hpp>
22 #include <boost/bind.hpp>
35 template<
class BAYESNET,
class GRAPH>
36 typename EliminationTree<BAYESNET,GRAPH>::sharedFactor
37 EliminationTree<BAYESNET,GRAPH>::Node::eliminate(
38 const boost::shared_ptr<BayesNetType>& output,
39 const Eliminate&
function,
const FastVector<sharedFactor>& childrenResults)
const
43 assert(childrenResults.size() ==
children.size());
49 gatheredFactors.push_back(childrenResults.begin(), childrenResults.end());
52 FastVector<Key> keyAsVector(1); keyAsVector[0] =
key;
53 std::pair<boost::shared_ptr<ConditionalType>, boost::shared_ptr<FactorType> > eliminationResult =
54 function(gatheredFactors, Ordering(keyAsVector));
57 output->push_back(eliminationResult.first);
60 return eliminationResult.second;
64 template<
class BAYESNET,
class GRAPH>
65 void EliminationTree<BAYESNET,GRAPH>::Node::print(
66 const std::string& str,
const KeyFormatter& keyFormatter)
const
68 std::cout << str <<
"(" << keyFormatter(key) <<
")\n";
73 std::cout << str <<
"null factor\n";
79 template<
class BAYESNET,
class GRAPH>
83 gttic(EliminationTree_Contructor);
87 const size_t m = graph.size();
88 const size_t n = order.size();
100 for (
size_t j = 0; j < n; j++)
104 nodes[j] = boost::make_shared<Node>();
105 nodes[j]->key = order[j];
108 BOOST_FOREACH(
const size_t i, factors) {
114 if (prevCol[i] != none) {
115 size_t k = prevCol[i];
119 while (parents[r] != none)
126 nodes[j]->children.push_back(nodes[r]);
131 nodes[j]->factors.push_back(graph[i]);
132 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_;
186 template<
class BAYESNET,
class GRAPH>
187 std::pair<boost::shared_ptr<BAYESNET>, boost::shared_ptr<GRAPH> >
190 gttic(EliminationTree_eliminate);
192 boost::shared_ptr<BayesNetType> result = boost::make_shared<BayesNetType>();
198 boost::shared_ptr<FactorGraphType> allRemainingFactors = boost::make_shared<FactorGraphType>();
199 allRemainingFactors->push_back(remainingFactors_.begin(), remainingFactors_.end());
200 allRemainingFactors->push_back(remainingFactors.begin(), remainingFactors.end());
203 return std::make_pair(result, allRemainingFactors);
207 template<
class BAYESNET,
class GRAPH>
214 template<
class BAYESNET,
class GRAPH>
218 std::stack<sharedNode, FastVector<sharedNode> > stack1, stack2;
223 BOOST_FOREACH(
const sharedNode& root, this->roots_) { keys.insert(std::make_pair(root->key, root)); }
225 BOOST_FOREACH(
const Key_Node& key_node, keys) { stack1.push(key_node.second); }
229 BOOST_FOREACH(
const sharedNode& root, expected.roots_) { keys.insert(std::make_pair(root->key, root)); }
231 BOOST_FOREACH(
const Key_Node& key_node, keys) { stack2.push(key_node.second); }
235 while(!stack1.empty() && !stack2.empty()) {
243 if(node1->key != node2->key)
245 if(node1->factors.size() != node2->factors.size()) {
248 for(
typename Node::Factors::const_iterator it1 = node1->factors.begin(), it2 = node2->factors.begin();
249 it1 != node1->factors.end(); ++it1, ++it2)
252 if(!(*it1)->equals(**it2, tol))
254 }
else if((*it1 && !*it2) || (*it2 && !*it1)) {
263 BOOST_FOREACH(
const sharedNode& node, node1->children) { keys.insert(std::make_pair(node->key, node)); }
265 BOOST_FOREACH(
const Key_Node& key_node, keys) { stack1.push(key_node.second); }
269 BOOST_FOREACH(
const sharedNode& node, node2->children) { keys.insert(std::make_pair(node->key, node)); }
271 BOOST_FOREACH(
const Key_Node& key_node, keys) { stack2.push(key_node.second); }
276 if(!stack1.empty() || !stack2.empty())
283 template<
class BAYESNET,
class GRAPH>
285 roots_.swap(other.roots_);
286 remainingFactors_.swap(other.remainingFactors_);
Contains generic inference algorithms that convert between templated graphical models, i.e., factor graphs, Bayes nets, and Bayes trees.
bool equals(const This &other, double tol=1e-9) const
Test whether the tree is equal to another.
Definition: EliminationTree-inst.h:215
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 print(const std::string &name="EliminationTree: ", const KeyFormatter &formatter=DefaultKeyFormatter) const
Print the tree to cout.
Definition: EliminationTree-inst.h:208
void swap(This &other)
Swap the data of this tree with another one, this operation is very fast.
Definition: EliminationTree-inst.h:284
Key key
key associated with root
Definition: EliminationTree.h:70
double max(const Vector &a)
Return the max element of a vector.
Definition: Vector.cpp:238
EliminationTree()
Protected default constructor.
Definition: EliminationTree.h:161
const FastVector< sharedFactor > & remainingFactors() const
Return the remaining factors that are not pulled into elimination.
Definition: EliminationTree.h:154
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:42
GRAPH FactorGraphType
The factor graph type.
Definition: EliminationTree.h:58
Children children
sub-trees
Definition: EliminationTree.h:72
std::pair< boost::shared_ptr< BayesNetType >, boost::shared_ptr< FactorGraphType > > eliminate(Eliminate function) const
Eliminate the factors to a Bayes net and remaining factor graph.
Definition: EliminationTree-inst.h:188
Definition: Ordering.h:30
An elimination tree is a data structure used intermediately during elimination.
Definition: EliminationTree.h:51
Definition: FastList.h:38
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
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
boost::shared_ptr< Node > sharedNode
Shared pointer to Node.
Definition: EliminationTree.h:80
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
boost::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
Definition: EliminationTree.h:60