20 #include <boost/make_shared.hpp> 21 #include <boost/bind.hpp> 34 template<
class BAYESNET,
class GRAPH>
37 const boost::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>
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();
98 for (
size_t j = 0;
j <
n;
j++)
102 const sharedNode node = boost::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)
126 node->children.push_back(nodes[r]);
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)
153 for(
size_t i = 0;
i <
m; ++
i)
154 if(!factorUsed[
i] && graph[
i])
159 template<
class BAYESNET,
class GRAPH>
166 This temp(factorGraph, variableIndex, order);
171 template<
class BAYESNET,
class GRAPH>
186 template<
class BAYESNET,
class GRAPH>
187 std::pair<boost::shared_ptr<BAYESNET>, boost::shared_ptr<GRAPH> >
190 gttic(EliminationTree_eliminate);
192 auto result = boost::make_shared<BayesNetType>();
198 auto allRemainingFactors = boost::make_shared<FactorGraphType>();
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;
225 for(
const Key_Node& key_node: keys) { stack1.push(key_node.second); }
231 for(
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 for(
const sharedNode& node: node1->children) { keys.insert(std::make_pair(node->key, node)); }
265 for(
const Key_Node& key_node: keys) { stack1.push(key_node.second); }
269 for(
const sharedNode& node: node2->children) { keys.insert(std::make_pair(node->key, node)); }
271 for(
const Key_Node& key_node: keys) { stack2.push(key_node.second); }
276 if(!stack1.empty() || !stack2.empty())
283 template<
class BAYESNET,
class GRAPH>
void print(const std::string &name="EliminationTree: ", const KeyFormatter &formatter=DefaultKeyFormatter) const
FastVector< sharedNode > roots_
Factors factors
factors associated with root
GRAPH FactorGraphType
The factor graph type.
void print(const std::string &str, const KeyFormatter &keyFormatter) const
FastVector< FactorIndex > FactorIndices
Define collection types:
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
sharedFactor eliminate(const boost::shared_ptr< BayesNetType > &output, const Eliminate &function, const FastVector< sharedFactor > &childrenFactors) const
const mpreal root(const mpreal &x, unsigned long int k, mp_rnd_t r=mpreal::get_default_rnd())
Children children
sub-trees
NonlinearFactorGraph graph
const KeyFormatter & formatter
boost::shared_ptr< Node > sharedNode
Shared pointer to Node.
EliminationTree()
Protected default constructor.
GRAPH::Eliminate Eliminate
const FastVector< sharedFactor > & remainingFactors() const
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
boost::shared_ptr< FactorType > sharedFactor
Shared pointer to a factor.
FastVector< typename TREE::sharedFactor > EliminateTree(RESULT &result, const TREE &tree, const typename TREE::Eliminate &function)
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
std::pair< boost::shared_ptr< BayesNetType >, boost::shared_ptr< FactorGraphType > > eliminate(Eliminate function) const
Array< double, 1, 3 > e(1./3., 0.5, 2.)
void PrintForest(const FOREST &forest, std::string str, const KeyFormatter &keyFormatter)
Key key
key associated with root
FastVector< boost::shared_ptr< typename FOREST::Node > > CloneForest(const FOREST &forest)
FastVector< sharedFactor > remainingFactors_
This & operator=(const This &other)
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Annotation for function names.