gtsam 4.1.1
gtsam
NonlinearFactorGraph.h
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
20// \callgraph
21
22#pragma once
23
28
29#include <boost/shared_ptr.hpp>
30#include <functional>
31
32namespace gtsam {
33
34 // Forward declarations
35 class Values;
36 class Ordering;
37 class GaussianFactorGraph;
38 class SymbolicFactorGraph;
39 template<typename T>
40 class Expression;
41 template<typename T>
42 class ExpressionFactor;
43
48 struct GTSAM_EXPORT GraphvizFormatting {
49 enum Axis { X, Y, Z, NEGX, NEGY, NEGZ };
54 double scale;
59 std::map<size_t, Point2> factorPositions;
63 paperHorizontalAxis(Y), paperVerticalAxis(X),
64 figureWidthInches(5), figureHeightInches(5), scale(1),
65 mergeSimilarFactors(false), plotFactorPoints(true),
66 connectKeysToFactor(true), binaryEdges(true) {}
67 };
68
69
78 class GTSAM_EXPORT NonlinearFactorGraph: public FactorGraph<NonlinearFactor> {
79
80 public:
81
84 typedef boost::shared_ptr<This> shared_ptr;
85
88
90 template<typename ITERATOR>
91 NonlinearFactorGraph(ITERATOR firstFactor, ITERATOR lastFactor) : Base(firstFactor, lastFactor) {}
92
94 template<class CONTAINER>
95 explicit NonlinearFactorGraph(const CONTAINER& factors) : Base(factors) {}
96
98 template<class DERIVEDFACTOR>
100
103
105 void print(
106 const std::string& str = "NonlinearFactorGraph: ",
107 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override;
108
110 void printErrors(const Values& values, const std::string& str = "NonlinearFactorGraph: ",
111 const KeyFormatter& keyFormatter = DefaultKeyFormatter,
112 const std::function<bool(const Factor* /*factor*/, double /*whitenedError*/, size_t /*index*/)>&
113 printCondition = [](const Factor *,double, size_t) {return true;}) const;
114
116 bool equals(const NonlinearFactorGraph& other, double tol = 1e-9) const;
117
119 void saveGraph(std::ostream& stm, const Values& values = Values(),
120 const GraphvizFormatting& graphvizFormatting = GraphvizFormatting(),
121 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
122
129 void saveGraph(const std::string& file, const Values& values = Values(),
130 const GraphvizFormatting& graphvizFormatting = GraphvizFormatting(),
131 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
132
134 double error(const Values& values) const;
135
137 double probPrime(const Values& values) const;
138
142 boost::shared_ptr<SymbolicFactorGraph> symbolic() const;
143
147 Ordering orderingCOLAMD() const;
148
157 Ordering orderingCOLAMDConstrained(const FastMap<Key, int>& constraints) const;
158
160 boost::shared_ptr<GaussianFactorGraph> linearize(const Values& linearizationPoint) const;
161
163 typedef std::function<void(const boost::shared_ptr<HessianFactor>& hessianFactor)> Dampen;
164
172 boost::shared_ptr<HessianFactor> linearizeToHessianFactor(
173 const Values& values, const Dampen& dampen = nullptr) const;
174
183 boost::shared_ptr<HessianFactor> linearizeToHessianFactor(
184 const Values& values, const Ordering& ordering, const Dampen& dampen = nullptr) const;
185
188 Values updateCholesky(const Values& values,
189 const Dampen& dampen = nullptr) const;
190
193 Values updateCholesky(const Values& values, const Ordering& ordering,
194 const Dampen& dampen = nullptr) const;
195
197 NonlinearFactorGraph clone() const;
198
208 NonlinearFactorGraph rekey(const std::map<Key,Key>& rekey_mapping) const;
209
216 template<typename T>
217 void addExpressionFactor(const SharedNoiseModel& R, const T& z,
218 const Expression<T>& h) {
219 push_back(boost::make_shared<ExpressionFactor<T> >(R, z, h));
220 }
221
228 template<typename T>
229 void addPrior(Key key, const T& prior,
230 const SharedNoiseModel& model = nullptr) {
231 emplace_shared<PriorFactor<T>>(key, prior, model);
232 }
233
244 template<typename T>
245 void addPrior(Key key, const T& prior, const Matrix& covariance) {
246 emplace_shared<PriorFactor<T>>(key, prior, covariance);
247 }
248
249 private:
250
255 boost::shared_ptr<HessianFactor> linearizeToHessianFactor(
256 const Values& values, const Scatter& scatter, const Dampen& dampen = nullptr) const;
257
259 friend class boost::serialization::access;
260 template<class ARCHIVE>
261 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
262 ar & boost::serialization::make_nvp("NonlinearFactorGraph",
263 boost::serialization::base_object<Base>(*this));
264 }
265
266 public:
267
268#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V41
270 boost::shared_ptr<HessianFactor> GTSAM_DEPRECATED linearizeToHessianFactor(
271 const Values& values, boost::none_t, const Dampen& dampen = nullptr) const
272 {return linearizeToHessianFactor(values, dampen);}
273
275 Values GTSAM_DEPRECATED updateCholesky(const Values& values, boost::none_t,
276 const Dampen& dampen = nullptr) const
277 {return updateCholesky(values, dampen);}
278#endif
279
280 };
281
283template<>
284struct traits<NonlinearFactorGraph> : public Testable<NonlinearFactorGraph> {
285};
286
287} //\ namespace gtsam
288
2D Point
Factor Graph Base Class.
Non-linear factor base classes.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:112
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:736
gtsam::enable_if_t< needs_eigen_aligned_allocator< T >::value, boost::shared_ptr< T > > make_shared(Args &&... args)
Add our own make_shared as a layer of wrapping on boost::make_shared This solves the problem with the...
Definition: make_shared.h:57
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:69
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
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:151
A factor graph is a bipartite graph with factor nodes connected to variable nodes.
Definition: FactorGraph.h:93
This is the base class for all factor types.
Definition: Factor.h:56
Definition: Ordering.h:34
Scatter is an intermediate data structure used when building a HessianFactor incrementally,...
Definition: Scatter.h:49
Factor that supports arbitrary expressions via AD.
Definition: ExpressionFactor.h:44
Expression class that supports automatic differentiation.
Definition: Expression.h:48
Formatting options when saving in GraphViz format using NonlinearFactorGraph::saveGraph.
Definition: NonlinearFactorGraph.h:48
bool binaryEdges
just use non-dotted edges for binary factors
Definition: NonlinearFactorGraph.h:58
Axis paperVerticalAxis
The world axis assigned to the vertical paper axis.
Definition: NonlinearFactorGraph.h:51
GraphvizFormatting()
Default constructor sets up robot coordinates.
Definition: NonlinearFactorGraph.h:62
bool plotFactorPoints
Plots each factor as a dot between the variables.
Definition: NonlinearFactorGraph.h:56
bool mergeSimilarFactors
Merge multiple factors that have the same connectivity.
Definition: NonlinearFactorGraph.h:55
double scale
Scale all positions to reduce / increase density.
Definition: NonlinearFactorGraph.h:54
std::map< size_t, Point2 > factorPositions
(optional for each factor) Manually specify factor "dot" positions.
Definition: NonlinearFactorGraph.h:59
double figureWidthInches
The figure width on paper in inches.
Definition: NonlinearFactorGraph.h:52
Axis paperHorizontalAxis
The world axis assigned to the horizontal paper axis.
Definition: NonlinearFactorGraph.h:50
bool connectKeysToFactor
Draw a line from each key within a factor to the dot of the factor.
Definition: NonlinearFactorGraph.h:57
double figureHeightInches
The figure height on paper in inches.
Definition: NonlinearFactorGraph.h:53
A non-linear factor graph is a graph of non-Gaussian, i.e.
Definition: NonlinearFactorGraph.h:78
NonlinearFactorGraph()
Default constructor.
Definition: NonlinearFactorGraph.h:87
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
Convenience method which adds a PriorFactor to the factor graph.
Definition: NonlinearFactorGraph.h:229
NonlinearFactorGraph(const CONTAINER &factors)
Construct from container of factors (shared_ptr or plain objects)
Definition: NonlinearFactorGraph.h:95
void addExpressionFactor(const SharedNoiseModel &R, const T &z, const Expression< T > &h)
Directly add ExpressionFactor that implements |h(x)-z|^2_R.
Definition: NonlinearFactorGraph.h:217
void addPrior(Key key, const T &prior, const Matrix &covariance)
Convenience method which adds a PriorFactor to the factor graph.
Definition: NonlinearFactorGraph.h:245
NonlinearFactorGraph(ITERATOR firstFactor, ITERATOR lastFactor)
Construct from iterator over factors.
Definition: NonlinearFactorGraph.h:91
std::function< void(const boost::shared_ptr< HessianFactor > &hessianFactor)> Dampen
typdef for dampen functions used below
Definition: NonlinearFactorGraph.h:163
NonlinearFactorGraph(const FactorGraph< DERIVEDFACTOR > &graph)
Implicit copy/downcast constructor to override explicit template container constructor.
Definition: NonlinearFactorGraph.h:99
virtual ~NonlinearFactorGraph()
Destructor.
Definition: NonlinearFactorGraph.h:102
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:63