gtsam
Loading...
Searching...
No Matches
NonlinearFactor.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
19
20// \callgraph
21
22#pragma once
23
29#include <gtsam/base/utilities.h>
30
31#if GTSAM_ENABLE_BOOST_SERIALIZATION
32#include <boost/serialization/base_object.hpp>
33#endif
34#include <cstddef>
35
36namespace gtsam {
37
38class NonlinearEqualityConstraints;
40
41/* ************************************************************************* */
42
51#define OptionalNone static_cast<gtsam::Matrix*>(nullptr)
52
57using OptionalMatrixType = Matrix*;
58
63using OptionalMatrixVecType = std::vector<Matrix>*;
64
70class GTSAM_EXPORT NonlinearFactor: public Factor {
71
72protected:
73
74 // Some handy typedefs
75 typedef Factor Base;
76 typedef NonlinearFactor This;
77
78public:
79
80 typedef std::shared_ptr<This> shared_ptr;
81
84
87
91 template<typename CONTAINER>
92 NonlinearFactor(const CONTAINER& keys) :
93 Base(keys) {}
94
98
100 void print(const std::string& s = "", const KeyFormatter& keyFormatter =
101 DefaultKeyFormatter) const override;
102
104 virtual bool equals(const NonlinearFactor& f, double tol = 1e-9) const;
105
109
122 virtual double error(const Values& c) const;
123
128 double error(const HybridValues& c) const override;
129
131 virtual size_t dim() const = 0;
132
143 virtual bool active(const Values& c) const { return true; }
144
146 virtual std::shared_ptr<GaussianFactor>
147 linearize(const Values& c) const = 0;
148
154 virtual void qcqpFactors(NonlinearFactorGraph* costs,
155 NonlinearEqualityConstraints* constraints,
156 size_t columnDimension = 1) const;
157
164 virtual shared_ptr clone() const {
165 // TODO: choose better exception to throw here
166 throw std::runtime_error("NonlinearFactor::clone(): Attempting to clone factor with no clone() implemented!");
167 return shared_ptr();
168 }
169
175 virtual shared_ptr rekey(const std::map<Key,Key>& rekey_mapping) const;
176
181 virtual shared_ptr rekey(const KeyVector& new_keys) const;
182
187 virtual bool sendable() const {
188 return true;
189 }
190
191}; // \class NonlinearFactor
192
194template<> struct traits<NonlinearFactor> : public Testable<NonlinearFactor> {
195};
196
197/* ************************************************************************* */
208class GTSAM_EXPORT NoiseModelFactor: public NonlinearFactor {
209
210protected:
211
212 // handy typedefs
213 typedef NonlinearFactor Base;
214 typedef NoiseModelFactor This;
215
216 SharedNoiseModel noiseModel_;
217
218public:
219
220 typedef std::shared_ptr<This> shared_ptr;
221
224
226 ~NoiseModelFactor() override {}
227
231 template<typename CONTAINER>
233 Base(keys), noiseModel_(noiseModel) {}
234
235protected:
236
241
242public:
244 void print(const std::string& s = "",
245 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override;
246
248 bool equals(const NonlinearFactor& f, double tol = 1e-9) const override;
249
251 size_t dim() const override {
252 if (!noiseModel_)
253 throw std::runtime_error("NoiseModelFactor::dim(): no noise model set");
254 return noiseModel_->dim();
255 }
256
259 return noiseModel_;
260 }
261
268 virtual Vector unwhitenedError(const Values& x, OptionalMatrixVecType H = nullptr) const = 0;
269
276 Vector unwhitenedError(const Values& x, std::vector<Matrix>& H) const {
277 return unwhitenedError(x, &H);
278 }
279
284 Vector whitenedError(const Values& c) const;
285
289 Vector unweightedWhitenedError(const Values& c) const;
290
294 double weight(const Values& c) const;
295
296 using NonlinearFactor::error;
297
304 double error(const Values& c) const override;
305
311 std::shared_ptr<GaussianFactor> linearize(const Values& x) const override;
312
317 shared_ptr cloneWithNewNoiseModel(const SharedNoiseModel newNoise) const;
318
319 private:
320#if GTSAM_ENABLE_BOOST_SERIALIZATION
322 friend class boost::serialization::access;
323 template<class ARCHIVE>
324 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
325 ar & boost::serialization::make_nvp("NonlinearFactor",
326 boost::serialization::base_object<Base>(*this));
327 ar & BOOST_SERIALIZATION_NVP(noiseModel_);
328 }
329#endif
330
331}; // \class NoiseModelFactor
332
333} // namespace gtsam
The base class for all factors.
A non-templated config holding any types of Manifold-group elements.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
Matrix * OptionalMatrixType
This typedef will be used everywhere boost::optional<Matrix&> reference was used previously.
Definition NonlinearFactor.h:57
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
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
std::vector< Matrix > * OptionalMatrixVecType
The OptionalMatrixVecType is a pointer to a vector of matrices.
Definition NonlinearFactor.h:63
noiseModel::Base::shared_ptr SharedNoiseModel
Aliases.
Definition NoiseModel.h:846
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
Template to create a binary predicate.
Definition Testable.h:112
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition Factor.h:143
Factor()
Default constructor for I/O.
Definition Factor.h:94
Nonlinear factor base class.
Definition NonlinearFactor.h:70
virtual size_t dim() const =0
get the dimension of the factor (number of rows on linearization)
virtual std::shared_ptr< GaussianFactor > linearize(const Values &c) const =0
linearize to a GaussianFactor
NonlinearFactor()
Default constructor for I/O only.
Definition NonlinearFactor.h:86
virtual void qcqpFactors(NonlinearFactorGraph *costs, NonlinearEqualityConstraints *constraints, size_t columnDimension=1) const
Add this factor's QCQP cost and constraints over matrix-valued QCQP variables with the given column d...
Definition NonlinearFactor.cpp:37
NonlinearFactor(const CONTAINER &keys)
Constructor from a collection of the keys involved in this factor.
Definition NonlinearFactor.h:92
virtual bool sendable() const
Should the factor be evaluated in the same thread as the caller This is to enable factors that has sh...
Definition NonlinearFactor.h:187
virtual shared_ptr clone() const
Creates a shared_ptr clone of the factor - needs to be specialized to allow for subclasses.
Definition NonlinearFactor.h:164
virtual bool active(const Values &c) const
Checks whether a factor should be used based on a set of values.
Definition NonlinearFactor.h:143
NoiseModelFactor(const SharedNoiseModel &noiseModel, const CONTAINER &keys)
Constructor.
Definition NonlinearFactor.h:232
Vector unwhitenedError(const Values &x, std::vector< Matrix > &H) const
support taking in the actual vector instead of the pointer as well to get access to this version of t...
Definition NonlinearFactor.h:276
~NoiseModelFactor() override
Destructor.
Definition NonlinearFactor.h:226
NoiseModelFactor(const SharedNoiseModel &noiseModel)
Constructor - only for subclasses, as this does not set keys.
Definition NonlinearFactor.h:240
std::shared_ptr< This > shared_ptr
Noise model.
Definition NonlinearFactor.h:220
virtual Vector unwhitenedError(const Values &x, OptionalMatrixVecType H=nullptr) const =0
Error function without the NoiseModel, .
size_t dim() const override
get the dimension of the factor (number of rows on linearization)
Definition NonlinearFactor.h:251
NoiseModelFactor()
Default constructor for I/O only.
Definition NonlinearFactor.h:223
const SharedNoiseModel & noiseModel() const
access to the noise model
Definition NonlinearFactor.h:258
Definition NonlinearFactorGraph.h:57
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65
the error.
In nonlinear factors, the error function returns the negative log-likelihood as a non-linear function...