g2o
Functions
simple_optimize.cpp File Reference
#include <iostream>
#include "g2o/core/sparse_optimizer.h"
#include "g2o/core/block_solver.h"
#include "g2o/core/optimization_algorithm_gauss_newton.h"
#include "g2o/core/optimization_algorithm_levenberg.h"
#include "g2o/solvers/csparse/linear_solver_csparse.h"
#include "g2o/core/factory.h"
#include "g2o/stuff/command_args.h"
Include dependency graph for simple_optimize.cpp:

Go to the source code of this file.

Functions

 G2O_USE_TYPE_GROUP (slam2d)
 
 G2O_USE_TYPE_GROUP (slam3d)
 
int main (int argc, char **argv)
 

Function Documentation

G2O_USE_TYPE_GROUP ( slam2d  )
G2O_USE_TYPE_GROUP ( slam3d  )
int main ( int  argc,
char **  argv 
)

Definition at line 48 of file simple_optimize.cpp.

References g2o::SparseOptimizer::initializeOptimization(), g2o::OptimizableGraph::load(), g2o::SparseOptimizer::optimize(), g2o::CommandArgs::param(), g2o::CommandArgs::paramLeftOver(), g2o::CommandArgs::parseArgs(), g2o::OptimizableGraph::save(), g2o::SparseOptimizer::setAlgorithm(), and g2o::SparseOptimizer::setVerbose().

49 {
50  // Command line parsing
51  int maxIterations;
52  string outputFilename;
53  string inputFilename;
54  CommandArgs arg;
55  arg.param("i", maxIterations, 10, "perform n iterations, if negative consider the gain");
56  arg.param("o", outputFilename, "", "output final version of the graph");
57  arg.paramLeftOver("graph-input", inputFilename, "", "graph file which will be processed");
58  arg.parseArgs(argc, argv);
59 
60  // create the linear solver
62 
63  // create the block solver on top of the linear solver
64  BlockSolverX* blockSolver = new BlockSolverX(linearSolver);
65 
66  // create the algorithm to carry out the optimization
67  //OptimizationAlgorithmGaussNewton* optimizationAlgorithm = new OptimizationAlgorithmGaussNewton(blockSolver);
68  OptimizationAlgorithmLevenberg* optimizationAlgorithm = new OptimizationAlgorithmLevenberg(blockSolver);
69 
70  // NOTE: We skip to fix a variable here, either this is stored in the file
71  // itself or Levenberg will handle it.
72 
73  // create the optimizer to load the data and carry out the optimization
74  SparseOptimizer optimizer;
75  optimizer.setVerbose(true);
76  optimizer.setAlgorithm(optimizationAlgorithm);
77 
78  ifstream ifs(inputFilename.c_str());
79  if (! ifs) {
80  cerr << "unable to open " << inputFilename << endl;
81  return 1;
82  }
83  optimizer.load(ifs);
84  optimizer.initializeOptimization();
85  optimizer.optimize(maxIterations);
86 
87  if (outputFilename.size() > 0) {
88  if (outputFilename == "-") {
89  cerr << "saving to stdout";
90  optimizer.save(cout);
91  } else {
92  cerr << "saving " << outputFilename << " ... ";
93  optimizer.save(outputFilename.c_str());
94  }
95  cerr << "done." << endl;
96  }
97  return 0;
98 }
Command line parsing of argc and argv.
Definition: command_args.h:46
Implementation of the Levenberg Algorithm.
int optimize(int iterations, bool online=false)
Traits::LinearSolverType LinearSolverType
Definition: block_solver.h:110
bool parseArgs(int argc, char **argv, bool exitOnError=true)
virtual bool save(std::ostream &os, int level=0) const
save the graph to a stream. Again uses the Factory system.
void setVerbose(bool verbose)
linear solver which uses CSparse
void paramLeftOver(const std::string &name, std::string &p, const std::string &defValue, const std::string &desc, bool optional=false)
void setAlgorithm(OptimizationAlgorithm *algorithm)
void param(const std::string &name, bool &p, bool defValue, const std::string &desc)
virtual bool load(std::istream &is, bool createEdges=true)
load the graph from a stream. Uses the Factory singleton for creating the vertices and edges...
Implementation of a solver operating on the blocks of the Hessian.
Definition: block_solver.h:96
virtual bool initializeOptimization(HyperGraph::EdgeSet &eset)
BlockSolver< BlockSolverTraits< Eigen::Dynamic, Eigen::Dynamic > > BlockSolverX
Definition: block_solver.h:179