testSerializationHybrid.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 
26 #include <gtsam/inference/Symbol.h>
28 
29 #include "Switching.h"
30 
31 // Include for test suite
33 
34 using namespace std;
35 using namespace gtsam;
39 
40 using namespace serializationTestHelpers;
41 
42 BOOST_CLASS_EXPORT_GUID(Factor, "gtsam_Factor");
43 BOOST_CLASS_EXPORT_GUID(HybridFactor, "gtsam_HybridFactor");
44 BOOST_CLASS_EXPORT_GUID(JacobianFactor, "gtsam_JacobianFactor");
45 BOOST_CLASS_EXPORT_GUID(GaussianConditional, "gtsam_GaussianConditional");
46 BOOST_CLASS_EXPORT_GUID(DiscreteConditional, "gtsam_DiscreteConditional");
47 
48 BOOST_CLASS_EXPORT_GUID(DecisionTreeFactor, "gtsam_DecisionTreeFactor");
50 BOOST_CLASS_EXPORT_GUID(ADT, "gtsam_AlgebraicDecisionTree");
51 BOOST_CLASS_EXPORT_GUID(ADT::Leaf, "gtsam_AlgebraicDecisionTree_Leaf");
52 BOOST_CLASS_EXPORT_GUID(ADT::Choice, "gtsam_AlgebraicDecisionTree_Choice")
53 
54 BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor, "gtsam_HybridGaussianFactor");
56  "gtsam_HybridGaussianFactor_Factors");
58  "gtsam_HybridGaussianFactor_Factors_Leaf");
59 BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor::FactorValuePairs::Choice,
60  "gtsam_HybridGaussianFactor_Factors_Choice");
61 
63  "gtsam_GaussianFactorGraphValuePair");
65  "gtsam_HybridGaussianProductFactor");
66 
68  "gtsam_HybridGaussianConditional");
70  "gtsam_HybridGaussianConditional_Conditionals");
72  "gtsam_HybridGaussianConditional_Conditionals_Leaf");
74  "gtsam_HybridGaussianConditional_Conditionals_Choice");
75 // Needed since GaussianConditional::FromMeanAndStddev uses it
76 BOOST_CLASS_EXPORT_GUID(noiseModel::Isotropic, "gtsam_noiseModel_Isotropic");
77 
78 BOOST_CLASS_EXPORT_GUID(HybridBayesNet, "gtsam_HybridBayesNet");
79 
80 /* ****************************************************************************/
81 // Test HybridGaussianFactor serialization.
82 TEST(HybridSerialization, HybridGaussianFactor) {
83  DiscreteKey discreteKey{M(0), 2};
84 
85  auto A = Matrix::Zero(2, 1);
86  auto b0 = Matrix::Zero(2, 1);
87  auto b1 = Matrix::Ones(2, 1);
88  auto f0 = std::make_shared<JacobianFactor>(X(0), A, b0);
89  auto f1 = std::make_shared<JacobianFactor>(X(0), A, b1);
90  std::vector<GaussianFactor::shared_ptr> factors{f0, f1};
91 
92  const HybridGaussianFactor factor(discreteKey, factors);
93 
94  EXPECT(equalsObj<HybridGaussianFactor>(factor));
95  EXPECT(equalsXML<HybridGaussianFactor>(factor));
96  EXPECT(equalsBinary<HybridGaussianFactor>(factor));
97 }
98 
99 /* ****************************************************************************/
100 // Test HybridConditional serialization.
101 TEST(HybridSerialization, HybridConditional) {
102  const DiscreteKey mode(M(0), 2);
103  Matrix1 I = Matrix1::Identity();
104  const auto conditional = std::make_shared<GaussianConditional>(
105  GaussianConditional::FromMeanAndStddev(Z(0), I, X(0), Vector1(0), 0.5));
106  const HybridConditional hc(conditional);
107 
108  EXPECT(equalsObj<HybridConditional>(hc));
109  EXPECT(equalsXML<HybridConditional>(hc));
110  EXPECT(equalsBinary<HybridConditional>(hc));
111 }
112 
113 /* ****************************************************************************/
114 // Test HybridGaussianConditional serialization.
115 TEST(HybridSerialization, HybridGaussianConditional) {
116  const DiscreteKey mode(M(0), 2);
117  Matrix1 I = Matrix1::Identity();
118  const auto conditional0 = std::make_shared<GaussianConditional>(
119  GaussianConditional::FromMeanAndStddev(Z(0), I, X(0), Vector1(0), 0.5));
120  const auto conditional1 = std::make_shared<GaussianConditional>(
121  GaussianConditional::FromMeanAndStddev(Z(0), I, X(0), Vector1(0), 3));
122  const HybridGaussianConditional gm(mode, {conditional0, conditional1});
123 
124  EXPECT(equalsObj<HybridGaussianConditional>(gm));
125  EXPECT(equalsXML<HybridGaussianConditional>(gm));
126  EXPECT(equalsBinary<HybridGaussianConditional>(gm));
127 }
128 
129 /* ****************************************************************************/
130 // Test HybridBayesNet serialization.
131 TEST(HybridSerialization, HybridBayesNet) {
132  Switching s(2);
133  HybridBayesNet hbn = *(s.linearizedFactorGraph().eliminateSequential());
134 
135  EXPECT(equalsObj<HybridBayesNet>(hbn));
136  EXPECT(equalsXML<HybridBayesNet>(hbn));
137  EXPECT(equalsBinary<HybridBayesNet>(hbn));
138 }
139 
140 /* ****************************************************************************/
141 // Test HybridBayesTree serialization.
142 TEST(HybridSerialization, HybridBayesTree) {
143  Switching s(2);
144  HybridBayesTree hbt = *(s.linearizedFactorGraph().eliminateMultifrontal());
145 
146  EXPECT(equalsObj<HybridBayesTree>(hbt));
147  EXPECT(equalsXML<HybridBayesTree>(hbt));
148  EXPECT(equalsBinary<HybridBayesTree>(hbt));
149 }
150 
151 /* ************************************************************************* */
152 int main() {
153  TestResult tr;
154  return TestRegistry::runAllTests(tr);
155 }
156 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:44
GaussianConditional.h
Conditional Gaussian Base class.
simple_graph::b1
Vector2 b1(2, -1)
test_constructor::f1
auto f1
Definition: testHybridNonlinearFactor.cpp:56
gtsam::HybridConditional
Definition: HybridConditional.h:60
Leaf
static sharedNode Leaf(Key key, const SymbolicFactorGraph &factors)
Definition: testSymbolicEliminationTree.cpp:78
HybridGaussianConditional.h
A hybrid conditional in the Conditional Linear Gaussian scheme.
s
RealScalar s
Definition: level1_cplx_impl.h:126
TEST
TEST(HybridSerialization, HybridGaussianFactor)
Definition: testSerializationHybrid.cpp:82
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
gtsam::HybridBayesNet
Definition: HybridBayesNet.h:37
gtsam::GaussianFactorGraphValuePair
std::pair< GaussianFactorGraph, double > GaussianFactorGraphValuePair
Definition: HybridGaussianProductFactor.h:31
Switching.h
simple_graph::factors
const GaussianFactorGraph factors
Definition: testJacobianFactor.cpp:213
gtsam::JacobianFactor
Definition: JacobianFactor.h:91
gtsam::HybridBayesTree
Definition: HybridBayesTree.h:62
gtsam::HybridFactor
Definition: HybridFactor.h:51
HybridBayesNet.h
A Bayes net of Gaussian Conditionals indexed by discrete keys.
gtsam::HybridGaussianProductFactor
Alias for DecisionTree of GaussianFactorGraphs and their scalar sums.
Definition: HybridGaussianProductFactor.h:34
X
#define X
Definition: icosphere.cpp:20
gtsam::Factor
Definition: Factor.h:70
DiscreteConditional.h
HybridGaussianFactor.h
A set of GaussianFactors, indexed by a set of discrete keys.
hc
Vector3d hc
Definition: Tridiagonalization_householderCoefficients.cpp:5
gtsam::AlgebraicDecisionTree< Key >
gtsam::Switching
ϕ(X(0)) .. ϕ(X(k),X(k+1)) .. ϕ(X(k);z_k) .. ϕ(M(0)) .. ϕ(M(K-3),M(K-2))
Definition: Switching.h:124
Vector1
Eigen::Matrix< double, 1, 1 > Vector1
Definition: testEvent.cpp:33
test_constructor::f0
auto f0
Definition: testHybridNonlinearFactor.cpp:55
main
int main()
Definition: testSerializationHybrid.cpp:152
I
#define I
Definition: main.h:112
mode
static const DiscreteKey mode(modeKey, 2)
gtsam::GaussianConditional
Definition: GaussianConditional.h:40
gtsam::HybridGaussianConditional
A conditional of gaussian conditionals indexed by discrete variables, as part of a Bayes Network....
Definition: HybridGaussianConditional.h:54
Symbol.h
HybridBayesTree.h
Hybrid Bayes Tree, the result of eliminating a HybridJunctionTree.
E1::A
@ A
serializationTestHelpers.h
TestResult
Definition: TestResult.h:26
gtsam::DiscreteConditional
Definition: DiscreteConditional.h:37
gtsam
traits
Definition: SFMdata.h:40
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std
Definition: BFloat16.h:88
gtsam::HybridGaussianFactor
Implementation of a discrete-conditioned hybrid factor. Implements a joint discrete-continuous factor...
Definition: HybridGaussianFactor.h:60
BOOST_CLASS_EXPORT_GUID
BOOST_CLASS_EXPORT_GUID(Factor, "gtsam_Factor")
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
Z
#define Z
Definition: icosphere.cpp:21
HybridConditional.h
M
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51


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