testHybridNonlinearFactor.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 
25 #include <gtsam/inference/Symbol.h>
27 
28 // Include for test suite
30 
31 using namespace std;
32 using namespace gtsam;
36 
37 /* ************************************************************************* */
38 // Check iterators of empty hybrid factor.
39 TEST(HybridNonlinearFactor, Constructor) {
41  HybridNonlinearFactor::const_iterator const_it = factor.begin();
42  CHECK(const_it == factor.end());
43  HybridNonlinearFactor::iterator it = factor.begin();
44  CHECK(it == factor.end());
45 }
46 /* ************************************************************************* */
47 namespace test_constructor {
48 DiscreteKey m1(1, 2);
49 double between0 = 0.0;
50 double between1 = 1.0;
51 
53 auto model = noiseModel::Diagonal::Sigmas(sigmas, false);
54 
55 auto f0 = std::make_shared<BetweenFactor<double>>(X(1), X(2), between0, model);
56 auto f1 = std::make_shared<BetweenFactor<double>>(X(1), X(2), between1, model);
57 } // namespace test_constructor
58 
59 /* ************************************************************************* */
60 // Test simple to complex constructors...
61 TEST(HybridGaussianFactor, ConstructorVariants) {
62  using namespace test_constructor;
63  HybridNonlinearFactor fromFactors(m1, {f0, f1});
64 
65  std::vector<NonlinearFactorValuePair> pairs{{f0, 0.0}, {f1, 0.0}};
66  HybridNonlinearFactor fromPairs(m1, pairs);
67  assert_equal(fromFactors, fromPairs);
68 
69  HybridNonlinearFactor::FactorValuePairs decisionTree({m1}, pairs);
70  HybridNonlinearFactor fromDecisionTree({m1}, decisionTree);
71  assert_equal(fromDecisionTree, fromPairs);
72 }
73 
74 /* ************************************************************************* */
75 // Test .print() output.
77  using namespace test_constructor;
78  HybridNonlinearFactor hybridFactor({m1}, {f0, f1});
79 
80  std::string expected =
81  R"(Hybrid [x1 x2; 1]
82 HybridNonlinearFactor
83  Choice(1)
84  0 Leaf BetweenFactor(x1,x2)
85  measured: 0
86  noise model: diagonal sigmas [1];
87 
88  1 Leaf BetweenFactor(x1,x2)
89  measured: 1
90  noise model: diagonal sigmas [1];
91 
92 )";
93  EXPECT(assert_print_equal(expected, hybridFactor));
94 }
95 
96 /* ************************************************************************* */
98  DiscreteKey m1(1, 2);
99 
100  double between0 = 0.0;
101  double between1 = 1.0;
102 
103  Vector1 sigmas = Vector1(1.0);
104  auto model = noiseModel::Diagonal::Sigmas(sigmas, false);
105 
106  auto f0 =
107  std::make_shared<BetweenFactor<double>>(X(1), X(2), between0, model);
108  auto f1 =
109  std::make_shared<BetweenFactor<double>>(X(1), X(2), between1, model);
110  return HybridNonlinearFactor(m1, {f0, f1});
111 }
112 
113 /* ************************************************************************* */
114 // Test the error of the HybridNonlinearFactor
115 TEST(HybridNonlinearFactor, Error) {
116  auto hybridFactor = getHybridNonlinearFactor();
117 
118  Values continuousValues;
119  continuousValues.insert<double>(X(1), 0);
120  continuousValues.insert<double>(X(2), 1);
121 
122  AlgebraicDecisionTree<Key> error_tree =
123  hybridFactor.errorTree(continuousValues);
124 
125  DiscreteKey m1(1, 2);
126  std::vector<DiscreteKey> discrete_keys = {m1};
127  std::vector<double> errors = {0.5, 0};
128  AlgebraicDecisionTree<Key> expected_error(discrete_keys, errors);
129 
130  EXPECT(assert_equal(expected_error, error_tree));
131 }
132 
133 /* ************************************************************************* */
134 // Test dim of the HybridNonlinearFactor
136  auto hybridFactor = getHybridNonlinearFactor();
137  EXPECT_LONGS_EQUAL(1, hybridFactor.dim());
138 }
139 
140 /* ************************************************************************* */
141 // Test restrict method
143  using namespace test_constructor;
145  DiscreteValues assignment = {{m1.first, 0}};
146  auto restricted = factor.restrict(assignment);
147  auto betweenFactor = dynamic_pointer_cast<BetweenFactor<double>>(restricted);
148  CHECK(betweenFactor);
149  EXPECT(assert_equal(*f0, *betweenFactor));
150 }
151 
152 /* ************************************************************************* */
153 int main() {
154  TestResult tr;
155  return TestRegistry::runAllTests(tr);
156 }
157 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
test_constructor::f1
auto f1
Definition: testHybridNonlinearFactor.cpp:56
main
int main()
Definition: testHybridNonlinearFactor.cpp:142
test_constructor::between1
double between1
Definition: testHybridNonlinearFactor.cpp:50
EXPECT_LONGS_EQUAL
#define EXPECT_LONGS_EQUAL(expected, actual)
Definition: Test.h:154
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
HybridNonlinearFactorGraph.h
Nonlinear hybrid factor graph that uses type erasure.
test_constructor::sigmas
Vector1 sigmas
Definition: testHybridNonlinearFactor.cpp:52
TestHarness.h
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:353
m1
Matrix3d m1
Definition: IOFormat.cpp:2
HybridBayesNet.h
A Bayes net of Gaussian Conditionals indexed by discrete keys.
X
#define X
Definition: icosphere.cpp:20
gtsam::Factor::begin
const_iterator begin() const
Definition: Factor.h:146
TestableAssertions.h
Provides additional testing facilities for common data structures.
gtsam::AlgebraicDecisionTree< Key >
pruning_fixture::factor
DecisionTreeFactor factor(D &C &B &A, "0.0 0.0 0.0 0.60658897 0.61241912 0.61241969 0.61247685 0.61247742 0.0 " "0.0 0.0 0.99995287 1.0 1.0 1.0 1.0")
Vector1
Eigen::Matrix< double, 1, 1 > Vector1
Definition: testEvent.cpp:33
test_constructor::f0
auto f0
Definition: testHybridNonlinearFactor.cpp:55
BetweenFactor.h
gtsam::noiseModel::Isotropic
Definition: NoiseModel.h:541
cholesky::expected
Matrix expected
Definition: testMatrix.cpp:916
Symbol.h
test_constructor::model
auto model
Definition: testHybridNonlinearFactor.cpp:53
gtsam::Factor::end
const_iterator end() const
Definition: Factor.h:149
TEST
TEST(HybridNonlinearFactor, Constructor)
Definition: testHybridNonlinearFactor.cpp:39
TestResult
Definition: TestResult.h:26
gtsam::DecisionTree< Key, NonlinearFactorValuePair >
test_constructor::between0
double between0
Definition: testHybridNonlinearFactor.cpp:49
HybridNonlinearFactor.h
A set of nonlinear factors indexed by a set of discrete keys.
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::Values
Definition: Values.h:65
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
gtsam::Values::insert
void insert(Key j, const Value &val)
Definition: Values.cpp:156
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:41
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
gtsam::DecisionTreeFactor::restrict
DiscreteFactor::shared_ptr restrict(const DiscreteValues &assignment) const override
Restrict the factor to the given assignment.
Definition: DecisionTreeFactor.cpp:548
gtsam::HybridNonlinearFactor
Implementation of a discrete-conditioned hybrid factor.
Definition: HybridNonlinearFactor.h:58
getHybridNonlinearFactor
static HybridNonlinearFactor getHybridNonlinearFactor()
Definition: testHybridNonlinearFactor.cpp:86
M
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:06:50