| 
									
										
										
										
											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
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |  */  | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/shared_ptr.hpp>
 | 
					
						
							| 
									
										
										
										
											2010-05-31 10:21:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/inference/FactorGraph.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/Errors.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianFactor.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianBayesNet.h>
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  |    *   VectorValues = A values structure 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: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     typedef boost::shared_ptr<GaussianFactorGraph> shared_ptr; | 
					
						
							|  |  |  |     typedef GaussianBayesNet bayesnet_type; | 
					
						
							|  |  |  |     typedef GaussianVariableIndex<> variableindex_type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * 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-12-10 23:33:52 +08:00
										 |  |  |   	/** Add a null factor */ | 
					
						
							|  |  |  |     inline void add(const Vector& b) { | 
					
						
							|  |  |  |     	push_back(sharedFactor(new GaussianFactor(b))); | 
					
						
							|  |  |  |   	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   	/** Add a unary factor */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     inline void add(Index key1, const Matrix& A1, | 
					
						
							| 
									
										
										
										
											2010-01-23 01:36:57 +08:00
										 |  |  |   			const Vector& b, const SharedDiagonal& model) { | 
					
						
							| 
									
										
										
										
											2010-01-21 02:32:48 +08:00
										 |  |  |     	push_back(sharedFactor(new GaussianFactor(key1,A1,b,model))); | 
					
						
							| 
									
										
										
										
											2009-12-10 23:33:52 +08:00
										 |  |  |   	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   	/** Add a binary factor */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     inline void add(Index key1, const Matrix& A1, | 
					
						
							|  |  |  |   			Index key2, const Matrix& A2, | 
					
						
							| 
									
										
										
										
											2010-01-23 01:36:57 +08:00
										 |  |  |   			const Vector& b, const SharedDiagonal& model) { | 
					
						
							| 
									
										
										
										
											2010-01-21 02:32:48 +08:00
										 |  |  |     	push_back(sharedFactor(new GaussianFactor(key1,A1,key2,A2,b,model))); | 
					
						
							| 
									
										
										
										
											2009-12-10 23:33:52 +08:00
										 |  |  |   	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   	/** Add a ternary factor */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     inline void add(Index key1, const Matrix& A1, | 
					
						
							|  |  |  |   			Index key2, const Matrix& A2, | 
					
						
							|  |  |  |   			Index key3, const Matrix& A3, | 
					
						
							| 
									
										
										
										
											2010-01-23 01:36:57 +08:00
										 |  |  |   			const Vector& b, const SharedDiagonal& model) { | 
					
						
							| 
									
										
										
										
											2010-01-21 02:32:48 +08:00
										 |  |  |     	push_back(sharedFactor(new GaussianFactor(key1,A1,key2,A2,key3,A3,b,model))); | 
					
						
							| 
									
										
										
										
											2009-12-10 23:33:52 +08:00
										 |  |  |   	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   	/** Add an n-ary factor */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     inline void add(const std::vector<std::pair<Index, Matrix> > &terms, | 
					
						
							| 
									
										
										
										
											2010-01-23 01:36:57 +08:00
										 |  |  |   	    const Vector &b, const SharedDiagonal& model) { | 
					
						
							| 
									
										
										
										
											2010-01-21 02:32:48 +08:00
										 |  |  |     	push_back(sharedFactor(new GaussianFactor(terms,b,model))); | 
					
						
							| 
									
										
										
										
											2009-12-10 23:33:52 +08:00
										 |  |  |   	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Return the set of variables involved in the factors (computes a set | 
					
						
							|  |  |  |      * union). | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     std::set<Index, std::less<Index>, boost::fast_pool_allocator<Index> > keys() const; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** Permute the variables in the factors */ | 
					
						
							|  |  |  |     void permuteWithInverse(const Permutation& inversePermutation); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  | 		/** return A*x-b */ | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 		Errors errors(const VectorValues& x) const; | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +08:00
										 |  |  | 		/** shared pointer version */ | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 		boost::shared_ptr<Errors> errors_(const VectorValues& x) const; | 
					
						
							| 
									
										
										
										
											2010-01-19 18:46:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  | 			/** unnormalized error */ | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 		double error(const VectorValues& x) const; | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-27 06:48:41 +08:00
										 |  |  | 		/** return A*x */ | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 		Errors operator*(const VectorValues& x) const; | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-31 01:31:05 +08:00
										 |  |  | 		/* In-place version e <- A*x that overwrites e. */ | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 		void multiplyInPlace(const VectorValues& x, Errors& e) const; | 
					
						
							| 
									
										
										
										
											2010-01-31 01:31:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/* In-place version e <- A*x that takes an iterator. */ | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 		void multiplyInPlace(const VectorValues& x, const Errors::iterator& e) const; | 
					
						
							| 
									
										
										
										
											2010-01-31 01:31:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //		/** return A^e */
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | //		VectorValues operator^(const Errors& e) const;
 | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-31 07:59:29 +08:00
										 |  |  | 		/** x += alpha*A'*e */ | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 		void transposeMultiplyAdd(double alpha, const Errors& e, VectorValues& x) const; | 
					
						
							| 
									
										
										
										
											2010-01-31 07:59:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-15 00:08:16 +08:00
										 |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * Calculate Gradient of A^(A*x-b) for a given config | 
					
						
							|  |  |  | 		 * @param x: VectorValues specifying where to calculate gradient | 
					
						
							|  |  |  | 		 * @return gradient, as a VectorValues as well | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		VectorValues gradient(const VectorValues& x) const; | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  | 		/** Unnormalized probability. O(n) */ | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | 		double probPrime(const VectorValues& c) const { | 
					
						
							| 
									
										
										
										
											2009-10-29 12:11:23 +08:00
										 |  |  | 			return exp(-0.5 * error(c)); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +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.
 | 
					
						
							|  |  |  | //     */
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | //    std::set<Index> find_separator(Index key) const;
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | //    /**
 | 
					
						
							|  |  |  | //     * Peforms a supposedly-faster (fewer matrix copy) version of elimination
 | 
					
						
							|  |  |  | //     * CURRENTLY IN TESTING
 | 
					
						
							|  |  |  | //     */
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | //    GaussianConditional::shared_ptr eliminateOneMatrixJoin(Index key);
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //    /**
 | 
					
						
							|  |  |  | //     * Eliminate multiple variables at once, mostly used to eliminate frontal variables
 | 
					
						
							|  |  |  | //     */
 | 
					
						
							|  |  |  | //    GaussianBayesNet eliminateFrontals(const Ordering& frontals);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //    /**
 | 
					
						
							|  |  |  | //     * optimize a linear factor graph
 | 
					
						
							|  |  |  | //     * @param ordering fg in order
 | 
					
						
							|  |  |  | //     * @param enableJoinFactor uses the older joint factor combine process when true,
 | 
					
						
							|  |  |  | //     *    and when false uses the newer single matrix combine
 | 
					
						
							|  |  |  | //     */
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | //    VectorValues optimize(const Ordering& ordering, bool enableJoinFactor = true);
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | //    /**
 | 
					
						
							|  |  |  | //     * optimize a linear factor graph using a multi-frontal solver
 | 
					
						
							|  |  |  | //     * @param ordering fg in order
 | 
					
						
							|  |  |  | //     */
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | //    VectorValues optimizeMultiFrontals(const Ordering& ordering);
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | //    /**
 | 
					
						
							|  |  |  | //     * shared pointer versions for MATLAB
 | 
					
						
							|  |  |  | //     */
 | 
					
						
							|  |  |  | //    boost::shared_ptr<GaussianBayesNet> eliminate_(const Ordering&);
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | //    boost::shared_ptr<VectorValues> optimize_(const Ordering&);
 | 
					
						
							| 
									
										
										
										
											2009-11-11 15:14:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //    /**
 | 
					
						
							|  |  |  | //     * Find all variables and their dimensions
 | 
					
						
							|  |  |  | //     * @return The set of all variable/dimension pairs
 | 
					
						
							|  |  |  | //     */
 | 
					
						
							|  |  |  | //    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 | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     GaussianFactorGraph add_priors(double sigma, const GaussianVariableIndex<>& variableIndex) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //    /**
 | 
					
						
							|  |  |  | //     * Return RHS (b./sigmas) as Errors class
 | 
					
						
							|  |  |  | //     */
 | 
					
						
							|  |  |  | //    Errors rhs() const;
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //    /**
 | 
					
						
							|  |  |  | //     * Return RHS (b./sigmas) as Vector
 | 
					
						
							|  |  |  | //     */
 | 
					
						
							|  |  |  | //    Vector rhsVector() const;
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //    /**
 | 
					
						
							|  |  |  | //     * 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;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //    /**
 | 
					
						
							|  |  |  | //     * split the source vector w.r.t. the given ordering and assemble a vector config
 | 
					
						
							|  |  |  | //     * @param v: the source vector
 | 
					
						
							|  |  |  | //     * @param ordeirng: the ordering corresponding to the vector
 | 
					
						
							|  |  |  | //     */
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | //    VectorValues assembleValues(const Vector& v, const Ordering& ordering) const;
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //    /**
 | 
					
						
							|  |  |  | //     * get the 1-based starting column indices for all variables
 | 
					
						
							|  |  |  | //     * @param ordering of variables needed for matrix column order
 | 
					
						
							|  |  |  | //     * @return The set of all variable/index pairs
 | 
					
						
							|  |  |  | //     */
 | 
					
						
							|  |  |  | //    std::pair<Dimensions, size_t> columnIndices_(const Ordering& ordering) const;
 | 
					
						
							|  |  |  | //    Dimensions columnIndices(const Ordering& ordering) const;
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //    /**
 | 
					
						
							|  |  |  | //     * return the size of corresponding A matrix
 | 
					
						
							|  |  |  | //     */
 | 
					
						
							|  |  |  | //    std::pair<std::size_t, std::size_t> sizeOfA() const;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  	/**
 | 
					
						
							|  |  |  | //  	 * 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;
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  	/**
 | 
					
						
							|  |  |  | //  	 * Version that takes column indices rather than ordering
 | 
					
						
							|  |  |  | //  	 */
 | 
					
						
							|  |  |  | //  	Matrix sparse(const Dimensions& indices) const;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  	/**
 | 
					
						
							|  |  |  | //		 * Find solution using gradient descent
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | //		 * @param x0: VectorValues specifying initial estimate
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //		 * @return solution
 | 
					
						
							|  |  |  | //		 */
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | //		VectorValues steepestDescent(const VectorValues& x0, bool verbose = false,
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //				double epsilon = 1e-3, size_t maxIterations = 0) const;
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //		/**
 | 
					
						
							|  |  |  | //		 * shared pointer versions for MATLAB
 | 
					
						
							|  |  |  | //		 */
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | //		boost::shared_ptr<VectorValues>
 | 
					
						
							|  |  |  | //		steepestDescent_(const VectorValues& x0, bool verbose = false,
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //				double epsilon = 1e-3, size_t maxIterations = 0) const;
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //		/**
 | 
					
						
							|  |  |  | //		 * Find solution using conjugate gradient descent
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | //		 * @param x0: VectorValues specifying initial estimate
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //		 * @return solution
 | 
					
						
							|  |  |  | //		 */
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | //		VectorValues conjugateGradientDescent(const VectorValues& x0, bool verbose =
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //				false, double epsilon = 1e-3, size_t maxIterations = 0) const;
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //		/**
 | 
					
						
							|  |  |  | //		 * shared pointer versions for MATLAB
 | 
					
						
							|  |  |  | //		 */
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  | //		boost::shared_ptr<VectorValues> conjugateGradientDescent_(
 | 
					
						
							|  |  |  | //				const VectorValues& x0, bool verbose = false, double epsilon = 1e-3,
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //				size_t maxIterations = 0) const;
 | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-29 02:46:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   template<class VariableIndexStorage> | 
					
						
							|  |  |  |   class GaussianVariableIndex : public VariableIndex<VariableIndexStorage> { | 
					
						
							|  |  |  |   public: | 
					
						
							|  |  |  |     typedef VariableIndex<VariableIndexStorage> Base; | 
					
						
							|  |  |  |     typedef typename VariableIndexStorage::template type_factory<size_t>::type storage_type; | 
					
						
							| 
									
										
										
										
											2010-01-23 08:57:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     storage_type dims_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public: | 
					
						
							|  |  |  |     typedef boost::shared_ptr<GaussianVariableIndex> shared_ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** Construct an empty GaussianVariableIndex */ | 
					
						
							|  |  |  |     GaussianVariableIndex() {} | 
					
						
							| 
									
										
										
										
											2009-11-06 13:43:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-23 11:49:05 +08:00
										 |  |  |     /**
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |      * Constructor from a GaussianFactorGraph, lets the base class build the | 
					
						
							|  |  |  |      * column-wise index then fills the dims_ array. | 
					
						
							| 
									
										
										
										
											2010-01-23 11:49:05 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     GaussianVariableIndex(const GaussianFactorGraph& factorGraph); | 
					
						
							| 
									
										
										
										
											2010-01-23 11:49:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 14:18:29 +08:00
										 |  |  |     /**
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |      * Constructor to "upgrade" from the base class without recomputing the | 
					
						
							|  |  |  |      * column index, i.e. just fills the dims_ array. | 
					
						
							| 
									
										
										
										
											2009-12-12 14:18:29 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     GaussianVariableIndex(const VariableIndex<VariableIndexStorage>& variableIndex, const GaussianFactorGraph& factorGraph); | 
					
						
							| 
									
										
										
										
											2009-12-12 14:18:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-26 16:05:31 +08:00
										 |  |  |     /**
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |      * Another constructor to upgrade from the base class using an existing | 
					
						
							|  |  |  |      * array of variable dimensions. | 
					
						
							| 
									
										
										
										
											2010-05-26 16:05:31 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     GaussianVariableIndex(const VariableIndex<VariableIndexStorage>& variableIndex, const storage_type& dimensions); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const storage_type& dims() const { return dims_; } | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     size_t dim(Index variable) const { Base::checkVar(variable); return dims_[variable]; } | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** Permute */ | 
					
						
							|  |  |  |     void permute(const Permutation& permutation); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** Augment this variable index with the contents of another one */ | 
					
						
							|  |  |  |     void augment(const GaussianFactorGraph& factorGraph); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   protected: | 
					
						
							|  |  |  |     GaussianVariableIndex(size_t nVars) : Base(nVars), dims_(nVars) {} | 
					
						
							|  |  |  |     void fillDims(const GaussianFactorGraph& factorGraph); | 
					
						
							| 
									
										
										
										
											2010-05-31 10:21:37 +08:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   template<class Storage> | 
					
						
							|  |  |  |   GaussianVariableIndex<Storage>::GaussianVariableIndex(const GaussianFactorGraph& factorGraph) : | 
					
						
							|  |  |  |   Base(factorGraph), dims_(Base::index_.size()) { | 
					
						
							|  |  |  |     fillDims(factorGraph); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   template<class Storage> | 
					
						
							|  |  |  |   GaussianVariableIndex<Storage>::GaussianVariableIndex( | 
					
						
							|  |  |  |       const VariableIndex<Storage>& variableIndex, const GaussianFactorGraph& factorGraph) : | 
					
						
							|  |  |  |       Base(variableIndex), dims_(Base::index_.size()) { | 
					
						
							|  |  |  |     fillDims(factorGraph); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   template<class Storage> | 
					
						
							|  |  |  |   GaussianVariableIndex<Storage>::GaussianVariableIndex( | 
					
						
							|  |  |  |       const VariableIndex<Storage>& variableIndex, const storage_type& dimensions) : | 
					
						
							|  |  |  |       Base(variableIndex), dims_(dimensions) { | 
					
						
							|  |  |  |     assert(Base::index_.size() == dims_.size()); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   template<class Storage> | 
					
						
							|  |  |  |   void GaussianVariableIndex<Storage>::fillDims(const GaussianFactorGraph& factorGraph) { | 
					
						
							|  |  |  |     // Store dimensions of each variable
 | 
					
						
							|  |  |  |     assert(dims_.size() == Base::index_.size()); | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     for(Index var=0; var<Base::index_.size(); ++var) | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |       if(!Base::index_[var].empty()) { | 
					
						
							|  |  |  |         size_t factorIndex = Base::operator [](var).front().factorIndex; | 
					
						
							|  |  |  |         size_t variablePosition = Base::operator [](var).front().variablePosition; | 
					
						
							|  |  |  |         boost::shared_ptr<const GaussianFactor> factor(factorGraph[factorIndex]); | 
					
						
							|  |  |  |         dims_[var] = factor->getDim(factor->begin() + variablePosition); | 
					
						
							|  |  |  |       } else | 
					
						
							|  |  |  |         dims_[var] = 0; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   template<class Storage> | 
					
						
							|  |  |  |   void GaussianVariableIndex<Storage>::permute(const Permutation& permutation) { | 
					
						
							|  |  |  |     VariableIndex<Storage>::permute(permutation); | 
					
						
							|  |  |  |     storage_type original(this->dims_.size()); | 
					
						
							|  |  |  |     this->dims_.swap(original); | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     for(Index j=0; j<permutation.size(); ++j) | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |       this->dims_[j] = original[permutation[j]]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* ************************************************************************* */ | 
					
						
							|  |  |  |   template<class Storage> | 
					
						
							|  |  |  |   void GaussianVariableIndex<Storage>::augment(const GaussianFactorGraph& factorGraph) { | 
					
						
							|  |  |  |     Base::augment(factorGraph); | 
					
						
							|  |  |  |     dims_.resize(Base::index_.size(), 0); | 
					
						
							|  |  |  |     BOOST_FOREACH(boost::shared_ptr<const GaussianFactor> factor, factorGraph) { | 
					
						
							|  |  |  |       for(GaussianFactor::const_iterator var=factor->begin(); var!=factor->end(); ++var) { | 
					
						
							|  |  |  | #ifndef NDEBUG
 | 
					
						
							|  |  |  |         if(dims_[*var] != 0) | 
					
						
							|  |  |  |           assert(dims_[*var] == factor->getDim(var)); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  |         if(dims_[*var] == 0) | 
					
						
							|  |  |  |           dims_[*var] = factor->getDim(var); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  | //    for(Index var=0; var<dims_.size(); ++var) {
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | //#ifndef NDEBUG
 | 
					
						
							|  |  |  | //      if(var >= varIndex.dims_.size() || varIndex.dims_[var] == 0)
 | 
					
						
							|  |  |  | //        assert(dims_[var] != 0);
 | 
					
						
							|  |  |  | //      else if(varIndex.dims_[var] != 0 && dims_[var] != 0)
 | 
					
						
							|  |  |  | //        assert(dims_[var] == varIndex.dims_[var]);
 | 
					
						
							|  |  |  | //#endif
 | 
					
						
							|  |  |  | //      if(dims_[var] == 0)
 | 
					
						
							|  |  |  | //        dims_[var] = varIndex.dims_[var];
 | 
					
						
							|  |  |  | //    }
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //	/**
 | 
					
						
							|  |  |  | //	 * Returns the augmented matrix version of a set of factors
 | 
					
						
							|  |  |  | //	 * with the corresponding noiseModel
 | 
					
						
							|  |  |  | //	 * @param factors is the set of factors to combine
 | 
					
						
							|  |  |  | //	 * @param ordering of variables needed for matrix column order
 | 
					
						
							|  |  |  | //	 * @return the augmented matrix and a noise model
 | 
					
						
							|  |  |  | //	 */
 | 
					
						
							|  |  |  | //	template <class Factors>
 | 
					
						
							|  |  |  | //	std::pair<Matrix, SharedDiagonal> combineFactorsAndCreateMatrix(
 | 
					
						
							|  |  |  | //			const Factors& factors,
 | 
					
						
							|  |  |  | //			const Ordering& order, const Dimensions& dimensions);
 | 
					
						
							| 
									
										
										
										
											2010-07-08 05:41:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-05 23:09:09 +08:00
										 |  |  | } // namespace gtsam
 |