gtsam
Loading...
Searching...
No Matches
gtsam::NonlinearOptimizer Class Referenceabstract

Detailed Description

This is the abstract interface for classes that can optimize for the maximum-likelihood estimate of a NonlinearFactorGraph.

To use a class derived from this interface, construct the class with a NonlinearFactorGraph and an initial Values variable assignment. Next, call the optimize() method, which returns the optimized variable assignment.

Simple and compact example:

// One-liner to do full optimization and use the result.
Values result = DoglegOptimizer(graph, initialValues).optimize();
This class performs Dogleg nonlinear optimization.
Definition DoglegOptimizer.h:68
const NonlinearFactorGraph & graph() const
return the graph with nonlinear factors
Definition NonlinearOptimizer.h:141
virtual const Values & optimize()
Optimize for the maximum-likelihood estimate, returning a the optimized variable assignments.
Definition NonlinearOptimizer.h:118
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65

Example exposing more functionality and details:

// Create initial optimizer.
DoglegOptimizer optimizer(graph, initialValues);
// Run full optimization until convergence.
Values result = optimizer.optimize();
// The optimizer has results and statistics.
cout << "Converged in " << optimizer.iterations() << " iterations "
<< "with final error " << optimizer.error() << endl;

Example of setting parameters before optimization:

// Each optimizer has a parameters class derived from
// NonlinearOptimizerParams.
DoglegParams params;
params.factorization = DoglegParams::QR;
params.relativeErrorTol = 1e-3;
params.absoluteErrorTol = 1e-3;
// Optimize.
Values result = DoglegOptimizer(graph, initialValues, params).optimize();
Parameters for Levenberg-Marquardt optimization.
Definition DoglegOptimizer.h:32
double absoluteErrorTol
The maximum absolute error decrease to stop iterating (default 1e-5).
Definition NonlinearOptimizerParams.h:46
double relativeErrorTol
The maximum relative error decrease to stop iterating (default 1e-5).
Definition NonlinearOptimizerParams.h:45

This interface also exposes an iterate() method, which performs one iteration. The optimize() method simply calls iterate() multiple times, until the error changes less than a threshold. We expose iterate() so that you can easily control what happens between iterations, such as drawing or printing, moving points from behind the camera to in front, etc.

For more flexibility, you may override virtual methods in your own derived class.

Inheritance diagram for gtsam::NonlinearOptimizer:

Public Member Functions

Standard interface
virtual const Valuesoptimize ()
 Optimize for the maximum-likelihood estimate, returning a the optimized variable assignments.
const ValuesoptimizeSafely ()
 Optimize, but return empty result if any uncaught exception is thrown Intended for MATLAB.
double error () const
 return error in current optimizer state
size_t iterations () const
 return number of iterations in current optimizer state
const Valuesvalues () const
 return values in current optimizer state
const NonlinearFactorGraphgraph () const
 return the graph with nonlinear factors
Advanced interface
virtual ~NonlinearOptimizer ()
 Virtual destructor.
virtual VectorValues solve (const GaussianFactorGraph &gfg, const NonlinearOptimizerParams &params) const
 Default function to do linear solve, i.e.
virtual GaussianFactorGraph::shared_ptr iterate ()=0
 Perform a single iteration, returning GaussianFactorGraph corresponding to the linearized factor graph.

Public Types

using shared_ptr = std::shared_ptr<const NonlinearOptimizer>
 A shared pointer to this class.

Protected Member Functions

void defaultOptimize ()
 A default implementation of the optimization loop, which calls iterate() until checkConvergence returns true.
virtual const NonlinearOptimizerParams_params () const =0
virtual bool ensureMultifrontalSolver (const NonlinearOptimizerParams &params, const Values &values) const
 Ensure that the nonlinearMultifrontalSolver_ is populated if (and only if) the params request the multifrontal Cholesky solver type (e.g., MULTIFRONTAL_SOLVER).
 NonlinearOptimizer (const NonlinearFactorGraph &graph, std::unique_ptr< internal::NonlinearOptimizerState > state)
 Constructor for initial construction of base classes.

Protected Attributes

std::shared_ptr< const NonlinearFactorGraphgraph_
 The graph with nonlinear factors.
std::unique_ptr< internal::NonlinearOptimizerState > state_
 PIMPL'd state.
std::unique_ptr< NonlinearMultifrontalSolvernonlinearMultifrontalSolver_
 Solver for multifrontal Cholesky, lazily created.
std::unique_ptr< internal::CholmodSolver > cholmodSolver_
 Optional reusable CHOLMOD numerical and symbolic session.

Constructor & Destructor Documentation

◆ NonlinearOptimizer()

gtsam::NonlinearOptimizer::NonlinearOptimizer ( const NonlinearFactorGraph & graph,
std::unique_ptr< internal::NonlinearOptimizerState > state )
protected

Constructor for initial construction of base classes.

Takes ownership of state.

Member Function Documentation

◆ _params()

virtual const NonlinearOptimizerParams & gtsam::NonlinearOptimizer::_params ( ) const
protectedpure virtual

◆ ensureMultifrontalSolver()

bool gtsam::NonlinearOptimizer::ensureMultifrontalSolver ( const NonlinearOptimizerParams & params,
const Values & values ) const
protectedvirtual

Ensure that the nonlinearMultifrontalSolver_ is populated if (and only if) the params request the multifrontal Cholesky solver type (e.g., MULTIFRONTAL_SOLVER).

Returns true if the multifrontal solver is available and ready. If a different solver type is requested in params, this function returns false without modifying any solver state. If constraints are present or multifrontal initialization fails, it also returns false (and callers should fall back to the legacy linear solver path).

Reimplemented in gtsam::SfmLevenbergMarquardtOptimizer.

◆ iterate()

virtual GaussianFactorGraph::shared_ptr gtsam::NonlinearOptimizer::iterate ( )
pure virtual

Perform a single iteration, returning GaussianFactorGraph corresponding to the linearized factor graph.

Implemented in gtsam::DoglegOptimizer, gtsam::GaussNewtonOptimizer, gtsam::LevenbergMarquardtOptimizer, and gtsam::NonlinearConjugateGradientOptimizer.

◆ optimize()

virtual const Values & gtsam::NonlinearOptimizer::optimize ( )
inlinevirtual

Optimize for the maximum-likelihood estimate, returning a the optimized variable assignments.

This function simply calls iterate() in a loop, checking for convergence with check_convergence(). For fine-grain control over the optimization process, you may call iterate() and check_convergence() yourself, and if needed modify the optimization state between iterations.

Reimplemented in gtsam::NonlinearConjugateGradientOptimizer.

◆ optimizeSafely()

const Values & gtsam::NonlinearOptimizer::optimizeSafely ( )

Optimize, but return empty result if any uncaught exception is thrown Intended for MATLAB.

In C++, use above and catch exceptions. No message is printed: it is up to the caller to check the result

Parameters
optimizera non-linear optimizer

◆ solve()

VectorValues gtsam::NonlinearOptimizer::solve ( const GaussianFactorGraph & gfg,
const NonlinearOptimizerParams & params ) const
virtual

Default function to do linear solve, i.e.

optimize a GaussianFactorGraph

Reimplemented in gtsam::SfmLevenbergMarquardtOptimizer.


The documentation for this class was generated from the following files: