IncrementalFixedLagSmoother.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
24 #include <gtsam/base/debug.h>
25 
26 namespace gtsam {
27 
28 /* ************************************************************************* */
29 void IncrementalFixedLagSmoother::print(const std::string& s,
30  const KeyFormatter& keyFormatter) const {
31  FixedLagSmoother::print(s, keyFormatter);
32  // TODO: What else to print?
33 }
34 
35 /* ************************************************************************* */
37  double tol) const {
39  dynamic_cast<const IncrementalFixedLagSmoother*>(&rhs);
40  return e != nullptr && FixedLagSmoother::equals(*e, tol)
41  && isam_.equals(e->isam_, tol);
42 }
43 
44 /* ************************************************************************* */
46  const NonlinearFactorGraph& newFactors, const Values& newTheta,
47  const KeyTimestampMap& timestamps, const FactorIndices& factorsToRemove) {
48 
49  const bool debug = ISDEBUG("IncrementalFixedLagSmoother update");
50 
51  if (debug) {
52  std::cout << "IncrementalFixedLagSmoother::update() Start" << std::endl;
53  PrintSymbolicTree(isam_, "Bayes Tree Before Update:");
54  std::cout << "END" << std::endl;
55  }
56 
57  FastVector<size_t> removedFactors;
58  std::optional<FastMap<Key, int> > constrainedKeys = {};
59 
60  // Update the Timestamps associated with the factor keys
62 
63  // Get current timestamp
64  double current_timestamp = getCurrentTimestamp();
65 
66  if (debug)
67  std::cout << "Current Timestamp: " << current_timestamp << std::endl;
68 
69  // Find the set of variables to be marginalized out
70  KeyVector marginalizableKeys = findKeysBefore(
71  current_timestamp - smootherLag_);
72 
73  if (debug) {
74  std::cout << "Marginalizable Keys: ";
75  for(Key key: marginalizableKeys) {
76  std::cout << DefaultKeyFormatter(key) << " ";
77  }
78  std::cout << std::endl;
79  }
80 
81  // Force iSAM2 to put the marginalizable variables at the beginning
82  createOrderingConstraints(marginalizableKeys, constrainedKeys);
83 
84  if (debug) {
85  std::cout << "Constrained Keys: ";
86  if (constrainedKeys) {
87  for (FastMap<Key, int>::const_iterator iter = constrainedKeys->begin();
88  iter != constrainedKeys->end(); ++iter) {
89  std::cout << DefaultKeyFormatter(iter->first) << "(" << iter->second
90  << ") ";
91  }
92  }
93  std::cout << std::endl;
94  }
95 
96  std::unordered_set<Key> additionalKeys =
98  isam_, marginalizableKeys);
99  KeyList additionalMarkedKeys(additionalKeys.begin(), additionalKeys.end());
100 
101  // Update iSAM2
102  isamResult_ = isam_.update(newFactors, newTheta,
103  factorsToRemove, constrainedKeys, {}, additionalMarkedKeys);
104 
105  if (debug) {
107  "Bayes Tree After Update, Before Marginalization:");
108  std::cout << "END" << std::endl;
109  }
110 
111  // Marginalize out any needed variables
112  if (marginalizableKeys.size() > 0) {
113  FastList<Key> leafKeys(marginalizableKeys.begin(),
114  marginalizableKeys.end());
115  isam_.marginalizeLeaves(leafKeys);
116  }
117 
118  // Remove marginalized keys from the KeyTimestampMap
119  eraseKeyTimestampMap(marginalizableKeys);
120 
121  if (debug) {
122  PrintSymbolicTree(isam_, "Final Bayes Tree:");
123  std::cout << "END" << std::endl;
124  }
125 
126  // TODO: Fill in result structure
127  Result result;
128  result.iterations = 1;
129  result.linearVariables = 0;
130  result.nonlinearVariables = 0;
131  result.error = 0;
132 
133  if (debug)
134  std::cout << "IncrementalFixedLagSmoother::update() Finish" << std::endl;
135 
136  return result;
137 }
138 
139 /* ************************************************************************* */
141  TimestampKeyMap::iterator end = timestampKeyMap_.lower_bound(timestamp);
142  TimestampKeyMap::iterator iter = timestampKeyMap_.begin();
143  while (iter != end) {
144  keyTimestampMap_.erase(iter->second);
145  timestampKeyMap_.erase(iter++);
146  }
147 }
148 
149 /* ************************************************************************* */
151  const KeyVector& marginalizableKeys,
152  std::optional<FastMap<Key, int> >& constrainedKeys) const {
153  if (marginalizableKeys.size() > 0) {
154  constrainedKeys = FastMap<Key, int>();
155  // Generate ordering constraints so that the marginalizable variables will be eliminated first
156  // Set all variables to Group1
157  for(const TimestampKeyMap::value_type& timestamp_key: timestampKeyMap_) {
158  constrainedKeys->operator[](timestamp_key.second) = 1;
159  }
160  // Set marginalizable variables to Group0
161  for(Key key: marginalizableKeys) {
162  constrainedKeys->operator[](key) = 0;
163  }
164  }
165 }
166 
167 /* ************************************************************************* */
169  const std::string& label) {
170  std::cout << label;
171  for(Key key: keys) {
172  std::cout << " " << DefaultKeyFormatter(key);
173  }
174  std::cout << std::endl;
175 }
176 
177 /* ************************************************************************* */
180  std::cout << "f(";
181  for(Key key: factor->keys()) {
182  std::cout << " " << DefaultKeyFormatter(key);
183  }
184  std::cout << " )" << std::endl;
185 }
186 
187 /* ************************************************************************* */
189  const GaussianFactorGraph& graph, const std::string& label) {
190  std::cout << label << std::endl;
193  }
194 }
195 
196 /* ************************************************************************* */
198  const std::string& label) {
199  std::cout << label << std::endl;
200  if (!isam.roots().empty()) {
201  for(const ISAM2::sharedClique& root: isam.roots()) {
203  }
204  } else
205  std::cout << "{Empty Tree}" << std::endl;
206 }
207 
208 /* ************************************************************************* */
210  const ISAM2Clique::shared_ptr& clique, const std::string indent) {
211 
212  // Print the current clique
213  std::cout << indent << "P( ";
214  for(Key key: clique->conditional()->frontals()) {
215  std::cout << DefaultKeyFormatter(key) << " ";
216  }
217  if (clique->conditional()->nrParents() > 0)
218  std::cout << "| ";
219  for(Key key: clique->conditional()->parents()) {
220  std::cout << DefaultKeyFormatter(key) << " ";
221  }
222  std::cout << ")" << std::endl;
223 
224  // Recursively print all of the children
225  for(const ISAM2Clique::shared_ptr& child: clique->children) {
226  PrintSymbolicTreeHelper(child, indent + " ");
227  }
228 }
229 
230 /* ************************************************************************* */
231 }
gtsam::ISAM2
Definition: ISAM2.h:45
gtsam::ISAM2::marginalizeLeaves
void marginalizeLeaves(const FastList< Key > &leafKeys, FactorIndices *marginalFactorsIndices=nullptr, FactorIndices *deletedFactorsIndices=nullptr)
Definition: ISAM2.cpp:487
BayesTreeMarginalizationHelper.h
Helper functions for marginalizing variables from a Bayes Tree.
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
gtsam::IncrementalFixedLagSmoother::PrintKeySet
static void PrintKeySet(const std::set< Key > &keys, const std::string &label="Keys:")
Definition: IncrementalFixedLagSmoother.cpp:168
gtsam::IncrementalFixedLagSmoother::isam_
ISAM2 isam_
Definition: IncrementalFixedLagSmoother.h:131
gtsam::FastVector
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
gtsam::IncrementalFixedLagSmoother::equals
bool equals(const FixedLagSmoother &rhs, double tol=1e-9) const override
Definition: IncrementalFixedLagSmoother.cpp:36
gtsam::FixedLagSmoother::timestamps
const KeyTimestampMap & timestamps() const
Definition: nonlinear/FixedLagSmoother.h:90
gtsam::FastMap
Definition: FastMap.h:39
gtsam::FixedLagSmoother::eraseKeyTimestampMap
void eraseKeyTimestampMap(const KeyVector &keys)
Definition: FixedLagSmoother.cpp:76
gtsam::IncrementalFixedLagSmoother::PrintSymbolicTreeHelper
static void PrintSymbolicTreeHelper(const gtsam::ISAM2Clique::shared_ptr &clique, const std::string indent="")
Definition: IncrementalFixedLagSmoother.cpp:209
gtsam::IncrementalFixedLagSmoother::PrintSymbolicFactor
static void PrintSymbolicFactor(const GaussianFactor::shared_ptr &factor)
Definition: IncrementalFixedLagSmoother.cpp:178
gtsam::ISAM2::sharedClique
Base::sharedClique sharedClique
Shared pointer to a clique.
Definition: ISAM2.h:103
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:92
result
Values result
Definition: OdometryOptimize.cpp:8
gtsam::BayesTreeMarginalizationHelper::gatherAdditionalKeysToReEliminate
static std::unordered_set< Key > gatherAdditionalKeysToReEliminate(const BayesTree &bayesTree, const KeyVector &marginalizableKeys)
Definition: BayesTreeMarginalizationHelper.h:67
gtsam::ISAM2::equals
virtual bool equals(const ISAM2 &other, double tol=1e-9) const
Definition: ISAM2.cpp:58
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
pruning_fixture::factor
DecisionTreeFactor factor(D &C &B &A, "0.0 0.0 0.0 0.60658897 0.61241912 0.61241969 0.61247685 0.61247742 0.0 " "0.0 0.0 0.99995287 1.0 1.0 1.0 1.0")
gtsam::FixedLagSmoother::timestampKeyMap_
TimestampKeyMap timestampKeyMap_
Definition: nonlinear/FixedLagSmoother.h:113
gtsam::GaussianFactorGraph
Definition: GaussianFactorGraph.h:73
ISDEBUG
#define ISDEBUG(S)
Definition: debug.h:60
gtsam::IncrementalFixedLagSmoother::isamResult_
ISAM2Result isamResult_
Definition: IncrementalFixedLagSmoother.h:134
gtsam::FixedLagSmoother
Definition: nonlinear/FixedLagSmoother.h:33
gtsam::IncrementalFixedLagSmoother::update
Result update(const NonlinearFactorGraph &newFactors=NonlinearFactorGraph(), const Values &newTheta=Values(), const KeyTimestampMap &timestamps=KeyTimestampMap(), const FactorIndices &factorsToRemove=FactorIndices()) override
Definition: IncrementalFixedLagSmoother.cpp:45
gtsam::IncrementalFixedLagSmoother
Definition: IncrementalFixedLagSmoother.h:34
gtsam::FixedLagSmoother::smootherLag_
double smootherLag_
Definition: nonlinear/FixedLagSmoother.h:110
gtsam::KeyFormatter
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
gtsam::IncrementalFixedLagSmoother::print
void print(const std::string &s="IncrementalFixedLagSmoother:\n", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Definition: IncrementalFixedLagSmoother.cpp:29
gtsam::FixedLagSmoother::Result
Definition: nonlinear/FixedLagSmoother.h:48
isam
NonlinearISAM isam(relinearizeInterval)
gtsam::NonlinearFactorGraph
Definition: NonlinearFactorGraph.h:55
debug
static constexpr bool debug
Definition: testDiscreteBayesTree.cpp:31
gtsam::GaussianFactor::shared_ptr
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: GaussianFactor.h:42
gtsam::FixedLagSmoother::getCurrentTimestamp
double getCurrentTimestamp() const
Definition: FixedLagSmoother.cpp:95
gtsam::FastList
Definition: FastList.h:43
key
const gtsam::Symbol key('X', 0)
IncrementalFixedLagSmoother.h
An iSAM2-based fixed-lag smoother.
gtsam::ISAM2Clique::shared_ptr
std::shared_ptr< This > shared_ptr
Definition: ISAM2Clique.h:41
gtsam::FixedLagSmoother::equals
virtual bool equals(const FixedLagSmoother &rhs, double tol=1e-9) const
Definition: FixedLagSmoother.cpp:40
gtsam::IncrementalFixedLagSmoother::PrintSymbolicTree
static void PrintSymbolicTree(const gtsam::ISAM2 &isam, const std::string &label="Bayes Tree:")
Definition: IncrementalFixedLagSmoother.cpp:197
gtsam::FixedLagSmoother::KeyTimestampMap
std::map< Key, double > KeyTimestampMap
Typedef for a Key-Timestamp map/database.
Definition: nonlinear/FixedLagSmoother.h:41
gtsam
traits
Definition: SFMdata.h:40
gtsam::ISAM2::update
virtual ISAM2Result update(const NonlinearFactorGraph &newFactors=NonlinearFactorGraph(), const Values &newTheta=Values(), const FactorIndices &removeFactorIndices=FactorIndices(), const std::optional< FastMap< Key, int > > &constrainedKeys={}, const std::optional< FastList< Key > > &noRelinKeys={}, const std::optional< FastList< Key > > &extraReelimKeys={}, bool force_relinearize=false)
Definition: ISAM2.cpp:401
gtsam::Values
Definition: Values.h:65
iter
iterator iter(handle obj)
Definition: pytypes.h:2475
gtsam::IncrementalFixedLagSmoother::eraseKeysBefore
void eraseKeysBefore(double timestamp)
Definition: IncrementalFixedLagSmoother.cpp:140
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::FixedLagSmoother::keyTimestampMap_
KeyTimestampMap keyTimestampMap_
Definition: nonlinear/FixedLagSmoother.h:114
Eigen::placeholders::end
static const EIGEN_DEPRECATED end_t end
Definition: IndexedViewHelper.h:181
graph
NonlinearFactorGraph graph
Definition: doc/Code/OdometryExample.cpp:2
gtsam::IncrementalFixedLagSmoother::createOrderingConstraints
void createOrderingConstraints(const KeyVector &marginalizableKeys, std::optional< FastMap< Key, int > > &constrainedKeys) const
Definition: IncrementalFixedLagSmoother.cpp:150
gtsam::FixedLagSmoother::findKeysBefore
KeyVector findKeysBefore(double timestamp) const
Definition: FixedLagSmoother.cpp:104
gtsam::FixedLagSmoother::print
virtual void print(const std::string &s="FixedLagSmoother:\n", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: FixedLagSmoother.cpp:34
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::FixedLagSmoother::updateKeyTimestampMap
void updateKeyTimestampMap(const KeyTimestampMap &newTimestamps)
Definition: FixedLagSmoother.cpp:46
debug.h
Global debugging flags.
gtsam::FactorIndices
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition: Factor.h:37
gtsam::IncrementalFixedLagSmoother::PrintSymbolicGraph
static void PrintSymbolicGraph(const GaussianFactorGraph &graph, const std::string &label="Factor Graph:")
Definition: IncrementalFixedLagSmoother.cpp:188


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:41