HybridGaussianISAM.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 
25 #include <gtsam/inference/Key.h>
26 
27 #include <iterator>
28 
29 namespace gtsam {
30 
31 // Instantiate base class
32 // template class ISAM<HybridBayesTree>;
33 
34 /* ************************************************************************* */
36 
37 /* ************************************************************************* */
39  : Base(bayesTree) {}
40 
41 /* ************************************************************************* */
44  const HybridGaussianFactorGraph& newFactors) {
45  // Get all the discrete keys from the factors
46  const KeySet allDiscrete = factors.discreteKeySet();
47 
48  // Create KeyVector with continuous keys followed by discrete keys.
49  KeyVector newKeysDiscreteLast;
50  const KeySet newFactorKeys = newFactors.keys();
51  // Insert continuous keys first.
52  for (auto& k : newFactorKeys) {
53  if (!allDiscrete.exists(k)) {
54  newKeysDiscreteLast.push_back(k);
55  }
56  }
57  // Insert discrete keys at the end
58  std::copy(allDiscrete.begin(), allDiscrete.end(),
59  std::back_inserter(newKeysDiscreteLast));
60 
61  const VariableIndex index(factors);
62 
63  // Get an ordering where the new keys are eliminated last
65  index, KeyVector(newKeysDiscreteLast.begin(), newKeysDiscreteLast.end()),
66  true);
67  return ordering;
68 }
69 
70 /* ************************************************************************* */
72  const HybridGaussianFactorGraph& newFactors,
73  HybridBayesTree::Cliques* orphans,
74  const std::optional<size_t>& maxNrLeaves,
75  const std::optional<Ordering>& ordering,
76  const HybridBayesTree::Eliminate& function) {
77  // Remove the contaminated part of the Bayes tree
78  BayesNetType bn;
79  const KeySet newFactorKeys = newFactors.keys();
80  if (!this->empty()) {
81  KeyVector keyVector(newFactorKeys.begin(), newFactorKeys.end());
82  this->removeTop(keyVector, &bn, orphans);
83  }
84 
85  // Add the removed top and the new factors
87  factors.push_back(bn);
88  factors.push_back(newFactors);
89 
90  // Add the orphaned subtrees
91  for (const sharedClique& orphan : *orphans) {
93  }
94 
95  const VariableIndex index(factors);
96  Ordering elimination_ordering;
97  if (ordering) {
98  elimination_ordering = *ordering;
99  } else {
100  elimination_ordering = GetOrdering(factors, newFactors);
101  }
102 
103  // eliminate all factors (top, added, orphans) into a new Bayes tree
104  HybridBayesTree::shared_ptr bayesTree =
105  factors.eliminateMultifrontal(elimination_ordering, function, std::cref(index));
106 
107  if (maxNrLeaves) {
108  bayesTree->prune(*maxNrLeaves);
109  }
110 
111  // Re-add into Bayes tree data structures
112  this->roots_.insert(this->roots_.end(), bayesTree->roots().begin(),
113  bayesTree->roots().end());
114  this->nodes_.insert(bayesTree->nodes().begin(), bayesTree->nodes().end());
115 }
116 
117 /* ************************************************************************* */
119  const std::optional<size_t>& maxNrLeaves,
120  const std::optional<Ordering>& ordering,
121  const HybridBayesTree::Eliminate& function) {
122  Cliques orphans;
123  this->updateInternal(newFactors, &orphans, maxNrLeaves, ordering, function);
124 }
125 
126 } // namespace gtsam
KeySet discreteKeySet() const
Get all the discrete keys in the factor graph, as a set.
void removeTop(const KeyVector &keys, BayesNetType *bn, Cliques *orphans)
FactorGraphType::Eliminate Eliminate
Definition: BayesTree.h:85
Incremental update functionality (iSAM) for BayesTree.
static Ordering GetOrdering(HybridGaussianFactorGraph &factors, const HybridGaussianFactorGraph &newFactors)
Helper method to get an ordering given the existing factors and any new factors added.
KeySet keys() const
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:196
void update(const HybridGaussianFactorGraph &newFactors, const std::optional< size_t > &maxNrLeaves={}, const std::optional< Ordering > &ordering={}, const HybridBayesTree::Eliminate &function=HybridBayesTree::EliminationTraitsType::DefaultEliminate)
Perform update step with new factors.
std::shared_ptr< This > shared_ptr
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:190
bool exists(const VALUE &e) const
Definition: FastSet.h:98
const GaussianFactorGraph factors
static enum @1107 ordering
std::shared_ptr< Clique > sharedClique
Shared pointer to a clique.
Definition: BayesTree.h:74
Hybrid Bayes Tree, the result of eliminating a HybridJunctionTree.
Linearized Hybrid factor graph that uses type erasure.
void updateInternal(const HybridGaussianFactorGraph &newFactors, HybridBayesTree::Cliques *orphans, const std::optional< size_t > &maxNrLeaves={}, const std::optional< Ordering > &ordering={}, const HybridBayesTree::Eliminate &function=HybridBayesTree::EliminationTraitsType::DefaultEliminate)
Internal method that performs the ISAM update.
traits
Definition: chartTesting.h:28
std::shared_ptr< BayesTreeType > eliminateMultifrontal(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
HybridBayesTreeClique ::BayesNetType BayesNetType
Definition: BayesTree.h:79
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
static Ordering ColamdConstrainedLast(const FACTOR_GRAPH &graph, const KeyVector &constrainLast, bool forceOrder=false)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:20