| 
									
										
										
										
											2009-12-29 13:57:05 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    ISAM2.h | 
					
						
							| 
									
										
										
										
											2009-12-30 12:27:14 +08:00
										 |  |  |  * @brief   Incremental update functionality (ISAM2) for BayesTree, with fluid relinearization. | 
					
						
							| 
									
										
										
										
											2009-12-29 13:57:05 +08:00
										 |  |  |  * @author  Michael Kaess | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <map>
 | 
					
						
							|  |  |  | #include <list>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | #include <boost/serialization/map.hpp>
 | 
					
						
							|  |  |  | #include <boost/serialization/list.hpp>
 | 
					
						
							|  |  |  | #include <stdexcept>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Testable.h"
 | 
					
						
							|  |  |  | #include "FactorGraph.h"
 | 
					
						
							|  |  |  | #include "NonlinearFactorGraph.h"
 | 
					
						
							|  |  |  | #include "BayesNet.h"
 | 
					
						
							|  |  |  | #include "BayesTree.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | #include "Key.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  | #include "SymbolMap.h"
 | 
					
						
							| 
									
										
										
										
											2009-12-29 13:57:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  | 	typedef SymbolMap<GaussianFactor::shared_ptr> CachedFactors; | 
					
						
							| 
									
										
										
										
											2010-01-17 14:06:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-29 13:57:05 +08:00
										 |  |  | 	template<class Conditional, class Config> | 
					
						
							|  |  |  | 	class ISAM2: public BayesTree<Conditional> { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-30 12:27:14 +08:00
										 |  |  | 	protected: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 12:32:45 +08:00
										 |  |  | 		// current linearization point
 | 
					
						
							| 
									
										
										
										
											2010-01-22 04:15:52 +08:00
										 |  |  | 		Config theta_; | 
					
						
							| 
									
										
										
										
											2010-01-22 14:28:12 +08:00
										 |  |  | 		Config thetaFuture_; // lin point of next iteration
 | 
					
						
							| 
									
										
										
										
											2010-01-18 12:32:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// for keeping all original nonlinear factors
 | 
					
						
							| 
									
										
										
										
											2009-12-29 13:57:05 +08:00
										 |  |  | 		NonlinearFactorGraph<Config> nonlinearFactors_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-17 14:06:20 +08:00
										 |  |  | 		// cached intermediate results for restarting computation in the middle
 | 
					
						
							| 
									
										
										
										
											2010-01-18 16:05:33 +08:00
										 |  |  | 		CachedFactors cached_; | 
					
						
							| 
									
										
										
										
											2010-01-17 14:06:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-22 14:28:12 +08:00
										 |  |  | 		// the linear solution, an update to the estimate in theta
 | 
					
						
							|  |  |  | 		VectorConfig delta_; | 
					
						
							|  |  |  | 		VectorConfig deltaMarked_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-21 15:38:37 +08:00
										 |  |  | 		// variables that have been updated, requiring the corresponding factors to be relinearized
 | 
					
						
							|  |  |  | 		std::list<Symbol> marked_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-29 13:57:05 +08:00
										 |  |  | 	public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** Create an empty Bayes Tree */ | 
					
						
							|  |  |  | 		ISAM2(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/** Create a Bayes Tree from a Bayes Net */ | 
					
						
							| 
									
										
										
										
											2009-12-30 12:27:14 +08:00
										 |  |  | 		ISAM2(const NonlinearFactorGraph<Config>& fg, const Ordering& ordering, const Config& config); | 
					
						
							| 
									
										
										
										
											2009-12-29 13:57:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/** Destructor */ | 
					
						
							|  |  |  | 		virtual ~ISAM2() { | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		typedef typename BayesTree<Conditional>::sharedClique sharedClique; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		typedef typename BayesTree<Conditional>::Cliques Cliques; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/**
 | 
					
						
							|  |  |  | 		 * ISAM2. (update_internal provides access to list of orphans for drawing purposes) | 
					
						
							|  |  |  | 		 */ | 
					
						
							| 
									
										
										
										
											2010-01-21 15:38:37 +08:00
										 |  |  | 		void update_internal(const NonlinearFactorGraph<Config>& newFactors, | 
					
						
							| 
									
										
										
										
											2010-01-22 14:28:12 +08:00
										 |  |  | 				const Config& newTheta, Cliques& orphans, | 
					
						
							| 
									
										
										
										
											2010-01-23 08:21:34 +08:00
										 |  |  | 				double wildfire_threshold, double relinearize_threshold, bool relinearize); | 
					
						
							| 
									
										
										
										
											2010-01-22 14:28:12 +08:00
										 |  |  | 		void update(const NonlinearFactorGraph<Config>& newFactors, const Config& newTheta, | 
					
						
							| 
									
										
										
										
											2010-01-23 08:21:34 +08:00
										 |  |  | 				double wildfire_threshold = 0., double relinearize_threshold = 0., bool relinearize = true); | 
					
						
							| 
									
										
										
										
											2009-12-29 13:57:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-22 14:28:12 +08:00
										 |  |  | 		// needed to create initial estimates (note that this will be the linearization point in the next step!)
 | 
					
						
							|  |  |  | 		const Config getLinearizationPoint() const {return thetaFuture_;} | 
					
						
							|  |  |  | 		// estimate based on incomplete delta (threshold!)
 | 
					
						
							|  |  |  | 		const Config calculateEstimate() const {return expmap(theta_, delta_);} | 
					
						
							|  |  |  | 		// estimate based on full delta (note that this is based on the actual current linearization point)
 | 
					
						
							|  |  |  | 		const Config calculateBestEstimate() const {return expmap(theta_, optimize2(*this, 0.));} | 
					
						
							| 
									
										
										
										
											2010-01-18 12:32:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-23 09:53:04 +08:00
										 |  |  | 		const std::list<Symbol>& getMarkedUnsafe() const { return marked_; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const NonlinearFactorGraph<Config>& getFactorsUnsafe() const { return nonlinearFactors_; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const Config& getThetaUnsafe() const { return theta_; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const VectorConfig& getDeltaUnsafe() const { return delta_; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		size_t lastAffectedVariableCount; | 
					
						
							|  |  |  | 		size_t lastAffectedFactorCount; | 
					
						
							|  |  |  | 		size_t lastAffectedCliqueCount; | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 12:32:45 +08:00
										 |  |  | 	private: | 
					
						
							| 
									
										
										
										
											2010-01-19 04:17:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-23 02:29:27 +08:00
										 |  |  | 		std::list<int> getAffectedFactors(const std::list<Symbol>& keys) const; | 
					
						
							| 
									
										
										
										
											2010-01-22 14:28:12 +08:00
										 |  |  | 		FactorGraph<GaussianFactor> relinearizeAffectedFactors(const std::set<Symbol>& affectedKeys) const; | 
					
						
							| 
									
										
										
										
											2010-01-18 12:32:45 +08:00
										 |  |  | 		FactorGraph<GaussianFactor> getCachedBoundaryFactors(Cliques& orphans); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-29 13:57:05 +08:00
										 |  |  | 	}; // ISAM2
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } /// namespace gtsam
 |