| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |  * 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.cpp | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam_unstable/nonlinear/BatchFixedLagSmoother.h>
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  | #include <gtsam/nonlinear/LinearContainerFactor.h>
 | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:33 +08:00
										 |  |  | #include <gtsam/linear/GaussianJunctionTree.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianFactorGraph.h>
 | 
					
						
							|  |  |  | #include <gtsam/linear/GaussianFactor.h>
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  | void BatchFixedLagSmoother::print(const string& s, | 
					
						
							| 
									
										
										
										
											2015-05-14 14:44:46 +08:00
										 |  |  |     const KeyFormatter& keyFormatter) const { | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   FixedLagSmoother::print(s, keyFormatter); | 
					
						
							|  |  |  |   // TODO: What else to print?
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-05-14 14:44:46 +08:00
										 |  |  | bool BatchFixedLagSmoother::equals(const FixedLagSmoother& rhs, | 
					
						
							|  |  |  |     double tol) const { | 
					
						
							|  |  |  |   const BatchFixedLagSmoother* e = | 
					
						
							|  |  |  |       dynamic_cast<const BatchFixedLagSmoother*>(&rhs); | 
					
						
							| 
									
										
										
										
											2020-04-07 05:31:05 +08:00
										 |  |  |   return e != nullptr && FixedLagSmoother::equals(*e, tol) | 
					
						
							| 
									
										
										
										
											2015-05-14 14:44:46 +08:00
										 |  |  |       && factors_.equals(e->factors_, tol) && theta_.equals(e->theta_, tol); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  | Matrix BatchFixedLagSmoother::marginalCovariance(Key key) const { | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   throw runtime_error( | 
					
						
							| 
									
										
										
										
											2015-05-14 14:44:46 +08:00
										 |  |  |       "BatchFixedLagSmoother::marginalCovariance not implemented"); | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-05-14 14:44:46 +08:00
										 |  |  | FixedLagSmoother::Result BatchFixedLagSmoother::update( | 
					
						
							|  |  |  |     const NonlinearFactorGraph& newFactors, const Values& newTheta, | 
					
						
							| 
									
										
										
										
											2019-05-19 10:47:58 +08:00
										 |  |  |     const KeyTimestampMap& timestamps, const FactorIndices& factorsToRemove) { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   // Update all of the internal variables with the new information
 | 
					
						
							|  |  |  |   gttic(augment_system); | 
					
						
							|  |  |  |   // Add the new variables to theta
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   theta_.insert(newTheta); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   // Add new variables to the end of the ordering
 | 
					
						
							| 
									
										
										
										
											2021-01-05 09:46:16 +08:00
										 |  |  |   for (const auto key_value : newTheta) { | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |     ordering_.push_back(key_value.key); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   // Augment Delta
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:30:08 +08:00
										 |  |  |   delta_.insert(newTheta.zeroVectors()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   // Add the new factors to the graph, updating the variable index
 | 
					
						
							|  |  |  |   insertFactors(newFactors); | 
					
						
							|  |  |  |   gttoc(augment_system); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-05 11:54:34 +08:00
										 |  |  |   // remove factors in factorToRemove
 | 
					
						
							| 
									
										
										
										
											2019-01-22 07:03:38 +08:00
										 |  |  |   for(const size_t i : factorsToRemove){ | 
					
						
							| 
									
										
										
										
											2016-08-05 11:54:34 +08:00
										 |  |  |     if(factors_[i]) | 
					
						
							|  |  |  |       factors_[i].reset(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   // Update the Timestamps associated with the factor keys
 | 
					
						
							|  |  |  |   updateKeyTimestampMap(timestamps); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Get current timestamp
 | 
					
						
							|  |  |  |   double current_timestamp = getCurrentTimestamp(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Find the set of variables to be marginalized out
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   KeyVector marginalizableKeys = findKeysBefore( | 
					
						
							| 
									
										
										
										
											2015-05-14 14:44:46 +08:00
										 |  |  |       current_timestamp - smootherLag_); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   // Reorder
 | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   gttic(reorder); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   reorder(marginalizableKeys); | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   gttoc(reorder); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   // Optimize
 | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   gttic(optimize); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   Result result; | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |   if (factors_.size() > 0) { | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |     result = optimize(); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   gttoc(optimize); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   // Marginalize out old variables.
 | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   gttic(marginalize); | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |   if (marginalizableKeys.size() > 0) { | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |     marginalize(marginalizableKeys); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   gttoc(marginalize); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-05-14 14:44:46 +08:00
										 |  |  | void BatchFixedLagSmoother::insertFactors( | 
					
						
							|  |  |  |     const NonlinearFactorGraph& newFactors) { | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   for(const auto& factor: newFactors) { | 
					
						
							| 
									
										
										
										
											2013-11-09 00:35:28 +08:00
										 |  |  |     Key index; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |     // Insert the factor into an existing hole in the factor graph, if possible
 | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |     if (availableSlots_.size() > 0) { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |       index = availableSlots_.front(); | 
					
						
							|  |  |  |       availableSlots_.pop(); | 
					
						
							|  |  |  |       factors_.replace(index, factor); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       index = factors_.size(); | 
					
						
							|  |  |  |       factors_.push_back(factor); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // Update the FactorIndex
 | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |     for(Key key: *factor) { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |       factorIndex_[key].insert(index); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-05-14 14:44:46 +08:00
										 |  |  | void BatchFixedLagSmoother::removeFactors( | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |     const set<size_t>& deleteFactors) { | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |   for(size_t slot: deleteFactors) { | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |     if (factors_.at(slot)) { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |       // Remove references to this factor from the FactorIndex
 | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |       for(Key key: *(factors_.at(slot))) { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |         factorIndex_[key].erase(slot); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       // Remove the factor from the factor graph
 | 
					
						
							|  |  |  |       factors_.remove(slot); | 
					
						
							|  |  |  |       // Add the factor's old slot to the list of available slots
 | 
					
						
							|  |  |  |       availableSlots_.push(slot); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       // TODO: Throw an error??
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |       cout << "Attempting to remove a factor from slot " << slot | 
					
						
							| 
									
										
										
										
											2020-04-07 05:31:05 +08:00
										 |  |  |           << ", but it is already nullptr." << endl; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  | void BatchFixedLagSmoother::eraseKeys(const KeyVector& keys) { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |   for(Key key: keys) { | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |     // Erase the key from the values
 | 
					
						
							|  |  |  |     theta_.erase(key); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Erase the key from the factor index
 | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |     factorIndex_.erase(key); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // Erase the key from the set of linearized keys
 | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |     if (linearKeys_.exists(key)) { | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |       linearKeys_.erase(key); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   eraseKeyTimestampMap(keys); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   // Remove marginalized keys from the ordering and delta
 | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |   for(Key key: keys) { | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |     ordering_.erase(find(ordering_.begin(), ordering_.end(), key)); | 
					
						
							| 
									
										
										
										
											2013-08-14 06:30:08 +08:00
										 |  |  |     delta_.erase(key); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  | void BatchFixedLagSmoother::reorder(const KeyVector& marginalizeKeys) { | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   // COLAMD groups will be used to place marginalize keys in Group 0, and everything else in Group 1
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   ordering_ = Ordering::ColamdConstrainedFirst(factors_, marginalizeKeys); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | FixedLagSmoother::Result BatchFixedLagSmoother::optimize() { | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   // Create output result structure
 | 
					
						
							|  |  |  |   Result result; | 
					
						
							|  |  |  |   result.nonlinearVariables = theta_.size() - linearKeys_.size(); | 
					
						
							|  |  |  |   result.linearVariables = linearKeys_.size(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Set optimization parameters
 | 
					
						
							|  |  |  |   double lambda = parameters_.lambdaInitial; | 
					
						
							|  |  |  |   double lambdaFactor = parameters_.lambdaFactor; | 
					
						
							|  |  |  |   double lambdaUpperBound = parameters_.lambdaUpperBound; | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   double lambdaLowerBound = 1.0e-10; | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   size_t maxIterations = parameters_.maxIterations; | 
					
						
							|  |  |  |   double relativeErrorTol = parameters_.relativeErrorTol; | 
					
						
							|  |  |  |   double absoluteErrorTol = parameters_.absoluteErrorTol; | 
					
						
							|  |  |  |   double errorTol = parameters_.errorTol; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Create a Values that holds the current evaluation point
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:30:08 +08:00
										 |  |  |   Values evalpoint = theta_.retract(delta_); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   result.error = factors_.error(evalpoint); | 
					
						
							| 
									
										
										
										
											2013-05-21 22:57:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   // check if we're already close enough
 | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |   if (result.error <= errorTol) { | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |     return result; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   // Use a custom optimization loop so the linearization points can be controlled
 | 
					
						
							|  |  |  |   double previousError; | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |   VectorValues newDelta; | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   do { | 
					
						
							|  |  |  |     previousError = result.error; | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |     // Do next iteration
 | 
					
						
							|  |  |  |     gttic(optimizer_iteration); | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       // Linearize graph around the linearization point
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:30:08 +08:00
										 |  |  |       GaussianFactorGraph linearFactorGraph = *factors_.linearize(theta_); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Keep increasing lambda until we make make progress
 | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |       while (true) { | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |         // Add prior factors at the current solution
 | 
					
						
							|  |  |  |         gttic(damp); | 
					
						
							| 
									
										
										
										
											2013-08-06 06:31:44 +08:00
										 |  |  |         GaussianFactorGraph dampedFactorGraph(linearFactorGraph); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |         dampedFactorGraph.reserve(linearFactorGraph.size() + delta_.size()); | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           // for each of the variables, add a prior at the current solution
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |           double sigma = 1.0 / sqrt(lambda); | 
					
						
							|  |  |  |           for(const auto& key_value: delta_) { | 
					
						
							| 
									
										
										
										
											2013-08-14 06:30:08 +08:00
										 |  |  |             size_t dim = key_value.second.size(); | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |             Matrix A = Matrix::Identity(dim, dim); | 
					
						
							| 
									
										
										
										
											2013-08-14 06:30:08 +08:00
										 |  |  |             Vector b = key_value.second; | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |             SharedDiagonal model = noiseModel::Isotropic::Sigma(dim, sigma); | 
					
						
							| 
									
										
										
										
											2015-05-14 14:44:46 +08:00
										 |  |  |             GaussianFactor::shared_ptr prior( | 
					
						
							|  |  |  |                 new JacobianFactor(key_value.first, A, b, model)); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |             dampedFactorGraph.push_back(prior); | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |         gttoc(damp); | 
					
						
							|  |  |  |         result.intermediateSteps++; | 
					
						
							| 
									
										
										
										
											2013-05-21 22:57:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |         gttic(solve); | 
					
						
							|  |  |  |         // Solve Damped Gaussian Factor Graph
 | 
					
						
							| 
									
										
										
										
											2015-05-14 14:44:46 +08:00
										 |  |  |         newDelta = dampedFactorGraph.optimize(ordering_, | 
					
						
							|  |  |  |             parameters_.getEliminationFunction()); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |         // update the evalpoint with the new delta
 | 
					
						
							| 
									
										
										
										
											2013-08-14 06:30:08 +08:00
										 |  |  |         evalpoint = theta_.retract(newDelta); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |         gttoc(solve); | 
					
						
							| 
									
										
										
										
											2013-05-21 22:57:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |         // Evaluate the new error
 | 
					
						
							|  |  |  |         gttic(compute_error); | 
					
						
							|  |  |  |         double error = factors_.error(evalpoint); | 
					
						
							|  |  |  |         gttoc(compute_error); | 
					
						
							| 
									
										
										
										
											2013-05-21 22:57:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |         if (error < result.error) { | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |           // Keep this change
 | 
					
						
							|  |  |  |           // Update the error value
 | 
					
						
							|  |  |  |           result.error = error; | 
					
						
							|  |  |  |           // Update the linearization point
 | 
					
						
							|  |  |  |           theta_ = evalpoint; | 
					
						
							|  |  |  |           // Reset the deltas to zeros
 | 
					
						
							|  |  |  |           delta_.setZero(); | 
					
						
							|  |  |  |           // Put the linearization points and deltas back for specific variables
 | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |           if (enforceConsistency_ && (linearKeys_.size() > 0)) { | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |             theta_.update(linearKeys_); | 
					
						
							| 
									
										
										
										
											2021-01-05 09:46:16 +08:00
										 |  |  |             for(const auto key_value: linearKeys_) { | 
					
						
							| 
									
										
										
										
											2013-08-14 06:30:08 +08:00
										 |  |  |               delta_.at(key_value.key) = newDelta.at(key_value.key); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           // Decrease lambda for next time
 | 
					
						
							|  |  |  |           lambda /= lambdaFactor; | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |           if (lambda < lambdaLowerBound) { | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |             lambda = lambdaLowerBound; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |           // End this lambda search iteration
 | 
					
						
							|  |  |  |           break; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           // Reject this change
 | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |           if (lambda >= lambdaUpperBound) { | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |             // The maximum lambda has been used. Print a warning and end the search.
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |             cout | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |                 << "Warning:  Levenberg-Marquardt giving up because cannot decrease error with maximum lambda" | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |                 << endl; | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |           } else { | 
					
						
							|  |  |  |             // Increase lambda and continue searching
 | 
					
						
							|  |  |  |             lambda *= lambdaFactor; | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |       } // end while
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |     gttoc(optimizer_iteration); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |     result.iterations++; | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |   } while (result.iterations < maxIterations | 
					
						
							| 
									
										
										
										
											2015-05-14 14:44:46 +08:00
										 |  |  |       && !checkConvergence(relativeErrorTol, absoluteErrorTol, errorTol, | 
					
						
							|  |  |  |           previousError, result.error, NonlinearOptimizerParams::SILENT)); | 
					
						
							| 
									
										
										
										
											2013-05-21 22:57:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  | void BatchFixedLagSmoother::marginalize(const KeyVector& marginalizeKeys) { | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   // In order to marginalize out the selected variables, the factors involved in those variables
 | 
					
						
							|  |  |  |   // must be identified and removed. Also, the effect of those removed factors on the
 | 
					
						
							|  |  |  |   // remaining variables needs to be accounted for. This will be done with linear container factors
 | 
					
						
							|  |  |  |   // from the result of a partial elimination. This function removes the marginalized factors and
 | 
					
						
							|  |  |  |   // adds the linearized factors back in.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   // Identify all of the factors involving any marginalized variable. These must be removed.
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   set<size_t> removedFactorSlots; | 
					
						
							|  |  |  |   const VariableIndex variableIndex(factors_); | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |   for(Key key: marginalizeKeys) { | 
					
						
							| 
									
										
										
										
											2019-04-11 22:49:07 +08:00
										 |  |  |     const auto& slots = variableIndex[key]; | 
					
						
							| 
									
										
										
										
											2014-02-24 22:29:30 +08:00
										 |  |  |     removedFactorSlots.insert(slots.begin(), slots.end()); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   // Add the removed factors to a factor graph
 | 
					
						
							|  |  |  |   NonlinearFactorGraph removedFactors; | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |   for(size_t slot: removedFactorSlots) { | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |     if (factors_.at(slot)) { | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |       removedFactors.push_back(factors_.at(slot)); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   // Calculate marginal factors on the remaining keys
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   NonlinearFactorGraph marginalFactors = CalculateMarginalFactors( | 
					
						
							|  |  |  |       removedFactors, theta_, marginalizeKeys, parameters_.getEliminationFunction()); | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   // Remove marginalized factors from the factor graph
 | 
					
						
							|  |  |  |   removeFactors(removedFactorSlots); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   // Remove marginalized keys from the system
 | 
					
						
							|  |  |  |   eraseKeys(marginalizeKeys); | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Insert the new marginal factors
 | 
					
						
							|  |  |  |   insertFactors(marginalFactors); | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  | void BatchFixedLagSmoother::PrintKeySet(const set<Key>& keys, | 
					
						
							|  |  |  |     const string& label) { | 
					
						
							|  |  |  |   cout << label; | 
					
						
							|  |  |  |   for(Key key: keys) { | 
					
						
							|  |  |  |     cout << " " << DefaultKeyFormatter(key); | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   cout << endl; | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  | void BatchFixedLagSmoother::PrintKeySet(const KeySet& keys, | 
					
						
							|  |  |  |     const string& label) { | 
					
						
							|  |  |  |   cout << label; | 
					
						
							|  |  |  |   for(Key key: keys) { | 
					
						
							|  |  |  |     cout << " " << DefaultKeyFormatter(key); | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   cout << endl; | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-05-14 14:44:46 +08:00
										 |  |  | void BatchFixedLagSmoother::PrintSymbolicFactor( | 
					
						
							|  |  |  |     const NonlinearFactor::shared_ptr& factor) { | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   cout << "f("; | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |   if (factor) { | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |     for(Key key: factor->keys()) { | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |       cout << " " << DefaultKeyFormatter(key); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   } else { | 
					
						
							| 
									
										
										
										
											2020-04-07 05:31:05 +08:00
										 |  |  |     cout << " nullptr"; | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   cout << " )" << endl; | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-05-14 14:44:46 +08:00
										 |  |  | void BatchFixedLagSmoother::PrintSymbolicFactor( | 
					
						
							|  |  |  |     const GaussianFactor::shared_ptr& factor) { | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   cout << "f("; | 
					
						
							| 
									
										
										
										
											2016-05-21 23:52:14 +08:00
										 |  |  |   for(Key key: factor->keys()) { | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |     cout << " " << DefaultKeyFormatter(key); | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   cout << " )" << endl; | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-05-14 14:44:46 +08:00
										 |  |  | void BatchFixedLagSmoother::PrintSymbolicGraph( | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |     const NonlinearFactorGraph& graph, const string& label) { | 
					
						
							|  |  |  |   cout << label << endl; | 
					
						
							|  |  |  |   for(const auto& factor: graph) { | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  |     PrintSymbolicFactor(factor); | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 04:23:47 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  | void BatchFixedLagSmoother::PrintSymbolicGraph(const GaussianFactorGraph& graph, | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |     const string& label) { | 
					
						
							|  |  |  |   cout << label << endl; | 
					
						
							|  |  |  |   for(const auto& factor: graph) { | 
					
						
							| 
									
										
										
										
											2013-08-14 06:30:08 +08:00
										 |  |  |     PrintSymbolicFactor(factor); | 
					
						
							| 
									
										
										
										
											2013-04-26 02:10:21 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  | GaussianFactorGraph BatchFixedLagSmoother::CalculateMarginalFactors( | 
					
						
							|  |  |  |     const GaussianFactorGraph& graph, const KeyVector& keys, | 
					
						
							| 
									
										
										
										
											2015-05-14 13:26:24 +08:00
										 |  |  |     const GaussianFactorGraph::Eliminate& eliminateFunction) { | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |   if (keys.size() == 0) { | 
					
						
							|  |  |  |     // There are no keys to marginalize. Simply return the input factors
 | 
					
						
							|  |  |  |     return graph; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     // .first is the eliminated Bayes tree, while .second is the remaining factor graph
 | 
					
						
							|  |  |  |     return *graph.eliminatePartialMultifrontal(keys, eliminateFunction).second; | 
					
						
							| 
									
										
										
										
											2016-02-25 03:01:19 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-02-25 03:01:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | NonlinearFactorGraph BatchFixedLagSmoother::CalculateMarginalFactors( | 
					
						
							|  |  |  |     const NonlinearFactorGraph& graph, const Values& theta, const KeyVector& keys, | 
					
						
							|  |  |  |     const GaussianFactorGraph::Eliminate& eliminateFunction) { | 
					
						
							|  |  |  |   if (keys.size() == 0) { | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |     // There are no keys to marginalize. Simply return the input factors
 | 
					
						
							|  |  |  |     return graph; | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     // Create the linear factor graph
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |     const auto linearFactorGraph = graph.linearize(theta); | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |     const auto marginalLinearFactors = | 
					
						
							|  |  |  |         CalculateMarginalFactors(*linearFactorGraph, keys, eliminateFunction); | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-19 14:13:59 +08:00
										 |  |  |     // Wrap in nonlinear container factors
 | 
					
						
							|  |  |  |     return LinearContainerFactor::ConvertLinearGraph(marginalLinearFactors, theta); | 
					
						
							| 
									
										
										
										
											2013-08-11 01:16:31 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-20 05:37:17 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | } /// namespace gtsam
 |