| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    FactorGraph.h | 
					
						
							|  |  |  |  * @brief   Factor Graph Base Class | 
					
						
							|  |  |  |  * @author  Carlos Nieto | 
					
						
							|  |  |  |  * @author  Christian Potthast | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  | #include <boost/serialization/map.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | #include <boost/serialization/list.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | #include <boost/serialization/vector.hpp>
 | 
					
						
							|  |  |  | #include <boost/serialization/shared_ptr.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  | #include "Testable.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-05 12:56:59 +08:00
										 |  |  | #include "BayesNet.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-14 10:07:14 +08:00
										 |  |  | #include "graph.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | #include "Key.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  | #include "SymbolMap.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | namespace gtsam { | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-13 12:13:03 +08:00
										 |  |  | 	class Ordering; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * A factor graph is a bipartite graph with factor nodes connected to variable nodes. | 
					
						
							|  |  |  | 	 * In this class, however, only factor nodes are kept around. | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 	 *  | 
					
						
							|  |  |  | 	 * Templated on the type of factors and configuration. | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  | 	template<class Factor> class FactorGraph: public Testable<FactorGraph<Factor> > { | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 		typedef typename boost::shared_ptr<Factor> sharedFactor; | 
					
						
							|  |  |  | 		typedef typename std::vector<sharedFactor>::iterator iterator; | 
					
						
							|  |  |  | 		typedef typename std::vector<sharedFactor>::const_iterator const_iterator; | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	protected: | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** Collection of factors */ | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 		std::vector<sharedFactor> factors_; | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-23 05:33:00 +08:00
										 |  |  | 		/** For each variable a list of factor indices connected to it  */ | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  | 		typedef SymbolMap<std::list<int> > Indices; | 
					
						
							| 
									
										
										
										
											2009-10-23 05:33:00 +08:00
										 |  |  | 		Indices indices_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 	public: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 12:56:59 +08:00
										 |  |  | 		/** Default constructor */ | 
					
						
							|  |  |  | 		FactorGraph() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** convert from Bayes net */ | 
					
						
							|  |  |  | 		template<class Conditional> | 
					
						
							|  |  |  | 		FactorGraph(const BayesNet<Conditional>& bayesNet); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-26 06:27:18 +08:00
										 |  |  | 		/** print out graph */ | 
					
						
							|  |  |  | 		void print(const std::string& s = "FactorGraph") const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** Check equality */ | 
					
						
							|  |  |  | 		bool equals(const FactorGraph& fg, double tol = 1e-9) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 13:27:19 +08:00
										 |  |  | 		/** STL begin and end, so we can use BOOST_FOREACH */ | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 13:27:19 +08:00
										 |  |  | 		inline       iterator begin()       { return factors_.begin();} | 
					
						
							|  |  |  | 		inline const_iterator begin() const { return factors_.begin();} | 
					
						
							|  |  |  | 		inline       iterator end()         { return factors_.end();  } | 
					
						
							|  |  |  | 		inline const_iterator end()   const { return factors_.end();  } | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/** Get a specific factor by index */ | 
					
						
							| 
									
										
										
										
											2009-11-23 13:27:19 +08:00
										 |  |  | 		inline sharedFactor operator[](size_t i) const {return factors_[i];} | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-21 05:21:01 +08:00
										 |  |  | 		/** delete factor without re-arranging indexes by inserting a NULL pointer */ | 
					
						
							|  |  |  | 		inline void remove(size_t i) { factors_[i].reset();} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  | 		/** return the number of factors and NULLS */ | 
					
						
							| 
									
										
										
										
											2009-12-21 05:21:01 +08:00
										 |  |  | 		inline size_t size() const { return factors_.size();} | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/** return the number valid factors */ | 
					
						
							| 
									
										
										
										
											2009-12-21 05:21:01 +08:00
										 |  |  | 		size_t nrFactors() const; | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/** Add a factor */ | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 		void push_back(sharedFactor factor); | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-19 14:34:07 +08:00
										 |  |  | 		/** push back many factors */ | 
					
						
							|  |  |  | 		void push_back(const FactorGraph<Factor>& factors); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-18 08:13:38 +08:00
										 |  |  | 		/** replace a factor by index */ | 
					
						
							|  |  |  | 		void replace(int index, sharedFactor factor); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-12 12:56:30 +08:00
										 |  |  | 		/** return keys in some random order */ | 
					
						
							|  |  |  | 		Ordering keys() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 13:14:34 +08:00
										 |  |  | 		/** Check whether a factor with this variable exists */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		bool involves(const Symbol& key) const { | 
					
						
							| 
									
										
										
										
											2009-11-20 13:14:34 +08:00
										 |  |  | 			return !(indices_.find(key)==indices_.end()); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-13 12:13:03 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Compute colamd ordering | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		Ordering getOrdering() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-21 05:21:01 +08:00
										 |  |  | 	  /**
 | 
					
						
							|  |  |  | 	   * shared pointer versions for MATLAB | 
					
						
							|  |  |  | 	   */ | 
					
						
							| 
									
										
										
										
											2009-12-12 06:53:48 +08:00
										 |  |  | 		boost::shared_ptr<Ordering>  getOrdering_() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 12:49:13 +08:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Return indices for all factors that involve the given node | 
					
						
							|  |  |  |      * @param key the key for the given node | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |     std::list<int> factors(const Symbol& key) const; | 
					
						
							| 
									
										
										
										
											2009-10-29 12:49:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * find all the factors that involve the given node and remove them | 
					
						
							|  |  |  |      * from the factor graph | 
					
						
							|  |  |  |      * @param key the key for the given node | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		std::vector<sharedFactor> findAndRemoveFactors(const Symbol& key); | 
					
						
							| 
									
										
										
										
											2009-10-29 13:39:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 19:15:37 +08:00
										 |  |  | 		/**
 | 
					
						
							| 
									
										
										
										
											2010-01-07 14:25:54 +08:00
										 |  |  | 		 * find the minimum spanning tree. | 
					
						
							| 
									
										
										
										
											2010-01-06 19:15:37 +08:00
										 |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 		template<class Key, class Factor2> PredecessorMap<Key> findMinimumSpanningTree() const; | 
					
						
							| 
									
										
										
										
											2010-01-06 19:15:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-09 03:07:46 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Split the graph into two parts: one corresponds to the given spanning tre, | 
					
						
							|  |  |  | 		 * and the other corresponds to the rest of the factors | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2010-01-16 09:16:59 +08:00
										 |  |  | 		template<class Key, class Factor2> void split(const PredecessorMap<Key>& tree, | 
					
						
							| 
									
										
										
										
											2010-01-14 07:59:46 +08:00
										 |  |  | 				FactorGraph<Factor>& Ab1, FactorGraph<Factor>& Ab2) const; | 
					
						
							| 
									
										
										
										
											2010-01-09 03:07:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-13 09:29:19 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Check consistency of the index map, useful for debugging | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		void checkGraphConsistency() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-31 12:13:57 +08:00
										 |  |  | 	private: | 
					
						
							| 
									
										
										
										
											2009-12-18 08:13:38 +08:00
										 |  |  | 		/** Associate factor index with the variables connected to the factor */ | 
					
						
							|  |  |  | 		void associateFactor(int index, sharedFactor factor); | 
					
						
							| 
									
										
										
										
											2009-08-31 12:13:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/** Serialization function */ | 
					
						
							|  |  |  | 		friend class boost::serialization::access; | 
					
						
							|  |  |  | 		template<class Archive> | 
					
						
							|  |  |  | 		void serialize(Archive & ar, const unsigned int version) { | 
					
						
							|  |  |  | 			ar & BOOST_SERIALIZATION_NVP(factors_); | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  | 			ar & BOOST_SERIALIZATION_NVP(indices_); | 
					
						
							| 
									
										
										
										
											2009-08-31 12:13:57 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}; // FactorGraph
 | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-11 13:12:45 +08:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Extract and combine all the factors that involve a given node | 
					
						
							|  |  |  |    * Put this here as not all Factors have a combine constructor | 
					
						
							|  |  |  |    * @param key the key for the given node | 
					
						
							|  |  |  |    * @return the combined linear factor | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 	template<class Factor> boost::shared_ptr<Factor> | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		removeAndCombineFactors(FactorGraph<Factor>& factorGraph, const Symbol& key); | 
					
						
							| 
									
										
										
										
											2009-11-11 13:12:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 	/**
 | 
					
						
							| 
									
										
										
										
											2009-11-08 10:50:19 +08:00
										 |  |  |    * static function that combines two factor graphs | 
					
						
							|  |  |  |    * @param const &fg1 Linear factor graph | 
					
						
							|  |  |  |    * @param const &fg2 Linear factor graph | 
					
						
							|  |  |  |    * @return a new combined factor graph | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  | 	template<class Factor> | 
					
						
							|  |  |  | 	FactorGraph<Factor> combine(const FactorGraph<Factor>& fg1, const FactorGraph<Factor>& fg2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } // namespace gtsam
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 
 |