HybridNonlinearISAM.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 
23 
24 #include <iostream>
25 
26 using namespace std;
27 
28 namespace gtsam {
29 
30 /* ************************************************************************* */
31 void HybridNonlinearISAM::saveGraph(const string& s,
32  const KeyFormatter& keyFormatter) const {
33  isam_.saveGraph(s, keyFormatter);
34 }
35 
36 /* ************************************************************************* */
38  const Values& initialValues,
39  const std::optional<size_t>& maxNrLeaves,
40  const std::optional<Ordering>& ordering) {
41  if (newFactors.size() > 0) {
42  // Reorder and relinearize every reorderInterval updates
43  if (reorderInterval_ > 0 && ++reorderCounter_ >= reorderInterval_) {
44  reorderRelinearize();
45  reorderCounter_ = 0;
46  }
47 
48  factors_.push_back(newFactors);
49 
50  // Linearize new factors and insert them
51  // TODO: optimize for whole config?
52  linPoint_.insert(initialValues);
53 
54  std::shared_ptr<HybridGaussianFactorGraph> linearizedNewFactors =
55  newFactors.linearize(linPoint_);
56 
57  // Update ISAM
58  isam_.update(*linearizedNewFactors, maxNrLeaves, ordering,
59  eliminationFunction_);
60  }
61 }
62 
63 /* ************************************************************************* */
64 void HybridNonlinearISAM::reorderRelinearize() {
65  if (factors_.size() > 0) {
66  // Obtain the new linearization point
67  const Values newLinPoint = estimate();
68 
69  DiscreteConditional::shared_ptr discreteProbabilities;
70 
71  auto discreteRoot = isam_.roots().at(0)->conditional();
72  if (discreteRoot->asDiscrete<TableDistribution>()) {
73  discreteProbabilities = discreteRoot->asDiscrete<TableDistribution>();
74  } else {
75  discreteProbabilities = discreteRoot->asDiscrete();
76  }
77 
78  isam_.clear();
79 
80  // Prune nonlinear factors based on discrete conditional probabilities
81  HybridNonlinearFactorGraph pruned_factors;
82  for (auto&& factor : factors_) {
83  if (auto nf = std::dynamic_pointer_cast<HybridNonlinearFactor>(factor)) {
84  pruned_factors.push_back(nf->prune(*discreteProbabilities));
85  } else {
86  pruned_factors.push_back(factor);
87  }
88  }
89  factors_ = pruned_factors;
90 
91  // Just recreate the whole BayesTree
92  // TODO: allow for constrained ordering here
93  // TODO: decouple re-linearization and reordering to avoid
94  isam_.update(*factors_.linearize(newLinPoint), {}, {},
95  eliminationFunction_);
96 
97  // Update linearization point
98  linPoint_ = newLinPoint;
99  }
100 }
101 
102 /* ************************************************************************* */
103 Values HybridNonlinearISAM::estimate() {
104  Values result;
105  if (isam_.size() > 0) {
106  HybridValues values = isam_.optimize();
107  assignment_ = values.discrete();
108  return linPoint_.retract(values.continuous());
109  } else {
110  return linPoint_;
111  }
112 }
113 
114 // /* *************************************************************************
115 // */ Matrix HybridNonlinearISAM::marginalCovariance(Key key) const {
116 // return isam_.marginalCovariance(key);
117 // }
118 
119 /* ************************************************************************* */
120 void HybridNonlinearISAM::print(const string& s,
121  const KeyFormatter& keyFormatter) const {
122  cout << s << "ReorderInterval: " << reorderInterval_
123  << " Current Count: " << reorderCounter_ << endl;
124  std::cout << "HybridGaussianISAM:" << std::endl;
125  isam_.print("", keyFormatter);
126  linPoint_.print("Linearization Point:\n", keyFormatter);
127  std::cout << "Nonlinear Graph:" << std::endl;
128  factors_.print("", keyFormatter);
129 }
130 
131 /* ************************************************************************* */
132 void HybridNonlinearISAM::printStats() const {
133  isam_.getCliqueData().getStats().print();
134 }
135 
136 /* ************************************************************************* */
137 
138 } // namespace gtsam
TableDistribution.h
Eigen::internal::print
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
Definition: NEON/PacketMath.h:3115
relicense.update
def update(text)
Definition: relicense.py:46
gtsam::HybridValues
Definition: HybridValues.h:37
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::HybridNonlinearFactorGraph
Definition: HybridNonlinearFactorGraph.h:33
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:247
Ordering.h
Variable ordering for the elimination algorithm.
result
Values result
Definition: OdometryOptimize.cpp:8
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::TableDistribution
Definition: TableDistribution.h:39
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
ordering
static enum @1096 ordering
gtsam::DiscreteConditional::shared_ptr
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: DiscreteConditional.h:43
gtsam::FactorGraph::size
size_t size() const
Definition: FactorGraph.h:297
HybridNonlinearFactor.h
A set of nonlinear factors indexed by a set of discrete keys.
HybridGaussianFactorGraph.h
Linearized Hybrid factor graph that uses type erasure.
gtsam
traits
Definition: SFMdata.h:40
gtsam::FactorGraph::push_back
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:147
gtsam::Values
Definition: Values.h:65
std
Definition: BFloat16.h:88
gtsam::HybridNonlinearFactorGraph::linearize
std::shared_ptr< HybridGaussianFactorGraph > linearize(const Values &continuousValues) const
Linearize all the continuous factors in the HybridNonlinearFactorGraph.
Definition: HybridNonlinearFactorGraph.cpp:139
HybridNonlinearISAM.h


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:01:48