| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    EliminationTree.h | 
					
						
							|  |  |  |  * @brief    | 
					
						
							|  |  |  |  * @author  Frank Dellaert | 
					
						
							|  |  |  |  * @created Oct 13, 2010 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <list>
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <utility>
 | 
					
						
							|  |  |  | #include <boost/pool/pool_alloc.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/inference/VariableIndex.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | #include <gtsam/inference/BayesNet.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | #include <gtsam/inference/FactorGraph.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class EliminationTreeTester; // for unit tests, see testEliminationTree
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  |  * An elimination tree is a data structure used intermediately during | 
					
						
							|  |  |  |  * elimination, and it can be used to save work between multiple eliminations. | 
					
						
							| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  | template<class FACTOR> | 
					
						
							|  |  |  | class EliminationTree: public Testable<EliminationTree<FACTOR> > { | 
					
						
							| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  |   typedef typename FACTOR::shared_ptr sharedFactor; | 
					
						
							|  |  |  |   typedef boost::shared_ptr<EliminationTree<FACTOR> > shared_ptr; | 
					
						
							|  |  |  |   typedef gtsam::BayesNet<typename FACTOR::Conditional> BayesNet; | 
					
						
							| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   typedef std::list<sharedFactor, boost::fast_pool_allocator<sharedFactor> > Factors; | 
					
						
							|  |  |  |   typedef std::list<shared_ptr, boost::fast_pool_allocator<shared_ptr> > SubTrees; | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  |   typedef std::vector<typename FACTOR::Conditional::shared_ptr> Conditionals; | 
					
						
							| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   Index key_; /** index associated with root */ | 
					
						
							|  |  |  |   Factors factors_; /** factors associated with root */ | 
					
						
							|  |  |  |   SubTrees subTrees_; /** sub-trees */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** default constructor, private, as you should use Create below */ | 
					
						
							|  |  |  |   EliminationTree(Index key = 0) : key_(key) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** add a factor, for Create use only */ | 
					
						
							|  |  |  |   void add(const sharedFactor& factor) { factors_.push_back(factor); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** add a subtree, for Create use only */ | 
					
						
							|  |  |  |   void add(const shared_ptr& child) { subTrees_.push_back(child); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Static internal function to build a vector of parent pointers using the | 
					
						
							|  |  |  |    * algorithm of Gilbert et al., 2001, BIT. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2010-10-23 02:02:55 +08:00
										 |  |  |   static std::vector<Index> ComputeParents(const VariableIndex& structure); | 
					
						
							| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Recursive routine that eliminates the factors arranged in an elimination tree | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  |   sharedFactor eliminate_(Conditionals& conditionals) const; | 
					
						
							| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Allow access to constructor and add methods for testing purposes
 | 
					
						
							|  |  |  |   friend class ::EliminationTreeTester; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Named constructor to build the elimination tree of a factor graph using | 
					
						
							|  |  |  |    * pre-computed column structure. | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   template<class DERIVEDFACTOR> | 
					
						
							| 
									
										
										
										
											2010-10-23 02:02:55 +08:00
										 |  |  |   static shared_ptr Create(const FactorGraph<DERIVEDFACTOR>& factorGraph, const VariableIndex& structure); | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  |   /** Named constructor to build the elimination tree of a factor graph */ | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  |   template<class DERIVEDFACTOR> | 
					
						
							|  |  |  |   static shared_ptr Create(const FactorGraph<DERIVEDFACTOR>& factorGraph); | 
					
						
							| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** Print the tree to cout */ | 
					
						
							|  |  |  |   void print(const std::string& name = "EliminationTree: ") const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Test whether the tree is equal to another */ | 
					
						
							|  |  |  |   bool equals(const EliminationTree& other, double tol = 1e-9) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Eliminate the factors to a Bayes Net */ | 
					
						
							| 
									
										
										
										
											2010-10-20 05:31:13 +08:00
										 |  |  |   typename BayesNet::shared_ptr eliminate() const; | 
					
						
							| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |