| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * JunctionTree.h | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *   Created on: Feb 4, 2010 | 
					
						
							|  |  |  |  *       Author: nikai | 
					
						
							|  |  |  |  *  Description: The junction tree | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <set>
 | 
					
						
							|  |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							|  |  |  | #include "BayesTree.h"
 | 
					
						
							| 
									
										
										
										
											2010-07-13 05:34:03 +08:00
										 |  |  | #include "SymbolicConditional.h"
 | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 	template <class FG> | 
					
						
							|  |  |  | 	class JunctionTree : public Testable<JunctionTree<FG> > { | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 	public: | 
					
						
							|  |  |  | 		// the class for subgraphs that also include the pointers to the parents and two children
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 		class Clique : public FG { | 
					
						
							|  |  |  | 		private: | 
					
						
							|  |  |  | 			typedef typename boost::shared_ptr<Clique> shared_ptr; | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 			shared_ptr parent_;                  // the parent subgraph node
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 			std::vector<shared_ptr> children_;   // the child cliques
 | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 			Ordering frontal_;                   // the frontal varaibles
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 			Unordered separator_;                // the separator variables
 | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 			friend class JunctionTree<FG>; | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// empty constructor
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 			Clique() {} | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// constructor with all the information
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 			Clique(const FG& fgLocal, const Ordering& frontal, const Unordered& separator, | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 					 const shared_ptr& parent) | 
					
						
							|  |  |  | 				: frontal_(frontal), separator_(separator), FG(fgLocal), parent_(parent) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// constructor for an empty graph
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 			Clique(const Ordering& frontal, const Unordered& separator, const shared_ptr& parent) | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 				: frontal_(frontal), separator_(separator), parent_(parent) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 			// return the members
 | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 			const Ordering& frontal() const            { return frontal_;} | 
					
						
							|  |  |  | 			const Unordered& separator() const         { return separator_;} | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 			const std::vector<shared_ptr>& children()  { return children_; } | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// add a child node
 | 
					
						
							|  |  |  | 			void addChild(const shared_ptr& child) { children_.push_back(child); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 			// print the object
 | 
					
						
							|  |  |  | 			void print(const std::string& indent) const; | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 			void printTree(const std::string& indent) const; | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			// check equality
 | 
					
						
							|  |  |  | 			bool equals(const Clique& other) const; | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// typedef for shared pointers to cliques
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 		typedef typename Clique::shared_ptr sharedClique; | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	protected: | 
					
						
							|  |  |  | 		// Root clique
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 		sharedClique root_; | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	private: | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 		// distribute the factors along the Bayes tree
 | 
					
						
							|  |  |  | 		sharedClique distributeFactors(FG& fg, const BayesTree<SymbolicConditional>::sharedClique clique); | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-11 15:30:27 +08:00
										 |  |  | 		// utility function called by eliminate
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 		template <class Conditional> | 
					
						
							| 
									
										
										
										
											2010-07-11 15:30:27 +08:00
										 |  |  | 		std::pair<FG, BayesTree<Conditional> > eliminateOneClique(sharedClique fg_); | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 		// constructor
 | 
					
						
							|  |  |  | 		JunctionTree() {} | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 		// constructor given a factor graph and the elimination ordering
 | 
					
						
							|  |  |  | 		JunctionTree(FG& fg, const Ordering& ordering); | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 		// return the root clique
 | 
					
						
							|  |  |  | 		sharedClique root() const { return root_; } | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// eliminate the factors in the subgraphs
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 		template <class Conditional> | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 		BayesTree<Conditional> eliminate(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// print the object
 | 
					
						
							|  |  |  | 		void print(const std::string& str) const { | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 			cout << str << endl; | 
					
						
							|  |  |  | 			if (root_.get()) root_->printTree(""); | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-09 15:31:15 +08:00
										 |  |  | 		/** check equality */ | 
					
						
							|  |  |  | 		bool equals(const JunctionTree<FG>& other, double tol = 1e-9) const; | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	}; // JunctionTree
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace gtsam
 |