34 template<
class BAYESNET,
class GRAPH>
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>
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 = std::make_shared<Node>();
103 node->key = order[
j];
106 node->children.reserve(
factors.size());
107 node->factors.reserve(
factors.size());
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)
159 template<
class BAYESNET,
class GRAPH>
166 This temp(factorGraph, variableIndex, order);
171 template<
class BAYESNET,
class GRAPH>
192 template<
class BAYESNET,
class GRAPH>
197 for (
auto&& root :
roots_) {
198 std::queue<sharedNode> bfs_queue;
201 bfs_queue.push(root);
206 while (!bfs_queue.empty()) {
208 auto node = bfs_queue.front();
212 for (
auto&& child : node->children) {
213 bfs_queue.push(child);
223 template<
class BAYESNET,
class GRAPH>
224 std::pair<std::shared_ptr<BAYESNET>, std::shared_ptr<GRAPH> >
227 gttic(EliminationTree_eliminate);
229 auto result = std::make_shared<BayesNetType>();
235 auto allRemainingFactors = std::make_shared<FactorGraphType>();
240 return {
result, allRemainingFactors};
244 template<
class BAYESNET,
class GRAPH>
251 template<
class BAYESNET,
class GRAPH>
255 std::stack<sharedNode, FastVector<sharedNode> > stack1, stack2;
260 for(
const sharedNode& root: this->
roots_) { keys.emplace(root->key, root); }
262 for(
const Key_Node& key_node:
keys) { stack1.push(key_node.second); }
268 for(
const Key_Node& key_node:
keys) { stack2.push(key_node.second); }
272 while(!stack1.empty() && !stack2.empty()) {
280 if(node1->key != node2->key)
282 if(node1->factors.size() != node2->factors.size()) {
285 for(
typename Node::Factors::const_iterator it1 = node1->factors.begin(), it2 = node2->factors.begin();
286 it1 != node1->factors.end(); ++it1, ++it2)
289 if(!(*it1)->equals(**it2,
tol))
291 }
else if((*it1 && !*it2) || (*it2 && !*it1)) {
300 for(
const sharedNode& node: node1->children) {
keys.emplace(node->key, node); }
302 for(
const Key_Node& key_node:
keys) { stack1.push(key_node.second); }
306 for(
const sharedNode& node: node2->children) {
keys.emplace(node->key, node); }
308 for(
const Key_Node& key_node:
keys) { stack2.push(key_node.second); }
313 if(!stack1.empty() || !stack2.empty())
320 template<
class BAYESNET,
class GRAPH>