| 
									
										
										
										
											2010-10-14 12:54:38 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation,  | 
					
						
							|  |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    FactorGraph.h | 
					
						
							|  |  |  |  * @brief   Factor Graph Base Class | 
					
						
							|  |  |  |  * @author  Carlos Nieto | 
					
						
							|  |  |  |  * @author  Christian Potthast | 
					
						
							| 
									
										
										
										
											2010-07-10 02:15:54 +08:00
										 |  |  |  * @author  Michael Kaess | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <boost/foreach.hpp>
 | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  | #include <boost/serialization/nvp.hpp>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <boost/pool/pool_alloc.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/base/types.h>
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/base/Testable.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/BayesNet.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/graph.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
										 |  |  | 
 | 
					
						
							|  |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * 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
										 |  |  | 	 *  | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 	 * Templated on the type of factors and values structure. | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  | 	template<class FACTOR> | 
					
						
							|  |  |  | 	class FactorGraph: public Testable<FactorGraph<FACTOR> > { | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  | 	  typedef FACTOR Factor; | 
					
						
							|  |  |  | 	  typedef boost::shared_ptr<FactorGraph<FACTOR> > shared_ptr; | 
					
						
							|  |  |  | 		typedef typename boost::shared_ptr<FACTOR> sharedFactor; | 
					
						
							| 
									
										
										
										
											2009-11-08 03:31:39 +08:00
										 |  |  | 		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
										 |  |  | 
 | 
					
						
							|  |  |  | 	public: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-14 21:55:32 +08:00
										 |  |  | 		/** ------------------ Creating Factor Graphs ---------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 12:56:59 +08:00
										 |  |  | 		/** Default constructor */ | 
					
						
							|  |  |  | 		FactorGraph() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** convert from Bayes net */ | 
					
						
							| 
									
										
										
										
											2010-10-20 09:10:27 +08:00
										 |  |  | 		template<class CONDITIONAL> | 
					
						
							|  |  |  | 		FactorGraph(const BayesNet<CONDITIONAL>& bayesNet); | 
					
						
							| 
									
										
										
										
											2009-11-05 12:56:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		/** convert from a derived type */ | 
					
						
							| 
									
										
										
										
											2010-10-20 09:10:27 +08:00
										 |  |  | 		template<class DERIVEDFACTOR> | 
					
						
							|  |  |  | 		FactorGraph(const FactorGraph<DERIVEDFACTOR>& factorGraph); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-14 21:55:32 +08:00
										 |  |  | 		/** Add a factor */ | 
					
						
							| 
									
										
										
										
											2010-10-20 09:10:27 +08:00
										 |  |  | 		template<class DERIVEDFACTOR> | 
					
						
							|  |  |  | 		void push_back(const boost::shared_ptr<DERIVEDFACTOR>& factor); | 
					
						
							| 
									
										
										
										
											2010-07-14 21:55:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/** push back many factors */ | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  | 		void push_back(const FactorGraph<FACTOR>& factors); | 
					
						
							| 
									
										
										
										
											2010-07-14 21:55:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  | 		/** push back many factors with an iterator */ | 
					
						
							| 
									
										
										
										
											2010-10-20 09:10:27 +08:00
										 |  |  | 		template<typename ITERATOR> | 
					
						
							|  |  |  | 		void push_back(ITERATOR firstFactor, ITERATOR lastFactor); | 
					
						
							| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-14 21:55:32 +08:00
										 |  |  | 		/** ------------------ Querying Factor Graphs ---------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		/** const cast to the underlying vector of factors */ | 
					
						
							|  |  |  | 		operator const std::vector<sharedFactor>&() const { return factors_; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 13:27:19 +08:00
										 |  |  | 		/** STL begin and end, so we can use BOOST_FOREACH */ | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  | 		const_iterator begin() const { return factors_.begin();} | 
					
						
							|  |  |  | 		const_iterator end()   const { return factors_.end();  } | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/** Get a specific factor by index */ | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  | 		sharedFactor operator[](size_t i) const { assert(i<factors_.size()); return factors_[i]; } | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** Get the first factor */ | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  |     sharedFactor front() const { return factors_.front(); } | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/** Get the last factor */ | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  | 		sharedFactor back() const { return factors_.back(); } | 
					
						
							| 
									
										
										
										
											2009-08-31 10:40:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  | 		/** return the number of factors and NULLS */ | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  | 		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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-14 21:55:32 +08:00
										 |  |  | 		/** ----------------- Modifying Factor Graphs ---------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** STL begin and end, so we can use BOOST_FOREACH */ | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  | 		iterator begin()       { return factors_.begin();} | 
					
						
							|  |  |  | 		iterator end()         { return factors_.end();  } | 
					
						
							| 
									
										
										
										
											2010-07-14 21:55:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Reserve space for the specified number of factors if you know in | 
					
						
							|  |  |  | 		 * advance how many there will be (works like std::vector::reserve). | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		void reserve(size_t size) { factors_.reserve(size); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-14 21:55:32 +08:00
										 |  |  | 		/** delete factor without re-arranging indexes by inserting a NULL pointer */ | 
					
						
							|  |  |  | 		inline void remove(size_t i) { factors_[i].reset();} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** replace a factor by index */ | 
					
						
							|  |  |  | 		void replace(size_t index, sharedFactor factor); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-31 12:13:57 +08:00
										 |  |  | 	private: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** Serialization function */ | 
					
						
							|  |  |  | 		friend class boost::serialization::access; | 
					
						
							| 
									
										
										
										
											2010-10-20 09:10:27 +08:00
										 |  |  | 		template<class ARCHIVE> | 
					
						
							|  |  |  | 		void serialize(ARCHIVE & ar, const unsigned int version) { | 
					
						
							| 
									
										
										
										
											2009-08-31 12:13:57 +08:00
										 |  |  | 			ar & BOOST_SERIALIZATION_NVP(factors_); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; // FactorGraph
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2010-10-20 09:10:27 +08:00
										 |  |  | 	template<class FACTORGRAPH> | 
					
						
							|  |  |  | 	FACTORGRAPH combine(const FACTORGRAPH& fg1, const FACTORGRAPH& fg2); | 
					
						
							| 
									
										
										
										
											2009-11-08 10:50:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * These functions are defined here because they are templated on an | 
					
						
							|  |  |  | 	 * additional parameter.  Putting them in the -inl.h file would mean these | 
					
						
							|  |  |  | 	 * would have to be explicitly instantiated for any possible derived factor | 
					
						
							|  |  |  | 	 * type. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  |   template<class FACTOR> | 
					
						
							| 
									
										
										
										
											2010-10-20 09:10:27 +08:00
										 |  |  |   template<class DERIVEDFACTOR> | 
					
						
							|  |  |  |   FactorGraph<FACTOR>::FactorGraph(const FactorGraph<DERIVEDFACTOR>& factorGraph) { | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     factors_.reserve(factorGraph.size()); | 
					
						
							| 
									
										
										
										
											2010-10-20 09:10:27 +08:00
										 |  |  |     BOOST_FOREACH(const typename DERIVEDFACTOR::shared_ptr& factor, factorGraph) { | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  |       if(factor) | 
					
						
							| 
									
										
										
										
											2010-10-22 06:59:54 +08:00
										 |  |  |         this->push_back(factor); | 
					
						
							| 
									
										
										
										
											2010-10-10 11:10:03 +08:00
										 |  |  |       else | 
					
						
							|  |  |  |         this->push_back(sharedFactor()); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-14 04:43:58 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  |   template<class FACTOR> | 
					
						
							| 
									
										
										
										
											2010-10-20 09:10:27 +08:00
										 |  |  |   template<class CONDITIONAL> | 
					
						
							|  |  |  |   FactorGraph<FACTOR>::FactorGraph(const BayesNet<CONDITIONAL>& bayesNet) { | 
					
						
							| 
									
										
										
										
											2010-10-14 04:43:58 +08:00
										 |  |  |     factors_.reserve(bayesNet.size()); | 
					
						
							| 
									
										
										
										
											2010-10-20 09:10:27 +08:00
										 |  |  |     BOOST_FOREACH(const typename CONDITIONAL::shared_ptr& cond, bayesNet) { | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  |       this->push_back(sharedFactor(new FACTOR(*cond))); | 
					
						
							| 
									
										
										
										
											2010-10-14 04:43:58 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  |   template<class FACTOR> | 
					
						
							| 
									
										
										
										
											2010-10-20 09:10:27 +08:00
										 |  |  |   template<class DERIVEDFACTOR> | 
					
						
							|  |  |  |   inline void FactorGraph<FACTOR>::push_back(const boost::shared_ptr<DERIVEDFACTOR>& factor) { | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #ifndef NDEBUG
 | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  |     factors_.push_back(boost::dynamic_pointer_cast<FACTOR>(factor)); // add the actual factor
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  |     factors_.push_back(boost::static_pointer_cast<FACTOR>(factor)); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #endif
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-10-18 01:10:10 +08:00
										 |  |  |   template<class FACTOR> | 
					
						
							| 
									
										
										
										
											2010-10-20 09:10:27 +08:00
										 |  |  |   template<typename ITERATOR> | 
					
						
							|  |  |  |   void FactorGraph<FACTOR>::push_back(ITERATOR firstFactor, ITERATOR lastFactor) { | 
					
						
							|  |  |  |     ITERATOR factor = firstFactor; | 
					
						
							| 
									
										
										
										
											2010-10-15 23:53:36 +08:00
										 |  |  |     while(factor != lastFactor) | 
					
						
							|  |  |  |       this->push_back(*(factor++)); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-07-14 21:55:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } // namespace gtsam
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  | 
 |