testSerializationDiscrete.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
12 /*
13  * testSerializtionDiscrete.cpp
14  *
15  * @date January 2023
16  * @author Varun Agrawal
17  */
18 
23 #include <gtsam/inference/Symbol.h>
24 
25 using namespace std;
26 using namespace gtsam;
27 
29 
30 BOOST_CLASS_EXPORT_GUID(Tree, "gtsam_DecisionTreeStringInt")
31 BOOST_CLASS_EXPORT_GUID(Tree::Leaf, "gtsam_DecisionTreeStringInt_Leaf")
32 BOOST_CLASS_EXPORT_GUID(Tree::Choice, "gtsam_DecisionTreeStringInt_Choice")
33 
34 BOOST_CLASS_EXPORT_GUID(DecisionTreeFactor, "gtsam_DecisionTreeFactor");
35 
37 BOOST_CLASS_EXPORT_GUID(ADT, "gtsam_AlgebraicDecisionTree");
38 BOOST_CLASS_EXPORT_GUID(ADT::Leaf, "gtsam_AlgebraicDecisionTree_Leaf")
39 BOOST_CLASS_EXPORT_GUID(ADT::Choice, "gtsam_AlgebraicDecisionTree_Choice")
40 
41 /* ****************************************************************************/
42 // Test DecisionTree serialization.
43 TEST(DiscreteSerialization, DecisionTree) {
44  Tree tree({{"A", 2}}, std::vector<int>{1, 2});
45 
46  using namespace serializationTestHelpers;
47 
48  // Object roundtrip
49  Tree outputObj = create<Tree>();
50  roundtrip<Tree>(tree, outputObj);
51  EXPECT(tree.equals(outputObj));
52 
53  // XML roundtrip
54  Tree outputXml = create<Tree>();
55  roundtripXML<Tree>(tree, outputXml);
56  EXPECT(tree.equals(outputXml));
57 
58  // Binary roundtrip
59  Tree outputBinary = create<Tree>();
60  roundtripBinary<Tree>(tree, outputBinary);
61  EXPECT(tree.equals(outputBinary));
62 }
63 
64 /* ************************************************************************* */
65 // Check serialization for AlgebraicDecisionTree and the DecisionTreeFactor
66 TEST(DiscreteSerialization, DecisionTreeFactor) {
67  using namespace serializationTestHelpers;
68 
69  DiscreteKey A(1, 2), B(2, 2), C(3, 2);
70 
71  DecisionTreeFactor::ADT tree(A & B & C, "1 5 3 7 2 6 4 8");
72  EXPECT(equalsObj<DecisionTreeFactor::ADT>(tree));
73  EXPECT(equalsXML<DecisionTreeFactor::ADT>(tree));
74  EXPECT(equalsBinary<DecisionTreeFactor::ADT>(tree));
75 
76  DecisionTreeFactor f(A & B & C, "1 5 3 7 2 6 4 8");
77  EXPECT(equalsObj<DecisionTreeFactor>(f));
78  EXPECT(equalsXML<DecisionTreeFactor>(f));
79  EXPECT(equalsBinary<DecisionTreeFactor>(f));
80 }
81 
82 /* ************************************************************************* */
83 // Check serialization for DiscreteConditional & DiscreteDistribution
84 TEST(DiscreteSerialization, DiscreteConditional) {
85  using namespace serializationTestHelpers;
86 
87  DiscreteKey A(Symbol('x', 1), 3);
88  DiscreteConditional conditional(A % "1/2/2");
89 
90  EXPECT(equalsObj<DiscreteConditional>(conditional));
91  EXPECT(equalsXML<DiscreteConditional>(conditional));
92  EXPECT(equalsBinary<DiscreteConditional>(conditional));
93 
94  DiscreteDistribution P(A % "3/2/1");
95  EXPECT(equalsObj<DiscreteDistribution>(P));
96  EXPECT(equalsXML<DiscreteDistribution>(P));
97  EXPECT(equalsBinary<DiscreteDistribution>(P));
98 }
99 
100 /* ************************************************************************* */
101 int main() {
102  TestResult tr;
103  return TestRegistry::runAllTests(tr);
104 }
105 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:44
Leaf
static sharedNode Leaf(Key key, const SymbolicFactorGraph &factors)
Definition: testSymbolicEliminationTree.cpp:78
main
int main()
Definition: testSerializationDiscrete.cpp:101
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
BOOST_CLASS_EXPORT_GUID
BOOST_CLASS_EXPORT_GUID(DecisionTreeFactor, "gtsam_DecisionTreeFactor")
tree
Definition: testExpression.cpp:212
gtsam::DiscreteDistribution
Definition: DiscreteDistribution.h:33
E1::B
@ B
gtsam::AlgebraicDecisionTree< Key >
Symbol.h
E1::A
@ A
serializationTestHelpers.h
TestResult
Definition: TestResult.h:26
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
gtsam::DecisionTree< string, int >
gtsam::DiscreteConditional
Definition: DiscreteConditional.h:37
C
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:50
gtsam
traits
Definition: SFMdata.h:40
DiscreteDistribution.h
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std
Definition: BFloat16.h:88
P
static double P[]
Definition: ellpe.c:68
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
TEST
TEST(DiscreteSerialization, DecisionTree)
Definition: testSerializationDiscrete.cpp:43
DecisionTreeFactor.h
gtsam::Symbol
Definition: inference/Symbol.h:37


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:08:24