| 
									
										
										
										
											2013-02-20 05:37:17 +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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @file    BatchFixedLagSmoother.h | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |  * @brief   An LM-based fixed-lag smoother. | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @author  Michael Kaess, Stephen Williams | 
					
						
							|  |  |  |  * @date    Oct 14, 2012 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  | #include <gtsam_unstable/nonlinear/FixedLagSmoother.h>
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | #include <gtsam/nonlinear/LevenbergMarquardtOptimizer.h>
 | 
					
						
							|  |  |  | #include <queue>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-14 02:56:21 +08:00
										 |  |  | class GTSAM_UNSTABLE_EXPORT BatchFixedLagSmoother : public FixedLagSmoother { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// Typedef for a shared pointer to an Incremental Fixed-Lag Smoother
 | 
					
						
							|  |  |  |   typedef boost::shared_ptr<BatchFixedLagSmoother> shared_ptr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** default constructor */ | 
					
						
							| 
									
										
										
										
											2013-04-12 04:07:58 +08:00
										 |  |  |   BatchFixedLagSmoother(double smootherLag = 0.0, const LevenbergMarquardtParams& parameters = LevenbergMarquardtParams(), bool enforceConsistency = true) : | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |     FixedLagSmoother(smootherLag), parameters_(parameters), enforceConsistency_(enforceConsistency) { }; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** destructor */ | 
					
						
							|  |  |  |   virtual ~BatchFixedLagSmoother() { }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Print the factor for debugging and testing (implementing Testable) */ | 
					
						
							|  |  |  |   virtual void print(const std::string& s = "BatchFixedLagSmoother:\n", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Check if two IncrementalFixedLagSmoother Objects are equal */ | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   virtual bool equals(const FixedLagSmoother& rhs, double tol = 1e-9) const; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   /** Add new factors, updating the solution and relinearizing as needed. */ | 
					
						
							|  |  |  |   Result update(const NonlinearFactorGraph& newFactors = NonlinearFactorGraph(), const Values& newTheta = Values(), | 
					
						
							|  |  |  |       const KeyTimestampMap& timestamps = KeyTimestampMap()); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** Compute an estimate from the incomplete linear delta computed during the last update.
 | 
					
						
							|  |  |  |    * This delta is incomplete because it was not updated below wildfire_threshold.  If only | 
					
						
							|  |  |  |    * a single variable is needed, it is faster to call calculateEstimate(const KEY&). | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   Values calculateEstimate() const { | 
					
						
							| 
									
										
										
										
											2013-08-09 04:08:54 +08:00
										 |  |  |     return theta_.retract(delta_); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Compute an estimate for a single variable using its incomplete linear delta computed
 | 
					
						
							|  |  |  |    * during the last update.  This is faster than calling the no-argument version of | 
					
						
							|  |  |  |    * calculateEstimate, which operates on all variables. | 
					
						
							|  |  |  |    * @param key | 
					
						
							|  |  |  |    * @return | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   template<class VALUE> | 
					
						
							|  |  |  |   VALUE calculateEstimate(Key key) const { | 
					
						
							| 
									
										
										
										
											2013-08-09 04:08:54 +08:00
										 |  |  |     const Vector delta = delta_.at(key); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |     return theta_.at<VALUE>(key).retract(delta); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   /** read the current set of optimizer parameters */ | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   const LevenbergMarquardtParams& params() const { | 
					
						
							|  |  |  |     return parameters_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   /** update the current set of optimizer parameters */ | 
					
						
							|  |  |  |   LevenbergMarquardtParams& params() { | 
					
						
							|  |  |  |     return parameters_; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   /** Access the current set of factors */ | 
					
						
							|  |  |  |   const NonlinearFactorGraph& getFactors() const { | 
					
						
							|  |  |  |     return factors_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Access the current linearization point */ | 
					
						
							|  |  |  |   const Values& getLinearizationPoint() const { | 
					
						
							|  |  |  |     return theta_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Access the current ordering */ | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   const Ordering& getOrdering() const { | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |     return ordering_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Access the current set of deltas to the linearization point */ | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   const VectorValues& getDelta() const { | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |     return delta_; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | protected: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   /** A typedef defining an Key-Factor mapping **/ | 
					
						
							|  |  |  |   typedef std::map<Key, std::set<Index> > FactorIndex; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** The L-M optimization parameters **/ | 
					
						
							|  |  |  |   LevenbergMarquardtParams parameters_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** A flag indicating if the optimizer should enforce probabilistic consistency by maintaining the
 | 
					
						
							|  |  |  |    * linearization point of all variables involved in linearized/marginal factors at the edge of the | 
					
						
							|  |  |  |    * smoothing window. This idea is from ??? TODO: Look up paper reference **/ | 
					
						
							|  |  |  |   bool enforceConsistency_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   /** The nonlinear factors **/ | 
					
						
							|  |  |  |   NonlinearFactorGraph factors_; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   /** The current linearization point **/ | 
					
						
							|  |  |  |   Values theta_; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   /** The set of keys involved in current linear factors. These keys should not be relinearized. **/ | 
					
						
							|  |  |  |   Values linearKeys_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** The current ordering */ | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   Ordering ordering_; | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** The current set of linear deltas */ | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   VectorValues delta_; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** The set of available factor graph slots. These occur because we are constantly deleting factors, leaving holes. **/ | 
					
						
							|  |  |  |   std::queue<size_t> availableSlots_; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   /** A cross-reference structure to allow efficient factor lookups by key **/ | 
					
						
							|  |  |  |   FactorIndex factorIndex_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** Augment the list of factors with a set of new factors */ | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   void insertFactors(const NonlinearFactorGraph& newFactors); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** Remove factors from the list of factors by slot index */ | 
					
						
							|  |  |  |   void removeFactors(const std::set<size_t>& deleteFactors); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Erase any keys associated with timestamps before the provided time */ | 
					
						
							|  |  |  |   void eraseKeys(const std::set<Key>& keys); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   /** Use colamd to update into an efficient ordering */ | 
					
						
							|  |  |  |   void reorder(const std::set<Key>& marginalizeKeys = std::set<Key>()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Optimize the current graph using a modified version of L-M */ | 
					
						
							|  |  |  |   Result optimize(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   /** Marginalize out selected variables */ | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   void marginalize(const std::set<Key>& marginalizableKeys); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   static NonlinearFactorGraph calculateMarginalFactors(const NonlinearFactorGraph& graph, const Values& theta, | 
					
						
							|  |  |  |       const std::set<Key>& marginalizeKeys, const GaussianFactorGraph::Eliminate& eliminateFunction); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   /** Private methods for printing debug information */ | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   static void PrintKeySet(const std::set<Key>& keys, const std::string& label); | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   static void PrintKeySet(const gtsam::FastSet<Key>& keys, const std::string& label); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   static void PrintSymbolicFactor(const NonlinearFactor::shared_ptr& factor); | 
					
						
							| 
									
										
										
										
											2013-08-14 06:30:08 +08:00
										 |  |  |   static void PrintSymbolicFactor(const GaussianFactor::shared_ptr& factor); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   static void PrintSymbolicGraph(const NonlinearFactorGraph& graph, const std::string& label); | 
					
						
							| 
									
										
										
										
											2013-08-14 06:30:08 +08:00
										 |  |  |   static void PrintSymbolicGraph(const GaussianFactorGraph& graph, const std::string& label); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | }; // BatchFixedLagSmoother
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } /// namespace gtsam
 |