testHybridGaussianFactor.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 
21 #include <gtsam/base/Testable.h>
30 #include <gtsam/inference/Symbol.h>
35 
36 // Include for test suite
38 
39 #include <memory>
40 
41 using namespace std;
42 using namespace gtsam;
46 
47 /* ************************************************************************* */
48 // Check iterators of empty hybrid factor.
49 TEST(HybridGaussianFactor, Constructor) {
50  HybridGaussianFactor factor;
51  HybridGaussianFactor::const_iterator const_it = factor.begin();
52  CHECK(const_it == factor.end());
53  HybridGaussianFactor::iterator it = factor.begin();
54  CHECK(it == factor.end());
55 }
56 
57 /* ************************************************************************* */
58 namespace test_constructor {
59 DiscreteKey m1(1, 2);
60 
61 auto A1 = Matrix::Zero(2, 1);
62 auto A2 = Matrix::Zero(2, 2);
63 auto b = Matrix::Zero(2, 1);
64 
65 auto f10 = std::make_shared<JacobianFactor>(X(1), A1, X(2), A2, b);
66 auto f11 = std::make_shared<JacobianFactor>(X(1), A1, X(2), A2, b);
67 } // namespace test_constructor
68 
69 /* ************************************************************************* */
70 // Test simple to complex constructors...
71 TEST(HybridGaussianFactor, ConstructorVariants) {
72  using namespace test_constructor;
73  HybridGaussianFactor fromFactors(m1, {f10, f11});
74 
75  std::vector<GaussianFactorValuePair> pairs{{f10, 0.0}, {f11, 0.0}};
76  HybridGaussianFactor fromPairs(m1, pairs);
77  assert_equal(fromFactors, fromPairs);
78 
79  HybridGaussianFactor::FactorValuePairs decisionTree({m1}, pairs);
80  HybridGaussianFactor fromDecisionTree({m1}, decisionTree);
81  assert_equal(fromDecisionTree, fromPairs);
82 }
83 
84 /* ************************************************************************* */
86  using namespace test_constructor;
88  // Check the number of keys matches what we expect
92 
93  DiscreteKey m2(2, 3);
94  auto A3 = Matrix::Zero(2, 3);
95  auto f20 = std::make_shared<JacobianFactor>(X(1), A1, X(3), A3, b);
96  auto f21 = std::make_shared<JacobianFactor>(X(1), A1, X(3), A3, b);
97  auto f22 = std::make_shared<JacobianFactor>(X(1), A1, X(3), A3, b);
99 
100  // Check the number of keys matches what we expect
104 }
105 
106 /* ************************************************************************* */
108  using namespace test_constructor;
109  HybridGaussianFactor hybridFactor(m1, {f10, f11});
110 
111  std::string expected =
112  R"(Hybrid [x1 x2; 1]{
113  Choice(1)
114  0 Leaf :
115  A[x1] = [
116  0;
117  0
118 ]
119  A[x2] = [
120  0, 0;
121  0, 0
122 ]
123  b = [ 0 0 ]
124  No noise model
125 scalar: 0
126 
127  1 Leaf :
128  A[x1] = [
129  0;
130  0
131 ]
132  A[x2] = [
133  0, 0;
134  0, 0
135 ]
136  b = [ 0 0 ]
137  No noise model
138 scalar: 0
139 
140 }
141 )";
142  EXPECT(assert_print_equal(expected, hybridFactor));
143 }
144 
145 /* ************************************************************************* */
147  DiscreteKeys dKeys;
148  dKeys.emplace_back(M(0), 2);
149  dKeys.emplace_back(M(1), 2);
150 
151  auto gaussians = std::make_shared<GaussianConditional>();
154 
155  EXPECT_LONGS_EQUAL(2, gm.discreteKeys().size());
156 }
157 
158 /* ************************************************************************* */
159 // Test the error of the HybridGaussianFactor
160 TEST(HybridGaussianFactor, Error) {
161  DiscreteKey m1(1, 2);
162 
163  auto A01 = Matrix2::Identity();
164  auto A02 = Matrix2::Identity();
165 
166  auto A11 = Matrix2::Identity();
167  auto A12 = Matrix2::Identity() * 2;
168 
169  auto b = Vector2::Zero();
170 
171  auto f0 = std::make_shared<JacobianFactor>(X(1), A01, X(2), A02, b);
172  auto f1 = std::make_shared<JacobianFactor>(X(1), A11, X(2), A12, b);
173  HybridGaussianFactor hybridFactor(m1, {f0, f1});
174 
175  VectorValues continuousValues;
176  continuousValues.insert(X(1), Vector2(0, 0));
177  continuousValues.insert(X(2), Vector2(1, 1));
178 
179  // error should return a tree of errors, with nodes for each discrete value.
180  AlgebraicDecisionTree<Key> error_tree =
181  hybridFactor.errorTree(continuousValues);
182 
183  std::vector<DiscreteKey> discrete_keys = {m1};
184  // Error values for regression test
185  std::vector<double> errors = {1, 4};
186  AlgebraicDecisionTree<Key> expected_error(discrete_keys, errors);
187 
188  EXPECT(assert_equal(expected_error, error_tree));
189 
190  // Test for single leaf given discrete assignment P(X|M,Z).
191  DiscreteValues discreteValues;
192  discreteValues[m1.first] = 1;
194  4.0, hybridFactor.error({continuousValues, discreteValues}), 1e-9);
195 }
196 
197 /* ************************************************************************* */
198 int main() {
199  TestResult tr;
200  return TestRegistry::runAllTests(tr);
201 }
202 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
GaussianFactorGraph.h
Linear Factor Graph where all factors are Gaussians.
test_constructor::b
auto b
Definition: testHybridGaussianFactor.cpp:63
test_constructor::f1
auto f1
Definition: testHybridNonlinearFactor.cpp:56
A3
static const double A3[]
Definition: expn.h:8
HybridGaussianConditional.h
A hybrid conditional in the Conditional Linear Gaussian scheme.
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
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
gtsam::Vector2
Eigen::Vector2d Vector2
Definition: Vector.h:42
TestHarness.h
test_constructor::m1
DiscreteKey m1(1, 2)
gtsam::VectorValues::insert
iterator insert(const std::pair< Key, Vector > &key_value)
Definition: VectorValues.cpp:90
test_constructor
Definition: testHybridGaussianFactor.cpp:58
gtsam::assert_print_equal
bool assert_print_equal(const std::string &expected, const V &actual, const std::string &s="")
Definition: TestableAssertions.h:352
HybridBayesNet.h
A Bayes net of Gaussian Conditionals indexed by discrete keys.
X
#define X
Definition: icosphere.cpp:20
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
DiscreteConditional.h
gtsam::Factor::begin
const_iterator begin() const
Definition: Factor.h:146
HybridGaussianFactor.h
A set of GaussianFactors, indexed by a set of discrete keys.
equal_constants::conditionals
const std::vector< GaussianConditional::shared_ptr > conditionals
Definition: testHybridGaussianConditional.cpp:53
test_constructor::f10
auto f10
Definition: testHybridGaussianFactor.cpp:65
TestableAssertions.h
Provides additional testing facilities for common data structures.
gtsam::AlgebraicDecisionTree< Key >
examples::f22
const auto f22
Definition: testHybridGaussianProductFactor.cpp:53
m2
MatrixType m2(n_dims)
PriorFactor.h
gtsam::VectorValues
Definition: VectorValues.h:74
test_constructor::f0
auto f0
Definition: testHybridNonlinearFactor.cpp:55
BetweenFactor.h
examples::f21
const auto f21
Definition: testHybridGaussianProductFactor.cpp:52
A2
static const double A2[]
Definition: expn.h:7
cholesky::expected
Matrix expected
Definition: testMatrix.cpp:971
gtsam::HybridGaussianConditional
A conditional of gaussian conditionals indexed by discrete variables, as part of a Bayes Network....
Definition: HybridGaussianConditional.h:54
main
int main()
Definition: testHybridGaussianFactor.cpp:169
test_constructor::f11
auto f11
Definition: testHybridGaussianFactor.cpp:66
Symbol.h
VectorValues.h
Factor Graph Values.
gtsam::Factor::end
const_iterator end() const
Definition: Factor.h:149
A12
static const double A12[]
Definition: expn.h:17
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
gtsam::DecisionTree< Key, GaussianFactorValuePair >
TEST
TEST(HybridGaussianFactor, Constructor)
Definition: testHybridGaussianFactor.cpp:49
HybridGaussianFactorGraph.h
Linearized Hybrid factor graph that uses type erasure.
gtsam
traits
Definition: SFMdata.h:40
DiscreteValues.h
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::Factor::keys
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition: Factor.h:143
CHECK
#define CHECK(condition)
Definition: Test.h:108
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
gtsam::HybridFactor::continuousKeys
const KeyVector & continuousKeys() const
Return only the continuous keys for this factor.
Definition: HybridFactor.h:134
A11
static const double A11[]
Definition: expn.h:16
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
examples::f20
const auto f20
Definition: testHybridGaussianProductFactor.cpp:51
Z
#define Z
Definition: icosphere.cpp:21
HybridValues.h
gtsam::HybridFactor::discreteKeys
const DiscreteKeys & discreteKeys() const
Return the discrete keys for this factor.
Definition: HybridFactor.h:131
M
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51


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