g2o
Classes | Functions
curve_fit.cpp File Reference
#include <Eigen/Core>
#include <iostream>
#include "g2o/stuff/sampler.h"
#include "g2o/stuff/command_args.h"
#include "g2o/core/sparse_optimizer.h"
#include "g2o/core/block_solver.h"
#include "g2o/core/solver.h"
#include "g2o/core/optimization_algorithm_levenberg.h"
#include "g2o/core/base_vertex.h"
#include "g2o/core/base_unary_edge.h"
#include "g2o/solvers/dense/linear_solver_dense.h"
Include dependency graph for curve_fit.cpp:

Go to the source code of this file.

Classes

class  VertexParams
 the params, a, b, and lambda for a * exp(-lambda * t) + b More...
 
class  EdgePointOnCurve
 measurement for a point on the curve More...
 

Functions

int main (int argc, char **argv)
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 113 of file curve_fit.cpp.

References g2o::OptimizableGraph::addEdge(), g2o::OptimizableGraph::addVertex(), g2o::BaseVertex< D, T >::estimate(), g2o::Sampler::gaussRand(), g2o::SparseOptimizer::initializeOptimization(), g2o::SparseOptimizer::optimize(), g2o::CommandArgs::param(), g2o::CommandArgs::parseArgs(), g2o::SparseOptimizer::setAlgorithm(), g2o::BaseVertex< D, T >::setEstimate(), g2o::OptimizableGraph::Vertex::setId(), g2o::BaseEdge< D, E >::setInformation(), g2o::BaseEdge< D, E >::setMeasurement(), g2o::SparseOptimizer::setVerbose(), g2o::HyperGraph::Edge::setVertex(), and g2o::Sampler::uniformRand().

114 {
115  int numPoints;
116  int maxIterations;
117  bool verbose;
118  std::vector<int> gaugeList;
119  string dumpFilename;
120  g2o::CommandArgs arg;
121  arg.param("dump", dumpFilename, "", "dump the points into a file");
122  arg.param("numPoints", numPoints, 50, "number of points sampled from the curve");
123  arg.param("i", maxIterations, 10, "perform n iterations");
124  arg.param("v", verbose, false, "verbose output of the optimization process");
125 
126  arg.parseArgs(argc, argv);
127 
128  // generate random data
129  double a = 2.;
130  double b = 0.4;
131  double lambda = 0.2;
132  Eigen::Vector2d* points = new Eigen::Vector2d[numPoints];
133  for (int i = 0; i < numPoints; ++i) {
134  double x = g2o::Sampler::uniformRand(0, 10);
135  double y = a * exp(-lambda * x) + b;
136  // add Gaussian noise
137  y += g2o::Sampler::gaussRand(0, 0.02);
138  points[i].x() = x;
139  points[i].y() = y;
140  }
141 
142  if (dumpFilename.size() > 0) {
143  ofstream fout(dumpFilename.c_str());
144  for (int i = 0; i < numPoints; ++i)
145  fout << points[i].transpose() << endl;
146  }
147 
148  // some handy typedefs
151 
152  // setup the solver
153  g2o::SparseOptimizer optimizer;
154  optimizer.setVerbose(false);
155  MyLinearSolver* linearSolver = new MyLinearSolver();
156  MyBlockSolver* solver_ptr = new MyBlockSolver(linearSolver);
158  optimizer.setAlgorithm(solver);
159 
160  // build the optimization problem given the points
161  // 1. add the parameter vertex
162  VertexParams* params = new VertexParams();
163  params->setId(0);
164  params->setEstimate(Eigen::Vector3d(1,1,1)); // some initial value for the params
165  optimizer.addVertex(params);
166  // 2. add the points we measured to be on the curve
167  for (int i = 0; i < numPoints; ++i) {
169  e->setInformation(Eigen::Matrix<double, 1, 1>::Identity());
170  e->setVertex(0, params);
171  e->setMeasurement(points[i]);
172  optimizer.addEdge(e);
173  }
174 
175  // perform the optimization
176  optimizer.initializeOptimization();
177  optimizer.setVerbose(verbose);
178  optimizer.optimize(maxIterations);
179 
180  if (verbose)
181  cout << endl;
182 
183  // print out the result
184  cout << "Target curve" << endl;
185  cout << "a * exp(-lambda * x) + b" << endl;
186  cout << "Iterative least squares solution" << endl;
187  cout << "a = " << params->estimate()(0) << endl;
188  cout << "b = " << params->estimate()(1) << endl;
189  cout << "lambda = " << params->estimate()(2) << endl;
190  cout << endl;
191 
192  // clean up
193  delete[] points;
194 
195  return 0;
196 }
Command line parsing of argc and argv.
Definition: command_args.h:46
measurement for a point on the curve
Definition: curve_fit.cpp:84
static double gaussRand(double mean, double sigma)
Definition: sampler.h:86
Implementation of the Levenberg Algorithm.
int optimize(int iterations, bool online=false)
virtual void setMeasurement(const Measurement &m)
Definition: base_edge.h:76
linear solver using dense cholesky decomposition
void setVertex(size_t i, Vertex *v)
Definition: hyper_graph.h:194
virtual void setId(int id)
sets the id of the node in the graph be sure that the graph keeps consistent after changing the id ...
static double uniformRand(double lowerBndr, double upperBndr)
Definition: sampler.h:100
bool parseArgs(int argc, char **argv, bool exitOnError=true)
void setVerbose(bool verbose)
void setAlgorithm(OptimizationAlgorithm *algorithm)
EIGEN_STRONG_INLINE void setInformation(const InformationType &information)
Definition: base_edge.h:69
void param(const std::string &name, bool &p, bool defValue, const std::string &desc)
void setEstimate(const EstimateType &et)
set the estimate for the vertex also calls updateCache()
Definition: base_vertex.h:101
the params, a, b, and lambda for a * exp(-lambda * t) + b
Definition: curve_fit.cpp:45
const EstimateType & estimate() const
return the current estimate of the vertex
Definition: base_vertex.h:99
Implementation of a solver operating on the blocks of the Hessian.
Definition: block_solver.h:96
virtual bool initializeOptimization(HyperGraph::EdgeSet &eset)
virtual bool addEdge(HyperGraph::Edge *e)
virtual bool addVertex(HyperGraph::Vertex *v, Data *userData)