23 #include <boost/serialization/base_object.hpp>
24 #include <boost/assign/list_of.hpp>
36 #define ADD_CLONE_NONLINEAR_FACTOR(Derived) \
37 virtual gtsam::NonlinearFactor::shared_ptr clone() const { \
38 return boost::static_pointer_cast<gtsam::NonlinearFactor>( \
39 gtsam::NonlinearFactor::shared_ptr(new Derived(*this))); }
43 using boost::assign::cref_list_of;
64 typedef boost::shared_ptr<This> shared_ptr;
75 template<
typename CONTAINER>
84 virtual void print(
const std::string& s =
"",
85 const KeyFormatter& keyFormatter = DefaultKeyFormatter)
const
87 std::cout << s <<
" keys = { ";
88 BOOST_FOREACH(
Key key, this->
keys()) { std::cout << keyFormatter(key) <<
" "; }
89 std::cout <<
"}" << std::endl;
113 virtual size_t dim()
const = 0;
128 virtual boost::shared_ptr<GaussianFactor>
150 throw std::runtime_error(
"NonlinearFactor::clone(): Attempting to clone factor with no clone() implemented!");
158 shared_ptr
rekey(
const std::map<Key,Key>& rekey_mapping)
const {
159 shared_ptr new_factor =
clone();
160 for (
size_t i=0; i<new_factor->size(); ++i) {
161 Key& cur_key = new_factor->keys()[i];
162 std::map<Key,Key>::const_iterator mapping = rekey_mapping.find(cur_key);
163 if (mapping != rekey_mapping.end())
164 cur_key = mapping->second;
173 shared_ptr
rekey(
const std::vector<Key>& new_keys)
const {
174 assert(new_keys.size() == this->
keys().size());
175 shared_ptr new_factor =
clone();
176 new_factor->keys() = new_keys;
217 template<
typename CONTAINER>
219 Base(keys), noiseModel_(noiseModel) {}
231 virtual void print(
const std::string& s =
"",
232 const KeyFormatter& keyFormatter = DefaultKeyFormatter)
const
235 this->noiseModel_->print(
" noise model: ");
242 ((!noiseModel_ && !e->noiseModel_) ||
243 (noiseModel_ && e->noiseModel_ && noiseModel_->equals(*e->noiseModel_, tol)));
247 virtual size_t dim()
const {
248 return noiseModel_->dim();
262 virtual Vector
unwhitenedError(
const Values& x, boost::optional<std::vector<Matrix>&> H = boost::none)
const = 0;
270 if((
size_t) unwhitenedErrorVec.size() != noiseModel_->dim())
271 throw std::invalid_argument(
"This factor was created with a NoiseModel of incorrect dimension.");
272 return noiseModel_->whiten(unwhitenedErrorVec);
284 if((
size_t) unwhitenedErrorVec.size() != noiseModel_->dim())
285 throw std::invalid_argument(
"This factor was created with a NoiseModel of incorrect dimension.");
286 return 0.5 * noiseModel_->distance(unwhitenedErrorVec);
300 return boost::shared_ptr<JacobianFactor>();
305 std::vector<Matrix> A(this->
size());
309 if((
size_t) b.size() != noiseModel_->dim())
310 throw std::invalid_argument(
"This factor was created with a NoiseModel of incorrect dimension.");
312 this->noiseModel_->WhitenSystem(A,b);
315 std::vector<std::pair<Key, Matrix> > terms(this->
size());
317 for(
size_t j=0; j<this->
size(); ++j) {
318 terms[j].first = this->
keys()[j];
319 terms[j].second.swap(A[j]);
325 noiseModel::Constrained::shared_ptr constrained =
340 template<
class ARCHIVE>
341 void serialize(ARCHIVE & ar,
const unsigned int version) {
342 ar & boost::serialization::make_nvp(
"NonlinearFactor",
343 boost::serialization::base_object<Base>(*
this));
344 ar & BOOST_SERIALIZATION_NVP(noiseModel_);
353 template<
class VALUE>
373 inline Key key()
const {
return keys_[0]; }
381 Base(noiseModel, cref_list_of<1>(key1)) {}
404 virtual Vector
evaluateError(
const X& x, boost::optional<Matrix&> H =
405 boost::none)
const = 0;
410 friend class boost::serialization::access;
411 template<
class ARCHIVE>
412 void serialize(ARCHIVE & ar,
const unsigned int version) {
413 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
414 boost::serialization::base_object<Base>(*
this));
422 template<
class VALUE1,
class VALUE2>
450 Base(noiseModel, cref_list_of<2>(j1)(j2)) {}
456 inline Key key2()
const {
return keys_[1]; }
462 const X1& x1 = x.
at<X1>(
keys_[0]);
463 const X2& x2 = x.
at<X2>(
keys_[1]);
480 evaluateError(
const X1&,
const X2&, boost::optional<Matrix&> H1 =
481 boost::none, boost::optional<Matrix&> H2 = boost::none)
const = 0;
486 friend class boost::serialization::access;
487 template<
class ARCHIVE>
488 void serialize(ARCHIVE & ar,
const unsigned int version) {
489 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
490 boost::serialization::base_object<Base>(*
this));
497 template<
class VALUE1,
class VALUE2,
class VALUE3>
527 Base(noiseModel, cref_list_of<3>(j1)(j2)(j3)) {}
533 inline Key key2()
const {
return keys_[1]; }
534 inline Key key3()
const {
return keys_[2]; }
556 boost::optional<Matrix&> H1 = boost::none,
557 boost::optional<Matrix&> H2 = boost::none,
558 boost::optional<Matrix&> H3 = boost::none)
const = 0;
563 friend class boost::serialization::access;
564 template<
class ARCHIVE>
565 void serialize(ARCHIVE & ar,
const unsigned int version) {
566 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
567 boost::serialization::base_object<Base>(*
this));
574 template<
class VALUE1,
class VALUE2,
class VALUE3,
class VALUE4>
606 Base(noiseModel, cref_list_of<4>(j1)(j2)(j3)(j4)) {}
612 inline Key key2()
const {
return keys_[1]; }
613 inline Key key3()
const {
return keys_[2]; }
614 inline Key key4()
const {
return keys_[3]; }
636 boost::optional<Matrix&> H1 = boost::none,
637 boost::optional<Matrix&> H2 = boost::none,
638 boost::optional<Matrix&> H3 = boost::none,
639 boost::optional<Matrix&> H4 = boost::none)
const = 0;
645 template<
class ARCHIVE>
646 void serialize(ARCHIVE & ar,
const unsigned int version) {
647 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
648 boost::serialization::base_object<Base>(*
this));
655 template<
class VALUE1,
class VALUE2,
class VALUE3,
class VALUE4,
class VALUE5>
689 Base(noiseModel, cref_list_of<5>(j1)(j2)(j3)(j4)(j5)) {}
695 inline Key key2()
const {
return keys_[1]; }
696 inline Key key3()
const {
return keys_[2]; }
697 inline Key key4()
const {
return keys_[3]; }
698 inline Key key5()
const {
return keys_[4]; }
719 evaluateError(
const X1&,
const X2&,
const X3&,
const X4&,
const X5&,
720 boost::optional<Matrix&> H1 = boost::none,
721 boost::optional<Matrix&> H2 = boost::none,
722 boost::optional<Matrix&> H3 = boost::none,
723 boost::optional<Matrix&> H4 = boost::none,
724 boost::optional<Matrix&> H5 = boost::none)
const = 0;
730 template<
class ARCHIVE>
731 void serialize(ARCHIVE & ar,
const unsigned int version) {
732 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
733 boost::serialization::base_object<Base>(*
this));
740 template<
class VALUE1,
class VALUE2,
class VALUE3,
class VALUE4,
class VALUE5,
class VALUE6>
776 Base(noiseModel, cref_list_of<6>(j1)(j2)(j3)(j4)(j5)(j6)) {}
782 inline Key key2()
const {
return keys_[1]; }
783 inline Key key3()
const {
return keys_[2]; }
784 inline Key key4()
const {
return keys_[3]; }
785 inline Key key5()
const {
return keys_[4]; }
786 inline Key key6()
const {
return keys_[5]; }
793 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]);
807 evaluateError(
const X1&,
const X2&,
const X3&,
const X4&,
const X5&,
const X6&,
808 boost::optional<Matrix&> H1 = boost::none,
809 boost::optional<Matrix&> H2 = boost::none,
810 boost::optional<Matrix&> H3 = boost::none,
811 boost::optional<Matrix&> H4 = boost::none,
812 boost::optional<Matrix&> H5 = boost::none,
813 boost::optional<Matrix&> H6 = boost::none)
const = 0;
819 template<
class ARCHIVE>
820 void serialize(ARCHIVE & ar,
const unsigned int version) {
821 ar & boost::serialization::make_nvp(
"NoiseModelFactor",
822 boost::serialization::base_object<Base>(*
this));
shared_ptr rekey(const std::vector< Key > &new_keys) const
Clones a factor and fully replaces its keys.
Definition: NonlinearFactor.h:173
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const
Calls the 6-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:790
virtual bool equals(const NonlinearFactor &f, double tol=1e-9) const
Check if two factors are equal.
Definition: NonlinearFactor.h:239
NoiseModelFactor1()
Default constructor for I/O only.
Definition: NonlinearFactor.h:369
virtual double error(const Values &c) const =0
Calculate the error of the factor This is typically equal to log-likelihood, e.g. ...
A nonlinear sum-of-squares factor with a zero-mean noise model implementing the density Templated on...
Definition: NonlinearFactor.h:194
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print
Definition: NonlinearFactor.h:84
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.
virtual size_t dim() const =0
get the dimension of the factor (number of rows on linearization)
Key key1() const
methods to retrieve keys
Definition: NonlinearFactor.h:611
virtual boost::shared_ptr< GaussianFactor > linearize(const Values &c) const =0
linearize to a GaussianFactor
virtual size_t dim() const
get the dimension of the factor (number of rows on linearization)
Definition: NonlinearFactor.h:247
NoiseModelFactor(const SharedNoiseModel &noiseModel, const CONTAINER &keys)
Constructor.
Definition: NonlinearFactor.h:218
Vector whitenedError(const Values &c) const
Vector of errors, whitened This is the raw error, i.e., i.e.
Definition: NonlinearFactor.h:268
friend class boost::serialization::access
Serialization function.
Definition: NonlinearFactor.h:339
NoiseModelFactor1(const SharedNoiseModel &noiseModel, Key key1)
Constructor.
Definition: NonlinearFactor.h:380
virtual Vector evaluateError(const X &x, boost::optional< Matrix & > H=boost::none) const =0
Override this method to finish implementing a unary factor.
NonlinearFactor()
Default constructor for I/O only.
Definition: NonlinearFactor.h:70
NoiseModelFactor4(const SharedNoiseModel &noiseModel, Key j1, Key j2, Key j3, Key j4)
Constructor.
Definition: NonlinearFactor.h:605
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.
FastVector< Key > keys_
The keys involved in this factor.
Definition: Factor.h:69
boost::shared_ptr< GaussianFactor > linearize(const Values &x) const
Linearize a non-linearFactorN to get a GaussianFactor, Hence .
Definition: NonlinearFactor.h:297
A non-templated config holding any types of Manifold-group elements.
A convenient base class for creating your own NoiseModelFactor with 2 variables.
Definition: NonlinearFactor.h:423
A Gaussian factor in the squared-error form.
Definition: JacobianFactor.h:82
const FastVector< Key > & keys() const
Access the factor's involved variable keys.
Definition: Factor.h:115
NoiseModelFactor5()
Default Constructor for I/O.
Definition: NonlinearFactor.h:677
NoiseModelFactor6()
Default Constructor for I/O.
Definition: NonlinearFactor.h:763
This is the base class for all factor types.
Definition: Factor.h:51
NoiseModelFactor6(const SharedNoiseModel &noiseModel, Key j1, Key j2, Key j3, Key j4, Key j5, Key j6)
Constructor.
Definition: NonlinearFactor.h:775
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const
Calls the 1-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:386
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.
NoiseModelFactor4()
Default Constructor for I/O.
Definition: NonlinearFactor.h:595
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:75
bool zero(const Vector &v)
check if all zero
Definition: Vector.cpp:39
virtual ~NoiseModelFactor()
Destructor.
Definition: NonlinearFactor.h:212
NonlinearFactor(const CONTAINER &keys)
Constructor from a collection of the keys involved in this factor.
Definition: NonlinearFactor.h:76
A Constrained constrained model is a specialization of Diagonal which allows some or all of the sigma...
Definition: NoiseModel.h:361
Key key1() const
methods to retrieve both keys
Definition: NonlinearFactor.h:455
const ValueType & at(Key j) const
Retrieve a variable by key j.
Definition: Values-inl.h:219
The base class for all factors.
NoiseModelFactor(const SharedNoiseModel &noiseModel)
Constructor - only for subclasses, as this does not set keys.
Definition: NonlinearFactor.h:226
virtual bool equals(const NonlinearFactor &f, double tol=1e-9) const
Check if two factors are equal.
Definition: NonlinearFactor.h:93
NoiseModelFactor()
Default constructor for I/O only.
Definition: NonlinearFactor.h:209
shared_ptr rekey(const std::map< Key, Key > &rekey_mapping) const
Creates a shared_ptr clone of the factor with different keys using a map from old->new keys...
Definition: NonlinearFactor.h:158
boost::shared_ptr< This > shared_ptr
Noise model.
Definition: NonlinearFactor.h:206
size_t Key
Integer nonlinear key type.
Definition: types.h:59
bool equals(const This &other, double tol=1e-9) const
check equality
Definition: Factor.cpp:43
NoiseModelFactor3()
Default Constructor for I/O.
Definition: NonlinearFactor.h:517
size_t size() const
Definition: Factor.h:126
friend class boost::serialization::access
Serialization function.
Definition: NonlinearFactor.h:729
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: GaussianFactor.h:39
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const =0
Error function without the NoiseModel, .
SharedNoiseModel get_noiseModel() const
access to the noise model
Definition: NonlinearFactor.h:252
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const
Calls the 5-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:702
A convenient base class for creating your own NoiseModelFactor with 1 variable.
Definition: NonlinearFactor.h:354
friend class boost::serialization::access
Serialization function.
Definition: NonlinearFactor.h:818
virtual shared_ptr clone() const
Create a symbolic factor using the given ordering to determine the variable indices.
Definition: NonlinearFactor.h:148
A convenient base class for creating your own NoiseModelFactor with 6 variables.
Definition: NonlinearFactor.h:741
NoiseModelFactor3(const SharedNoiseModel &noiseModel, Key j1, Key j2, Key j3)
Constructor.
Definition: NonlinearFactor.h:526
Key key1() const
methods to retrieve keys
Definition: NonlinearFactor.h:532
A convenient base class for creating your own NoiseModelFactor with 4 variables.
Definition: NonlinearFactor.h:575
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const
Calls the 4-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:618
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const
Calls the 3-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:538
virtual double error(const Values &c) const
Calculate the error of the factor.
Definition: NonlinearFactor.h:281
virtual bool active(const Values &c) const
Checks whether a factor should be used based on a set of values.
Definition: NonlinearFactor.h:125
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.
Key key1() const
methods to retrieve keys
Definition: NonlinearFactor.h:694
virtual ~NonlinearFactor()
Destructor.
Definition: NonlinearFactor.h:102
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Print.
Definition: NonlinearFactor.h:231
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:884
Nonlinear factor base class.
Definition: NonlinearFactor.h:54
friend class boost::serialization::access
Serialization function.
Definition: NonlinearFactor.h:644
NoiseModelFactor2(const SharedNoiseModel &noiseModel, Key j1, Key j2)
Constructor.
Definition: NonlinearFactor.h:449
Key key1() const
methods to retrieve keys
Definition: NonlinearFactor.h:781
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62
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.
virtual Vector unwhitenedError(const Values &x, boost::optional< std::vector< Matrix > & > H=boost::none) const
Calls the 2-key specific version of evaluateError, which is pure virtual so must be implemented in th...
Definition: NonlinearFactor.h:460
A convenient base class for creating your own NoiseModelFactor with 5 variables.
Definition: NonlinearFactor.h:656
NoiseModelFactor2()
Default Constructor for I/O.
Definition: NonlinearFactor.h:441
A convenient base class for creating your own NoiseModelFactor with 3 variables.
Definition: NonlinearFactor.h:498
NoiseModelFactor5(const SharedNoiseModel &noiseModel, Key j1, Key j2, Key j3, Key j4, Key j5)
Constructor.
Definition: NonlinearFactor.h:688