gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 a new NonlinearOptimizer object containing the optimized variable assignment. Call the values() method to retrieve the optimized estimate. Alternatively, to take a shortcut, instead of calling optimize(), call optimized(), which performs full optimization and returns the resulting Values instead of the new optimizer.

Note: This class is immutable, optimize() and iterate() return new NonlinearOptimizer objects, so be sure to use the returned object and not simply keep the unchanged original.

Simple and compact example:

// One-liner to do full optimization and use the result.
// Note use of "optimized()" to directly return Values, instead of "optimize()" that returns a new optimizer.
Values::const_shared_ptr result = DoglegOptimizer(graph, initialValues).optimized();

Example exposing more functionality and details:

// Create initial optimizer
DoglegOptimizer initial(graph, initialValues);
// Run full optimization until convergence.
// Note use of "optimize()" to return a new optimizer, instead of "optimized()" that returns only the Values.
// NonlinearOptimizer pointers are always returned, though they are actually a derived optimizer type.
NonlinearOptimizer::auto_ptr final = initial->optimize();
// The new optimizer has results and statistics
cout << "Converged in " << final->iterations() << " iterations "
"with final error " << final->error() << endl;
// The values are a const_shared_ptr (boost::shared_ptr<const Values>)
Values::const_shared_ptr result = final->values();
// Use the results
useTheResult(result);

Example of setting parameters before optimization:

// Each derived optimizer type has its own parameters class, which inherits from NonlinearOptimizerParams
DoglegParams params;
params.factorization = DoglegParams::QR;
params.relativeErrorTol = 1e-3;
params.absoluteErrorTol = 1e-3;
// Optimize
Values::const_shared_ptr result = DoglegOptimizer(graph, initialValues, params).optimized();

This interface also exposes an iterate() method, which performs one iteration, returning a NonlinearOptimizer containing the adjusted variable assignment. 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.

To modify the graph, values, or parameters between iterations, call the update() functions, which preserve all other state (for example, the trust region size in DoglegOptimizer). Derived optimizer classes also have additional update methods, not in this abstract interface, for updating algorithm-specific state.

For more flexibility, since all functions are virtual, you may override them 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 new NonlinearOptimizer class containing the optimized variable assignments, which may be retrieved with values(). More...
 
const ValuesoptimizeSafely ()
 Optimize, but return empty result if any uncaught exception is thrown Intended for MATLAB. More...
 
double error () const
 return error
 
int iterations () const
 return number of iterations
 
const Valuesvalues () const
 return values
 
Advanced interface
virtual ~NonlinearOptimizer ()
 Virtual destructor.
 
virtual VectorValues solve (const GaussianFactorGraph &gfg, const Values &initial, const NonlinearOptimizerParams &params) const
 Default function to do linear solve, i.e. More...
 
virtual void iterate ()=0
 Perform a single iteration, returning a new NonlinearOptimizer class containing the updated variable assignments, which may be retrieved with values().
 

Public Types

typedef boost::shared_ptr
< const NonlinearOptimizer
shared_ptr
 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
NonlinearOptimizerState
_state () const =0
 
virtual const
NonlinearOptimizerParams
_params () const =0
 
 NonlinearOptimizer (const NonlinearFactorGraph &graph)
 Constructor for initial construction of base classes. More...
 

Protected Attributes

NonlinearFactorGraph graph_
 

Constructor & Destructor Documentation

gtsam::NonlinearOptimizer::NonlinearOptimizer ( const NonlinearFactorGraph graph)
inlineprotected

Constructor for initial construction of base classes.

Member Function Documentation

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

Optimize for the maximum-likelihood estimate, returning a new NonlinearOptimizer class containing the optimized variable assignments, which may be retrieved with values().

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.

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
VectorValues gtsam::NonlinearOptimizer::solve ( const GaussianFactorGraph gfg,
const Values initial,
const NonlinearOptimizerParams params 
) const
virtual

Default function to do linear solve, i.e.

optimize a GaussianFactorGraph


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