From 1b5daf9a0e9fe8afd526ffd2d843acd4585658f0 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Fri, 26 Aug 2022 19:34:54 -0400 Subject: [PATCH] remove HybridLookupDAG --- gtsam/hybrid/HybridLookupDAG.cpp | 76 ------ gtsam/hybrid/HybridLookupDAG.h | 119 --------- gtsam/hybrid/tests/testHybridLookupDAG.cpp | 272 --------------------- 3 files changed, 467 deletions(-) delete mode 100644 gtsam/hybrid/HybridLookupDAG.cpp delete mode 100644 gtsam/hybrid/HybridLookupDAG.h delete mode 100644 gtsam/hybrid/tests/testHybridLookupDAG.cpp diff --git a/gtsam/hybrid/HybridLookupDAG.cpp b/gtsam/hybrid/HybridLookupDAG.cpp deleted file mode 100644 index a322a8177..000000000 --- a/gtsam/hybrid/HybridLookupDAG.cpp +++ /dev/null @@ -1,76 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * 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 DiscreteLookupDAG.cpp - * @date Aug, 2022 - * @author Shangjie Xue - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -using std::pair; -using std::vector; - -namespace gtsam { - -/* ************************************************************************** */ -void HybridLookupTable::argmaxInPlace(HybridValues* values) const { - // For discrete conditional, uses argmaxInPlace() method in - // DiscreteLookupTable. - if (isDiscrete()) { - boost::static_pointer_cast(inner_)->argmaxInPlace( - &(values->discrete)); - } else if (isContinuous()) { - // For Gaussian conditional, uses solve() method in GaussianConditional. - values->continuous.insert( - boost::static_pointer_cast(inner_)->solve( - values->continuous)); - } else if (isHybrid()) { - // For hybrid conditional, since children should not contain discrete - // variable, we can condition on the discrete variable in the parents and - // solve the resulting GaussianConditional. - auto conditional = - boost::static_pointer_cast(inner_)->conditionals()( - values->discrete); - values->continuous.insert(conditional->solve(values->continuous)); - } -} - -/* ************************************************************************** */ -HybridLookupDAG HybridLookupDAG::FromBayesNet(const HybridBayesNet& bayesNet) { - HybridLookupDAG dag; - for (auto&& conditional : bayesNet) { - HybridLookupTable hlt(*conditional); - dag.push_back(hlt); - } - return dag; -} - -/* ************************************************************************** */ -HybridValues HybridLookupDAG::argmax(HybridValues result) const { - // Argmax each node in turn in topological sort order (parents first). - for (auto lookupTable : boost::adaptors::reverse(*this)) - lookupTable->argmaxInPlace(&result); - return result; -} - -} // namespace gtsam diff --git a/gtsam/hybrid/HybridLookupDAG.h b/gtsam/hybrid/HybridLookupDAG.h deleted file mode 100644 index cc1c58c58..000000000 --- a/gtsam/hybrid/HybridLookupDAG.h +++ /dev/null @@ -1,119 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * 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 HybridLookupDAG.h - * @date Aug, 2022 - * @author Shangjie Xue - */ - -#pragma once - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -namespace gtsam { - -/** - * @brief HybridLookupTable table for max-product - * - * Similar to DiscreteLookupTable, inherits from hybrid conditional for - * convenience. Is used in the max-product algorithm. - */ -class GTSAM_EXPORT HybridLookupTable : public HybridConditional { - public: - using Base = HybridConditional; - using This = HybridLookupTable; - using shared_ptr = boost::shared_ptr; - using BaseConditional = Conditional; - - /** - * @brief Construct a new Hybrid Lookup Table object form a HybridConditional. - * - * @param conditional input hybrid conditional - */ - HybridLookupTable(HybridConditional& conditional) : Base(conditional){}; - - /** - * @brief Calculate assignment for frontal variables that maximizes value. - * @param (in/out) parentsValues Known assignments for the parents. - */ - void argmaxInPlace(HybridValues* parentsValues) const; -}; - -/** A DAG made from hybrid lookup tables, as defined above. Similar to - * DiscreteLookupDAG */ -class GTSAM_EXPORT HybridLookupDAG : public BayesNet { - public: - using Base = BayesNet; - using This = HybridLookupDAG; - using shared_ptr = boost::shared_ptr; - - /// @name Standard Constructors - /// @{ - - /// Construct empty DAG. - HybridLookupDAG() {} - - /// Create from BayesNet with LookupTables - static HybridLookupDAG FromBayesNet(const HybridBayesNet& bayesNet); - - /// Destructor - virtual ~HybridLookupDAG() {} - - /// @} - - /// @name Standard Interface - /// @{ - - /** Add a DiscreteLookupTable */ - template - void add(Args&&... args) { - emplace_shared(std::forward(args)...); - } - - /** - * @brief argmax by back-substitution, optionally given certain variables. - * - * Assumes the DAG is reverse topologically sorted, i.e. last - * conditional will be optimized first *and* that the - * DAG does not contain any conditionals for the given variables. If the DAG - * resulted from eliminating a factor graph, this is true for the elimination - * ordering. - * - * @return given assignment extended w. optimal assignment for all variables. - */ - HybridValues argmax(HybridValues given = HybridValues()) const; - /// @} - - private: - /** Serialization function */ - friend class boost::serialization::access; - template - void serialize(ARCHIVE& ar, const unsigned int /*version*/) { - ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base); - } -}; - -// traits -template <> -struct traits : public Testable {}; - -} // namespace gtsam diff --git a/gtsam/hybrid/tests/testHybridLookupDAG.cpp b/gtsam/hybrid/tests/testHybridLookupDAG.cpp deleted file mode 100644 index 0ab012d10..000000000 --- a/gtsam/hybrid/tests/testHybridLookupDAG.cpp +++ /dev/null @@ -1,272 +0,0 @@ -/* ---------------------------------------------------------------------------- - - * 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 testHybridLookupDAG.cpp - * @date Aug, 2022 - * @author Shangjie Xue - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// Include for test suite -#include - -#include - -using namespace std; -using namespace gtsam; -using noiseModel::Isotropic; -using symbol_shorthand::M; -using symbol_shorthand::X; - -TEST(HybridLookupTable, basics) { - // create a conditional gaussian node - Matrix S1(2, 2); - S1(0, 0) = 1; - S1(1, 0) = 2; - S1(0, 1) = 3; - S1(1, 1) = 4; - - Matrix S2(2, 2); - S2(0, 0) = 6; - S2(1, 0) = 0.2; - S2(0, 1) = 8; - S2(1, 1) = 0.4; - - Matrix R1(2, 2); - R1(0, 0) = 0.1; - R1(1, 0) = 0.3; - R1(0, 1) = 0.0; - R1(1, 1) = 0.34; - - Matrix R2(2, 2); - R2(0, 0) = 0.1; - R2(1, 0) = 0.3; - R2(0, 1) = 0.0; - R2(1, 1) = 0.34; - - SharedDiagonal model = noiseModel::Diagonal::Sigmas(Vector2(1.0, 0.34)); - - Vector2 d1(0.2, 0.5), d2(0.5, 0.2); - - auto conditional0 = boost::make_shared(X(1), d1, R1, - X(2), S1, model), - conditional1 = boost::make_shared(X(1), d2, R2, - X(2), S2, model); - - // Create decision tree - DiscreteKey m1(1, 2); - GaussianMixture::Conditionals conditionals( - {m1}, - vector{conditional0, conditional1}); - // GaussianMixture mixtureFactor2({X(1)}, {X(2)}, {m1}, conditionals); - - boost::shared_ptr mixtureFactor( - new GaussianMixture({X(1)}, {X(2)}, {m1}, conditionals)); - - HybridConditional hc(mixtureFactor); - - GaussianMixture::Conditionals conditional2 = - boost::static_pointer_cast(hc.inner())->conditionals(); - - DiscreteValues dv; - dv[1] = 1; - - VectorValues cv; - cv.insert(X(2), Vector2(0.0, 0.0)); - - HybridValues hv(dv, cv); - - // std::cout << conditional2(values).markdown(); - EXPECT(assert_equal(*conditional2(dv), *conditionals(dv), 1e-6)); - EXPECT(conditional2(dv) == conditionals(dv)); - HybridLookupTable hlt(hc); - - // hlt.argmaxInPlace(&hv); - - HybridLookupDAG dag; - dag.push_back(hlt); - dag.argmax(hv); - - // HybridBayesNet hbn; - // hbn.push_back(hc); - // hbn.optimize(); -} - -TEST(HybridLookupTable, hybrid_argmax) { - Matrix S1(2, 2); - S1(0, 0) = 1; - S1(1, 0) = 0; - S1(0, 1) = 0; - S1(1, 1) = 1; - - Vector2 d1(0.2, 0.5), d2(-0.5, 0.6); - - SharedDiagonal model = noiseModel::Diagonal::Sigmas(Vector2(1.0, 0.34)); - - auto conditional0 = - boost::make_shared(X(1), d1, S1, model), - conditional1 = - boost::make_shared(X(1), d2, S1, model); - - DiscreteKey m1(1, 2); - GaussianMixture::Conditionals conditionals( - {m1}, - vector{conditional0, conditional1}); - boost::shared_ptr mixtureFactor( - new GaussianMixture({X(1)}, {}, {m1}, conditionals)); - - HybridConditional hc(mixtureFactor); - - DiscreteValues dv; - dv[1] = 1; - VectorValues cv; - // cv.insert(X(2),Vector2(0.0, 0.0)); - HybridValues hv(dv, cv); - - HybridLookupTable hlt(hc); - - hlt.argmaxInPlace(&hv); - - EXPECT(assert_equal(hv.at(X(1)), d2)); -} - -TEST(HybridLookupTable, discrete_argmax) { - DiscreteKey X(0, 2), Y(1, 2); - - auto conditional = boost::make_shared(X | Y = "0/1 3/2"); - - HybridConditional hc(conditional); - - HybridLookupTable hlt(hc); - - DiscreteValues dv; - dv[1] = 0; - VectorValues cv; - // cv.insert(X(2),Vector2(0.0, 0.0)); - HybridValues hv(dv, cv); - - hlt.argmaxInPlace(&hv); - - EXPECT(assert_equal(hv.atDiscrete(0), 1)); - - DecisionTreeFactor f1(X, "2 3"); - auto conditional2 = boost::make_shared(1, f1); - - HybridConditional hc2(conditional2); - - HybridLookupTable hlt2(hc2); - - HybridValues hv2; - - hlt2.argmaxInPlace(&hv2); - - EXPECT(assert_equal(hv2.atDiscrete(0), 1)); -} - -TEST(HybridLookupTable, gaussian_argmax) { - Matrix S1(2, 2); - S1(0, 0) = 1; - S1(1, 0) = 0; - S1(0, 1) = 0; - S1(1, 1) = 1; - - Vector2 d1(0.2, 0.5), d2(-0.5, 0.6); - - SharedDiagonal model = noiseModel::Diagonal::Sigmas(Vector2(1.0, 0.34)); - - auto conditional = - boost::make_shared(X(1), d1, S1, X(2), -S1, model); - - HybridConditional hc(conditional); - - HybridLookupTable hlt(hc); - - DiscreteValues dv; - // dv[1]=0; - VectorValues cv; - cv.insert(X(2), d2); - HybridValues hv(dv, cv); - - hlt.argmaxInPlace(&hv); - - EXPECT(assert_equal(hv.at(X(1)), d1 + d2)); -} - -TEST(HybridLookupDAG, argmax) { - Matrix S1(2, 2); - S1(0, 0) = 1; - S1(1, 0) = 0; - S1(0, 1) = 0; - S1(1, 1) = 1; - - Vector2 d1(0.2, 0.5), d2(-0.5, 0.6); - - SharedDiagonal model = noiseModel::Diagonal::Sigmas(Vector2(1.0, 0.34)); - - auto conditional0 = - boost::make_shared(X(2), d1, S1, model), - conditional1 = - boost::make_shared(X(2), d2, S1, model); - - DiscreteKey m1(1, 2); - GaussianMixture::Conditionals conditionals( - {m1}, - vector{conditional0, conditional1}); - boost::shared_ptr mixtureFactor( - new GaussianMixture({X(2)}, {}, {m1}, conditionals)); - HybridConditional hc2(mixtureFactor); - HybridLookupTable hlt2(hc2); - - auto conditional2 = - boost::make_shared(X(1), d1, S1, X(2), -S1, model); - - HybridConditional hc1(conditional2); - HybridLookupTable hlt1(hc1); - - DecisionTreeFactor f1(m1, "2 3"); - auto discrete_conditional = boost::make_shared(1, f1); - - HybridConditional hc3(discrete_conditional); - HybridLookupTable hlt3(hc3); - - HybridLookupDAG dag; - dag.push_back(hlt1); - dag.push_back(hlt2); - dag.push_back(hlt3); - auto hv = dag.argmax(); - - EXPECT(assert_equal(hv.atDiscrete(1), 1)); - EXPECT(assert_equal(hv.at(X(2)), d2)); - EXPECT(assert_equal(hv.at(X(1)), d2 + d1)); -} - -/* ************************************************************************* */ -int main() { - TestResult tr; - return TestRegistry::runAllTests(tr); -} -/* ************************************************************************* */