testAntiFactor.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 
20 #include <gtsam/slam/AntiFactor.h>
24 #include <gtsam/nonlinear/Values.h>
29 #include <gtsam/geometry/Pose3.h>
30 
31 using namespace std;
32 using namespace gtsam;
33 
34 /* ************************************************************************* */
35 TEST( AntiFactor, NegativeHessian)
36 {
37  // The AntiFactor should produce a Hessian Factor with negative matrices
38 
39  // Create linearization points
40  Pose3 pose1(Rot3(), Point3(0, 0, 0));
41  Pose3 pose2(Rot3(), Point3(2, 1, 3));
42  Pose3 z(Rot3(), Point3(1, 1, 1));
43  SharedNoiseModel sigma(noiseModel::Unit::Create(6));
44 
45  // Create a configuration corresponding to the ground truth
46  Values values;
47  values.insert(1, pose1);
48  values.insert(2, pose2);
49 
50  // Create a "standard" factor
52 
53  // Linearize it into a Jacobian Factor
54  GaussianFactor::shared_ptr originalJacobian = originalFactor->linearize(values);
55 
56  // Convert it to a Hessian Factor
57  HessianFactor::shared_ptr originalHessian = HessianFactor::shared_ptr(new HessianFactor(*originalJacobian));
58 
59  // Create the AntiFactor version of the original nonlinear factor
60  AntiFactor::shared_ptr antiFactor(new AntiFactor(originalFactor));
61 
62  // Linearize the AntiFactor into a Hessian Factor
63  GaussianFactor::shared_ptr antiGaussian = antiFactor->linearize(values);
64  HessianFactor::shared_ptr antiHessian = std::dynamic_pointer_cast<HessianFactor>(antiGaussian);
65 
66  Matrix expected_information = -originalHessian->information();
67  Matrix actual_information = antiHessian->information();
68  EXPECT(assert_equal(expected_information, actual_information));
69 
70  Vector expected_linear = -originalHessian->linearTerm();
71  Vector actual_linear = antiHessian->linearTerm();
72  EXPECT(assert_equal(expected_linear, actual_linear));
73 
74  double expected_f = -originalHessian->constantTerm();
75  double actual_f = antiHessian->constantTerm();
76  EXPECT_DOUBLES_EQUAL(expected_f, actual_f, 1e-5);
77 }
78 
79 /* ************************************************************************* */
80 TEST( AntiFactor, EquivalentBayesNet)
81 {
82  // Test the AntiFactor by creating a simple graph and eliminating into a BayesNet
83  // Then add an additional factor and the corresponding AntiFactor and eliminate
84  // The resulting BayesNet should be identical to the first
85 
86  Pose3 pose1(Rot3(), Point3(0, 0, 0));
87  Pose3 pose2(Rot3(), Point3(2, 1, 3));
88  Pose3 z(Rot3(), Point3(1, 1, 1));
89  SharedNoiseModel sigma(noiseModel::Unit::Create(6));
90 
94 
95  // Create a configuration corresponding to the ground truth
96  Values values;
97  values.insert(1, pose1);
98  values.insert(2, pose2);
99 
100  // Define an elimination ordering
102 
103  // Eliminate into a BayesNet
105  GaussianBayesNet::shared_ptr expectedBayesNet = lin_graph.eliminateSequential(ordering);
106 
107  // Back-substitute to find the optimal deltas
108  VectorValues expectedDeltas = expectedBayesNet->optimize();
109 
110  // Add an additional factor between Pose1 and Pose2
112  graph.push_back(f1);
113 
114  // Add the corresponding AntiFactor between Pose1 and Pose2
116  graph.push_back(f2);
117 
118  // Again, Eliminate into a BayesNet
119  GaussianFactorGraph lin_graph1 = *graph.linearize(values);
120  GaussianBayesNet::shared_ptr actualBayesNet = lin_graph1.eliminateSequential(ordering);
121 
122  // Back-substitute to find the optimal deltas
123  VectorValues actualDeltas = actualBayesNet->optimize();
124 
125  // Verify the BayesNets are identical
126  CHECK(assert_equal(*expectedBayesNet, *actualBayesNet, 1e-5));
127  CHECK(assert_equal(expectedDeltas, actualDeltas, 1e-5));
128 }
129 
130 /* ************************************************************************* */
131  int main() { TestResult tr; return TestRegistry::runAllTests(tr);}
132 /* ************************************************************************* */
TestRegistry::runAllTests
static int runAllTests(TestResult &result)
Definition: TestRegistry.cpp:27
gtsam::LieGroup::between
Class between(const Class &g) const
Definition: Lie.h:52
gtsam::EliminateableFactorGraph::eliminateSequential
std::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Definition: EliminateableFactorGraph-inst.h:29
GaussianFactorGraph.h
Linear Factor Graph where all factors are Gaussians.
test_constructor::f1
auto f1
Definition: testHybridNonlinearFactor.cpp:56
gtsam::HessianFactor
A Gaussian factor using the canonical parameters (information form)
Definition: HessianFactor.h:100
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
gtsam::AntiFactor::shared_ptr
std::shared_ptr< AntiFactor > shared_ptr
Definition: AntiFactor.h:44
EXPECT
#define EXPECT(condition)
Definition: Test.h:150
TestHarness.h
NonlinearOptimizer.h
Base class and parameters for nonlinear optimization algorithms.
f2
double f2(const Vector2 &x)
Definition: testNumericalDerivative.cpp:56
HessianFactor.h
Contains the HessianFactor class, a general quadratic factor.
gtsam::Matrix
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
gtsam::NonlinearFactorGraph::linearize
std::shared_ptr< GaussianFactorGraph > linearize(const Values &linearizationPoint) const
Linearize a nonlinear factor graph.
Definition: NonlinearFactorGraph.cpp:239
gtsam::HessianFactor::shared_ptr
std::shared_ptr< This > shared_ptr
A shared_ptr to this class.
Definition: HessianFactor.h:109
Ordering.h
Variable ordering for the elimination algorithm.
simple::pose2
static Pose3 pose2
Definition: testInitializePose3.cpp:58
GaussianBayesNet.h
Chordal Bayes Net, the result of eliminating a factor graph.
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
gtsam::NonlinearFactorGraph::orderingCOLAMD
Ordering orderingCOLAMD() const
Definition: NonlinearFactorGraph.cpp:182
sampling::sigma
static const double sigma
Definition: testGaussianBayesNet.cpp:170
gtsam::NoiseModelFactor::linearize
std::shared_ptr< GaussianFactor > linearize(const Values &x) const override
Definition: NonlinearFactor.cpp:150
gtsam::GaussianFactorGraph
Definition: GaussianFactorGraph.h:73
AntiFactor.h
gtsam::VectorValues
Definition: VectorValues.h:74
BetweenFactor.h
gtsam::Rot3
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition: Rot3.h:58
gtsam::Pose3
Definition: Pose3.h:37
gtsam::BetweenFactor::shared_ptr
std::shared_ptr< BetweenFactor > shared_ptr
Definition: BetweenFactor.h:63
gtsam::NonlinearFactorGraph
Definition: NonlinearFactorGraph.h:55
gtsam::GaussianFactor::shared_ptr
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: GaussianFactor.h:42
gtsam::NonlinearFactorGraph::addPrior
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
Definition: NonlinearFactorGraph.h:199
pybind_wrapper_test_script.z
z
Definition: pybind_wrapper_test_script.py:61
gtsam::HybridValues::insert
void insert(Key j, const Vector &value)
Definition: HybridValues.cpp:85
gtsam::SharedNoiseModel
noiseModel::Base::shared_ptr SharedNoiseModel
Definition: NoiseModel.h:762
EXPECT_DOUBLES_EQUAL
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
Definition: Test.h:161
ordering
static enum @1096 ordering
TestResult
Definition: TestResult.h:26
main
int main()
Definition: testAntiFactor.cpp:131
gtsam
traits
Definition: SFMdata.h:40
pose1
static const Pose3 pose1(Rot3(), Point3(0, 1, 0.5))
NonlinearFactorGraph.h
Factor Graph consisting of non-linear factors.
gtsam::FactorGraph::push_back
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
Definition: FactorGraph.h:147
gtsam::Values
Definition: Values.h:65
CHECK
#define CHECK(condition)
Definition: Test.h:108
std
Definition: BFloat16.h:88
gtsam::assert_equal
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
gtsam::Point3
Vector3 Point3
Definition: Point3.h:38
TEST
TEST(AntiFactor, NegativeHessian)
Definition: testAntiFactor.cpp:35
gtsam::AntiFactor
Definition: AntiFactor.h:31
graph
NonlinearFactorGraph graph
Definition: doc/Code/OdometryExample.cpp:2
gtsam::Ordering
Definition: inference/Ordering.h:33
gtsam::GaussianBayesNet::shared_ptr
std::shared_ptr< This > shared_ptr
Definition: GaussianBayesNet.h:42
Values.h
A non-templated config holding any types of Manifold-group elements.
Pose3.h
3D Pose
gtsam::FactorGraph::emplace_shared
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:153
gtsam::BetweenFactor
Definition: BetweenFactor.h:40


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