gtsam/cpp/NonlinearEquality.h

94 lines
2.7 KiB
C
Raw Normal View History

/*
* @file NonlinearEquality.h
* @brief Factor to handle enforced equality between factors
* @author Alex Cunningham
*/
#pragma once
#include <limits>
#include <iostream>
2010-01-14 06:25:03 +08:00
#include "Key.h"
#include "NonlinearFactor.h"
namespace gtsam {
2010-01-11 03:25:19 +08:00
/**
2010-01-14 06:25:03 +08:00
* Template default compare function that assumes a testable T
2010-01-11 03:25:19 +08:00
*/
2010-01-14 06:25:03 +08:00
template<class T>
bool compare(const T& a, const T& b) {return a.equals(b); }
/**
2010-01-11 03:25:19 +08:00
* An equality factor that forces either one variable to a constant,
* or a set of variables to be equal to each other.
* Throws an error at linearization if the constraints are not met.
*/
2010-01-14 06:25:03 +08:00
template<class Config, class Key, class T>
class NonlinearEquality: public NonlinearFactor1<Config, Key, T> {
2010-01-11 03:25:19 +08:00
private:
2010-01-14 06:25:03 +08:00
// feasible value
T feasible_;
2010-01-11 03:25:19 +08:00
public:
/**
2010-01-14 06:25:03 +08:00
* Function that compares two values
2010-01-11 03:25:19 +08:00
*/
2010-01-14 06:25:03 +08:00
bool (*compare_)(const T& a, const T& b);
2010-01-11 03:25:19 +08:00
2010-01-14 06:25:03 +08:00
typedef NonlinearFactor1<Config, Key, T> Base;
2010-01-11 03:25:19 +08:00
/**
2010-01-14 06:25:03 +08:00
* Constructor
2010-01-11 03:25:19 +08:00
*/
2010-01-14 06:25:03 +08:00
NonlinearEquality(const Key& j, const T& feasible, bool (*compare)(const T&, const T&) = compare<T>) :
Base(noiseModel::Constrained::All(dim(feasible)), j), feasible_(feasible), compare_(compare) {
2010-01-11 03:25:19 +08:00
}
2010-01-11 03:25:19 +08:00
void print(const std::string& s = "") const {
2010-01-14 06:25:03 +08:00
std::cout << "Constraint: " << s << " on [" << (std::string)(this->key_) << "]\n";
gtsam::print(feasible_,"Feasible Point");
std::cout << "Variable Dimension: " << dim(feasible_) << std::endl;
2010-01-11 03:25:19 +08:00
}
2010-01-11 03:25:19 +08:00
/** Check if two factors are equal */
bool equals(const Factor<Config>& f, double tol = 1e-9) const {
2010-01-14 06:25:03 +08:00
const NonlinearEquality<Config,Key,T>* p =
dynamic_cast<const NonlinearEquality<Config,Key,T>*> (&f);
2010-01-11 03:25:19 +08:00
if (p == NULL) return false;
2010-01-14 06:25:03 +08:00
if (!Base::equals(*p)) return false;
return compare_(feasible_, p->feasible_);
2010-01-11 03:25:19 +08:00
}
2010-01-11 03:25:19 +08:00
/** error function */
2010-01-14 06:25:03 +08:00
inline Vector evaluateError(const T& xj, boost::optional<Matrix&> H) const {
size_t nj = dim(feasible_);
if (compare_(feasible_,xj)) {
if (H) *H = eye(nj);
return zero(nj); // set error to zero if equal
2010-01-11 03:25:19 +08:00
} else {
2010-01-14 06:25:03 +08:00
if (H) throw std::invalid_argument(
"Linearization point not feasible for " + (std::string)(this->key_) + "!");
return repeat(nj, std::numeric_limits<double>::infinity()); // set error to infinity if not equal
2010-01-11 03:25:19 +08:00
}
}
// Linearize is over-written, because base linearization tries to whiten
virtual boost::shared_ptr<GaussianFactor> linearize(const Config& x) const {
const T& xj = x[this->key_];
Matrix A;
Vector b = - evaluateError(xj, A);
// TODO pass unwhitened + noise model to Gaussian factor
SharedDiagonal model = noiseModel::Constrained::All(b.size());
2010-01-21 02:32:48 +08:00
return GaussianFactor::shared_ptr(new GaussianFactor(this->key_, A, b, model));
}
2010-01-11 03:25:19 +08:00
}; // NonlinearEquality
2010-01-11 03:25:19 +08:00
} // namespace gtsam