gtsam 4.1.1
gtsam
NonlinearEquality.h
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
12/*
13 * @file NonlinearEquality.h
14 * @brief Factor to handle enforced equality between factors
15 * @author Alex Cunningham
16 */
17
18#pragma once
19
21#include <gtsam/base/Testable.h>
22#include <gtsam/base/Manifold.h>
23
24#include <boost/bind/bind.hpp>
25
26#include <limits>
27#include <iostream>
28#include <cmath>
29
30namespace gtsam {
31
44template<class VALUE>
46
47public:
48 typedef VALUE T;
49
50private:
51
52 // feasible value
53 T feasible_;
54
55 // error handling flag
56 bool allow_error_;
57
58 // error gain in allow error case
59 double error_gain_;
60
61 // typedef to this class
63
64 // typedef to base class
66
67public:
68
70 using CompareFunction = std::function<bool(const T&, const T&)>;
71 CompareFunction compare_;
72
75 }
76
77 ~NonlinearEquality() override {
78 }
79
82
86 NonlinearEquality(Key j, const T& feasible,
87 const CompareFunction &_compare = std::bind(traits<T>::Equals,
88 std::placeholders::_1, std::placeholders::_2, 1e-9)) :
89 Base(noiseModel::Constrained::All(traits<T>::GetDimension(feasible)),
90 j), feasible_(feasible), allow_error_(false), error_gain_(0.0), //
91 compare_(_compare) {
92 }
93
97 NonlinearEquality(Key j, const T& feasible, double error_gain,
98 const CompareFunction &_compare = std::bind(traits<T>::Equals,
99 std::placeholders::_1, std::placeholders::_2, 1e-9)) :
100 Base(noiseModel::Constrained::All(traits<T>::GetDimension(feasible)),
101 j), feasible_(feasible), allow_error_(true), error_gain_(error_gain), //
102 compare_(_compare) {
103 }
104
108
109 void print(const std::string& s = "",
110 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
111 std::cout << (s.empty() ? s : s + " ") << "Constraint: on ["
112 << keyFormatter(this->key()) << "]\n";
113 traits<VALUE>::Print(feasible_, "Feasible Point:\n");
114 std::cout << "Variable Dimension: " << traits<T>::GetDimension(feasible_)
115 << std::endl;
116 }
117
119 bool equals(const NonlinearFactor& f, double tol = 1e-9) const override {
120 const This* e = dynamic_cast<const This*>(&f);
121 return e && Base::equals(f) && traits<T>::Equals(feasible_,e->feasible_, tol)
122 && std::abs(error_gain_ - e->error_gain_) < tol;
123 }
124
128
130 double error(const Values& c) const override {
131 const T& xj = c.at<T>(this->key());
132 Vector e = this->unwhitenedError(c);
133 if (allow_error_ || !compare_(xj, feasible_)) {
134 return error_gain_ * dot(e, e);
135 } else {
136 return 0.0;
137 }
138 }
139
141 Vector evaluateError(const T& xj,
142 boost::optional<Matrix&> H = boost::none) const override {
143 const size_t nj = traits<T>::GetDimension(feasible_);
144 if (allow_error_) {
145 if (H)
146 *H = Matrix::Identity(nj,nj); // FIXME: this is not the right linearization for nonlinear compare
147 return traits<T>::Local(xj,feasible_);
148 } else if (compare_(feasible_, xj)) {
149 if (H)
150 *H = Matrix::Identity(nj,nj);
151 return Vector::Zero(nj); // set error to zero if equal
152 } else {
153 if (H)
154 throw std::invalid_argument(
155 "Linearization point not feasible for "
156 + DefaultKeyFormatter(this->key()) + "!");
157 return Vector::Constant(nj, std::numeric_limits<double>::infinity()); // set error to infinity if not equal
158 }
159 }
160
162 GaussianFactor::shared_ptr linearize(const Values& x) const override {
163 const T& xj = x.at<T>(this->key());
164 Matrix A;
165 Vector b = evaluateError(xj, A);
166 SharedDiagonal model = noiseModel::Constrained::All(b.size());
168 new JacobianFactor(this->key(), A, b, model));
169 }
170
172 gtsam::NonlinearFactor::shared_ptr clone() const override {
173 return boost::static_pointer_cast<gtsam::NonlinearFactor>(
174 gtsam::NonlinearFactor::shared_ptr(new This(*this)));
175 }
176
178
180
181private:
182
185 template<class ARCHIVE>
186 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
187 ar
188 & boost::serialization::make_nvp("NoiseModelFactor1",
189 boost::serialization::base_object<Base>(*this));
190 ar & BOOST_SERIALIZATION_NVP(feasible_);
191 ar & BOOST_SERIALIZATION_NVP(allow_error_);
192 ar & BOOST_SERIALIZATION_NVP(error_gain_);
193 }
194
195};
196// \class NonlinearEquality
197
198template <typename VALUE>
199struct traits<NonlinearEquality<VALUE>> : Testable<NonlinearEquality<VALUE>> {};
200
201/* ************************************************************************* */
205template<class VALUE>
207
208public:
209 typedef VALUE X;
210
211protected:
214
217 }
218
219 X value_;
220
221 GTSAM_CONCEPT_MANIFOLD_TYPE(X)
222 GTSAM_CONCEPT_TESTABLE_TYPE(X)
223
224public:
225
226 typedef boost::shared_ptr<NonlinearEquality1<VALUE> > shared_ptr;
227
234 NonlinearEquality1(const X& value, Key key, double mu = 1000.0)
235 : Base(noiseModel::Constrained::All(traits<X>::GetDimension(value),
236 std::abs(mu)),
237 key),
238 value_(value) {}
239
240 ~NonlinearEquality1() override {
241 }
242
244 gtsam::NonlinearFactor::shared_ptr clone() const override {
245 return boost::static_pointer_cast<gtsam::NonlinearFactor>(
246 gtsam::NonlinearFactor::shared_ptr(new This(*this)));
247 }
248
250 Vector evaluateError(const X& x1,
251 boost::optional<Matrix&> H = boost::none) const override {
252 if (H)
253 (*H) = Matrix::Identity(traits<X>::GetDimension(x1),traits<X>::GetDimension(x1));
254 // manifold equivalent of h(x)-z -> log(z,h(x))
255 return traits<X>::Local(value_,x1);
256 }
257
259 void print(const std::string& s = "",
260 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
261 std::cout << s << ": NonlinearEquality1(" << keyFormatter(this->key())
262 << ")," << "\n";
263 this->noiseModel_->print();
264 traits<X>::Print(value_, "Value");
265 }
266
268
269private:
270
273 template<class ARCHIVE>
274 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
275 ar
276 & boost::serialization::make_nvp("NoiseModelFactor1",
277 boost::serialization::base_object<Base>(*this));
278 ar & BOOST_SERIALIZATION_NVP(value_);
279 }
280};
281// \NonlinearEquality1
282
283template <typename VALUE>
285 : Testable<NonlinearEquality1<VALUE> > {};
286
287/* ************************************************************************* */
292template <class T>
294 protected:
297
298 GTSAM_CONCEPT_MANIFOLD_TYPE(T)
299
300
302
303 public:
304 typedef boost::shared_ptr<NonlinearEquality2<T>> shared_ptr;
305
312 NonlinearEquality2(Key key1, Key key2, double mu = 1e4)
313 : Base(noiseModel::Constrained::All(traits<T>::dimension, std::abs(mu)),
314 key1, key2) {}
315 ~NonlinearEquality2() override {}
316
318 gtsam::NonlinearFactor::shared_ptr clone() const override {
319 return boost::static_pointer_cast<gtsam::NonlinearFactor>(
320 gtsam::NonlinearFactor::shared_ptr(new This(*this)));
321 }
322
325 const T& x1, const T& x2, boost::optional<Matrix&> H1 = boost::none,
326 boost::optional<Matrix&> H2 = boost::none) const override {
327 static const size_t p = traits<T>::dimension;
328 if (H1) *H1 = -Matrix::Identity(p, p);
329 if (H2) *H2 = Matrix::Identity(p, p);
330 return traits<T>::Local(x1, x2);
331 }
332
334
335 private:
338 template <class ARCHIVE>
339 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
340 ar& boost::serialization::make_nvp(
341 "NoiseModelFactor2", boost::serialization::base_object<Base>(*this));
342 }
343};
344// \NonlinearEquality2
345
346template <typename VALUE>
347struct traits<NonlinearEquality2<VALUE>> : Testable<NonlinearEquality2<VALUE>> {
348};
349
350}// namespace gtsam
#define GTSAM_MAKE_ALIGNED_OPERATOR_NEW
This marks a GTSAM object to require alignment.
Definition: types.h:277
Concept check for values that can be used in unit tests.
Base class and basic functions for Manifold types.
Non-linear factor base classes.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
double dot(const V1 &a, const V2 &b)
Dot product.
Definition: Vector.h:194
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
This is the base class for all factor types.
Definition: Factor.h:56
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: GaussianFactor.h:42
A Gaussian factor in the squared-error form.
Definition: JacobianFactor.h:91
static shared_ptr All(size_t dim)
Fully constrained variations.
Definition: NoiseModel.h:471
An equality factor that forces either one variable to a constant, or a set of variables to be equal t...
Definition: NonlinearEquality.h:45
double error(const Values &c) const override
Actual error function calculation.
Definition: NonlinearEquality.h:130
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
print
Definition: NonlinearEquality.h:109
std::function< bool(const T &, const T &)> CompareFunction
Function that compares two values.
Definition: NonlinearEquality.h:70
NonlinearEquality()
Default constructor - only for serialization.
Definition: NonlinearEquality.h:74
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
Check if two factors are equal.
Definition: NonlinearEquality.h:119
NonlinearEquality(Key j, const T &feasible, double error_gain, const CompareFunction &_compare=std::bind(traits< T >::Equals, std::placeholders::_1, std::placeholders::_2, 1e-9))
Constructor - allows inexact evaluation.
Definition: NonlinearEquality.h:97
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition: NonlinearEquality.h:172
Vector evaluateError(const T &xj, boost::optional< Matrix & > H=boost::none) const override
Error function.
Definition: NonlinearEquality.h:141
friend class boost::serialization::access
Serialization function.
Definition: NonlinearEquality.h:184
NonlinearEquality(Key j, const T &feasible, const CompareFunction &_compare=std::bind(traits< T >::Equals, std::placeholders::_1, std::placeholders::_2, 1e-9))
Constructor - forces exact evaluation.
Definition: NonlinearEquality.h:86
GaussianFactor::shared_ptr linearize(const Values &x) const override
Linearize is over-written, because base linearization tries to whiten.
Definition: NonlinearEquality.h:162
Simple unary equality constraint - fixes a value for a variable.
Definition: NonlinearEquality.h:206
Vector evaluateError(const X &x1, boost::optional< Matrix & > H=boost::none) const override
g(x) with optional derivative
Definition: NonlinearEquality.h:250
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition: NonlinearEquality.h:244
boost::shared_ptr< NonlinearEquality1< VALUE > > shared_ptr
fixed value for variable
Definition: NonlinearEquality.h:226
NonlinearEquality1(const X &value, Key key, double mu=1000.0)
Constructor.
Definition: NonlinearEquality.h:234
NonlinearEquality1()
Default constructor to allow for serialization.
Definition: NonlinearEquality.h:216
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Print.
Definition: NonlinearEquality.h:259
friend class boost::serialization::access
Serialization function.
Definition: NonlinearEquality.h:272
Simple binary equality constraint - this constraint forces two variables to be the same.
Definition: NonlinearEquality.h:293
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition: NonlinearEquality.h:318
Vector evaluateError(const T &x1, const T &x2, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const override
g(x) with optional derivative2
Definition: NonlinearEquality.h:324
NonlinearEquality2(Key key1, Key key2, double mu=1e4)
Constructor.
Definition: NonlinearEquality.h:312
friend class boost::serialization::access
Serialization function.
Definition: NonlinearEquality.h:337
Nonlinear factor base class.
Definition: NonlinearFactor.h:43
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
Check if two factors are equal.
Definition: NonlinearFactor.cpp:71
const SharedNoiseModel & noiseModel() const
access to the noise model
Definition: NonlinearFactor.h:211
A convenient base class for creating your own NoiseModelFactor with 1 variable.
Definition: NonlinearFactor.h:285
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
Key key1() const
methods to retrieve both keys
Definition: NonlinearFactor.h:401
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