From cf6c1cad1fad46d43857b941cc9227db0fd34f78 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Tue, 18 Jul 2023 22:38:24 -0400 Subject: [PATCH 01/10] fix tests --- .../tests/testDiscreteFactorGraph.cpp | 82 +++++++++---------- gtsam/hybrid/MixtureFactor.h | 2 +- 2 files changed, 42 insertions(+), 42 deletions(-) diff --git a/gtsam/discrete/tests/testDiscreteFactorGraph.cpp b/gtsam/discrete/tests/testDiscreteFactorGraph.cpp index 6e8621595..08b5f2db9 100644 --- a/gtsam/discrete/tests/testDiscreteFactorGraph.cpp +++ b/gtsam/discrete/tests/testDiscreteFactorGraph.cpp @@ -356,37 +356,37 @@ size: 2 factor 0: f[ (m0,2), (m1,2), (m2,2), ] Choice(m2) 0 Choice(m1) - 0 0 Leaf [2] 0 + 0 0 Leaf [2] 0 0 1 Choice(m0) - 0 1 0 Leaf [1]0.27527634 - 0 1 1 Leaf [1] 0 + 0 1 0 Leaf [1] 0.27527634 + 0 1 1 Leaf [1] 0 1 Choice(m1) - 1 0 Leaf [2] 0 + 1 0 Leaf [2] 0 1 1 Choice(m0) - 1 1 0 Leaf [1]0.44944733 - 1 1 1 Leaf [1]0.27527634 + 1 1 0 Leaf [1] 0.44944733 + 1 1 1 Leaf [1] 0.27527634 factor 1: f[ (m0,2), (m1,2), (m2,2), (m3,2), ] Choice(m3) 0 Choice(m2) 0 0 Choice(m1) - 0 0 0 Leaf [2] 1 - 0 0 1 Leaf [2]0.015366387 + 0 0 0 Leaf [2] 1 + 0 0 1 Leaf [2] 0.015366387 0 1 Choice(m1) - 0 1 0 Leaf [2] 1 + 0 1 0 Leaf [2] 1 0 1 1 Choice(m0) - 0 1 1 0 Leaf [1] 1 - 0 1 1 1 Leaf [1]0.015365663 + 0 1 1 0 Leaf [1] 1 + 0 1 1 1 Leaf [1] 0.015365663 1 Choice(m2) 1 0 Choice(m1) - 1 0 0 Leaf [2] 1 + 1 0 0 Leaf [2] 1 1 0 1 Choice(m0) - 1 0 1 0 Leaf [1]0.0094115739 - 1 0 1 1 Leaf [1]0.0094115652 + 1 0 1 0 Leaf [1] 0.0094115739 + 1 0 1 1 Leaf [1] 0.0094115652 1 1 Choice(m1) - 1 1 0 Leaf [2] 1 + 1 1 0 Leaf [2] 1 1 1 1 Choice(m0) - 1 1 1 0 Leaf [1] 1 - 1 1 1 1 Leaf [1]0.009321081 + 1 1 1 0 Leaf [1] 1 + 1 1 1 1 Leaf [1] 0.009321081 )"; #else string expected_dfg = R"( @@ -395,50 +395,50 @@ factor 0: f[ (m0,2), (m1,2), (m2,2), ] Choice(m2) 0 Choice(m1) 0 0 Choice(m0) - 0 0 0 Leaf [1] 0 - 0 0 1 Leaf [1] 0 + 0 0 0 Leaf [1] 0 + 0 0 1 Leaf [1] 0 0 1 Choice(m0) - 0 1 0 Leaf [1]0.27527634 - 0 1 1 Leaf [1]0.44944733 + 0 1 0 Leaf [1] 0.27527634 + 0 1 1 Leaf [1] 0.44944733 1 Choice(m1) 1 0 Choice(m0) - 1 0 0 Leaf [1] 0 - 1 0 1 Leaf [1] 0 + 1 0 0 Leaf [1] 0 + 1 0 1 Leaf [1] 0 1 1 Choice(m0) - 1 1 0 Leaf [1] 0 - 1 1 1 Leaf [1]0.27527634 + 1 1 0 Leaf [1] 0 + 1 1 1 Leaf [1] 0.27527634 factor 1: f[ (m0,2), (m1,2), (m2,2), (m3,2), ] Choice(m3) 0 Choice(m2) 0 0 Choice(m1) 0 0 0 Choice(m0) - 0 0 0 0 Leaf [1] 1 - 0 0 0 1 Leaf [1] 1 + 0 0 0 0 Leaf [1] 1 + 0 0 0 1 Leaf [1] 1 0 0 1 Choice(m0) - 0 0 1 0 Leaf [1]0.015366387 - 0 0 1 1 Leaf [1]0.015366387 + 0 0 1 0 Leaf [1] 0.015366387 + 0 0 1 1 Leaf [1] 0.015366387 0 1 Choice(m1) 0 1 0 Choice(m0) - 0 1 0 0 Leaf [1] 1 - 0 1 0 1 Leaf [1] 1 + 0 1 0 0 Leaf [1] 1 + 0 1 0 1 Leaf [1] 1 0 1 1 Choice(m0) - 0 1 1 0 Leaf [1] 1 - 0 1 1 1 Leaf [1]0.015365663 + 0 1 1 0 Leaf [1] 1 + 0 1 1 1 Leaf [1] 0.015365663 1 Choice(m2) 1 0 Choice(m1) 1 0 0 Choice(m0) - 1 0 0 0 Leaf [1] 1 - 1 0 0 1 Leaf [1] 1 + 1 0 0 0 Leaf [1] 1 + 1 0 0 1 Leaf [1] 1 1 0 1 Choice(m0) - 1 0 1 0 Leaf [1]0.0094115739 - 1 0 1 1 Leaf [1]0.0094115652 + 1 0 1 0 Leaf [1] 0.0094115739 + 1 0 1 1 Leaf [1] 0.0094115652 1 1 Choice(m1) 1 1 0 Choice(m0) - 1 1 0 0 Leaf [1] 1 - 1 1 0 1 Leaf [1] 1 + 1 1 0 0 Leaf [1] 1 + 1 1 0 1 Leaf [1] 1 1 1 1 Choice(m0) - 1 1 1 0 Leaf [1] 1 - 1 1 1 1 Leaf [1]0.009321081 + 1 1 1 0 Leaf [1] 1 + 1 1 1 1 Leaf [1] 0.009321081 )"; #endif diff --git a/gtsam/hybrid/MixtureFactor.h b/gtsam/hybrid/MixtureFactor.h index 529c8687b..df8e0193a 100644 --- a/gtsam/hybrid/MixtureFactor.h +++ b/gtsam/hybrid/MixtureFactor.h @@ -191,7 +191,7 @@ class MixtureFactor : public HybridFactor { std::cout << "\nMixtureFactor\n"; auto valueFormatter = [](const sharedFactor& v) { if (v) { - return " Nonlinear factor on " + std::to_string(v->size()) + " keys"; + return "Nonlinear factor on " + std::to_string(v->size()) + " keys"; } else { return std::string("nullptr"); } From 1dfb388587b86c6808e354b8b3086c3725bdfcb4 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Thu, 20 Jul 2023 15:47:29 -0400 Subject: [PATCH 02/10] fix odd behavior in nrAssignments --- gtsam/discrete/DecisionTree-inl.h | 29 ++++++++++++------- gtsam/discrete/DecisionTree.h | 8 +++-- gtsam/discrete/tests/testDecisionTree.cpp | 2 +- .../tests/testDiscreteFactorGraph.cpp | 11 ++++--- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/gtsam/discrete/DecisionTree-inl.h b/gtsam/discrete/DecisionTree-inl.h index c3f6b3c0e..541bd77f4 100644 --- a/gtsam/discrete/DecisionTree-inl.h +++ b/gtsam/discrete/DecisionTree-inl.h @@ -137,8 +137,8 @@ namespace gtsam { // Applying binary operator to two leaves results in a leaf NodePtr apply_g_op_fL(const Leaf& fL, const Binary& op) const override { // fL op gL - // TODO(Varun) nrAssignments setting is not correct. - // Depending on f and g, the nrAssignments can be different. This is a bug! + // The nrAssignments is always set to fL since we consider g operating on + // (or modifying) f. NodePtr h(new Leaf(op(fL.constant_, constant_), fL.nrAssignments())); return h; } @@ -149,8 +149,9 @@ namespace gtsam { } /** choose a branch, create new memory ! */ - NodePtr choose(const L& label, size_t index) const override { - return NodePtr(new Leaf(constant(), nrAssignments())); + NodePtr choose(const L& label, size_t index, + bool make_unique = true) const override { + return NodePtr(new Leaf(constant(), 1)); } bool isLeaf() const override { return true; } @@ -468,14 +469,22 @@ namespace gtsam { } /** choose a branch, recursively */ - NodePtr choose(const L& label, size_t index) const override { + NodePtr choose(const L& label, size_t index, + bool make_unique = true) const override { if (label_ == label) return branches_[index]; // choose branch // second case, not label of interest, just recurse auto r = std::make_shared(label_, branches_.size()); - for (auto&& branch : branches_) - r->push_back(branch->choose(label, index)); - return Unique(r); + for (auto&& branch : branches_) { + r->push_back(branch->choose(label, index, make_unique)); + } + + if (make_unique) { + return Unique(r); + } else { + return r; + } + // return Unique(r); } private: @@ -997,9 +1006,9 @@ namespace gtsam { template DecisionTree DecisionTree::combine(const L& label, size_t cardinality, const Binary& op) const { - DecisionTree result = choose(label, 0); + DecisionTree result = choose(label, 0, false); for (size_t index = 1; index < cardinality; index++) { - DecisionTree chosen = choose(label, index); + DecisionTree chosen = choose(label, index, false); result = result.apply(chosen, op); } return result; diff --git a/gtsam/discrete/DecisionTree.h b/gtsam/discrete/DecisionTree.h index 48a1a4596..7761de84e 100644 --- a/gtsam/discrete/DecisionTree.h +++ b/gtsam/discrete/DecisionTree.h @@ -129,7 +129,8 @@ namespace gtsam { virtual Ptr apply_f_op_g(const Node&, const Binary&) const = 0; virtual Ptr apply_g_op_fL(const Leaf&, const Binary&) const = 0; virtual Ptr apply_g_op_fC(const Choice&, const Binary&) const = 0; - virtual Ptr choose(const L& label, size_t index) const = 0; + virtual Ptr choose(const L& label, size_t index, + bool make_unique = true) const = 0; virtual bool isLeaf() const = 0; private: @@ -403,8 +404,9 @@ namespace gtsam { /** create a new function where value(label)==index * It's like "restrict" in Darwiche09book pg329, 330? */ - DecisionTree choose(const L& label, size_t index) const { - NodePtr newRoot = root_->choose(label, index); + DecisionTree choose(const L& label, size_t index, + bool make_unique = true) const { + NodePtr newRoot = root_->choose(label, index, make_unique); return DecisionTree(newRoot); } diff --git a/gtsam/discrete/tests/testDecisionTree.cpp b/gtsam/discrete/tests/testDecisionTree.cpp index e94c72c34..ff50c8952 100644 --- a/gtsam/discrete/tests/testDecisionTree.cpp +++ b/gtsam/discrete/tests/testDecisionTree.cpp @@ -236,7 +236,7 @@ TEST(DecisionTree, Example) { #ifdef GTSAM_DT_MERGING EXPECT(assert_equal(DT(0.0), actual0)); #else - // EXPECT(assert_equal(DT({0.0, 0.0}), actual0)); + EXPECT(assert_equal(DT({0.0, 0.0}), actual0)); #endif DOT(actual0); diff --git a/gtsam/discrete/tests/testDiscreteFactorGraph.cpp b/gtsam/discrete/tests/testDiscreteFactorGraph.cpp index 08b5f2db9..871583a2c 100644 --- a/gtsam/discrete/tests/testDiscreteFactorGraph.cpp +++ b/gtsam/discrete/tests/testDiscreteFactorGraph.cpp @@ -349,6 +349,7 @@ TEST(DiscreteFactorGraph, markdown) { EXPECT_DOUBLES_EQUAL(0.3, graph[0]->operator()(values), 1e-9); } +/* ************************************************************************* */ TEST(DiscreteFactorGraph, NrAssignments) { #ifdef GTSAM_DT_MERGING string expected_dfg = R"( @@ -358,13 +359,13 @@ factor 0: f[ (m0,2), (m1,2), (m2,2), ] 0 Choice(m1) 0 0 Leaf [2] 0 0 1 Choice(m0) - 0 1 0 Leaf [1] 0.27527634 + 0 1 0 Leaf [1] 0.17054468 0 1 1 Leaf [1] 0 1 Choice(m1) 1 0 Leaf [2] 0 1 1 Choice(m0) - 1 1 0 Leaf [1] 0.44944733 - 1 1 1 Leaf [1] 0.27527634 + 1 1 0 Leaf [1] 0.27845056 + 1 1 1 Leaf [1] 0.17054468 factor 1: f[ (m0,2), (m1,2), (m2,2), (m3,2), ] Choice(m3) 0 Choice(m2) @@ -445,9 +446,7 @@ factor 1: f[ (m0,2), (m1,2), (m2,2), (m3,2), ] DiscreteKeys d0{{M(0), 2}, {M(1), 2}, {M(2), 2}}; std::vector p0 = {0, 0, 0.17054468, 0.27845056, 0, 0, 0, 0.17054468}; AlgebraicDecisionTree dt(d0, p0); - //TODO(Varun) Passing ADT to DiscreteConditional causes nrAssignments to get messed up - // Issue seems to be in DecisionTreeFactor.cpp L104 - DiscreteConditional f0(3, DecisionTreeFactor(d0, dt)); + DiscreteConditional f0(3, d0, dt); DiscreteKeys d1{{M(0), 2}, {M(1), 2}, {M(2), 2}, {M(3), 2}}; std::vector p1 = { From ea24a2c7e8e8ca10cdd02ddea663748905cf8db4 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Thu, 20 Jul 2023 15:47:58 -0400 Subject: [PATCH 03/10] park changes so I can come back to them later --- gtsam/discrete/DiscreteFactorGraph.cpp | 10 ++ .../tests/testDiscreteConditional.cpp | 106 ++++++++++++++---- gtsam/hybrid/HybridBayesNet.cpp | 13 +++ gtsam/hybrid/HybridGaussianFactorGraph.cpp | 2 +- 4 files changed, 111 insertions(+), 20 deletions(-) diff --git a/gtsam/discrete/DiscreteFactorGraph.cpp b/gtsam/discrete/DiscreteFactorGraph.cpp index 4ededbb8b..e0d2ed4de 100644 --- a/gtsam/discrete/DiscreteFactorGraph.cpp +++ b/gtsam/discrete/DiscreteFactorGraph.cpp @@ -204,12 +204,18 @@ namespace gtsam { std::pair // EliminateDiscrete(const DiscreteFactorGraph& factors, const Ordering& frontalKeys) { + factors.print("The Factors to eliminate:"); // PRODUCT: multiply all factors gttic(product); DecisionTreeFactor product; for (auto&& factor : factors) product = (*factor) * product; gttoc(product); + std::cout << "\n\n==========" << std::endl; + std::cout << "Product" << std::endl; + std::cout << std::endl; + product.print(); + // Max over all the potentials by pretending all keys are frontal: auto normalization = product.max(product.size()); @@ -221,6 +227,10 @@ namespace gtsam { DecisionTreeFactor::shared_ptr sum = product.sum(frontalKeys); gttoc(sum); + std::cout << "\n->Sum" << std::endl; + sum->print(); + std::cout << "----------------------" << std::endl; + // Ordering keys for the conditional so that frontalKeys are really in front Ordering orderedKeys; orderedKeys.insert(orderedKeys.end(), frontalKeys.begin(), diff --git a/gtsam/discrete/tests/testDiscreteConditional.cpp b/gtsam/discrete/tests/testDiscreteConditional.cpp index aa393d74c..397e7ff0c 100644 --- a/gtsam/discrete/tests/testDiscreteConditional.cpp +++ b/gtsam/discrete/tests/testDiscreteConditional.cpp @@ -22,13 +22,15 @@ #include #include #include +#include +#include using namespace std; using namespace gtsam; /* ************************************************************************* */ -TEST(DiscreteConditional, constructors) { +TEST_DISABLED(DiscreteConditional, constructors) { DiscreteKey X(0, 2), Y(2, 3), Z(1, 2); // watch ordering ! DiscreteConditional actual(X | Y = "1/1 2/3 1/4"); @@ -49,7 +51,7 @@ TEST(DiscreteConditional, constructors) { } /* ************************************************************************* */ -TEST(DiscreteConditional, constructors_alt_interface) { +TEST_DISABLED(DiscreteConditional, constructors_alt_interface) { DiscreteKey X(0, 2), Y(2, 3), Z(1, 2); // watch ordering ! const Signature::Row r1{1, 1}, r2{2, 3}, r3{1, 4}; @@ -68,7 +70,7 @@ TEST(DiscreteConditional, constructors_alt_interface) { } /* ************************************************************************* */ -TEST(DiscreteConditional, constructors2) { +TEST_DISABLED(DiscreteConditional, constructors2) { DiscreteKey C(0, 2), B(1, 2); Signature signature((C | B) = "4/1 3/1"); DiscreteConditional actual(signature); @@ -78,7 +80,7 @@ TEST(DiscreteConditional, constructors2) { } /* ************************************************************************* */ -TEST(DiscreteConditional, constructors3) { +TEST_DISABLED(DiscreteConditional, constructors3) { DiscreteKey C(0, 2), B(1, 2), A(2, 2); Signature signature((C | B, A) = "4/1 1/1 1/1 1/4"); DiscreteConditional actual(signature); @@ -89,7 +91,7 @@ TEST(DiscreteConditional, constructors3) { /* ****************************************************************************/ // Test evaluate for a discrete Prior P(Asia). -TEST(DiscreteConditional, PriorProbability) { +TEST_DISABLED(DiscreteConditional, PriorProbability) { constexpr Key asiaKey = 0; const DiscreteKey Asia(asiaKey, 2); DiscreteConditional dc(Asia, "4/6"); @@ -100,7 +102,7 @@ TEST(DiscreteConditional, PriorProbability) { /* ************************************************************************* */ // Check that error, logProbability, evaluate all work as expected. -TEST(DiscreteConditional, probability) { +TEST_DISABLED(DiscreteConditional, probability) { DiscreteKey C(2, 2), D(4, 2), E(3, 2); DiscreteConditional C_given_DE((C | D, E) = "4/1 1/1 1/1 1/4"); @@ -114,7 +116,7 @@ TEST(DiscreteConditional, probability) { /* ************************************************************************* */ // Check calculation of joint P(A,B) -TEST(DiscreteConditional, Multiply) { +TEST_DISABLED(DiscreteConditional, Multiply) { DiscreteKey A(1, 2), B(0, 2); DiscreteConditional conditional(A | B = "1/2 2/1"); DiscreteConditional prior(B % "1/2"); @@ -139,7 +141,7 @@ TEST(DiscreteConditional, Multiply) { /* ************************************************************************* */ // Check calculation of conditional joint P(A,B|C) -TEST(DiscreteConditional, Multiply2) { +TEST_DISABLED(DiscreteConditional, Multiply2) { DiscreteKey A(0, 2), B(1, 2), C(2, 2); DiscreteConditional A_given_B(A | B = "1/3 3/1"); DiscreteConditional B_given_C(B | C = "1/3 3/1"); @@ -159,7 +161,7 @@ TEST(DiscreteConditional, Multiply2) { /* ************************************************************************* */ // Check calculation of conditional joint P(A,B|C), double check keys -TEST(DiscreteConditional, Multiply3) { +TEST_DISABLED(DiscreteConditional, Multiply3) { DiscreteKey A(1, 2), B(2, 2), C(0, 2); // different keys!!! DiscreteConditional A_given_B(A | B = "1/3 3/1"); DiscreteConditional B_given_C(B | C = "1/3 3/1"); @@ -179,7 +181,7 @@ TEST(DiscreteConditional, Multiply3) { /* ************************************************************************* */ // Check calculation of conditional joint P(A,B,C|D,E) = P(A,B|D) P(C|D,E) -TEST(DiscreteConditional, Multiply4) { +TEST_DISABLED(DiscreteConditional, Multiply4) { DiscreteKey A(0, 2), B(1, 2), C(2, 2), D(4, 2), E(3, 2); DiscreteConditional A_given_B(A | B = "1/3 3/1"); DiscreteConditional B_given_D(B | D = "1/3 3/1"); @@ -203,7 +205,7 @@ TEST(DiscreteConditional, Multiply4) { /* ************************************************************************* */ // Check calculation of marginals for joint P(A,B) -TEST(DiscreteConditional, marginals) { +TEST_DISABLED(DiscreteConditional, marginals) { DiscreteKey A(1, 2), B(0, 2); DiscreteConditional conditional(A | B = "1/2 2/1"); DiscreteConditional prior(B % "1/2"); @@ -225,7 +227,7 @@ TEST(DiscreteConditional, marginals) { /* ************************************************************************* */ // Check calculation of marginals in case branches are pruned -TEST(DiscreteConditional, marginals2) { +TEST_DISABLED(DiscreteConditional, marginals2) { DiscreteKey A(0, 2), B(1, 2); // changing keys need to make pruning happen! DiscreteConditional conditional(A | B = "2/2 3/1"); DiscreteConditional prior(B % "1/2"); @@ -241,7 +243,7 @@ TEST(DiscreteConditional, marginals2) { } /* ************************************************************************* */ -TEST(DiscreteConditional, likelihood) { +TEST_DISABLED(DiscreteConditional, likelihood) { DiscreteKey X(0, 2), Y(1, 3); DiscreteConditional conditional(X | Y = "2/8 4/6 5/5"); @@ -256,7 +258,7 @@ TEST(DiscreteConditional, likelihood) { /* ************************************************************************* */ // Check choose on P(C|D,E) -TEST(DiscreteConditional, choose) { +TEST_DISABLED(DiscreteConditional, choose) { DiscreteKey C(2, 2), D(4, 2), E(3, 2); DiscreteConditional C_given_DE((C | D, E) = "4/1 1/1 1/1 1/4"); @@ -284,7 +286,7 @@ TEST(DiscreteConditional, choose) { /* ************************************************************************* */ // Check markdown representation looks as expected, no parents. -TEST(DiscreteConditional, markdown_prior) { +TEST_DISABLED(DiscreteConditional, markdown_prior) { DiscreteKey A(Symbol('x', 1), 3); DiscreteConditional conditional(A % "1/2/2"); string expected = @@ -300,7 +302,7 @@ TEST(DiscreteConditional, markdown_prior) { /* ************************************************************************* */ // Check markdown representation looks as expected, no parents + names. -TEST(DiscreteConditional, markdown_prior_names) { +TEST_DISABLED(DiscreteConditional, markdown_prior_names) { Symbol x1('x', 1); DiscreteKey A(x1, 3); DiscreteConditional conditional(A % "1/2/2"); @@ -318,7 +320,7 @@ TEST(DiscreteConditional, markdown_prior_names) { /* ************************************************************************* */ // Check markdown representation looks as expected, multivalued. -TEST(DiscreteConditional, markdown_multivalued) { +TEST_DISABLED(DiscreteConditional, markdown_multivalued) { DiscreteKey A(Symbol('a', 1), 3), B(Symbol('b', 1), 5); DiscreteConditional conditional( A | B = "2/88/10 2/20/78 33/33/34 33/33/34 95/2/3"); @@ -337,7 +339,7 @@ TEST(DiscreteConditional, markdown_multivalued) { /* ************************************************************************* */ // Check markdown representation looks as expected, two parents + names. -TEST(DiscreteConditional, markdown) { +TEST_DISABLED(DiscreteConditional, markdown) { DiscreteKey A(2, 2), B(1, 2), C(0, 3); DiscreteConditional conditional(A, {B, C}, "0/1 1/3 1/1 3/1 0/1 1/0"); string expected = @@ -360,7 +362,7 @@ TEST(DiscreteConditional, markdown) { /* ************************************************************************* */ // Check html representation looks as expected, two parents + names. -TEST(DiscreteConditional, html) { +TEST_DISABLED(DiscreteConditional, html) { DiscreteKey A(2, 2), B(1, 2), C(0, 3); DiscreteConditional conditional(A, {B, C}, "0/1 1/3 1/1 3/1 0/1 1/0"); string expected = @@ -388,6 +390,72 @@ TEST(DiscreteConditional, html) { EXPECT(actual == expected); } +/* ************************************************************************* */ +TEST(DiscreteConditional, NrAssignments) { +#ifdef GTSAM_DT_MERGING + string expected = R"( P( 0 1 2 ): + Choice(2) + 0 Choice(1) + 0 0 Leaf [2] 0 + 0 1 Choice(0) + 0 1 0 Leaf [1] 0.27527634 + 0 1 1 Leaf [1] 0 + 1 Choice(1) + 1 0 Leaf [2] 0 + 1 1 Choice(0) + 1 1 0 Leaf [1] 0.44944733 + 1 1 1 Leaf [1] 0.27527634 + +)"; +#else + string expected = R"( P( 0 1 2 ): + Choice(2) + 0 Choice(1) + 0 0 Choice(0) + 0 0 0 Leaf [1] 0 + 0 0 1 Leaf [1] 0 + 0 1 Choice(0) + 0 1 0 Leaf [1] 0.27527634 + 0 1 1 Leaf [1] 0.44944733 + 1 Choice(1) + 1 0 Choice(0) + 1 0 0 Leaf [1] 0 + 1 0 1 Leaf [1] 0 + 1 1 Choice(0) + 1 1 0 Leaf [1] 0 + 1 1 1 Leaf [1] 0.27527634 + +)"; +#endif + + DiscreteKeys d0{{0, 2}, {1, 2}, {2, 2}}; + std::vector p0 = {0, 0, 0.17054468, 0.27845056, 0, 0, 0, 0.17054468}; + AlgebraicDecisionTree dt(d0, p0); + DecisionTreeFactor dtf(d0, dt); + DiscreteConditional f0(3, dtf); + + EXPECT(assert_print_equal(expected, f0)); + + DiscreteFactorGraph dfg{f0}; + dfg.print(); + auto dbn = dfg.eliminateSequential(); + dbn->print(); + + // DiscreteKeys d0{{0, 2}, {1, 2}}; + // std::vector p0 = {0, 1, 0, 2}; + // AlgebraicDecisionTree dt0(d0, p0); + // dt0.print("", DefaultKeyFormatter); + + // DiscreteKeys d1{{0, 2}}; + // std::vector p1 = {1, 1, 1, 1}; + // AlgebraicDecisionTree dt1(d0, p1); + // dt1.print("", DefaultKeyFormatter); + + // auto dd = dt0 / dt1; + // dd.print("", DefaultKeyFormatter); +} + + /* ************************************************************************* */ int main() { TestResult tr; diff --git a/gtsam/hybrid/HybridBayesNet.cpp b/gtsam/hybrid/HybridBayesNet.cpp index b4bf61220..a6163fd3c 100644 --- a/gtsam/hybrid/HybridBayesNet.cpp +++ b/gtsam/hybrid/HybridBayesNet.cpp @@ -140,15 +140,26 @@ DecisionTreeFactor HybridBayesNet::pruneDiscreteConditionals( for (size_t i = 0; i < this->size(); i++) { auto conditional = this->at(i); if (conditional->isDiscrete()) { + std::cout << ">>>" << std::endl; + conditional->print(); discreteProbs = discreteProbs * (*conditional->asDiscrete()); + // discreteProbs.print(); + // std::cout << "================\n" << std::endl; Ordering conditional_keys(conditional->frontals()); discrete_frontals += conditional_keys; discrete_factor_idxs.push_back(i); } } + std::cout << "Original Joint Prob:" << std::endl; + std::cout << discreteProbs.nrAssignments() << std::endl; + discreteProbs.print(); const DecisionTreeFactor prunedDiscreteProbs = discreteProbs.prune(maxNrLeaves); + std::cout << "Pruned Joint Prob:" << std::endl; + std::cout << prunedDiscreteProbs.nrAssignments() << std::endl; + prunedDiscreteProbs.print(); + std::cout << "\n\n\n"; gttoc_(HybridBayesNet_PruneDiscreteConditionals); // Eliminate joint probability back into conditionals @@ -159,6 +170,8 @@ DecisionTreeFactor HybridBayesNet::pruneDiscreteConditionals( // Assign pruned discrete conditionals back at the correct indices. for (size_t i = 0; i < discrete_factor_idxs.size(); i++) { size_t idx = discrete_factor_idxs.at(i); + // std::cout << i << std::endl; + // dbn->at(i)->print(); this->at(idx) = std::make_shared(dbn->at(i)); } gttoc_(HybridBayesNet_UpdateDiscreteConditionals); diff --git a/gtsam/hybrid/HybridGaussianFactorGraph.cpp b/gtsam/hybrid/HybridGaussianFactorGraph.cpp index 2d4ac83f6..444cef439 100644 --- a/gtsam/hybrid/HybridGaussianFactorGraph.cpp +++ b/gtsam/hybrid/HybridGaussianFactorGraph.cpp @@ -178,7 +178,7 @@ discreteElimination(const HybridGaussianFactorGraph &factors, throwRuntimeError("continuousElimination", f); } } - + dfg.print("The DFG to eliminate"); // NOTE: This does sum-product. For max-product, use EliminateForMPE. auto result = EliminateDiscrete(dfg, frontalKeys); From b35fb0f668b30a087f5516600758e3f874800183 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Fri, 28 Jul 2023 15:48:13 -0400 Subject: [PATCH 04/10] update tests --- gtsam/discrete/tests/testDecisionTree.cpp | 40 +++++++------------ .../tests/testDiscreteConditional.cpp | 38 +++++++++--------- 2 files changed, 34 insertions(+), 44 deletions(-) diff --git a/gtsam/discrete/tests/testDecisionTree.cpp b/gtsam/discrete/tests/testDecisionTree.cpp index ff50c8952..54e5b3de9 100644 --- a/gtsam/discrete/tests/testDecisionTree.cpp +++ b/gtsam/discrete/tests/testDecisionTree.cpp @@ -473,47 +473,37 @@ TEST(DecisionTree, VisitWithPruned) { #ifdef GTSAM_DT_MERGING expectedAssignment = {{"B", 0}, {"C", 0}}; EXPECT(expectedAssignment == choices.at(0)); -#else - expectedAssignment = {{"A", 0}, {"B", 0}, {"C", 0}}; - EXPECT(expectedAssignment == choices.at(0)); -#endif -#ifdef GTSAM_DT_MERGING expectedAssignment = {{"A", 0}, {"B", 1}, {"C", 0}}; EXPECT(expectedAssignment == choices.at(1)); -#else - expectedAssignment = {{"A", 1}, {"B", 0}, {"C", 0}}; - EXPECT(expectedAssignment == choices.at(1)); -#endif -#ifdef GTSAM_DT_MERGING expectedAssignment = {{"A", 1}, {"B", 1}, {"C", 0}}; EXPECT(expectedAssignment == choices.at(2)); -#else - expectedAssignment = {{"A", 0}, {"B", 1}, {"C", 0}}; - EXPECT(expectedAssignment == choices.at(2)); -#endif -#ifdef GTSAM_DT_MERGING expectedAssignment = {{"B", 0}, {"C", 1}}; EXPECT(expectedAssignment == choices.at(3)); -#else - expectedAssignment = {{"A", 1}, {"B", 1}, {"C", 0}}; - EXPECT(expectedAssignment == choices.at(3)); -#endif -#ifdef GTSAM_DT_MERGING expectedAssignment = {{"A", 0}, {"B", 1}, {"C", 1}}; EXPECT(expectedAssignment == choices.at(4)); -#else - expectedAssignment = {{"A", 0}, {"B", 0}, {"C", 1}}; - EXPECT(expectedAssignment == choices.at(4)); -#endif -#ifdef GTSAM_DT_MERGING expectedAssignment = {{"A", 1}, {"B", 1}, {"C", 1}}; EXPECT(expectedAssignment == choices.at(5)); #else + expectedAssignment = {{"A", 0}, {"B", 0}, {"C", 0}}; + EXPECT(expectedAssignment == choices.at(0)); + + expectedAssignment = {{"A", 1}, {"B", 0}, {"C", 0}}; + EXPECT(expectedAssignment == choices.at(1)); + + expectedAssignment = {{"A", 0}, {"B", 1}, {"C", 0}}; + EXPECT(expectedAssignment == choices.at(2)); + + expectedAssignment = {{"A", 1}, {"B", 1}, {"C", 0}}; + EXPECT(expectedAssignment == choices.at(3)); + + expectedAssignment = {{"A", 0}, {"B", 0}, {"C", 1}}; + EXPECT(expectedAssignment == choices.at(4)); + expectedAssignment = {{"A", 1}, {"B", 0}, {"C", 1}}; EXPECT(expectedAssignment == choices.at(5)); #endif diff --git a/gtsam/discrete/tests/testDiscreteConditional.cpp b/gtsam/discrete/tests/testDiscreteConditional.cpp index 397e7ff0c..3b944b055 100644 --- a/gtsam/discrete/tests/testDiscreteConditional.cpp +++ b/gtsam/discrete/tests/testDiscreteConditional.cpp @@ -30,7 +30,7 @@ using namespace std; using namespace gtsam; /* ************************************************************************* */ -TEST_DISABLED(DiscreteConditional, constructors) { +TEST(DiscreteConditional, constructors) { DiscreteKey X(0, 2), Y(2, 3), Z(1, 2); // watch ordering ! DiscreteConditional actual(X | Y = "1/1 2/3 1/4"); @@ -51,7 +51,7 @@ TEST_DISABLED(DiscreteConditional, constructors) { } /* ************************************************************************* */ -TEST_DISABLED(DiscreteConditional, constructors_alt_interface) { +TEST(DiscreteConditional, constructors_alt_interface) { DiscreteKey X(0, 2), Y(2, 3), Z(1, 2); // watch ordering ! const Signature::Row r1{1, 1}, r2{2, 3}, r3{1, 4}; @@ -70,7 +70,7 @@ TEST_DISABLED(DiscreteConditional, constructors_alt_interface) { } /* ************************************************************************* */ -TEST_DISABLED(DiscreteConditional, constructors2) { +TEST(DiscreteConditional, constructors2) { DiscreteKey C(0, 2), B(1, 2); Signature signature((C | B) = "4/1 3/1"); DiscreteConditional actual(signature); @@ -80,7 +80,7 @@ TEST_DISABLED(DiscreteConditional, constructors2) { } /* ************************************************************************* */ -TEST_DISABLED(DiscreteConditional, constructors3) { +TEST(DiscreteConditional, constructors3) { DiscreteKey C(0, 2), B(1, 2), A(2, 2); Signature signature((C | B, A) = "4/1 1/1 1/1 1/4"); DiscreteConditional actual(signature); @@ -91,7 +91,7 @@ TEST_DISABLED(DiscreteConditional, constructors3) { /* ****************************************************************************/ // Test evaluate for a discrete Prior P(Asia). -TEST_DISABLED(DiscreteConditional, PriorProbability) { +TEST(DiscreteConditional, PriorProbability) { constexpr Key asiaKey = 0; const DiscreteKey Asia(asiaKey, 2); DiscreteConditional dc(Asia, "4/6"); @@ -102,7 +102,7 @@ TEST_DISABLED(DiscreteConditional, PriorProbability) { /* ************************************************************************* */ // Check that error, logProbability, evaluate all work as expected. -TEST_DISABLED(DiscreteConditional, probability) { +TEST(DiscreteConditional, probability) { DiscreteKey C(2, 2), D(4, 2), E(3, 2); DiscreteConditional C_given_DE((C | D, E) = "4/1 1/1 1/1 1/4"); @@ -116,7 +116,7 @@ TEST_DISABLED(DiscreteConditional, probability) { /* ************************************************************************* */ // Check calculation of joint P(A,B) -TEST_DISABLED(DiscreteConditional, Multiply) { +TEST(DiscreteConditional, Multiply) { DiscreteKey A(1, 2), B(0, 2); DiscreteConditional conditional(A | B = "1/2 2/1"); DiscreteConditional prior(B % "1/2"); @@ -141,7 +141,7 @@ TEST_DISABLED(DiscreteConditional, Multiply) { /* ************************************************************************* */ // Check calculation of conditional joint P(A,B|C) -TEST_DISABLED(DiscreteConditional, Multiply2) { +TEST(DiscreteConditional, Multiply2) { DiscreteKey A(0, 2), B(1, 2), C(2, 2); DiscreteConditional A_given_B(A | B = "1/3 3/1"); DiscreteConditional B_given_C(B | C = "1/3 3/1"); @@ -161,7 +161,7 @@ TEST_DISABLED(DiscreteConditional, Multiply2) { /* ************************************************************************* */ // Check calculation of conditional joint P(A,B|C), double check keys -TEST_DISABLED(DiscreteConditional, Multiply3) { +TEST(DiscreteConditional, Multiply3) { DiscreteKey A(1, 2), B(2, 2), C(0, 2); // different keys!!! DiscreteConditional A_given_B(A | B = "1/3 3/1"); DiscreteConditional B_given_C(B | C = "1/3 3/1"); @@ -181,7 +181,7 @@ TEST_DISABLED(DiscreteConditional, Multiply3) { /* ************************************************************************* */ // Check calculation of conditional joint P(A,B,C|D,E) = P(A,B|D) P(C|D,E) -TEST_DISABLED(DiscreteConditional, Multiply4) { +TEST(DiscreteConditional, Multiply4) { DiscreteKey A(0, 2), B(1, 2), C(2, 2), D(4, 2), E(3, 2); DiscreteConditional A_given_B(A | B = "1/3 3/1"); DiscreteConditional B_given_D(B | D = "1/3 3/1"); @@ -205,7 +205,7 @@ TEST_DISABLED(DiscreteConditional, Multiply4) { /* ************************************************************************* */ // Check calculation of marginals for joint P(A,B) -TEST_DISABLED(DiscreteConditional, marginals) { +TEST(DiscreteConditional, marginals) { DiscreteKey A(1, 2), B(0, 2); DiscreteConditional conditional(A | B = "1/2 2/1"); DiscreteConditional prior(B % "1/2"); @@ -227,7 +227,7 @@ TEST_DISABLED(DiscreteConditional, marginals) { /* ************************************************************************* */ // Check calculation of marginals in case branches are pruned -TEST_DISABLED(DiscreteConditional, marginals2) { +TEST(DiscreteConditional, marginals2) { DiscreteKey A(0, 2), B(1, 2); // changing keys need to make pruning happen! DiscreteConditional conditional(A | B = "2/2 3/1"); DiscreteConditional prior(B % "1/2"); @@ -243,7 +243,7 @@ TEST_DISABLED(DiscreteConditional, marginals2) { } /* ************************************************************************* */ -TEST_DISABLED(DiscreteConditional, likelihood) { +TEST(DiscreteConditional, likelihood) { DiscreteKey X(0, 2), Y(1, 3); DiscreteConditional conditional(X | Y = "2/8 4/6 5/5"); @@ -258,7 +258,7 @@ TEST_DISABLED(DiscreteConditional, likelihood) { /* ************************************************************************* */ // Check choose on P(C|D,E) -TEST_DISABLED(DiscreteConditional, choose) { +TEST(DiscreteConditional, choose) { DiscreteKey C(2, 2), D(4, 2), E(3, 2); DiscreteConditional C_given_DE((C | D, E) = "4/1 1/1 1/1 1/4"); @@ -286,7 +286,7 @@ TEST_DISABLED(DiscreteConditional, choose) { /* ************************************************************************* */ // Check markdown representation looks as expected, no parents. -TEST_DISABLED(DiscreteConditional, markdown_prior) { +TEST(DiscreteConditional, markdown_prior) { DiscreteKey A(Symbol('x', 1), 3); DiscreteConditional conditional(A % "1/2/2"); string expected = @@ -302,7 +302,7 @@ TEST_DISABLED(DiscreteConditional, markdown_prior) { /* ************************************************************************* */ // Check markdown representation looks as expected, no parents + names. -TEST_DISABLED(DiscreteConditional, markdown_prior_names) { +TEST(DiscreteConditional, markdown_prior_names) { Symbol x1('x', 1); DiscreteKey A(x1, 3); DiscreteConditional conditional(A % "1/2/2"); @@ -320,7 +320,7 @@ TEST_DISABLED(DiscreteConditional, markdown_prior_names) { /* ************************************************************************* */ // Check markdown representation looks as expected, multivalued. -TEST_DISABLED(DiscreteConditional, markdown_multivalued) { +TEST(DiscreteConditional, markdown_multivalued) { DiscreteKey A(Symbol('a', 1), 3), B(Symbol('b', 1), 5); DiscreteConditional conditional( A | B = "2/88/10 2/20/78 33/33/34 33/33/34 95/2/3"); @@ -339,7 +339,7 @@ TEST_DISABLED(DiscreteConditional, markdown_multivalued) { /* ************************************************************************* */ // Check markdown representation looks as expected, two parents + names. -TEST_DISABLED(DiscreteConditional, markdown) { +TEST(DiscreteConditional, markdown) { DiscreteKey A(2, 2), B(1, 2), C(0, 3); DiscreteConditional conditional(A, {B, C}, "0/1 1/3 1/1 3/1 0/1 1/0"); string expected = @@ -362,7 +362,7 @@ TEST_DISABLED(DiscreteConditional, markdown) { /* ************************************************************************* */ // Check html representation looks as expected, two parents + names. -TEST_DISABLED(DiscreteConditional, html) { +TEST(DiscreteConditional, html) { DiscreteKey A(2, 2), B(1, 2), C(0, 3); DiscreteConditional conditional(A, {B, C}, "0/1 1/3 1/1 3/1 0/1 1/0"); string expected = From 4e9d8495957d337e585c1cae9563b1ad9e23317f Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Fri, 28 Jul 2023 15:51:00 -0400 Subject: [PATCH 05/10] remove prints --- gtsam/hybrid/HybridBayesNet.cpp | 15 +-------------- gtsam/hybrid/HybridGaussianFactorGraph.cpp | 3 +-- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/gtsam/hybrid/HybridBayesNet.cpp b/gtsam/hybrid/HybridBayesNet.cpp index 1a9a9dcc5..3bafe5a9c 100644 --- a/gtsam/hybrid/HybridBayesNet.cpp +++ b/gtsam/hybrid/HybridBayesNet.cpp @@ -139,27 +139,16 @@ DecisionTreeFactor HybridBayesNet::pruneDiscreteConditionals( for (size_t i = 0; i < this->size(); i++) { auto conditional = this->at(i); if (conditional->isDiscrete()) { - std::cout << ">>>" << std::endl; - conditional->print(); discreteProbs = discreteProbs * (*conditional->asDiscrete()); - // discreteProbs.print(); - // std::cout << "================\n" << std::endl; Ordering conditional_keys(conditional->frontals()); discrete_frontals += conditional_keys; discrete_factor_idxs.push_back(i); } } - std::cout << "Original Joint Prob:" << std::endl; - std::cout << discreteProbs.nrAssignments() << std::endl; - discreteProbs.print(); + const DecisionTreeFactor prunedDiscreteProbs = discreteProbs.prune(maxNrLeaves); - std::cout << "Pruned Joint Prob:" << std::endl; - std::cout << prunedDiscreteProbs.nrAssignments() << std::endl; - prunedDiscreteProbs.print(); - std::cout << "\n\n\n"; - gttoc_(HybridBayesNet_PruneDiscreteConditionals); // Eliminate joint probability back into conditionals DiscreteFactorGraph dfg{prunedDiscreteProbs}; @@ -168,8 +157,6 @@ DecisionTreeFactor HybridBayesNet::pruneDiscreteConditionals( // Assign pruned discrete conditionals back at the correct indices. for (size_t i = 0; i < discrete_factor_idxs.size(); i++) { size_t idx = discrete_factor_idxs.at(i); - // std::cout << i << std::endl; - // dbn->at(i)->print(); this->at(idx) = std::make_shared(dbn->at(i)); } diff --git a/gtsam/hybrid/HybridGaussianFactorGraph.cpp b/gtsam/hybrid/HybridGaussianFactorGraph.cpp index ee36ecc87..191e13d76 100644 --- a/gtsam/hybrid/HybridGaussianFactorGraph.cpp +++ b/gtsam/hybrid/HybridGaussianFactorGraph.cpp @@ -96,7 +96,6 @@ static GaussianFactorGraphTree addGaussian( // TODO(dellaert): it's probably more efficient to first collect the discrete // keys, and then loop over all assignments to populate a vector. GaussianFactorGraphTree HybridGaussianFactorGraph::assembleGraphTree() const { - GaussianFactorGraphTree result; for (auto &f : factors_) { @@ -175,7 +174,7 @@ discreteElimination(const HybridGaussianFactorGraph &factors, throwRuntimeError("continuousElimination", f); } } - dfg.print("The DFG to eliminate"); + // NOTE: This does sum-product. For max-product, use EliminateForMPE. auto result = EliminateDiscrete(dfg, frontalKeys); From 4580c510ea384b5897bf3b5d32fcdef80fae49d8 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Fri, 28 Jul 2023 15:51:59 -0400 Subject: [PATCH 06/10] undo change --- gtsam/hybrid/HybridGaussianFactorGraph.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/gtsam/hybrid/HybridGaussianFactorGraph.cpp b/gtsam/hybrid/HybridGaussianFactorGraph.cpp index 191e13d76..7a7ca0cbf 100644 --- a/gtsam/hybrid/HybridGaussianFactorGraph.cpp +++ b/gtsam/hybrid/HybridGaussianFactorGraph.cpp @@ -96,6 +96,7 @@ static GaussianFactorGraphTree addGaussian( // TODO(dellaert): it's probably more efficient to first collect the discrete // keys, and then loop over all assignments to populate a vector. GaussianFactorGraphTree HybridGaussianFactorGraph::assembleGraphTree() const { + GaussianFactorGraphTree result; for (auto &f : factors_) { From 8cb33dd4f89414f262c38d1d3dd851894115a6f6 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Sat, 29 Jul 2023 10:10:57 -0400 Subject: [PATCH 07/10] remove make_unique flag --- gtsam/discrete/DecisionTree-inl.h | 19 ++++++------------- gtsam/discrete/DecisionTree.h | 8 +++----- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/gtsam/discrete/DecisionTree-inl.h b/gtsam/discrete/DecisionTree-inl.h index 541bd77f4..413b4a924 100644 --- a/gtsam/discrete/DecisionTree-inl.h +++ b/gtsam/discrete/DecisionTree-inl.h @@ -149,8 +149,7 @@ namespace gtsam { } /** choose a branch, create new memory ! */ - NodePtr choose(const L& label, size_t index, - bool make_unique = true) const override { + NodePtr choose(const L& label, size_t index) const override { return NodePtr(new Leaf(constant(), 1)); } @@ -469,22 +468,16 @@ namespace gtsam { } /** choose a branch, recursively */ - NodePtr choose(const L& label, size_t index, - bool make_unique = true) const override { + NodePtr choose(const L& label, size_t index) const override { if (label_ == label) return branches_[index]; // choose branch // second case, not label of interest, just recurse auto r = std::make_shared(label_, branches_.size()); for (auto&& branch : branches_) { - r->push_back(branch->choose(label, index, make_unique)); + r->push_back(branch->choose(label, index)); } - if (make_unique) { - return Unique(r); - } else { - return r; - } - // return Unique(r); + return Unique(r); } private: @@ -1006,9 +999,9 @@ namespace gtsam { template DecisionTree DecisionTree::combine(const L& label, size_t cardinality, const Binary& op) const { - DecisionTree result = choose(label, 0, false); + DecisionTree result = choose(label, 0); for (size_t index = 1; index < cardinality; index++) { - DecisionTree chosen = choose(label, index, false); + DecisionTree chosen = choose(label, index); result = result.apply(chosen, op); } return result; diff --git a/gtsam/discrete/DecisionTree.h b/gtsam/discrete/DecisionTree.h index 7761de84e..48a1a4596 100644 --- a/gtsam/discrete/DecisionTree.h +++ b/gtsam/discrete/DecisionTree.h @@ -129,8 +129,7 @@ namespace gtsam { virtual Ptr apply_f_op_g(const Node&, const Binary&) const = 0; virtual Ptr apply_g_op_fL(const Leaf&, const Binary&) const = 0; virtual Ptr apply_g_op_fC(const Choice&, const Binary&) const = 0; - virtual Ptr choose(const L& label, size_t index, - bool make_unique = true) const = 0; + virtual Ptr choose(const L& label, size_t index) const = 0; virtual bool isLeaf() const = 0; private: @@ -404,9 +403,8 @@ namespace gtsam { /** create a new function where value(label)==index * It's like "restrict" in Darwiche09book pg329, 330? */ - DecisionTree choose(const L& label, size_t index, - bool make_unique = true) const { - NodePtr newRoot = root_->choose(label, index, make_unique); + DecisionTree choose(const L& label, size_t index) const { + NodePtr newRoot = root_->choose(label, index); return DecisionTree(newRoot); } From 94d737e1db7aadef5751f1ad93e2f7ae3b1aea2c Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Sat, 29 Jul 2023 10:13:56 -0400 Subject: [PATCH 08/10] remove printing --- gtsam/discrete/DiscreteFactorGraph.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/gtsam/discrete/DiscreteFactorGraph.cpp b/gtsam/discrete/DiscreteFactorGraph.cpp index e0d2ed4de..4ededbb8b 100644 --- a/gtsam/discrete/DiscreteFactorGraph.cpp +++ b/gtsam/discrete/DiscreteFactorGraph.cpp @@ -204,18 +204,12 @@ namespace gtsam { std::pair // EliminateDiscrete(const DiscreteFactorGraph& factors, const Ordering& frontalKeys) { - factors.print("The Factors to eliminate:"); // PRODUCT: multiply all factors gttic(product); DecisionTreeFactor product; for (auto&& factor : factors) product = (*factor) * product; gttoc(product); - std::cout << "\n\n==========" << std::endl; - std::cout << "Product" << std::endl; - std::cout << std::endl; - product.print(); - // Max over all the potentials by pretending all keys are frontal: auto normalization = product.max(product.size()); @@ -227,10 +221,6 @@ namespace gtsam { DecisionTreeFactor::shared_ptr sum = product.sum(frontalKeys); gttoc(sum); - std::cout << "\n->Sum" << std::endl; - sum->print(); - std::cout << "----------------------" << std::endl; - // Ordering keys for the conditional so that frontalKeys are really in front Ordering orderedKeys; orderedKeys.insert(orderedKeys.end(), frontalKeys.begin(), From 4386c5114c82ea4ce68030186c8a4bce83ea42ae Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Mon, 6 Nov 2023 09:36:30 -0500 Subject: [PATCH 09/10] remove nrAssignments from DecisionTree --- gtsam/discrete/DecisionTree-inl.h | 47 ++------ gtsam/discrete/DecisionTree.h | 36 ------ gtsam/discrete/tests/testDecisionTree.cpp | 85 ------------- .../tests/testDiscreteConditional.cpp | 66 ----------- .../tests/testDiscreteFactorGraph.cpp | 112 ------------------ .../tests/testGaussianMixtureFactor.cpp | 4 +- .../tests/testHybridNonlinearFactorGraph.cpp | 58 ++++----- gtsam/hybrid/tests/testMixtureFactor.cpp | 4 +- 8 files changed, 41 insertions(+), 371 deletions(-) diff --git a/gtsam/discrete/DecisionTree-inl.h b/gtsam/discrete/DecisionTree-inl.h index 413b4a924..06161c2e1 100644 --- a/gtsam/discrete/DecisionTree-inl.h +++ b/gtsam/discrete/DecisionTree-inl.h @@ -53,26 +53,17 @@ namespace gtsam { /** constant stored in this leaf */ Y constant_; - /** The number of assignments contained within this leaf. - * Particularly useful when leaves have been pruned. - */ - size_t nrAssignments_; - /// Default constructor for serialization. Leaf() {} /// Constructor from constant - Leaf(const Y& constant, size_t nrAssignments = 1) - : constant_(constant), nrAssignments_(nrAssignments) {} + Leaf(const Y& constant) : constant_(constant) {} /// Return the constant const Y& constant() const { return constant_; } - /// Return the number of assignments contained within this leaf. - size_t nrAssignments() const { return nrAssignments_; } - /// Leaf-Leaf equality bool sameLeaf(const Leaf& q) const override { return constant_ == q.constant_; @@ -93,8 +84,7 @@ namespace gtsam { /// print void print(const std::string& s, const LabelFormatter& labelFormatter, const ValueFormatter& valueFormatter) const override { - std::cout << s << " Leaf [" << nrAssignments() << "] " - << valueFormatter(constant_) << std::endl; + std::cout << s << " Leaf " << valueFormatter(constant_) << std::endl; } /** Write graphviz format to stream `os`. */ @@ -114,14 +104,14 @@ namespace gtsam { /** apply unary operator */ NodePtr apply(const Unary& op) const override { - NodePtr f(new Leaf(op(constant_), nrAssignments_)); + NodePtr f(new Leaf(op(constant_))); return f; } /// Apply unary operator with assignment NodePtr apply(const UnaryAssignment& op, const Assignment& assignment) const override { - NodePtr f(new Leaf(op(assignment, constant_), nrAssignments_)); + NodePtr f(new Leaf(op(assignment, constant_))); return f; } @@ -137,9 +127,7 @@ namespace gtsam { // Applying binary operator to two leaves results in a leaf NodePtr apply_g_op_fL(const Leaf& fL, const Binary& op) const override { // fL op gL - // The nrAssignments is always set to fL since we consider g operating on - // (or modifying) f. - NodePtr h(new Leaf(op(fL.constant_, constant_), fL.nrAssignments())); + NodePtr h(new Leaf(op(fL.constant_, constant_))); return h; } @@ -150,7 +138,7 @@ namespace gtsam { /** choose a branch, create new memory ! */ NodePtr choose(const L& label, size_t index) const override { - return NodePtr(new Leaf(constant(), 1)); + return NodePtr(new Leaf(constant())); } bool isLeaf() const override { return true; } @@ -165,7 +153,6 @@ namespace gtsam { void serialize(ARCHIVE& ar, const unsigned int /*version*/) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base); ar& BOOST_SERIALIZATION_NVP(constant_); - ar& BOOST_SERIALIZATION_NVP(nrAssignments_); } #endif }; // Leaf @@ -235,16 +222,8 @@ namespace gtsam { assert(f->branches().size() > 0); NodePtr f0 = f->branches_[0]; - // Compute total number of assignments - size_t nrAssignments = 0; - for (auto branch : f->branches()) { - if (auto leaf = std::dynamic_pointer_cast(branch)) { - nrAssignments += leaf->nrAssignments(); - } - } NodePtr newLeaf( - new Leaf(std::dynamic_pointer_cast(f0)->constant(), - nrAssignments)); + new Leaf(std::dynamic_pointer_cast(f0)->constant())); return newLeaf; } #endif @@ -732,7 +711,7 @@ namespace gtsam { // If leaf, apply unary conversion "op" and create a unique leaf. using MXLeaf = typename DecisionTree::Leaf; if (auto leaf = std::dynamic_pointer_cast(f)) { - return NodePtr(new Leaf(Y_of_X(leaf->constant()), leaf->nrAssignments())); + return NodePtr(new Leaf(Y_of_X(leaf->constant()))); } // Check if Choice @@ -879,16 +858,6 @@ namespace gtsam { return total; } - /****************************************************************************/ - template - size_t DecisionTree::nrAssignments() const { - size_t n = 0; - this->visitLeaf([&n](const DecisionTree::Leaf& leaf) { - n += leaf.nrAssignments(); - }); - return n; - } - /****************************************************************************/ // fold is just done with a visit template diff --git a/gtsam/discrete/DecisionTree.h b/gtsam/discrete/DecisionTree.h index 48a1a4596..f1ad6e553 100644 --- a/gtsam/discrete/DecisionTree.h +++ b/gtsam/discrete/DecisionTree.h @@ -328,42 +328,6 @@ namespace gtsam { /// Return the number of leaves in the tree. size_t nrLeaves() const; - /** - * @brief This is a convenience function which returns the total number of - * leaf assignments in the decision tree. - * This function is not used for anymajor operations within the discrete - * factor graph framework. - * - * Leaf assignments represent the cardinality of each leaf node, e.g. in a - * binary tree each leaf has 2 assignments. This includes counts removed - * from implicit pruning hence, it will always be >= nrLeaves(). - * - * E.g. we have a decision tree as below, where each node has 2 branches: - * - * Choice(m1) - * 0 Choice(m0) - * 0 0 Leaf 0.0 - * 0 1 Leaf 0.0 - * 1 Choice(m0) - * 1 0 Leaf 1.0 - * 1 1 Leaf 2.0 - * - * In the unpruned form, the tree will have 4 assignments, 2 for each key, - * and 4 leaves. - * - * In the pruned form, the number of assignments is still 4 but the number - * of leaves is now 3, as below: - * - * Choice(m1) - * 0 Leaf 0.0 - * 1 Choice(m0) - * 1 0 Leaf 1.0 - * 1 1 Leaf 2.0 - * - * @return size_t - */ - size_t nrAssignments() const; - /** * @brief Fold a binary function over the tree, returning accumulator. * diff --git a/gtsam/discrete/tests/testDecisionTree.cpp b/gtsam/discrete/tests/testDecisionTree.cpp index 54e5b3de9..c625e1ba6 100644 --- a/gtsam/discrete/tests/testDecisionTree.cpp +++ b/gtsam/discrete/tests/testDecisionTree.cpp @@ -370,59 +370,6 @@ TEST(DecisionTree, Containers) { StringContainerTree converted(stringIntTree, container_of_int); } -/* ************************************************************************** */ -// Test nrAssignments. -TEST(DecisionTree, NrAssignments) { - const std::pair A("A", 2), B("B", 2), C("C", 2); - DT tree({A, B, C}, "1 1 1 1 1 1 1 1"); - - EXPECT_LONGS_EQUAL(8, tree.nrAssignments()); - -#ifdef GTSAM_DT_MERGING - EXPECT(tree.root_->isLeaf()); - auto leaf = std::dynamic_pointer_cast(tree.root_); - EXPECT_LONGS_EQUAL(8, leaf->nrAssignments()); -#endif - - DT tree2({C, B, A}, "1 1 1 2 3 4 5 5"); - /* The tree is - Choice(C) - 0 Choice(B) - 0 0 Leaf 1 - 0 1 Choice(A) - 0 1 0 Leaf 1 - 0 1 1 Leaf 2 - 1 Choice(B) - 1 0 Choice(A) - 1 0 0 Leaf 3 - 1 0 1 Leaf 4 - 1 1 Leaf 5 - */ - - EXPECT_LONGS_EQUAL(8, tree2.nrAssignments()); - - auto root = std::dynamic_pointer_cast(tree2.root_); - CHECK(root); - auto choice0 = std::dynamic_pointer_cast(root->branches()[0]); - CHECK(choice0); - -#ifdef GTSAM_DT_MERGING - EXPECT(choice0->branches()[0]->isLeaf()); - auto choice00 = std::dynamic_pointer_cast(choice0->branches()[0]); - CHECK(choice00); - EXPECT_LONGS_EQUAL(2, choice00->nrAssignments()); - - auto choice1 = std::dynamic_pointer_cast(root->branches()[1]); - CHECK(choice1); - auto choice10 = std::dynamic_pointer_cast(choice1->branches()[0]); - CHECK(choice10); - auto choice11 = std::dynamic_pointer_cast(choice1->branches()[1]); - CHECK(choice11); - EXPECT(choice11->isLeaf()); - EXPECT_LONGS_EQUAL(2, choice11->nrAssignments()); -#endif -} - /* ************************************************************************** */ // Test visit. TEST(DecisionTree, visit) { @@ -625,38 +572,6 @@ TEST(DecisionTree, ApplyWithAssignment) { #endif } -/* ************************************************************************** */ -// Test number of assignments. -TEST(DecisionTree, NrAssignments2) { - using gtsam::symbol_shorthand::M; - - std::vector probs = {0, 0, 1, 2}; - - /* Create the decision tree - Choice(m1) - 0 Leaf 0.000000 - 1 Choice(m0) - 1 0 Leaf 1.000000 - 1 1 Leaf 2.000000 - */ - DiscreteKeys keys{{M(1), 2}, {M(0), 2}}; - DecisionTree dt1(keys, probs); - EXPECT_LONGS_EQUAL(4, dt1.nrAssignments()); - - /* Create the DecisionTree - Choice(m1) - 0 Choice(m0) - 0 0 Leaf 0.000000 - 0 1 Leaf 1.000000 - 1 Choice(m0) - 1 0 Leaf 0.000000 - 1 1 Leaf 2.000000 - */ - DiscreteKeys keys2{{M(0), 2}, {M(1), 2}}; - DecisionTree dt2(keys2, probs); - EXPECT_LONGS_EQUAL(4, dt2.nrAssignments()); -} - /* ************************************************************************* */ int main() { TestResult tr; diff --git a/gtsam/discrete/tests/testDiscreteConditional.cpp b/gtsam/discrete/tests/testDiscreteConditional.cpp index 3b944b055..dae279713 100644 --- a/gtsam/discrete/tests/testDiscreteConditional.cpp +++ b/gtsam/discrete/tests/testDiscreteConditional.cpp @@ -390,72 +390,6 @@ TEST(DiscreteConditional, html) { EXPECT(actual == expected); } -/* ************************************************************************* */ -TEST(DiscreteConditional, NrAssignments) { -#ifdef GTSAM_DT_MERGING - string expected = R"( P( 0 1 2 ): - Choice(2) - 0 Choice(1) - 0 0 Leaf [2] 0 - 0 1 Choice(0) - 0 1 0 Leaf [1] 0.27527634 - 0 1 1 Leaf [1] 0 - 1 Choice(1) - 1 0 Leaf [2] 0 - 1 1 Choice(0) - 1 1 0 Leaf [1] 0.44944733 - 1 1 1 Leaf [1] 0.27527634 - -)"; -#else - string expected = R"( P( 0 1 2 ): - Choice(2) - 0 Choice(1) - 0 0 Choice(0) - 0 0 0 Leaf [1] 0 - 0 0 1 Leaf [1] 0 - 0 1 Choice(0) - 0 1 0 Leaf [1] 0.27527634 - 0 1 1 Leaf [1] 0.44944733 - 1 Choice(1) - 1 0 Choice(0) - 1 0 0 Leaf [1] 0 - 1 0 1 Leaf [1] 0 - 1 1 Choice(0) - 1 1 0 Leaf [1] 0 - 1 1 1 Leaf [1] 0.27527634 - -)"; -#endif - - DiscreteKeys d0{{0, 2}, {1, 2}, {2, 2}}; - std::vector p0 = {0, 0, 0.17054468, 0.27845056, 0, 0, 0, 0.17054468}; - AlgebraicDecisionTree dt(d0, p0); - DecisionTreeFactor dtf(d0, dt); - DiscreteConditional f0(3, dtf); - - EXPECT(assert_print_equal(expected, f0)); - - DiscreteFactorGraph dfg{f0}; - dfg.print(); - auto dbn = dfg.eliminateSequential(); - dbn->print(); - - // DiscreteKeys d0{{0, 2}, {1, 2}}; - // std::vector p0 = {0, 1, 0, 2}; - // AlgebraicDecisionTree dt0(d0, p0); - // dt0.print("", DefaultKeyFormatter); - - // DiscreteKeys d1{{0, 2}}; - // std::vector p1 = {1, 1, 1, 1}; - // AlgebraicDecisionTree dt1(d0, p1); - // dt1.print("", DefaultKeyFormatter); - - // auto dd = dt0 / dt1; - // dd.print("", DefaultKeyFormatter); -} - - /* ************************************************************************* */ int main() { TestResult tr; diff --git a/gtsam/discrete/tests/testDiscreteFactorGraph.cpp b/gtsam/discrete/tests/testDiscreteFactorGraph.cpp index 871583a2c..341eb63e3 100644 --- a/gtsam/discrete/tests/testDiscreteFactorGraph.cpp +++ b/gtsam/discrete/tests/testDiscreteFactorGraph.cpp @@ -349,118 +349,6 @@ TEST(DiscreteFactorGraph, markdown) { EXPECT_DOUBLES_EQUAL(0.3, graph[0]->operator()(values), 1e-9); } -/* ************************************************************************* */ -TEST(DiscreteFactorGraph, NrAssignments) { -#ifdef GTSAM_DT_MERGING - string expected_dfg = R"( -size: 2 -factor 0: f[ (m0,2), (m1,2), (m2,2), ] - Choice(m2) - 0 Choice(m1) - 0 0 Leaf [2] 0 - 0 1 Choice(m0) - 0 1 0 Leaf [1] 0.17054468 - 0 1 1 Leaf [1] 0 - 1 Choice(m1) - 1 0 Leaf [2] 0 - 1 1 Choice(m0) - 1 1 0 Leaf [1] 0.27845056 - 1 1 1 Leaf [1] 0.17054468 -factor 1: f[ (m0,2), (m1,2), (m2,2), (m3,2), ] - Choice(m3) - 0 Choice(m2) - 0 0 Choice(m1) - 0 0 0 Leaf [2] 1 - 0 0 1 Leaf [2] 0.015366387 - 0 1 Choice(m1) - 0 1 0 Leaf [2] 1 - 0 1 1 Choice(m0) - 0 1 1 0 Leaf [1] 1 - 0 1 1 1 Leaf [1] 0.015365663 - 1 Choice(m2) - 1 0 Choice(m1) - 1 0 0 Leaf [2] 1 - 1 0 1 Choice(m0) - 1 0 1 0 Leaf [1] 0.0094115739 - 1 0 1 1 Leaf [1] 0.0094115652 - 1 1 Choice(m1) - 1 1 0 Leaf [2] 1 - 1 1 1 Choice(m0) - 1 1 1 0 Leaf [1] 1 - 1 1 1 1 Leaf [1] 0.009321081 -)"; -#else - string expected_dfg = R"( -size: 2 -factor 0: f[ (m0,2), (m1,2), (m2,2), ] - Choice(m2) - 0 Choice(m1) - 0 0 Choice(m0) - 0 0 0 Leaf [1] 0 - 0 0 1 Leaf [1] 0 - 0 1 Choice(m0) - 0 1 0 Leaf [1] 0.27527634 - 0 1 1 Leaf [1] 0.44944733 - 1 Choice(m1) - 1 0 Choice(m0) - 1 0 0 Leaf [1] 0 - 1 0 1 Leaf [1] 0 - 1 1 Choice(m0) - 1 1 0 Leaf [1] 0 - 1 1 1 Leaf [1] 0.27527634 -factor 1: f[ (m0,2), (m1,2), (m2,2), (m3,2), ] - Choice(m3) - 0 Choice(m2) - 0 0 Choice(m1) - 0 0 0 Choice(m0) - 0 0 0 0 Leaf [1] 1 - 0 0 0 1 Leaf [1] 1 - 0 0 1 Choice(m0) - 0 0 1 0 Leaf [1] 0.015366387 - 0 0 1 1 Leaf [1] 0.015366387 - 0 1 Choice(m1) - 0 1 0 Choice(m0) - 0 1 0 0 Leaf [1] 1 - 0 1 0 1 Leaf [1] 1 - 0 1 1 Choice(m0) - 0 1 1 0 Leaf [1] 1 - 0 1 1 1 Leaf [1] 0.015365663 - 1 Choice(m2) - 1 0 Choice(m1) - 1 0 0 Choice(m0) - 1 0 0 0 Leaf [1] 1 - 1 0 0 1 Leaf [1] 1 - 1 0 1 Choice(m0) - 1 0 1 0 Leaf [1] 0.0094115739 - 1 0 1 1 Leaf [1] 0.0094115652 - 1 1 Choice(m1) - 1 1 0 Choice(m0) - 1 1 0 0 Leaf [1] 1 - 1 1 0 1 Leaf [1] 1 - 1 1 1 Choice(m0) - 1 1 1 0 Leaf [1] 1 - 1 1 1 1 Leaf [1] 0.009321081 -)"; -#endif - - DiscreteKeys d0{{M(0), 2}, {M(1), 2}, {M(2), 2}}; - std::vector p0 = {0, 0, 0.17054468, 0.27845056, 0, 0, 0, 0.17054468}; - AlgebraicDecisionTree dt(d0, p0); - DiscreteConditional f0(3, d0, dt); - - DiscreteKeys d1{{M(0), 2}, {M(1), 2}, {M(2), 2}, {M(3), 2}}; - std::vector p1 = { - 1, 1, 1, 1, 0.015366387, 0.0094115739, 1, 1, - 1, 1, 1, 1, 0.015366387, 0.0094115652, 0.015365663, 0.009321081}; - DecisionTreeFactor f1(d1, p1); - - DiscreteFactorGraph dfg; - dfg.add(f0); - dfg.add(f1); - - EXPECT(assert_print_equal(expected_dfg, dfg)); -} - /* ************************************************************************* */ int main() { TestResult tr; diff --git a/gtsam/hybrid/tests/testGaussianMixtureFactor.cpp b/gtsam/hybrid/tests/testGaussianMixtureFactor.cpp index 549223497..75ba5a059 100644 --- a/gtsam/hybrid/tests/testGaussianMixtureFactor.cpp +++ b/gtsam/hybrid/tests/testGaussianMixtureFactor.cpp @@ -108,7 +108,7 @@ TEST(GaussianMixtureFactor, Printing) { std::string expected = R"(Hybrid [x1 x2; 1]{ Choice(1) - 0 Leaf [1] : + 0 Leaf : A[x1] = [ 0; 0 @@ -120,7 +120,7 @@ TEST(GaussianMixtureFactor, Printing) { b = [ 0 0 ] No noise model - 1 Leaf [1] : + 1 Leaf : A[x1] = [ 0; 0 diff --git a/gtsam/hybrid/tests/testHybridNonlinearFactorGraph.cpp b/gtsam/hybrid/tests/testHybridNonlinearFactorGraph.cpp index 379eff27b..a493de5c5 100644 --- a/gtsam/hybrid/tests/testHybridNonlinearFactorGraph.cpp +++ b/gtsam/hybrid/tests/testHybridNonlinearFactorGraph.cpp @@ -493,7 +493,7 @@ factor 0: factor 1: Hybrid [x0 x1; m0]{ Choice(m0) - 0 Leaf [1] : + 0 Leaf : A[x0] = [ -1 ] @@ -503,7 +503,7 @@ Hybrid [x0 x1; m0]{ b = [ -1 ] No noise model - 1 Leaf [1] : + 1 Leaf : A[x0] = [ -1 ] @@ -517,7 +517,7 @@ Hybrid [x0 x1; m0]{ factor 2: Hybrid [x1 x2; m1]{ Choice(m1) - 0 Leaf [1] : + 0 Leaf : A[x1] = [ -1 ] @@ -527,7 +527,7 @@ Hybrid [x1 x2; m1]{ b = [ -1 ] No noise model - 1 Leaf [1] : + 1 Leaf : A[x1] = [ -1 ] @@ -551,16 +551,16 @@ factor 4: b = [ -10 ] No noise model factor 5: P( m0 ): - Leaf [2] 0.5 + Leaf 0.5 factor 6: P( m1 | m0 ): Choice(m1) 0 Choice(m0) - 0 0 Leaf [1] 0.33333333 - 0 1 Leaf [1] 0.6 + 0 0 Leaf 0.33333333 + 0 1 Leaf 0.6 1 Choice(m0) - 1 0 Leaf [1] 0.66666667 - 1 1 Leaf [1] 0.4 + 1 0 Leaf 0.66666667 + 1 1 Leaf 0.4 )"; #else @@ -575,7 +575,7 @@ factor 0: factor 1: Hybrid [x0 x1; m0]{ Choice(m0) - 0 Leaf [1]: + 0 Leaf: A[x0] = [ -1 ] @@ -585,7 +585,7 @@ Hybrid [x0 x1; m0]{ b = [ -1 ] No noise model - 1 Leaf [1]: + 1 Leaf: A[x0] = [ -1 ] @@ -599,7 +599,7 @@ Hybrid [x0 x1; m0]{ factor 2: Hybrid [x1 x2; m1]{ Choice(m1) - 0 Leaf [1]: + 0 Leaf: A[x1] = [ -1 ] @@ -609,7 +609,7 @@ Hybrid [x1 x2; m1]{ b = [ -1 ] No noise model - 1 Leaf [1]: + 1 Leaf: A[x1] = [ -1 ] @@ -634,17 +634,17 @@ factor 4: No noise model factor 5: P( m0 ): Choice(m0) - 0 Leaf [1] 0.5 - 1 Leaf [1] 0.5 + 0 Leaf 0.5 + 1 Leaf 0.5 factor 6: P( m1 | m0 ): Choice(m1) 0 Choice(m0) - 0 0 Leaf [1]0.33333333 - 0 1 Leaf [1] 0.6 + 0 0 Leaf 0.33333333 + 0 1 Leaf 0.6 1 Choice(m0) - 1 0 Leaf [1]0.66666667 - 1 1 Leaf [1] 0.4 + 1 0 Leaf 0.66666667 + 1 1 Leaf 0.4 )"; #endif @@ -657,13 +657,13 @@ size: 3 conditional 0: Hybrid P( x0 | x1 m0) Discrete Keys = (m0, 2), Choice(m0) - 0 Leaf [1] p(x0 | x1) + 0 Leaf p(x0 | x1) R = [ 10.0499 ] S[x1] = [ -0.0995037 ] d = [ -9.85087 ] No noise model - 1 Leaf [1] p(x0 | x1) + 1 Leaf p(x0 | x1) R = [ 10.0499 ] S[x1] = [ -0.0995037 ] d = [ -9.95037 ] @@ -673,26 +673,26 @@ conditional 1: Hybrid P( x1 | x2 m0 m1) Discrete Keys = (m0, 2), (m1, 2), Choice(m1) 0 Choice(m0) - 0 0 Leaf [1] p(x1 | x2) + 0 0 Leaf p(x1 | x2) R = [ 10.099 ] S[x2] = [ -0.0990196 ] d = [ -9.99901 ] No noise model - 0 1 Leaf [1] p(x1 | x2) + 0 1 Leaf p(x1 | x2) R = [ 10.099 ] S[x2] = [ -0.0990196 ] d = [ -9.90098 ] No noise model 1 Choice(m0) - 1 0 Leaf [1] p(x1 | x2) + 1 0 Leaf p(x1 | x2) R = [ 10.099 ] S[x2] = [ -0.0990196 ] d = [ -10.098 ] No noise model - 1 1 Leaf [1] p(x1 | x2) + 1 1 Leaf p(x1 | x2) R = [ 10.099 ] S[x2] = [ -0.0990196 ] d = [ -10 ] @@ -702,14 +702,14 @@ conditional 2: Hybrid P( x2 | m0 m1) Discrete Keys = (m0, 2), (m1, 2), Choice(m1) 0 Choice(m0) - 0 0 Leaf [1] p(x2) + 0 0 Leaf p(x2) R = [ 10.0494 ] d = [ -10.1489 ] mean: 1 elements x2: -1.0099 No noise model - 0 1 Leaf [1] p(x2) + 0 1 Leaf p(x2) R = [ 10.0494 ] d = [ -10.1479 ] mean: 1 elements @@ -717,14 +717,14 @@ conditional 2: Hybrid P( x2 | m0 m1) No noise model 1 Choice(m0) - 1 0 Leaf [1] p(x2) + 1 0 Leaf p(x2) R = [ 10.0494 ] d = [ -10.0504 ] mean: 1 elements x2: -1.0001 No noise model - 1 1 Leaf [1] p(x2) + 1 1 Leaf p(x2) R = [ 10.0494 ] d = [ -10.0494 ] mean: 1 elements diff --git a/gtsam/hybrid/tests/testMixtureFactor.cpp b/gtsam/hybrid/tests/testMixtureFactor.cpp index 03fdccff2..67a7fd8ae 100644 --- a/gtsam/hybrid/tests/testMixtureFactor.cpp +++ b/gtsam/hybrid/tests/testMixtureFactor.cpp @@ -63,8 +63,8 @@ TEST(MixtureFactor, Printing) { R"(Hybrid [x1 x2; 1] MixtureFactor Choice(1) - 0 Leaf [1] Nonlinear factor on 2 keys - 1 Leaf [1] Nonlinear factor on 2 keys + 0 Leaf Nonlinear factor on 2 keys + 1 Leaf Nonlinear factor on 2 keys )"; EXPECT(assert_print_equal(expected, mixtureFactor)); } From c4d11c498c75213c1dfb8cb70330e174d4f65b9c Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Mon, 6 Nov 2023 11:05:23 -0500 Subject: [PATCH 10/10] fix unittest assertion deprecation --- python/gtsam/tests/test_Robust.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/gtsam/tests/test_Robust.py b/python/gtsam/tests/test_Robust.py index c2ab9d4a4..1d7e9f52d 100644 --- a/python/gtsam/tests/test_Robust.py +++ b/python/gtsam/tests/test_Robust.py @@ -10,10 +10,11 @@ Author: Fan Jiang """ import unittest -import gtsam import numpy as np from gtsam.utils.test_case import GtsamTestCase +import gtsam + class TestRobust(GtsamTestCase): @@ -37,7 +38,7 @@ class TestRobust(GtsamTestCase): v = gtsam.Values() v.insert(0, 0.0) - self.assertAlmostEquals(f.error(v), 0.125) + self.assertAlmostEqual(f.error(v), 0.125) if __name__ == "__main__": unittest.main()