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(GaussianMixtureFactor, "gtsam_GaussianMixtureFactor");
55 BOOST_CLASS_EXPORT_GUID(GaussianMixtureFactor::Factors,
56  "gtsam_GaussianMixtureFactor_Factors");
57 BOOST_CLASS_EXPORT_GUID(GaussianMixtureFactor::Factors::Leaf,
58  "gtsam_GaussianMixtureFactor_Factors_Leaf");
59 BOOST_CLASS_EXPORT_GUID(GaussianMixtureFactor::Factors::Choice,
60  "gtsam_GaussianMixtureFactor_Factors_Choice");
61 
62 BOOST_CLASS_EXPORT_GUID(GaussianMixture, "gtsam_GaussianMixture");
63 BOOST_CLASS_EXPORT_GUID(GaussianMixture::Conditionals,
64  "gtsam_GaussianMixture_Conditionals");
65 BOOST_CLASS_EXPORT_GUID(GaussianMixture::Conditionals::Leaf,
66  "gtsam_GaussianMixture_Conditionals_Leaf");
67 BOOST_CLASS_EXPORT_GUID(GaussianMixture::Conditionals::Choice,
68  "gtsam_GaussianMixture_Conditionals_Choice");
69 // Needed since GaussianConditional::FromMeanAndStddev uses it
70 BOOST_CLASS_EXPORT_GUID(noiseModel::Isotropic, "gtsam_noiseModel_Isotropic");
71 
72 BOOST_CLASS_EXPORT_GUID(HybridBayesNet, "gtsam_HybridBayesNet");
73 
74 /* ****************************************************************************/
75 // Test GaussianMixtureFactor serialization.
76 TEST(HybridSerialization, GaussianMixtureFactor) {
77  KeyVector continuousKeys{X(0)};
78  DiscreteKeys discreteKeys{{M(0), 2}};
79 
80  auto A = Matrix::Zero(2, 1);
81  auto b0 = Matrix::Zero(2, 1);
82  auto b1 = Matrix::Ones(2, 1);
83  auto f0 = std::make_shared<JacobianFactor>(X(0), A, b0);
84  auto f1 = std::make_shared<JacobianFactor>(X(0), A, b1);
85  std::vector<GaussianFactor::shared_ptr> factors{f0, f1};
86 
87  const GaussianMixtureFactor factor(continuousKeys, discreteKeys, factors);
88 
89  EXPECT(equalsObj<GaussianMixtureFactor>(factor));
90  EXPECT(equalsXML<GaussianMixtureFactor>(factor));
91  EXPECT(equalsBinary<GaussianMixtureFactor>(factor));
92 }
93 
94 /* ****************************************************************************/
95 // Test HybridConditional serialization.
96 TEST(HybridSerialization, HybridConditional) {
97  const DiscreteKey mode(M(0), 2);
98  Matrix1 I = Matrix1::Identity();
99  const auto conditional = std::make_shared<GaussianConditional>(
100  GaussianConditional::FromMeanAndStddev(Z(0), I, X(0), Vector1(0), 0.5));
101  const HybridConditional hc(conditional);
102 
103  EXPECT(equalsObj<HybridConditional>(hc));
104  EXPECT(equalsXML<HybridConditional>(hc));
105  EXPECT(equalsBinary<HybridConditional>(hc));
106 }
107 
108 /* ****************************************************************************/
109 // Test GaussianMixture serialization.
110 TEST(HybridSerialization, GaussianMixture) {
111  const DiscreteKey mode(M(0), 2);
112  Matrix1 I = Matrix1::Identity();
113  const auto conditional0 = std::make_shared<GaussianConditional>(
114  GaussianConditional::FromMeanAndStddev(Z(0), I, X(0), Vector1(0), 0.5));
115  const auto conditional1 = std::make_shared<GaussianConditional>(
116  GaussianConditional::FromMeanAndStddev(Z(0), I, X(0), Vector1(0), 3));
117  const GaussianMixture gm({Z(0)}, {X(0)}, {mode},
118  {conditional0, conditional1});
119 
120  EXPECT(equalsObj<GaussianMixture>(gm));
121  EXPECT(equalsXML<GaussianMixture>(gm));
122  EXPECT(equalsBinary<GaussianMixture>(gm));
123 }
124 
125 /* ****************************************************************************/
126 // Test HybridBayesNet serialization.
127 TEST(HybridSerialization, HybridBayesNet) {
128  Switching s(2);
130 
131  EXPECT(equalsObj<HybridBayesNet>(hbn));
132  EXPECT(equalsXML<HybridBayesNet>(hbn));
133  EXPECT(equalsBinary<HybridBayesNet>(hbn));
134 }
135 
136 /* ****************************************************************************/
137 // Test HybridBayesTree serialization.
138 TEST(HybridSerialization, HybridBayesTree) {
139  Switching s(2);
141 
142  EXPECT(equalsObj<HybridBayesTree>(hbt));
143  EXPECT(equalsXML<HybridBayesTree>(hbt));
144  EXPECT(equalsBinary<HybridBayesTree>(hbt));
145 }
146 
147 /* ************************************************************************* */
148 int main() {
149  TestResult tr;
150  return TestRegistry::runAllTests(tr);
151 }
152 /* ************************************************************************* */
A set of GaussianFactors, indexed by a set of discrete keys.
A hybrid conditional in the Conditional Linear Gaussian scheme.
#define I
Definition: main.h:112
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51
static int runAllTests(TestResult &result)
Eigen::Matrix< double, 1, 1 > Vector1
Definition: testEvent.cpp:33
Implementation of a discrete conditional mixture factor. Implements a joint discrete-continuous facto...
const GaussianFactorGraph factors
Definition: BFloat16.h:88
A Bayes net of Gaussian Conditionals indexed by discrete keys.
HybridGaussianFactorGraph linearizedFactorGraph
Definition: Switching.h:124
#define Z
Definition: icosphere.cpp:21
A conditional of gaussian mixtures indexed by discrete variables, as part of a Bayes Network...
Hybrid Bayes Tree, the result of eliminating a HybridJunctionTree.
#define EXPECT(condition)
Definition: Test.h:150
TEST(HybridSerialization, GaussianMixtureFactor)
std::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
RealScalar s
Conditional Gaussian Base class.
static sharedNode Leaf(Key key, const SymbolicFactorGraph &factors)
traits
Definition: chartTesting.h:28
Point2 f1(const Point3 &p, OptionalJacobian< 2, 3 > H)
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
std::shared_ptr< BayesTreeType > eliminateMultifrontal(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
BOOST_CLASS_EXPORT_GUID(Factor, "gtsam_Factor")
static const DiscreteKey mode(modeKey, 2)
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
The matrix class, also used for vectors and row-vectors.
#define X
Definition: icosphere.cpp:20
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
Vector2 b1(2, -1)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:39:18