/* ---------------------------------------------------------------------------- * 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: typedef T type; /// 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: typedef T type; /// Constructor with a single key LeafExpression(Key key) : key_(key) { } T value(const Values& values) const { return values.at(key_); } }; //----------------------------------------------------------------------------- /// Unary Expression template class UnaryExpression { public: typedef T (*function)(const typename E::type&); private: const E expression_; function f_; public: typedef T type; /// Constructor with a single key UnaryExpression(function f, const E& expression) : expression_(expression), f_(f) { } T value(const Values& values) const { return f_(expression_.value(values)); } }; //----------------------------------------------------------------------------- /// Binary Expression template class BinaryExpression { public: typedef T (*function)(const typename E1::type&, const typename E2::type&); private: const E1 expression1_; const E2 expression2_; function f_; public: typedef T type; /// Constructor with a single key BinaryExpression(function f, const E1& expression1, const E2& expression2) : expression1_(expression1), expression2_(expression2), f_(f) { } T value(const Values& values) const { return f_(expression1_.value(values), expression2_.value(values)); } }; //----------------------------------------------------------------------------- /// 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.squaredNorm(); } 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; //----------------------------------------------------------------------------- Point3 transformTo(const Pose3& x, const Point3& p) { return x.transform_to(p); } Point2 project(const Point3& p) { return PinholeCamera::project_to_camera(p); } template Point2 uncalibrate(const CAL& K, const Point2& p) { return K.uncalibrate(p); } /* ************************************************************************* */ 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(-17, 30); 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 typedef BinaryExpression, LeafExpression > Binary1; Binary1 p_cam(transformTo, x, p); typedef UnaryExpression Unary1; Unary1 projection(project, p_cam); typedef BinaryExpression, Unary1> Binary2; Binary2 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); } /* ************************************************************************* */