Pose2SLAMwSPCG.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 // For an explanation of headers below, please see Pose2SLAMExample.cpp
21 #include <gtsam/geometry/Pose2.h>
23 
24 // In contrast to that example, however, we will use a PCG solver here
26 
27 using namespace std;
28 using namespace gtsam;
29 
30 int main(int argc, char** argv) {
31  // 1. Create a factor graph container and add factors to it
33 
34  // 2a. Add a prior on the first pose, setting it to the origin
35  Pose2 prior(0.0, 0.0, 0.0); // prior at origin
36  auto priorNoise = noiseModel::Diagonal::Sigmas(Vector3(0.3, 0.3, 0.1));
37  graph.addPrior(1, prior, priorNoise);
38 
39  // 2b. Add odometry factors
40  auto odometryNoise = noiseModel::Diagonal::Sigmas(Vector3(0.2, 0.2, 0.1));
41  graph.emplace_shared<BetweenFactor<Pose2> >(1, 2, Pose2(2.0, 0.0, M_PI_2), odometryNoise);
42  graph.emplace_shared<BetweenFactor<Pose2> >(2, 3, Pose2(2.0, 0.0, M_PI_2), odometryNoise);
43  graph.emplace_shared<BetweenFactor<Pose2> >(3, 4, Pose2(2.0, 0.0, M_PI_2), odometryNoise);
44  graph.emplace_shared<BetweenFactor<Pose2> >(4, 5, Pose2(2.0, 0.0, M_PI_2), odometryNoise);
45 
46  // 2c. Add the loop closure constraint
47  auto model = noiseModel::Diagonal::Sigmas(Vector3(0.2, 0.2, 0.1));
48  graph.emplace_shared<BetweenFactor<Pose2> >(5, 1, Pose2(0.0, 0.0, 0.0),
49  model);
50  graph.print("\nFactor Graph:\n"); // print
51 
52  // 3. Create the data structure to hold the initialEstimate estimate to the
53  // solution
54  Values initialEstimate;
55  initialEstimate.insert(1, Pose2(0.5, 0.0, 0.2));
56  initialEstimate.insert(2, Pose2(2.3, 0.1, 1.1));
57  initialEstimate.insert(3, Pose2(2.1, 1.9, 2.8));
58  initialEstimate.insert(4, Pose2(-.3, 2.5, 4.2));
59  initialEstimate.insert(5, Pose2(0.1, -0.7, 5.8));
60  initialEstimate.print("\nInitial Estimate:\n"); // print
61 
62  // 4. Single Step Optimization using Levenberg-Marquardt
64  parameters.verbosity = NonlinearOptimizerParams::ERROR;
65  parameters.verbosityLM = LevenbergMarquardtParams::LAMBDA;
66 
67  // LM is still the outer optimization loop, but by specifying "Iterative"
68  // below We indicate that an iterative linear solver should be used. In
69  // addition, the *type* of the iterativeParams decides on the type of
70  // iterative solver, in this case the SPCG (subgraph PCG)
71  parameters.linearSolverType = NonlinearOptimizerParams::Iterative;
72  parameters.iterativeParams = boost::make_shared<SubgraphSolverParameters>();
73 
74  LevenbergMarquardtOptimizer optimizer(graph, initialEstimate, parameters);
75  Values result = optimizer.optimize();
76  result.print("Final Result:\n");
77  cout << "subgraph solver final error = " << graph.error(result) << endl;
78 
79  return 0;
80 }
noiseModel::Diagonal::shared_ptr odometryNoise
Eigen::Vector3d Vector3
Definition: Vector.h:43
Point2 prior(const Point2 &x)
Prior on a single pose.
Definition: simulated2D.h:87
noiseModel::Diagonal::shared_ptr model
void insert(Key j, const Value &val)
Definition: Values.cpp:140
Subgraph Solver from IROS 2010.
Verbosity verbosity
The printing verbosity during optimization (default SILENT)
Definition: Half.h:150
NonlinearFactorGraph graph
int main(int argc, char **argv)
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&...args)
Emplace a shared pointer to factor of given type.
Definition: FactorGraph.h:172
Values result
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
IterativeOptimizationParameters::shared_ptr iterativeParams
The container for iterativeOptimization parameters. used in CG Solvers.
static ConjugateGradientParameters parameters
traits
Definition: chartTesting.h:28
void print(const std::string &str="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Definition: Values.cpp:77
void print(const std::string &str="NonlinearFactorGraph: ", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
noiseModel::Diagonal::shared_ptr priorNoise
double error(const Values &values) const
2D Pose
VerbosityLM verbosityLM
The verbosity level for Levenberg-Marquardt (default: SILENT), see also NonlinearOptimizerParams::ver...
LinearSolverType linearSolverType
The type of linear solver to use in the nonlinear optimizer.


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:43:27