29#include <boost/serialization/base_object.hpp>
30#include <boost/assign/list_of.hpp>
34using boost::assign::cref_list_of;
53 typedef boost::shared_ptr<This> shared_ptr;
64 template<
typename CONTAINER>
74 DefaultKeyFormatter)
const override;
94 virtual size_t dim()
const = 0;
109 virtual boost::shared_ptr<GaussianFactor>
120 throw std::runtime_error(
"NonlinearFactor::clone(): Attempting to clone factor with no clone() implemented!");
129 virtual shared_ptr rekey(
const std::map<Key,Key>& rekey_mapping)
const;
135 virtual shared_ptr rekey(
const KeyVector& new_keys)
const;
185 template<
typename CONTAINER>
187 Base(keys), noiseModel_(noiseModel) {}
199 void print(
const std::string& s =
"",
200 const KeyFormatter& keyFormatter = DefaultKeyFormatter)
const override;
206 size_t dim()
const override {
207 return noiseModel_->dim();
222 boost::optional<std::vector<Matrix>&> H = boost::none)
const = 0;
228 Vector whitenedError(
const Values& c)
const;
233 Vector unweightedWhitenedError(
const Values& c)
const;
238 double weight(
const Values& c)
const;
246 double error(
const Values& c)
const override;
253 boost::shared_ptr<GaussianFactor> linearize(
const Values& x)
const override;
263 friend class boost::serialization::access;
264 template<
class ARCHIVE>
265 void serialize(ARCHIVE & ar,
const unsigned int ) {
266 ar & boost::serialization::make_nvp(
"NonlinearFactor",
267 boost::serialization::base_object<Base>(*
this));
268 ar & BOOST_SERIALIZATION_NVP(noiseModel_);
306 inline Key key()
const {
return keys_[0]; }
326 boost::optional<std::vector<Matrix> &> H = boost::none)
const override {
328 const X &x1 = x.
at<X>(
keys_[0]);
335 return Vector::Zero(this->
dim());
350 boost::optional<Matrix &> H = boost::none)
const = 0;
357 template<
class ARCHIVE>
358 void serialize(ARCHIVE & ar,
const unsigned int ) {
359 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
360 boost::serialization::base_object<Base>(*
this));
368template<
class VALUE1,
class VALUE2>
402 inline Key key2()
const {
return keys_[1]; }
408 const X1& x1 = x.
at<X1>(
keys_[0]);
409 const X2& x2 = x.
at<X2>(
keys_[1]);
416 return Vector::Zero(this->
dim());
427 boost::none, boost::optional<Matrix&> H2 = boost::none)
const = 0;
433 template<
class ARCHIVE>
434 void serialize(ARCHIVE & ar,
const unsigned int ) {
435 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
436 boost::serialization::base_object<Base>(*
this));
443template<
class VALUE1,
class VALUE2,
class VALUE3>
479 inline Key key2()
const {
return keys_[1]; }
480 inline Key key3()
const {
return keys_[2]; }
491 return Vector::Zero(this->
dim());
502 boost::optional<Matrix&> H1 = boost::none,
503 boost::optional<Matrix&> H2 = boost::none,
504 boost::optional<Matrix&> H3 = boost::none)
const = 0;
510 template<
class ARCHIVE>
511 void serialize(ARCHIVE & ar,
const unsigned int ) {
512 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
513 boost::serialization::base_object<Base>(*
this));
520template<
class VALUE1,
class VALUE2,
class VALUE3,
class VALUE4>
558 inline Key key2()
const {
return keys_[1]; }
559 inline Key key3()
const {
return keys_[2]; }
560 inline Key key4()
const {
return keys_[3]; }
571 return Vector::Zero(this->
dim());
582 boost::optional<Matrix&> H1 = boost::none,
583 boost::optional<Matrix&> H2 = boost::none,
584 boost::optional<Matrix&> H3 = boost::none,
585 boost::optional<Matrix&> H4 = boost::none)
const = 0;
591 template<
class ARCHIVE>
592 void serialize(ARCHIVE & ar,
const unsigned int ) {
593 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
594 boost::serialization::base_object<Base>(*
this));
601template<
class VALUE1,
class VALUE2,
class VALUE3,
class VALUE4,
class VALUE5>
641 inline Key key2()
const {
return keys_[1]; }
642 inline Key key3()
const {
return keys_[2]; }
643 inline Key key4()
const {
return keys_[3]; }
644 inline Key key5()
const {
return keys_[4]; }
655 return Vector::Zero(this->
dim());
666 boost::optional<Matrix&> H1 = boost::none,
667 boost::optional<Matrix&> H2 = boost::none,
668 boost::optional<Matrix&> H3 = boost::none,
669 boost::optional<Matrix&> H4 = boost::none,
670 boost::optional<Matrix&> H5 = boost::none)
const = 0;
676 template<
class ARCHIVE>
677 void serialize(ARCHIVE & ar,
const unsigned int ) {
678 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
679 boost::serialization::base_object<Base>(*
this));
686template<
class VALUE1,
class VALUE2,
class VALUE3,
class VALUE4,
class VALUE5,
class VALUE6>
728 inline Key key2()
const {
return keys_[1]; }
729 inline Key key3()
const {
return keys_[2]; }
730 inline Key key4()
const {
return keys_[3]; }
731 inline Key key5()
const {
return keys_[4]; }
732 inline Key key6()
const {
return keys_[5]; }
739 return evaluateError(x.
at<X1>(
keys_[0]), x.
at<X2>(
keys_[1]), x.
at<X3>(
keys_[2]), x.
at<X4>(
keys_[3]), x.
at<X5>(
keys_[4]), x.
at<X6>(
keys_[5]), (*H)[0], (*H)[1], (*H)[2], (*H)[3], (*H)[4], (*H)[5]);
743 return Vector::Zero(this->
dim());
753 evaluateError(
const X1&,
const X2&,
const X3&,
const X4&,
const X5&,
const X6&,
754 boost::optional<Matrix&> H1 = boost::none,
755 boost::optional<Matrix&> H2 = boost::none,
756 boost::optional<Matrix&> H3 = boost::none,
757 boost::optional<Matrix&> H4 = boost::none,
758 boost::optional<Matrix&> H5 = boost::none,
759 boost::optional<Matrix&> H6 = boost::none)
const = 0;
765 template<
class ARCHIVE>
766 void serialize(ARCHIVE & ar,
const unsigned int ) {
767 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
768 boost::serialization::base_object<Base>(*
this));
Special class for optional Jacobian arguments.
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
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
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
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
Template to create a binary predicate.
Definition: Testable.h:111
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:151
This is the base class for all factor types.
Definition: Factor.h:56
KeyVector keys_
The keys involved in this factor.
Definition: Factor.h:73
Nonlinear factor base class.
Definition: NonlinearFactor.h:43
virtual boost::shared_ptr< GaussianFactor > linearize(const Values &c) const =0
linearize to a GaussianFactor
virtual size_t dim() const =0
get the dimension of the factor (number of rows on linearization)
NonlinearFactor()
Default constructor for I/O only.
Definition: NonlinearFactor.h:59
virtual bool active(const Values &) const
Checks whether a factor should be used based on a set of values.
Definition: NonlinearFactor.h:106
NonlinearFactor(const CONTAINER &keys)
Constructor from a collection of the keys involved in this factor.
Definition: NonlinearFactor.h:65
virtual double error(const Values &c) const =0
Calculate the error of the factor This is typically equal to log-likelihood, e.g.
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:141
virtual shared_ptr clone() const
Creates a shared_ptr clone of the factor - needs to be specialized to allow for subclasses.
Definition: NonlinearFactor.h:118
virtual ~NonlinearFactor()
Destructor.
Definition: NonlinearFactor.h:83
A nonlinear sum-of-squares factor with a zero-mean noise model implementing the density Templated on...
Definition: NonlinearFactor.h:162
NoiseModelFactor(const SharedNoiseModel &noiseModel, const CONTAINER &keys)
Constructor.
Definition: NonlinearFactor.h:186
~NoiseModelFactor() override
Destructor.
Definition: NonlinearFactor.h:180
NoiseModelFactor(const SharedNoiseModel &noiseModel)
Constructor - only for subclasses, as this does not set keys.
Definition: NonlinearFactor.h:194
boost::shared_ptr< This > shared_ptr
Noise model.
Definition: NonlinearFactor.h:174
size_t dim() const override
get the dimension of the factor (number of rows on linearization)
Definition: NonlinearFactor.h:206
NoiseModelFactor()
Default constructor for I/O only.
Definition: NonlinearFactor.h:177
const SharedNoiseModel & noiseModel() const
access to the noise model
Definition: NonlinearFactor.h:211
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const =0
Error function without the NoiseModel, .
A convenient base class for creating your own NoiseModelFactor with 1 variable.
Definition: NonlinearFactor.h:285
virtual Vector evaluateError(const X &x, boost::optional< Matrix & > H=boost::none) const =0
Override this method to finish implementing a unary factor.
NoiseModelFactor1(const SharedNoiseModel &noiseModel, Key key1)
Constructor.
Definition: NonlinearFactor.h:313
NoiseModelFactor1()
Default constructor for I/O only.
Definition: NonlinearFactor.h:302
friend class boost::serialization::access
Serialization function.
Definition: NonlinearFactor.h:356
Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const override
Calls the 1-key specific version of evaluateError below, which is pure virtual so must be implemented...
Definition: NonlinearFactor.h:324
A convenient base class for creating your own NoiseModelFactor with 2 variables.
Definition: NonlinearFactor.h:369
NoiseModelFactor2()
Default Constructor for I/O.
Definition: NonlinearFactor.h:387
NoiseModelFactor2(const SharedNoiseModel &noiseModel, Key j1, Key j2)
Constructor.
Definition: NonlinearFactor.h:395
Key key1() const
methods to retrieve both keys
Definition: NonlinearFactor.h:401
virtual Vector evaluateError(const X1 &, const X2 &, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const =0
Override this method to finish implementing a binary factor.
Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const override
Calls the 2-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:406
friend class boost::serialization::access
Serialization function.
Definition: NonlinearFactor.h:432
A convenient base class for creating your own NoiseModelFactor with 3 variables.
Definition: NonlinearFactor.h:444
NoiseModelFactor3()
Default Constructor for I/O.
Definition: NonlinearFactor.h:463
virtual Vector evaluateError(const X1 &, const X2 &, const X3 &, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none, boost::optional< Matrix & > H3=boost::none) const =0
Override this method to finish implementing a trinary factor.
Key key1() const
methods to retrieve keys
Definition: NonlinearFactor.h:478
NoiseModelFactor3(const SharedNoiseModel &noiseModel, Key j1, Key j2, Key j3)
Constructor.
Definition: NonlinearFactor.h:472
friend class boost::serialization::access
Serialization function.
Definition: NonlinearFactor.h:509
Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const override
Calls the 3-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:484
A convenient base class for creating your own NoiseModelFactor with 4 variables.
Definition: NonlinearFactor.h:521
virtual Vector evaluateError(const X1 &, const X2 &, const X3 &, const X4 &, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none, boost::optional< Matrix & > H3=boost::none, boost::optional< Matrix & > H4=boost::none) const =0
Override this method to finish implementing a 4-way factor.
NoiseModelFactor4()
Default Constructor for I/O.
Definition: NonlinearFactor.h:541
Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const override
Calls the 4-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:564
NoiseModelFactor4(const SharedNoiseModel &noiseModel, Key j1, Key j2, Key j3, Key j4)
Constructor.
Definition: NonlinearFactor.h:551
friend class boost::serialization::access
Serialization function.
Definition: NonlinearFactor.h:590
Key key1() const
methods to retrieve keys
Definition: NonlinearFactor.h:557
A convenient base class for creating your own NoiseModelFactor with 5 variables.
Definition: NonlinearFactor.h:602
NoiseModelFactor5(const SharedNoiseModel &noiseModel, Key j1, Key j2, Key j3, Key j4, Key j5)
Constructor.
Definition: NonlinearFactor.h:634
NoiseModelFactor5()
Default Constructor for I/O.
Definition: NonlinearFactor.h:623
Key key1() const
methods to retrieve keys
Definition: NonlinearFactor.h:640
Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const override
Calls the 5-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:648
friend class boost::serialization::access
Serialization function.
Definition: NonlinearFactor.h:675
virtual Vector evaluateError(const X1 &, const X2 &, const X3 &, const X4 &, const X5 &, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none, boost::optional< Matrix & > H3=boost::none, boost::optional< Matrix & > H4=boost::none, boost::optional< Matrix & > H5=boost::none) const =0
Override this method to finish implementing a 5-way factor.
A convenient base class for creating your own NoiseModelFactor with 6 variables.
Definition: NonlinearFactor.h:687
NoiseModelFactor6()
Default Constructor for I/O.
Definition: NonlinearFactor.h:709
Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const override
Calls the 6-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:736
virtual Vector evaluateError(const X1 &, const X2 &, const X3 &, const X4 &, const X5 &, const X6 &, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none, boost::optional< Matrix & > H3=boost::none, boost::optional< Matrix & > H4=boost::none, boost::optional< Matrix & > H5=boost::none, boost::optional< Matrix & > H6=boost::none) const =0
Override this method to finish implementing a 6-way factor.
friend class boost::serialization::access
Serialization function.
Definition: NonlinearFactor.h:764
NoiseModelFactor6(const SharedNoiseModel &noiseModel, Key j1, Key j2, Key j3, Key j4, Key j5, Key j6)
Constructor.
Definition: NonlinearFactor.h:721
Key key1() const
methods to retrieve keys
Definition: NonlinearFactor.h:727
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:63
const ValueType at(Key j) const
Retrieve a variable by key j.
Definition: Values-inl.h:346