| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |  * @file    GaussianFactorGraph.h | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  * @brief   Linear Factor Graph where all factors are Gaussians | 
					
						
							|  |  |  |  * @author  Kai Ni | 
					
						
							|  |  |  |  * @author  Christian Potthast | 
					
						
							|  |  |  |  * @author  Alireza Fathi | 
					
						
							|  |  |  |  */  | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | // $Id: GaussianFactorGraph.h,v 1.24 2009/08/14 20:48:51 acunning Exp $
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							|  |  |  | #include "FactorGraph.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  | #include "GaussianFactor.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-05 11:51:08 +08:00
										 |  |  | #include "GaussianBayesNet.h" // needed for MATLAB toolbox !!
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	class Ordering; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * A Linear Factor Graph is a factor graph where all factors are Gaussian, i.e. | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |    *   Factor == GaussianFactor | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |    *   VectorConfig = A configuration of vectors | 
					
						
							| 
									
										
										
										
											2009-10-07 02:25:04 +08:00
										 |  |  |    * Most of the time, linear factor graphs arise by linearizing a non-linear factor graph. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |   class GaussianFactorGraph : public FactorGraph<GaussianFactor> { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Default constructor  | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |     GaussianFactorGraph() {} | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |      * Constructor that receives a Chordal Bayes Net and returns a GaussianFactorGraph | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |     GaussianFactorGraph(const GaussianBayesNet& CBN); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  | 		/** unnormalized error */ | 
					
						
							|  |  |  | 		double error(const VectorConfig& c) const { | 
					
						
							|  |  |  | 			double total_error = 0.; | 
					
						
							|  |  |  | 			// iterate over all the factors_ to accumulate the log probabilities
 | 
					
						
							|  |  |  | 			for (const_iterator factor = factors_.begin(); factor != factors_.end(); factor++) | 
					
						
							|  |  |  | 				total_error += (*factor)->error(c); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			return total_error; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** Unnormalized probability. O(n) */ | 
					
						
							|  |  |  | 		double probPrime(const VectorConfig& c) const { | 
					
						
							|  |  |  | 			return exp(-0.5 * error(c)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * find the separator, i.e. all the nodes that have at least one | 
					
						
							|  |  |  |      * common factor with the given node. FD: not used AFAIK. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     std::set<std::string> find_separator(const std::string& key) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 05:03:30 +08:00
										 |  |  |   	/**
 | 
					
						
							|  |  |  |      * Eliminate a single node yielding a conditional Gaussian | 
					
						
							|  |  |  |      * Eliminates the factors from the factor graph through findAndRemoveFactors | 
					
						
							|  |  |  |      * and adds a new factor on the separator to the factor graph | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |     GaussianConditional::shared_ptr eliminateOne(const std::string& key); | 
					
						
							| 
									
										
										
										
											2009-11-08 05:03:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * eliminate factor graph in place(!) in the given order, yielding | 
					
						
							| 
									
										
										
										
											2009-11-05 12:56:59 +08:00
										 |  |  |      * a chordal Bayes net. Allows for passing an incomplete ordering | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |      * that does not completely eliminate the graph | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-09 15:04:26 +08:00
										 |  |  |     GaussianBayesNet eliminate(const Ordering& ordering); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 		 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * optimize a linear factor graph | 
					
						
							|  |  |  |      * @param ordering fg in order | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |     VectorConfig optimize(const Ordering& ordering); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-11 15:14:13 +08:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * shared pointer versions for MATLAB | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     boost::shared_ptr<GaussianBayesNet> eliminate_(const Ordering&); | 
					
						
							|  |  |  |     boost::shared_ptr<VectorConfig> optimize_(const Ordering&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     /**
 | 
					
						
							| 
									
										
										
										
											2009-08-25 10:36:30 +08:00
										 |  |  |      * static function that combines two factor graphs | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |      * @param const &lfg1 Linear factor graph | 
					
						
							|  |  |  |      * @param const &lfg2 Linear factor graph | 
					
						
							|  |  |  |      * @return a new combined factor graph | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |     static GaussianFactorGraph combine2(const GaussianFactorGraph& lfg1, | 
					
						
							|  |  |  | 				const GaussianFactorGraph& lfg2); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 		 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * combine two factor graphs | 
					
						
							|  |  |  |      * @param *lfg Linear factor graph | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |     void combine(const GaussianFactorGraph &lfg); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Find all variables and their dimensions | 
					
						
							|  |  |  |      * @return The set of all variable/dimension pairs | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  |     Dimensions dimensions() const; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Add zero-mean i.i.d. Gaussian prior terms to each variable | 
					
						
							|  |  |  |      * @param sigma Standard deviation of Gaussian | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:16:32 +08:00
										 |  |  |     GaussianFactorGraph add_priors(double sigma) const; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Return (dense) matrix associated with factor graph | 
					
						
							|  |  |  |      * @param ordering of variables needed for matrix column order | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     std::pair<Matrix,Vector> matrix (const Ordering& ordering) const; | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   	/**
 | 
					
						
							|  |  |  |   	 * Return 3*nzmax matrix where the rows correspond to the vectors i, j, and s | 
					
						
							|  |  |  |   	 * to generate an m-by-n sparse matrix, which can be given to MATLAB's sparse function. | 
					
						
							|  |  |  |   	 * The standard deviations are baked into A and b | 
					
						
							|  |  |  |   	 * @param ordering of variables needed for matrix column order | 
					
						
							|  |  |  |   	 */ | 
					
						
							|  |  |  |   	Matrix sparse(const Ordering& ordering) const; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |