/* ---------------------------------------------------------------------------- * GTSAM Copyright 2010, Georgia Tech Research Corporation, * Atlanta, Georgia 30332-0415 * All Rights Reserved * Authors: Frank Dellaert, et al. (see THANKS for the full author list) * See LICENSE for the license information * -------------------------------------------------------------------------- */ /** * @file testBAD.cpp * @date September 18, 2014 * @author Frank Dellaert * @brief unit tests for Block Automatic Differentiation */ #include #include #include #include #include #include #include #include namespace gtsam { /// Constant Expression template class ConstantExpression { T value_; public: /// Constructor with a value, yielding a constant ConstantExpression(const T& value) : value_(value) { } T value(const Values& values) const { return value_; } }; /// Leaf Expression template class LeafExpression { Key key_; public: /// Constructor with a single key LeafExpression(Key key):key_(key) { } T value(const Values& values) const { return values.at(key_); } }; /// Expression version of transform template LeafExpression transformTo(const E1& x, const E2& p) { return LeafExpression(0); } /// Expression version of project template LeafExpression project(const E& p) { return LeafExpression(0); } /// Expression version of uncalibrate template LeafExpression uncalibrate(const E1& K, const E2& p) { return LeafExpression(0); } /// Expression version of Point2.sub template LeafExpression operator -(const E1& p, const E2& q) { return LeafExpression(0); } /// AD Factor template class BADFactor: NonlinearFactor { const T measurement_; const E expression_; /// get value from expression and calculate error with respect to measurement Vector unwhitenedError(const Values& values) const { const T& value = expression_.value(values); return measurement_.localCoordinates(value); } public: /// Constructor BADFactor(const T& measurement, const E& expression) : measurement_(measurement), expression_(expression) { } /** * Calculate the error of the factor. * This is the log-likelihood, e.g. \f$ 0.5(h(x)-z)^2/\sigma^2 \f$ in case of Gaussian. * In this class, we take the raw prediction error \f$ h(x)-z \f$, ask the noise model * to transform it to \f$ (h(x)-z)^2/\sigma^2 \f$, and then multiply by 0.5. */ virtual double error(const Values& values) const { if (this->active(values)) { const Vector e = unwhitenedError(values); return 0.5 * e.norm(); } else { return 0.0; } } /// get the dimension of the factor (number of rows on linearization) size_t dim() const { return 0; } /// linearize to a GaussianFactor boost::shared_ptr linearize(const Values& values) const { // We will construct an n-ary factor below, where terms is a container whose // value type is std::pair, specifying the // collection of keys and matrices making up the factor. std::map terms; Vector b = unwhitenedError(values); SharedDiagonal model = SharedDiagonal(); return boost::shared_ptr( new JacobianFactor(terms, b, model)); } }; } using namespace std; using namespace gtsam; /* ************************************************************************* */ TEST(BAD, test) { // Create some values Values values; values.insert(1, Pose3()); values.insert(2, Point3(0, 0, 1)); values.insert(3, Cal3_S2()); // Create old-style factor to create expected value and derivatives Point2 measured(0, 1); SharedNoiseModel model = noiseModel::Unit::Create(2); GeneralSFMFactor2 old(measured, model, 1, 2, 3); double expected_error = old.error(values); GaussianFactor::shared_ptr expected = old.linearize(values); // Create leaves LeafExpression x(1); LeafExpression p(2); LeafExpression K(3); // Create expression tree LeafExpression p_cam = transformTo(x, p); LeafExpression projection = project(p_cam); LeafExpression uv_hat = uncalibrate(K, projection); // Create factor BADFactor > f(measured, uv_hat); // Check value EXPECT_DOUBLES_EQUAL(expected_error, f.error(values), 1e-9); // Check dimension EXPECT_LONGS_EQUAL(0, f.dim()); // Check linearization boost::shared_ptr gf = f.linearize(values); EXPECT( assert_equal(*expected, *gf, 1e-9)); } /* ************************************************************************* */ int main() { TestResult tr; return TestRegistry::runAllTests(tr); } /* ************************************************************************* */