g2o
Loading...
Searching...
No Matches
Classes | Functions
polynomial_fit.cpp File Reference
#include <unsupported/Eigen/Polynomials>
#include "g2o/core/base_binary_edge.h"
#include "g2o/core/base_dynamic_vertex.h"
#include "g2o/core/base_unary_edge.h"
#include "g2o/core/block_solver.h"
#include "g2o/core/optimization_algorithm_levenberg.h"
#include "g2o/core/sparse_optimizer.h"
#include "g2o/solvers/eigen/linear_solver_eigen.h"
#include "g2o/stuff/sampler.h"
Include dependency graph for polynomial_fit.cpp:

Go to the source code of this file.

Classes

class  PolynomialCoefficientVertex
 
class  PolynomialSingleValueEdge
 

Functions

int main (int argc, const char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
const char *  argv[] 
)

Definition at line 133 of file polynomial_fit.cpp.

133 {
134 // Number of dimensions of the polynomial; the default is 4
135 int polynomialDimension = 4;
136 if (argc > 1) {
137 polynomialDimension = atoi(argv[1]);
138 }
139
140 // Create the coefficients for the polynomial (all drawn randomly)
141 Eigen::VectorXd p(polynomialDimension);
142 for (int i = 0; i < polynomialDimension; ++i) {
143 p[i] = g2o::sampleUniform(-1, 1);
144 }
145
146 std::cout << "Ground truth vector=" << p.transpose() << std::endl;
147
148 // The number of observations in the polynomial; the default is 6
149 int obs = 6;
150 if (argc > 2) {
151 obs = atoi(argv[2]);
152 }
153
154 // Sample the observations; we don't do anything with them here, but
155 // they could be plotted
156 double sigmaZ = 0.1;
157 Eigen::VectorXd x(obs);
158 Eigen::VectorXd z(obs);
159
160 for (int i = 0; i < obs; ++i) {
161 x[i] = g2o::sampleUniform(-5, 5);
162 z[i] = Eigen::poly_eval(p, x[i]) + sigmaZ * g2o::sampleGaussian();
163 }
164
165 // Construct the graph and set up the solver and optimiser
166 std::unique_ptr<g2o::BlockSolverX::LinearSolverType> linearSolver =
167 std::make_unique<
169
170 // Set up the solver
171 std::unique_ptr<g2o::BlockSolverX> blockSolver =
172 std::make_unique<g2o::BlockSolverX>(std::move(linearSolver));
173
174 // Set up the optimisation algorithm
175 g2o::OptimizationAlgorithm* optimisationAlgorithm =
176 new g2o::OptimizationAlgorithmLevenberg(std::move(blockSolver));
177
178 // Create the graph and configure it
179 std::unique_ptr<g2o::SparseOptimizer> optimizer =
180 std::make_unique<g2o::SparseOptimizer>();
181 optimizer->setVerbose(true);
182 optimizer->setAlgorithm(optimisationAlgorithm);
183
184 // Create the vertex; note its dimension is currently is undefined
186 pv->setId(0);
187 optimizer->addVertex(pv);
188
189 // Create the information matrix
191 PolynomialSingleValueEdge::InformationType::Zero();
192 omega(0, 0) = 1 / (sigmaZ * sigmaZ);
193
194 // Create the observations and the edges
195 for (int i = 0; i < obs; ++i) {
197 new PolynomialSingleValueEdge(x[i], z[i], omega);
198 pe->setVertex(0, pv);
199 optimizer->addEdge(pe);
200 }
201
202 // Now run the same optimization problem for different choices of
203 // dimension of the polynomial vertex. This shows how we can
204 // dynamically change the vertex dimensions in an alreacy
205 // constructed graph. Note that you must call initializeOptimization
206 // before you can optimize after a state dimension has changed.
207 for (int testDimension = 1; testDimension <= polynomialDimension;
208 ++testDimension) {
209 pv->setDimension(testDimension);
210 optimizer->initializeOptimization();
211 optimizer->optimize(10);
212 std::cout << "Computed parameters = " << pv->estimate().transpose()
213 << std::endl;
214 }
215 for (int testDimension = polynomialDimension - 1; testDimension >= 1;
216 --testDimension) {
217 pv->setDimension(testDimension);
218 optimizer->initializeOptimization();
219 optimizer->optimize(10);
220 std::cout << "Computed parameters = " << pv->estimate().transpose()
221 << std::endl;
222 }
223}
virtual bool setDimension(int newDimension)
internal::BaseEdgeTraits< D >::InformationType InformationType
Definition base_edge.h:91
const EstimateType & estimate() const
return the current estimate of the vertex
void setVertex(size_t i, Vertex *v)
linear solver which uses the sparse Cholesky solver from Eigen
Implementation of the Levenberg Algorithm.
Generic interface for a non-linear solver operating on a graph.
double sampleUniform(double min, double max, std::mt19937 *generator)
Definition sampler.cpp:35
double sampleGaussian(std::mt19937 *generator)
Definition sampler.cpp:40

References g2o::BaseVertex< D, T >::estimate(), g2o::sampleGaussian(), g2o::sampleUniform(), g2o::BaseDynamicVertex< T >::setDimension(), g2o::OptimizableGraph::Vertex::setId(), and g2o::HyperGraph::Edge::setVertex().