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 
24 #include <gtsam/inference/Symbol.h>
25 
26 using namespace std;
27 using namespace gtsam;
28 
30 
31 BOOST_CLASS_EXPORT_GUID(Tree, "gtsam_DecisionTreeStringInt")
32 BOOST_CLASS_EXPORT_GUID(Tree::Leaf, "gtsam_DecisionTreeStringInt_Leaf")
33 BOOST_CLASS_EXPORT_GUID(Tree::Choice, "gtsam_DecisionTreeStringInt_Choice")
34 
35 BOOST_CLASS_EXPORT_GUID(DecisionTreeFactor, "gtsam_DecisionTreeFactor");
36 BOOST_CLASS_EXPORT_GUID(TableFactor, "gtsam_TableFactor");
37 
39 BOOST_CLASS_EXPORT_GUID(ADT, "gtsam_AlgebraicDecisionTree");
40 BOOST_CLASS_EXPORT_GUID(ADT::Leaf, "gtsam_AlgebraicDecisionTree_Leaf")
41 BOOST_CLASS_EXPORT_GUID(ADT::Choice, "gtsam_AlgebraicDecisionTree_Choice")
42 
43 /* ****************************************************************************/
44 // Test DecisionTree serialization.
45 TEST(DiscreteSerialization, DecisionTree) {
46  Tree tree({{"A", 2}}, std::vector<int>{1, 2});
47 
48  using namespace serializationTestHelpers;
49 
50  // Object roundtrip
51  Tree outputObj = create<Tree>();
52  roundtrip<Tree>(tree, outputObj);
53  EXPECT(tree.equals(outputObj));
54 
55  // XML roundtrip
56  Tree outputXml = create<Tree>();
57  roundtripXML<Tree>(tree, outputXml);
58  EXPECT(tree.equals(outputXml));
59 
60  // Binary roundtrip
61  Tree outputBinary = create<Tree>();
62  roundtripBinary<Tree>(tree, outputBinary);
63  EXPECT(tree.equals(outputBinary));
64 }
65 
66 /* ************************************************************************* */
67 // Check serialization for AlgebraicDecisionTree and the DecisionTreeFactor
68 TEST(DiscreteSerialization, DecisionTreeFactor) {
69  using namespace serializationTestHelpers;
70 
71  DiscreteKey A(1, 2), B(2, 2), C(3, 2);
72 
73  DecisionTreeFactor::ADT tree(A & B & C, "1 5 3 7 2 6 4 8");
74  EXPECT(equalsObj<DecisionTreeFactor::ADT>(tree));
75  EXPECT(equalsXML<DecisionTreeFactor::ADT>(tree));
76  EXPECT(equalsBinary<DecisionTreeFactor::ADT>(tree));
77 
78  DecisionTreeFactor f(A & B & C, "1 5 3 7 2 6 4 8");
79  EXPECT(equalsObj<DecisionTreeFactor>(f));
80  EXPECT(equalsXML<DecisionTreeFactor>(f));
81  EXPECT(equalsBinary<DecisionTreeFactor>(f));
82 }
83 
84 /* ************************************************************************* */
85 // Check serialization for TableFactor
86 TEST(DiscreteSerialization, TableFactor) {
87  using namespace serializationTestHelpers;
88 
89  DiscreteKey A(Symbol('x', 1), 3);
90  TableFactor tf(A, "1 2 2");
91 
92  EXPECT(equalsObj<TableFactor>(tf));
93  EXPECT(equalsXML<TableFactor>(tf));
94  EXPECT(equalsBinary<TableFactor>(tf));
95 }
96 
97 /* ************************************************************************* */
98 // Check serialization for DiscreteConditional & DiscreteDistribution
99 TEST(DiscreteSerialization, DiscreteConditional) {
100  using namespace serializationTestHelpers;
101 
102  DiscreteKey A(Symbol('x', 1), 3);
103  DiscreteConditional conditional(A % "1/2/2");
104 
105  EXPECT(equalsObj<DiscreteConditional>(conditional));
106  EXPECT(equalsXML<DiscreteConditional>(conditional));
107  EXPECT(equalsBinary<DiscreteConditional>(conditional));
108 
109  DiscreteDistribution P(A % "3/2/1");
110  EXPECT(equalsObj<DiscreteDistribution>(P));
111  EXPECT(equalsXML<DiscreteDistribution>(P));
112  EXPECT(equalsBinary<DiscreteDistribution>(P));
113 }
114 
115 /* ************************************************************************* */
116 int main() {
117  TestResult tr;
118  return TestRegistry::runAllTests(tr);
119 }
120 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
gtsam::TableFactor
Definition: TableFactor.h:53
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:45
Leaf
static sharedNode Leaf(Key key, const SymbolicFactorGraph &factors)
Definition: testSymbolicEliminationTree.cpp:78
main
int main()
Definition: testSerializationDiscrete.cpp:116
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
TableFactor.h
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:45
DecisionTreeFactor.h
gtsam::Symbol
Definition: inference/Symbol.h:37


gtsam
Author(s):
autogenerated on Sat Jan 4 2025 04:06:55