testIterative.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 
18 #include <tests/smallExample.h>
21 #include <gtsam/inference/Symbol.h>
22 #include <gtsam/linear/iterative.h>
23 #include <gtsam/geometry/Pose2.h>
24 
26 
27 using namespace std;
28 using namespace gtsam;
29 using namespace example;
30 using symbol_shorthand::X; // to create pose keys
31 using symbol_shorthand::L; // to create landmark keys
32 
34 // add following below to add printing:
35 // parameters.verbosity_ = ConjugateGradientParameters::COMPLEXITY;
36 
37 /* ************************************************************************* */
38 TEST( Iterative, steepestDescent )
39 {
40  // Create factor graph
42 
43  // eliminate and solve
45 
46  // Do gradient descent
47  VectorValues zero = VectorValues::Zero(expected); // TODO, how do we do this normally?
48  VectorValues actual = steepestDescent(fg, zero, parameters);
49  CHECK(assert_equal(expected,actual,1e-2));
50 }
51 
52 /* ************************************************************************* */
54 {
55  // Create factor graph
57 
58  // eliminate and solve
60 
61  // get matrices
62  Matrix A;
63  Vector b;
64  Vector x0 = Z_6x1;
65  boost::tie(A, b) = fg.jacobian();
66  Vector expectedX = (Vector(6) << -0.1, 0.1, -0.1, -0.1, 0.1, -0.2).finished();
67 
68  // Do conjugate gradient descent, System version
69  System Ab(A, b);
70  Vector actualX = conjugateGradientDescent(Ab, x0, parameters);
71  CHECK(assert_equal(expectedX,actualX,1e-9));
72 
73  // Do conjugate gradient descent, Matrix version
74  Vector actualX2 = conjugateGradientDescent(A, b, x0, parameters);
75  CHECK(assert_equal(expectedX,actualX2,1e-9));
76 
77  // Do conjugate gradient descent on factor graph
78  VectorValues zero = VectorValues::Zero(expected);
79  VectorValues actual = conjugateGradientDescent(fg, zero, parameters);
80  CHECK(assert_equal(expected,actual,1e-2));
81 }
82 
83 /* ************************************************************************* */
84 TEST( Iterative, conjugateGradientDescent_hard_constraint )
85 {
86  Values config;
87  Pose2 pose1 = Pose2(0.,0.,0.);
88  config.insert(X(1), pose1);
89  config.insert(X(2), Pose2(1.5,0.,0.));
90 
92  graph += NonlinearEquality<Pose2>(X(1), pose1);
93  graph += BetweenFactor<Pose2>(X(1),X(2), Pose2(1.,0.,0.), noiseModel::Isotropic::Sigma(3, 1));
94 
95  boost::shared_ptr<GaussianFactorGraph> fg = graph.linearize(config);
96 
97  VectorValues zeros = config.zeroVectors();
98 
100  parameters.setEpsilon_abs(1e-3);
101  parameters.setEpsilon_rel(1e-5);
102  parameters.setMaxIterations(100);
103  VectorValues actual = conjugateGradientDescent(*fg, zeros, parameters);
104 
106  expected.insert(X(1), Z_3x1);
107  expected.insert(X(2), Vector3(-0.5,0.,0.));
108  CHECK(assert_equal(expected, actual));
109 }
110 
111 /* ************************************************************************* */
112 TEST( Iterative, conjugateGradientDescent_soft_constraint )
113 {
114  Values config;
115  config.insert(X(1), Pose2(0.,0.,0.));
116  config.insert(X(2), Pose2(1.5,0.,0.));
117 
119  graph.addPrior(X(1), Pose2(0.,0.,0.), noiseModel::Isotropic::Sigma(3, 1e-10));
120  graph += BetweenFactor<Pose2>(X(1),X(2), Pose2(1.,0.,0.), noiseModel::Isotropic::Sigma(3, 1));
121 
122  boost::shared_ptr<GaussianFactorGraph> fg = graph.linearize(config);
123 
124  VectorValues zeros = config.zeroVectors();
125 
127  parameters.setEpsilon_abs(1e-3);
128  parameters.setEpsilon_rel(1e-5);
129  parameters.setMaxIterations(100);
130  VectorValues actual = conjugateGradientDescent(*fg, zeros, parameters);
131 
133  expected.insert(X(1), Z_3x1);
134  expected.insert(X(2), Vector3(-0.5,0.,0.));
135  CHECK(assert_equal(expected, actual));
136 }
137 
138 /* ************************************************************************* */
139 int main() {
140  TestResult tr;
141  return TestRegistry::runAllTests(tr);
142 }
143 /* ************************************************************************* */
#define CHECK(condition)
Definition: Test.h:109
static const Eigen::MatrixBase< Vector3 >::ConstantReturnType Z_3x1
Definition: Vector.h:46
VectorValues optimize(const Eliminate &function=EliminationTraitsType::DefaultEliminate) const
Scalar * b
Definition: benchVecAdd.cpp:17
static int runAllTests(TestResult &result)
Eigen::Vector3d Vector3
Definition: Vector.h:43
VectorValues zeroVectors() const
Definition: Values.cpp:216
GaussianFactorGraph createGaussianFactorGraph()
Definition: smallExample.h:272
void insert(Key j, const Value &val)
Definition: Values.cpp:140
Matrix expected
Definition: testMatrix.cpp:974
EIGEN_DONT_INLINE Scalar zero()
Definition: svd_common.h:271
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:43
Iterative methods, implementation.
MatrixXd L
Definition: LLT_example.cpp:6
Definition: Half.h:150
NonlinearFactorGraph graph
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
GaussianFactor::shared_ptr linearize(const Values &x) const override
Eigen::VectorXd Vector
Definition: Vector.h:38
Array< double, 1, 3 > e(1./3., 0.5, 2.)
static ConjugateGradientParameters parameters
static const Pose3 pose1(Rot3(), Point3(0, 1, 0.5))
std::pair< Matrix, Vector > jacobian(const Ordering &ordering) const
TEST(Iterative, steepestDescent)
static Symbol x0('x', 0)
traits
Definition: chartTesting.h:28
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
Vector conjugateGradientDescent(const System &Ab, const Vector &x, const ConjugateGradientParameters &parameters)
Definition: iterative.cpp:45
Create small example with two poses and one landmark.
2D Pose
boost::shared_ptr< GaussianFactorGraph > linearize(const Values &linearizationPoint) const
Linearize a nonlinear factor graph.
#define X
Definition: icosphere.cpp:20
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
iterator insert(const std::pair< Key, Vector > &key_value)
Vector steepestDescent(const System &Ab, const Vector &x, const ConjugateGradientParameters &parameters)
Definition: iterative.cpp:40
int main()


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:47:42