/* * JunctionTree-inl.h * * Created on: Feb 4, 2010 * Author: nikai * Description: the junction tree */ #pragma once #include #include #include #include "Pose2.h" #include "BayesTree-inl.h" #include "SymbolicFactorGraph.h" #include "JunctionTree.h" #define DEBUG(i) \ if (verboseLevel>i) cout namespace gtsam { using namespace std; /* ************************************************************************* */ /** * Linear JunctionTree */ template void JunctionTree::SubFG::printTree(const string& indent) const { print(indent); BOOST_FOREACH(const shared_ptr& child, children_) child->printTree(indent+" "); } /* ************************************************************************* */ template pair::sharedClique> JunctionTree::eliminateOneClique(sharedSubFG current, BayesTree& bayesTree) { FG fg; // factor graph will be assembled from local factors and marginalized children list children; fg.push_back(*current); // add the local factor graph BOOST_FOREACH(sharedSubFG& child, current->children_) { // receive the factors from the child and its clique point FG fgChild; sharedClique cliqueChild; boost::tie(fgChild, cliqueChild) = eliminateOneClique(child, bayesTree); if (!cliqueChild.get()) throw runtime_error("eliminateOneClique: child clique is invalid!"); fg.push_back(fgChild); children.push_back(cliqueChild); } // eliminate the combined factors // warning: fg is being eliminated in-place and will contain marginal afterwards // BayesNet bn = fg.eliminate(current->frontal_); BayesNet bn = fg.eliminateFrontals(current->frontal_); // create a new clique corresponding the combined factors sharedClique new_clique = bayesTree.insert(bn, children); return make_pair(fg, new_clique); } /* ************************************************************************* */ template BayesTree JunctionTree::eliminate() { BayesTree bayesTree; eliminateOneClique(rootFG_, bayesTree); return bayesTree; } /* ************************************************************************* */ template void JunctionTree::iterSubGraphsDFS(VisitorSubFG visitor, sharedSubFG current) { if (!current.get()) current = rootFG_; // iterateBFS(visitor, current); } /* ************************************************************************* */ template void JunctionTree::iterSubGraphsBFS(VisitorSubFG visitor) { // iterateDFS(visitor, rootFG_); } /* ************************************************************************* */ /** * Linear JunctionTree */ template void LinearJunctionTree::btreeBackSubstitue(typename BayesTree::sharedClique current, VectorConfig& config) { // solve the bayes net in the current node typename BayesNet::const_reverse_iterator it = current->rbegin(); for (; it!=current->rend(); it++) { Vector x = (*it)->solve(config); // Solve for that variable config.insert((*it)->key(),x); // store result in partial solution } // solve the bayes nets in the child nodes BOOST_FOREACH(sharedClique child, current->children_) { btreeBackSubstitue(child, config); } } /* ************************************************************************* */ template VectorConfig LinearJunctionTree::optimize() { // eliminate from leaves to the root BayesTree bayesTree = JunctionTree::eliminate(); VectorConfig result; btreeBackSubstitue(bayesTree.root(), result); return result; } } //namespace gtsam