Marginals.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 
19 #include <gtsam/base/timing.h>
23 
24 using namespace std;
25 
26 namespace gtsam {
27 
28 /* ************************************************************************* */
29 Marginals::Marginals(const NonlinearFactorGraph& graph, const Values& solution, Factorization factorization)
30  : values_(solution), factorization_(factorization) {
31  gttic(MarginalsConstructor);
32  graph_ = *graph.linearize(solution);
34 }
35 
36 /* ************************************************************************* */
38  Factorization factorization)
39  : values_(solution), factorization_(factorization) {
40  gttic(MarginalsConstructor);
41  graph_ = *graph.linearize(solution);
43 }
44 
45 /* ************************************************************************* */
46 Marginals::Marginals(const GaussianFactorGraph& graph, const Values& solution, Factorization factorization)
47  : graph_(graph), values_(solution), factorization_(factorization) {
48  gttic(MarginalsConstructor);
50 }
51 
52 /* ************************************************************************* */
54  Factorization factorization)
55  : graph_(graph), values_(solution), factorization_(factorization) {
56  gttic(MarginalsConstructor);
58 }
59 
60 /* ************************************************************************* */
62  : graph_(graph), factorization_(factorization) {
63  gttic(MarginalsConstructor);
64  for (const auto& keyValue: solution) {
65  values_.insert(keyValue.first, keyValue.second);
66  }
68 }
69 
70 /* ************************************************************************* */
72  Factorization factorization)
73  : graph_(graph), factorization_(factorization) {
74  gttic(MarginalsConstructor);
75  for (const auto& keyValue: solution) {
76  values_.insert(keyValue.first, keyValue.second);
77  }
79 }
80 
81 /* ************************************************************************* */
83  // The default ordering to use.
84  const Ordering::OrderingType defaultOrderingType = Ordering::COLAMD;
85  // Compute BayesTree
86  if (factorization_ == CHOLESKY)
87  bayesTree_ = *graph_.eliminateMultifrontal(defaultOrderingType,
89  else if (factorization_ == QR)
90  bayesTree_ =
91  *graph_.eliminateMultifrontal(defaultOrderingType, EliminateQR);
92 }
93 
94 /* ************************************************************************* */
96  // Compute BayesTree
99  else if(factorization_ == QR)
101 }
102 
103 /* ************************************************************************* */
104 void Marginals::print(const std::string& str, const KeyFormatter& keyFormatter) const
105 {
106  graph_.print(str+"Graph: ");
107  values_.print(str+"Solution: ", keyFormatter);
108  bayesTree_.print(str+"Bayes Tree: ");
109 }
110 
111 /* ************************************************************************* */
114 
115  // Compute marginal factor
116  if(factorization_ == CHOLESKY)
118  else if(factorization_ == QR)
119  return bayesTree_.marginalFactor(variable, EliminateQR);
120  else
121  throw std::runtime_error("Marginals::marginalFactor: Unknown factorization");
122 }
123 
124 /* ************************************************************************* */
126 
127  // Get information matrix (only store upper-right triangle)
129  return marginalFactor(variable)->information();
130 }
131 
132 /* ************************************************************************* */
134  return marginalInformation(variable).inverse();
135 }
136 
137 /* ************************************************************************* */
140  info.blockMatrix_.invertInPlace();
141  return info;
142 }
143 
144 /* ************************************************************************* */
146 
147  // If 2 variables, we can use the BayesTree::joint function, otherwise we
148  // have to use sequential elimination.
149  if(variables.size() == 1)
150  {
151  Matrix info = marginalInformation(variables.front());
152  std::vector<size_t> dims;
153  dims.push_back(info.rows());
154  return JointMarginal(info, dims, variables);
155  }
156  else
157  {
158  // Compute joint marginal factor graph.
159  GaussianFactorGraph jointFG;
160  if(variables.size() == 2) {
161  if(factorization_ == CHOLESKY)
162  jointFG = *bayesTree_.joint(variables[0], variables[1], EliminatePreferCholesky);
163  else if(factorization_ == QR)
164  jointFG = *bayesTree_.joint(variables[0], variables[1], EliminateQR);
165  } else {
166  if(factorization_ == CHOLESKY)
168  else if(factorization_ == QR)
170  }
171 
172  // Get information matrix
173  Matrix augmentedInfo = jointFG.augmentedHessian();
174  Matrix info = augmentedInfo.topLeftCorner(augmentedInfo.rows()-1, augmentedInfo.cols()-1);
175 
176  // Information matrix will be returned with sorted keys
177  KeyVector variablesSorted = variables;
178  std::sort(variablesSorted.begin(), variablesSorted.end());
179 
180  // Get dimensions from factor graph
181  std::vector<size_t> dims;
182  dims.reserve(variablesSorted.size());
183  for(const auto& key: variablesSorted) {
184  dims.push_back(values_.at(key).dim());
185  }
186 
187  return JointMarginal(info, dims, variablesSorted);
188  }
189 }
190 
191 /* ************************************************************************* */
193  return bayesTree_.optimize();
194 }
195 
196 /* ************************************************************************* */
197 void JointMarginal::print(const std::string& s, const KeyFormatter& formatter) const {
198  cout << s << "Joint marginal on keys ";
199  bool first = true;
200  for(const auto& key: keys_) {
201  if(!first)
202  cout << ", ";
203  else
204  first = false;
205  cout << formatter(key);
206  }
207  cout << ". Use 'at' or 'operator()' to query matrix blocks." << endl;
208 }
209 
210 } /* namespace gtsam */
timing.h
Timing utilities.
gtsam::Marginals::jointMarginalInformation
JointMarginal jointMarginalInformation(const KeyVector &variables) const
Definition: Marginals.cpp:145
gtsam::JointMarginal::print
void print(const std::string &s="", const KeyFormatter &formatter=DefaultKeyFormatter) const
Definition: Marginals.cpp:197
gtsam::Marginals::Marginals
Marginals()
Default constructor only for wrappers.
Definition: Marginals.h:52
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::Marginals::factorization_
Factorization factorization_
Definition: Marginals.h:46
gtsam::FactorGraph::print
virtual void print(const std::string &s="FactorGraph", const KeyFormatter &formatter=DefaultKeyFormatter) const
Print out graph to std::cout, with optional key formatter.
Definition: FactorGraph-inst.h:37
gtsam::Marginals::marginalInformation
Matrix marginalInformation(Key variable) const
Definition: Marginals.cpp:125
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
HessianFactor.h
Contains the HessianFactor class, a general quadratic factor.
gtsam::Matrix
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
gtsam::BayesTree::marginalFactor
sharedConditional marginalFactor(Key j, const Eliminate &function=EliminationTraitsType::DefaultEliminate) const
Definition: BayesTree-inst.h:309
gtsam::BayesTree::print
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: BayesTree-inst.h:251
gtsam::Marginals::print
void print(const std::string &str="Marginals: ", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: Marginals.cpp:104
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:92
gtsam::Marginals::marginalCovariance
Matrix marginalCovariance(Key variable) const
Definition: Marginals.cpp:133
gtsam::EliminateableFactorGraph::eliminateMultifrontal
std::shared_ptr< BayesTreeType > eliminateMultifrontal(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Definition: EliminateableFactorGraph-inst.h:89
gtsam::Marginals::CHOLESKY
@ CHOLESKY
Definition: Marginals.h:38
gtsam::EliminatePreferCholesky
std::pair< std::shared_ptr< GaussianConditional >, std::shared_ptr< GaussianFactor > > EliminatePreferCholesky(const GaussianFactorGraph &factors, const Ordering &keys)
Definition: HessianFactor.cpp:541
gtsam::Marginals::values_
Values values_
Definition: Marginals.h:45
gtsam::GaussianFactorGraph
Definition: GaussianFactorGraph.h:73
gtsam::Values::at
const ValueType at(Key j) const
Definition: Values-inl.h:261
gtsam::VectorValues
Definition: VectorValues.h:74
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::NonlinearFactorGraph
Definition: NonlinearFactorGraph.h:55
gtsam::GaussianFactor::shared_ptr
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: GaussianFactor.h:42
gtsam::Marginals::jointMarginalCovariance
JointMarginal jointMarginalCovariance(const KeyVector &variables) const
Definition: Marginals.cpp:138
info
else if n * info
Definition: 3rdparty/Eigen/lapack/cholesky.cpp:18
gtsam::Marginals::marginalFactor
GaussianFactor::shared_ptr marginalFactor(Key variable) const
Definition: Marginals.cpp:112
gtsam::GaussianBayesTree::optimize
VectorValues optimize() const
Definition: GaussianBayesTree.cpp:67
ordering
static enum @1096 ordering
str
Definition: pytypes.h:1558
key
const gtsam::Symbol key('X', 0)
gtsam::Marginals::graph_
GaussianFactorGraph graph_
Definition: Marginals.h:44
JacobianFactor.h
gtsam::Ordering::OrderingType
OrderingType
Type of ordering to use.
Definition: inference/Ordering.h:40
gtsam::GaussianFactorGraph::augmentedHessian
Matrix augmentedHessian(const Ordering &ordering) const
Definition: GaussianFactorGraph.cpp:247
gtsam::JointMarginal::keys_
KeyVector keys_
Definition: Marginals.h:141
gtsam::Values::print
void print(const std::string &str="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: Values.cpp:67
gtsam
traits
Definition: SFMdata.h:40
Marginals.h
A class for computing marginals in a NonlinearFactorGraph.
gtsam::Values
Definition: Values.h:65
std
Definition: BFloat16.h:88
gtsam::Values::insert
void insert(Key j, const Value &val)
Definition: Values.cpp:156
gtsam::Marginals::computeBayesTree
void computeBayesTree()
Definition: Marginals.cpp:82
gtsam::Marginals::optimize
VectorValues optimize() const
Definition: Marginals.cpp:192
gtsam::Marginals::Factorization
Factorization
Definition: Marginals.h:37
gtsam::Marginals::bayesTree_
GaussianBayesTree bayesTree_
Definition: Marginals.h:47
gtsam::Marginals::QR
@ QR
Definition: Marginals.h:39
gtsam::BayesTree::joint
sharedFactorGraph joint(Key j1, Key j2, const Eliminate &function=EliminationTraitsType::DefaultEliminate) const
Definition: BayesTree-inst.h:332
graph
NonlinearFactorGraph graph
Definition: doc/Code/OdometryExample.cpp:2
gtsam::JointMarginal
Definition: Marginals.h:137
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::Ordering
Definition: inference/Ordering.h:33
gtsam::Ordering::COLAMD
@ COLAMD
Definition: inference/Ordering.h:41
gtsam::EliminateableFactorGraph::marginalMultifrontalBayesTree
std::shared_ptr< BayesTreeType > marginalMultifrontalBayesTree(const Ordering &variables, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Definition: EliminateableFactorGraph-inst.h:335
gttic
#define gttic(label)
Definition: timing.h:295
gtsam::EliminateQR
std::pair< GaussianConditional::shared_ptr, JacobianFactor::shared_ptr > EliminateQR(const GaussianFactorGraph &factors, const Ordering &keys)
Definition: JacobianFactor.cpp:779


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