testHybridGaussianProductFactor.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 
19 #include <gtsam/base/Testable.h>
23 #include <gtsam/inference/Key.h>
24 #include <gtsam/inference/Symbol.h>
27 
28 // Include for test suite
30 
31 #include <memory>
32 
33 using namespace std;
34 using namespace gtsam;
37 
38 /* ************************************************************************* */
39 namespace examples {
40 static const DiscreteKey m1(M(1), 2), m2(M(2), 3);
41 
42 const auto A1 = Matrix::Zero(2, 1);
43 const auto A2 = Matrix::Zero(2, 2);
44 const auto b = Matrix::Zero(2, 1);
45 
46 const auto f10 = std::make_shared<JacobianFactor>(X(1), A1, X(2), A2, b);
47 const auto f11 = std::make_shared<JacobianFactor>(X(1), A1, X(2), A2, b);
48 const HybridGaussianFactor hybridFactorA(m1, {{f10, 10}, {f11, 11}});
49 
50 const auto A3 = Matrix::Zero(2, 3);
51 const auto f20 = std::make_shared<JacobianFactor>(X(1), A1, X(3), A3, b);
52 const auto f21 = std::make_shared<JacobianFactor>(X(1), A1, X(3), A3, b);
53 const auto f22 = std::make_shared<JacobianFactor>(X(1), A1, X(3), A3, b);
54 
55 const HybridGaussianFactor hybridFactorB(m2, {{f20, 20}, {f21, 21}, {f22, 22}});
56 // Simulate a pruned hybrid factor, in this case m2==1 is nulled out.
58  m2, {{f20, 20}, {nullptr, 1000}, {f22, 22}});
59 } // namespace examples
60 
61 /* ************************************************************************* */
62 // Constructor
65 }
66 
67 /* ************************************************************************* */
68 // Add two Gaussian factors and check only one leaf in tree
69 TEST(HybridGaussianProductFactor, AddTwoGaussianFactors) {
70  using namespace examples;
71 
73  product += f10;
74  product += f11;
75 
76  // Check that the product has only one leaf and no discrete variables.
77  EXPECT_LONGS_EQUAL(1, product.nrLeaves());
78  EXPECT(product.labels().empty());
79 
80  // Retrieve the single leaf
81  auto leaf = product(Assignment<Key>());
82 
83  // Check that the leaf contains both factors
84  EXPECT_LONGS_EQUAL(2, leaf.first.size());
85  EXPECT(leaf.first.at(0) == f10);
86  EXPECT(leaf.first.at(1) == f11);
87  EXPECT_DOUBLES_EQUAL(0, leaf.second, 1e-9);
88 }
89 
90 /* ************************************************************************* */
91 // Add two GaussianConditionals and check the resulting tree
92 TEST(HybridGaussianProductFactor, AddTwoGaussianConditionals) {
93  // Create two GaussianConditionals
94  Vector1 d(1.0);
95  Matrix11 R = I_1x1, S = I_1x1;
96  auto gc1 = std::make_shared<GaussianConditional>(X(1), d, R, X(2), S);
97  auto gc2 = std::make_shared<GaussianConditional>(X(2), d, R);
98 
99  // Create a HybridGaussianProductFactor and add the conditionals
101  product += std::static_pointer_cast<GaussianFactor>(gc1);
102  product += std::static_pointer_cast<GaussianFactor>(gc2);
103 
104  // Check that the product has only one leaf and no discrete variables
105  EXPECT_LONGS_EQUAL(1, product.nrLeaves());
106  EXPECT(product.labels().empty());
107 
108  // Retrieve the single leaf
109  auto leaf = product(Assignment<Key>());
110 
111  // Check that the leaf contains both conditionals
112  EXPECT_LONGS_EQUAL(2, leaf.first.size());
113  EXPECT(leaf.first.at(0) == gc1);
114  EXPECT(leaf.first.at(1) == gc2);
115  EXPECT_DOUBLES_EQUAL(0, leaf.second, 1e-9);
116 }
117 
118 /* ************************************************************************* */
119 // Check AsProductFactor
121  using namespace examples;
123 
124  // Let's check that this worked:
126  mode[m1.first] = 0;
127  auto actual = product(mode);
128  EXPECT(actual.first.at(0) == f10);
129  EXPECT_DOUBLES_EQUAL(10, actual.second, 1e-9);
130 
131  mode[m1.first] = 1;
132  actual = product(mode);
133  EXPECT(actual.first.at(0) == f11);
134  EXPECT_DOUBLES_EQUAL(11, actual.second, 1e-9);
135 }
136 
137 /* ************************************************************************* */
138 // "Add" one hybrid factors together.
140  using namespace examples;
143 
144  // Let's check that this worked:
146  mode[m1.first] = 0;
147  auto actual = product(mode);
148  EXPECT(actual.first.at(0) == f10);
149  EXPECT_DOUBLES_EQUAL(10, actual.second, 1e-9);
150 
151  mode[m1.first] = 1;
152  actual = product(mode);
153  EXPECT(actual.first.at(0) == f11);
154  EXPECT_DOUBLES_EQUAL(11, actual.second, 1e-9);
155 }
156 
157 /* ************************************************************************* */
158 // "Add" two HFG together.
160  using namespace examples;
161 
162  // Create product of two hybrid factors: it will be a decision tree now on
163  // both discrete variables m1 and m2:
167 
168  // Let's check that this worked:
169  auto actual00 = product({{M(1), 0}, {M(2), 0}});
170  EXPECT(actual00.first.at(0) == f10);
171  EXPECT(actual00.first.at(1) == f20);
172  EXPECT_DOUBLES_EQUAL(10 + 20, actual00.second, 1e-9);
173 
174  auto actual12 = product({{M(1), 1}, {M(2), 2}});
175  EXPECT(actual12.first.at(0) == f11);
176  EXPECT(actual12.first.at(1) == f22);
177  EXPECT_DOUBLES_EQUAL(11 + 22, actual12.second, 1e-9);
178 }
179 
180 /* ************************************************************************* */
181 // "Add" two HFG together.
183  using namespace examples;
184 
185  // Create product of two hybrid factors: it will be a decision tree now on
186  // both discrete variables m1 and m2:
190  EXPECT_LONGS_EQUAL(6, product.nrLeaves());
191 
192 #ifdef GTSAM_DT_MERGING
193  auto pruned = product.removeEmpty();
194  EXPECT_LONGS_EQUAL(5, pruned.nrLeaves());
195 #endif
196 }
197 
198 /* ************************************************************************* */
199 int main() {
200  TestResult tr;
201  return TestRegistry::runAllTests(tr);
202 }
203 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
main
int main()
Definition: testHybridGaussianProductFactor.cpp:199
GaussianConditional.h
Conditional Gaussian Base class.
A3
static const double A3[]
Definition: expn.h:8
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
d
static const double d[K][N]
Definition: igam.h:11
EXPECT_LONGS_EQUAL
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:154
examples::hybridFactorA
const HybridGaussianFactor hybridFactorA(m1, {{f10, 10}, {f11, 11}})
Testable.h
Concept check for values that can be used in unit tests.
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
gtsam::HybridGaussianProductFactor
Alias for DecisionTree of GaussianFactorGraphs and their scalar sums.
Definition: HybridGaussianProductFactor.h:34
X
#define X
Definition: icosphere.cpp:20
HybridGaussianFactor.h
A set of GaussianFactors, indexed by a set of discrete keys.
TestableAssertions.h
Provides additional testing facilities for common data structures.
examples::f22
const auto f22
Definition: testHybridGaussianProductFactor.cpp:53
Key.h
examples::f10
const auto f10
Definition: testHybridGaussianProductFactor.cpp:46
TEST
TEST(HybridGaussianProductFactor, Construct)
Definition: testHybridGaussianProductFactor.cpp:63
mode
static const DiscreteKey mode(modeKey, 2)
examples::f21
const auto f21
Definition: testHybridGaussianProductFactor.cpp:52
HybridGaussianProductFactor.h
A2
static const double A2[]
Definition: expn.h:7
Symbol.h
gtsam::Assignment< Key >
examples::hybridFactorB
const HybridGaussianFactor hybridFactorB(m2, {{f20, 20}, {f21, 21}, {f22, 22}})
EXPECT_DOUBLES_EQUAL
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
Definition: Test.h:161
TestResult
Definition: TestResult.h:26
JacobianFactor.h
examples::b
const auto b
Definition: testHybridGaussianProductFactor.cpp:44
gtsam
traits
Definition: SFMdata.h:40
gtsam::HybridGaussianFactor::asProductFactor
virtual HybridGaussianProductFactor asProductFactor() const
Helper function to return factors and functional to create a DecisionTree of Gaussian Factor Graphs.
Definition: HybridGaussianFactor.cpp:166
examples::m2
static const DiscreteKey m2(M(2), 3)
examples::m1
static const DiscreteKey m1(M(1), 2)
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
A1
static const double A1[]
Definition: expn.h:6
leaf
Definition: testExpressionFactor.cpp:42
examples
Definition: testHybridGaussianProductFactor.cpp:39
product
void product(const MatrixType &m)
Definition: product.h:20
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
examples::f11
const auto f11
Definition: testHybridGaussianProductFactor.cpp:47
examples::f20
const auto f20
Definition: testHybridGaussianProductFactor.cpp:51
Construct
static NonlinearClusterTree Construct()
Definition: testNonlinearClusterTree.cpp:103
R
Rot2 R(Rot2::fromAngle(0.1))
examples::prunedFactorB
const HybridGaussianFactor prunedFactorB(m2, {{f20, 20}, {nullptr, 1000}, {f22, 22}})
S
DiscreteKey S(1, 2)
M
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:38