| 
									
										
										
										
											2009-11-12 12:52:40 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    inference.h | 
					
						
							|  |  |  |  * @brief   Contains *generic* inference algorithms that convert between templated | 
					
						
							|  |  |  |  * graphical models, i.e., factor graphs, Bayes nets, and Bayes trees | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |  * @author  Frank Dellaert, Richard Roberts | 
					
						
							| 
									
										
										
										
											2009-11-12 12:52:40 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/base/types.h>
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/inference/FactorGraph.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/BayesNet.h>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/inference/VariableIndex.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/Permutation.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | #include <deque>
 | 
					
						
							| 
									
										
										
										
											2009-11-12 12:52:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | class Factor; | 
					
						
							|  |  |  | class Conditional; | 
					
						
							| 
									
										
										
										
											2009-11-12 12:52:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	class Inference { | 
					
						
							|  |  |  | 	private: | 
					
						
							|  |  |  | 	  /* Static members only, private constructor */ | 
					
						
							|  |  |  | 	  Inference() {} | 
					
						
							| 
									
										
										
										
											2009-11-12 12:52:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2009-11-12 12:52:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	  /**
 | 
					
						
							|  |  |  | 	   * Eliminate a factor graph in its natural ordering, i.e. eliminating | 
					
						
							|  |  |  | 	   * variables in order starting from 0. | 
					
						
							|  |  |  | 	   */ | 
					
						
							|  |  |  | 	  template<class FactorGraph> | 
					
						
							|  |  |  | 	  static typename FactorGraph::bayesnet_type::shared_ptr Eliminate(const FactorGraph& factorGraph); | 
					
						
							| 
									
										
										
										
											2009-11-12 12:52:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Eliminate a factor graph in its natural ordering, i.e. eliminating | 
					
						
							|  |  |  |      * variables in order starting from 0.  Special fast version for symbolic | 
					
						
							|  |  |  |      * elimination. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 	  template<class Factor> | 
					
						
							|  |  |  |     static BayesNet<Conditional>::shared_ptr EliminateSymbolic(const FactorGraph<Factor>& factorGraph); | 
					
						
							| 
									
										
										
										
											2009-11-12 12:52:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	  /**
 | 
					
						
							|  |  |  | 	   * Eliminate a factor graph in its natural ordering, i.e. eliminating | 
					
						
							|  |  |  | 	   * variables in order starting from 0.  Uses an existing | 
					
						
							|  |  |  | 	   * variable index instead of building one from scratch. | 
					
						
							|  |  |  | 	   */ | 
					
						
							|  |  |  | 	  template<class FactorGraph> | 
					
						
							|  |  |  | 	  static typename FactorGraph::bayesnet_type::shared_ptr Eliminate( | 
					
						
							|  |  |  | 	      FactorGraph& factorGraph, typename FactorGraph::variableindex_type& variableIndex); | 
					
						
							| 
									
										
										
										
											2009-11-12 12:52:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 	  /**
 | 
					
						
							|  |  |  | 	   * Partially eliminate a factor graph, up to but not including the given | 
					
						
							|  |  |  | 	   * variable. | 
					
						
							|  |  |  | 	   */ | 
					
						
							|  |  |  |     template<class FactorGraph> | 
					
						
							|  |  |  |     static typename FactorGraph::bayesnet_type::shared_ptr | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     EliminateUntil(const FactorGraph& factorGraph, Index bound); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Partially eliminate a factor graph, up to but not including the given | 
					
						
							|  |  |  |      * variable.  Use an existing variable index instead of building one from | 
					
						
							|  |  |  |      * scratch. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     template<class FactorGraph> | 
					
						
							|  |  |  |     static typename FactorGraph::bayesnet_type::shared_ptr | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     EliminateUntil(FactorGraph& factorGraph, Index bound, typename FactorGraph::variableindex_type& variableIndex); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	  /**
 | 
					
						
							|  |  |  | 	   * Eliminate a single variable, updating an existing factor graph and | 
					
						
							|  |  |  | 	   * variable index. | 
					
						
							|  |  |  | 	   */ | 
					
						
							|  |  |  |     template<class FactorGraph> | 
					
						
							|  |  |  |     static typename FactorGraph::bayesnet_type::sharedConditional | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     EliminateOne(FactorGraph& factorGraph, typename FactorGraph::variableindex_type& variableIndex, Index var); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Eliminate a single variable, updating an existing factor graph and | 
					
						
							|  |  |  |      * variable index.  This is a specialized faster version for purely | 
					
						
							|  |  |  |      * symbolic factor graphs. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     static boost::shared_ptr<Conditional> | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     EliminateOneSymbolic(FactorGraph<Factor>& factorGraph, VariableIndex<>& variableIndex, Index var); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-10 08:51:57 +08:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Eliminate all variables except the specified ones.  Internally this | 
					
						
							|  |  |  |      * permutes these variables to the end of the ordering, eliminates all | 
					
						
							|  |  |  |      * other variables, and then undoes the permutation.  This is | 
					
						
							|  |  |  |      * inefficient if multiple marginals are needed - in that case use the | 
					
						
							|  |  |  |      * BayesTree which supports efficiently computing marginals for multiple | 
					
						
							|  |  |  |      * variables. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     template<class FactorGraph, class VarContainer> | 
					
						
							| 
									
										
										
										
											2010-10-14 04:41:26 +08:00
										 |  |  |     static FactorGraph Marginal(const FactorGraph& factorGraph, const VarContainer& variables); | 
					
						
							| 
									
										
										
										
											2010-10-10 08:51:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Compute a permutation (variable ordering) using colamd | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     template<class VariableIndexType> | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     static boost::shared_ptr<Permutation> PermutationCOLAMD(const VariableIndexType& variableIndex) { return PermutationCOLAMD(variableIndex, std::vector<Index>()); } | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     template<class VariableIndexType, typename ConstraintContainer> | 
					
						
							|  |  |  |     static boost::shared_ptr<Permutation> PermutationCOLAMD(const VariableIndexType& variableIndex, const ConstraintContainer& constrainLast); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //    /**
 | 
					
						
							|  |  |  | //     * Join several factors into one.  This involves determining the set of
 | 
					
						
							|  |  |  | //     * shared variables and the correct variable positions in the new joint
 | 
					
						
							|  |  |  | //     * factor.
 | 
					
						
							|  |  |  | //     */
 | 
					
						
							|  |  |  | //    template<class FactorGraph, typename InputIterator>
 | 
					
						
							|  |  |  | //    static typename FactorGraph::shared_factor Combine(const FactorGraph& factorGraph,
 | 
					
						
							|  |  |  | //        InputIterator indicesBegin, InputIterator indicesEnd);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// ELIMINATE: FACTOR GRAPH -> BAYES NET
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //	/**
 | 
					
						
							|  |  |  | //   * Eliminate a single node yielding a Conditional
 | 
					
						
							|  |  |  | //   * Eliminates the factors from the factor graph through findAndRemoveFactors
 | 
					
						
							|  |  |  | //   * and adds a new factor on the separator to the factor graph
 | 
					
						
							|  |  |  | //   */
 | 
					
						
							|  |  |  | //	template<class Factor, class Conditional>
 | 
					
						
							|  |  |  | //	boost::shared_ptr<Conditional>
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | //	eliminateOne(FactorGraph<Factor>& factorGraph, Index key);
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //	/**
 | 
					
						
							|  |  |  | //	 * eliminate factor graph using the given (not necessarily complete)
 | 
					
						
							|  |  |  | //	 * ordering, yielding a chordal Bayes net and (partially eliminated) FG
 | 
					
						
							|  |  |  | //	 */
 | 
					
						
							|  |  |  | //	template<class Factor, class Conditional>
 | 
					
						
							|  |  |  | //	BayesNet<Conditional> eliminate(FactorGraph<Factor>& factorGraph, const Ordering& ordering);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// FACTOR/MARGINALIZE: BAYES NET -> FACTOR GRAPH
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //	/**
 | 
					
						
							|  |  |  | //	 * Factor P(X) as P(not keys|keys) P(keys)
 | 
					
						
							|  |  |  | //	 * @return P(not keys|keys) as an incomplete BayesNet, and P(keys) as a factor graph
 | 
					
						
							|  |  |  | //	 */
 | 
					
						
							|  |  |  | //	template<class Factor, class Conditional>
 | 
					
						
							|  |  |  | //	std::pair< BayesNet<Conditional>, FactorGraph<Factor> >
 | 
					
						
							|  |  |  | //	factor(const BayesNet<Conditional>& bn, const Ordering& keys);
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //	/**
 | 
					
						
							|  |  |  | //	 * integrate out all except ordering, might be inefficient as the ordering
 | 
					
						
							|  |  |  | //	 * will simply be the current ordering with the keys put in the back
 | 
					
						
							|  |  |  | //	 */
 | 
					
						
							|  |  |  | //	template<class Factor, class Conditional>
 | 
					
						
							|  |  |  | //	FactorGraph<Factor> marginalize(const BayesNet<Conditional>& bn, const Ordering& keys);
 | 
					
						
							| 
									
										
										
										
											2009-11-12 12:52:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-05 06:03:40 +08:00
										 |  |  | 	/**
 | 
					
						
							|  |  |  | 	 * Hacked-together function to compute a Gaussian marginal for the given variable. | 
					
						
							|  |  |  | 	 * todo: This is inefficient! | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2010-03-05 06:40:01 +08:00
										 |  |  | 	//std::pair<Vector,Matrix> marginalGaussian(const GaussianFactorGraph& fg, const Symbol& key);
 | 
					
						
							| 
									
										
										
										
											2010-03-05 06:03:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-12 12:52:40 +08:00
										 |  |  | } /// namespace gtsam
 |