diff --git a/gtsam/hybrid/tests/testSerializationHybrid.cpp b/gtsam/hybrid/tests/testSerializationHybrid.cpp index bb1a492feb..5b06312ba4 100644 --- a/gtsam/hybrid/tests/testSerializationHybrid.cpp +++ b/gtsam/hybrid/tests/testSerializationHybrid.cpp @@ -39,112 +39,114 @@ using symbol_shorthand::Z; using namespace serializationTestHelpers; -// BOOST_CLASS_EXPORT_GUID(Factor, "gtsam_Factor"); -// BOOST_CLASS_EXPORT_GUID(HybridFactor, "gtsam_HybridFactor"); -// BOOST_CLASS_EXPORT_GUID(JacobianFactor, "gtsam_JacobianFactor"); -// BOOST_CLASS_EXPORT_GUID(GaussianConditional, "gtsam_GaussianConditional"); -// BOOST_CLASS_EXPORT_GUID(DiscreteConditional, "gtsam_DiscreteConditional"); - -// BOOST_CLASS_EXPORT_GUID(DecisionTreeFactor, "gtsam_DecisionTreeFactor"); -// using ADT = AlgebraicDecisionTree; -// BOOST_CLASS_EXPORT_GUID(ADT, "gtsam_AlgebraicDecisionTree"); -// BOOST_CLASS_EXPORT_GUID(ADT::Leaf, "gtsam_AlgebraicDecisionTree_Leaf"); -// BOOST_CLASS_EXPORT_GUID(ADT::Choice, "gtsam_AlgebraicDecisionTree_Choice") - -// BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor, "gtsam_HybridGaussianFactor"); -// BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor::FactorValuePairs, -// "gtsam_HybridGaussianFactor_Factors"); -// BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor::FactorValuePairs::Leaf, -// "gtsam_HybridGaussianFactor_Factors_Leaf"); -// BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor::FactorValuePairs::Choice, -// "gtsam_HybridGaussianFactor_Factors_Choice"); - -// BOOST_CLASS_EXPORT_GUID(GaussianFactorGraphValuePair, -// "gtsam_GaussianFactorGraphValuePair"); - -// BOOST_CLASS_EXPORT_GUID(HybridGaussianConditional, -// "gtsam_HybridGaussianConditional"); -// BOOST_CLASS_EXPORT_GUID(HybridGaussianConditional::Conditionals, -// "gtsam_HybridGaussianConditional_Conditionals"); -// BOOST_CLASS_EXPORT_GUID(HybridGaussianConditional::Conditionals::Leaf, -// "gtsam_HybridGaussianConditional_Conditionals_Leaf"); -// BOOST_CLASS_EXPORT_GUID(HybridGaussianConditional::Conditionals::Choice, -// "gtsam_HybridGaussianConditional_Conditionals_Choice"); -// // Needed since GaussianConditional::FromMeanAndStddev uses it -// BOOST_CLASS_EXPORT_GUID(noiseModel::Isotropic, "gtsam_noiseModel_Isotropic"); - -// BOOST_CLASS_EXPORT_GUID(HybridBayesNet, "gtsam_HybridBayesNet"); - -// /* ****************************************************************************/ -// // Test HybridGaussianFactor serialization. -// TEST(HybridSerialization, HybridGaussianFactor) { -// DiscreteKey discreteKey{M(0), 2}; - -// auto A = Matrix::Zero(2, 1); -// auto b0 = Matrix::Zero(2, 1); -// auto b1 = Matrix::Ones(2, 1); -// auto f0 = std::make_shared(X(0), A, b0); -// auto f1 = std::make_shared(X(0), A, b1); -// std::vector factors{f0, f1}; - -// const HybridGaussianFactor factor(discreteKey, factors); - -// EXPECT(equalsObj(factor)); -// EXPECT(equalsXML(factor)); -// EXPECT(equalsBinary(factor)); -// } - -// /* ****************************************************************************/ -// // Test HybridConditional serialization. -// TEST(HybridSerialization, HybridConditional) { -// const DiscreteKey mode(M(0), 2); -// Matrix1 I = Matrix1::Identity(); -// const auto conditional = std::make_shared( -// GaussianConditional::FromMeanAndStddev(Z(0), I, X(0), Vector1(0), 0.5)); -// const HybridConditional hc(conditional); - -// EXPECT(equalsObj(hc)); -// EXPECT(equalsXML(hc)); -// EXPECT(equalsBinary(hc)); -// } - -// /* ****************************************************************************/ -// // Test HybridGaussianConditional serialization. -// TEST(HybridSerialization, HybridGaussianConditional) { -// const DiscreteKey mode(M(0), 2); -// Matrix1 I = Matrix1::Identity(); -// const auto conditional0 = std::make_shared( -// GaussianConditional::FromMeanAndStddev(Z(0), I, X(0), Vector1(0), 0.5)); -// const auto conditional1 = std::make_shared( -// GaussianConditional::FromMeanAndStddev(Z(0), I, X(0), Vector1(0), 3)); -// const HybridGaussianConditional gm(mode, {conditional0, conditional1}); - -// EXPECT(equalsObj(gm)); -// EXPECT(equalsXML(gm)); -// EXPECT(equalsBinary(gm)); -// } - -// /* ****************************************************************************/ -// // Test HybridBayesNet serialization. -// TEST(HybridSerialization, HybridBayesNet) { -// Switching s(2); -// HybridBayesNet hbn = *(s.linearizedFactorGraph.eliminateSequential()); - -// EXPECT(equalsObj(hbn)); -// EXPECT(equalsXML(hbn)); -// EXPECT(equalsBinary(hbn)); -// } - -// /* ****************************************************************************/ -// // Test HybridBayesTree serialization. -// TEST(HybridSerialization, HybridBayesTree) { -// Switching s(2); -// HybridBayesTree hbt = *(s.linearizedFactorGraph.eliminateMultifrontal()); - -// EXPECT(equalsObj(hbt)); -// EXPECT(equalsXML(hbt)); -// EXPECT(equalsBinary(hbt)); -// } +BOOST_CLASS_EXPORT_GUID(Factor, "gtsam_Factor"); +BOOST_CLASS_EXPORT_GUID(HybridFactor, "gtsam_HybridFactor"); +BOOST_CLASS_EXPORT_GUID(JacobianFactor, "gtsam_JacobianFactor"); +BOOST_CLASS_EXPORT_GUID(GaussianConditional, "gtsam_GaussianConditional"); +BOOST_CLASS_EXPORT_GUID(DiscreteConditional, "gtsam_DiscreteConditional"); + +BOOST_CLASS_EXPORT_GUID(DecisionTreeFactor, "gtsam_DecisionTreeFactor"); +using ADT = AlgebraicDecisionTree; +BOOST_CLASS_EXPORT_GUID(ADT, "gtsam_AlgebraicDecisionTree"); +BOOST_CLASS_EXPORT_GUID(ADT::Leaf, "gtsam_AlgebraicDecisionTree_Leaf"); +BOOST_CLASS_EXPORT_GUID(ADT::Choice, "gtsam_AlgebraicDecisionTree_Choice") + +BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor, "gtsam_HybridGaussianFactor"); +BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor::FactorValuePairs, + "gtsam_HybridGaussianFactor_Factors"); +BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor::FactorValuePairs::Leaf, + "gtsam_HybridGaussianFactor_Factors_Leaf"); +BOOST_CLASS_EXPORT_GUID(HybridGaussianFactor::FactorValuePairs::Choice, + "gtsam_HybridGaussianFactor_Factors_Choice"); + +BOOST_CLASS_EXPORT_GUID(GaussianFactorGraphValuePair, + "gtsam_GaussianFactorGraphValuePair"); +BOOST_CLASS_EXPORT_GUID(HybridGaussianProductFactor, + "gtsam_HybridGaussianProductFactor"); + +BOOST_CLASS_EXPORT_GUID(HybridGaussianConditional, + "gtsam_HybridGaussianConditional"); +BOOST_CLASS_EXPORT_GUID(HybridGaussianConditional::Conditionals, + "gtsam_HybridGaussianConditional_Conditionals"); +BOOST_CLASS_EXPORT_GUID(HybridGaussianConditional::Conditionals::Leaf, + "gtsam_HybridGaussianConditional_Conditionals_Leaf"); +BOOST_CLASS_EXPORT_GUID(HybridGaussianConditional::Conditionals::Choice, + "gtsam_HybridGaussianConditional_Conditionals_Choice"); +// Needed since GaussianConditional::FromMeanAndStddev uses it +BOOST_CLASS_EXPORT_GUID(noiseModel::Isotropic, "gtsam_noiseModel_Isotropic"); + +BOOST_CLASS_EXPORT_GUID(HybridBayesNet, "gtsam_HybridBayesNet"); + +/* ****************************************************************************/ +// Test HybridGaussianFactor serialization. +TEST(HybridSerialization, HybridGaussianFactor) { + DiscreteKey discreteKey{M(0), 2}; + + auto A = Matrix::Zero(2, 1); + auto b0 = Matrix::Zero(2, 1); + auto b1 = Matrix::Ones(2, 1); + auto f0 = std::make_shared(X(0), A, b0); + auto f1 = std::make_shared(X(0), A, b1); + std::vector factors{f0, f1}; + + const HybridGaussianFactor factor(discreteKey, factors); + + EXPECT(equalsObj(factor)); + EXPECT(equalsXML(factor)); + EXPECT(equalsBinary(factor)); +} + +/* ****************************************************************************/ +// Test HybridConditional serialization. +TEST(HybridSerialization, HybridConditional) { + const DiscreteKey mode(M(0), 2); + Matrix1 I = Matrix1::Identity(); + const auto conditional = std::make_shared( + GaussianConditional::FromMeanAndStddev(Z(0), I, X(0), Vector1(0), 0.5)); + const HybridConditional hc(conditional); + + EXPECT(equalsObj(hc)); + EXPECT(equalsXML(hc)); + EXPECT(equalsBinary(hc)); +} + +/* ****************************************************************************/ +// Test HybridGaussianConditional serialization. +TEST(HybridSerialization, HybridGaussianConditional) { + const DiscreteKey mode(M(0), 2); + Matrix1 I = Matrix1::Identity(); + const auto conditional0 = std::make_shared( + GaussianConditional::FromMeanAndStddev(Z(0), I, X(0), Vector1(0), 0.5)); + const auto conditional1 = std::make_shared( + GaussianConditional::FromMeanAndStddev(Z(0), I, X(0), Vector1(0), 3)); + const HybridGaussianConditional gm(mode, {conditional0, conditional1}); + + EXPECT(equalsObj(gm)); + EXPECT(equalsXML(gm)); + EXPECT(equalsBinary(gm)); +} + +/* ****************************************************************************/ +// Test HybridBayesNet serialization. +TEST(HybridSerialization, HybridBayesNet) { + Switching s(2); + HybridBayesNet hbn = *(s.linearizedFactorGraph.eliminateSequential()); + + EXPECT(equalsObj(hbn)); + EXPECT(equalsXML(hbn)); + EXPECT(equalsBinary(hbn)); +} + +/* ****************************************************************************/ +// Test HybridBayesTree serialization. +TEST(HybridSerialization, HybridBayesTree) { + Switching s(2); + HybridBayesTree hbt = *(s.linearizedFactorGraph.eliminateMultifrontal()); + + EXPECT(equalsObj(hbt)); + EXPECT(equalsXML(hbt)); + EXPECT(equalsBinary(hbt)); +} /* ************************************************************************* */ int main() {