/* ---------------------------------------------------------------------------- * GTSAM Copyright 2010, Georgia Tech Research Corporation, * Atlanta, Georgia 30332-0415 * All Rights Reserved * Authors: Frank Dellaert, et al. (see THANKS for the full author list) * See LICENSE for the license information * -------------------------------------------------------------------------- */ /* * NonlinearOptimization-inl.h * * Created on: Oct 17, 2010 * Author: Kai Ni * Description: Easy interfaces for NonlinearOptimizer */ #pragma once #include #include #include #include #include #include #include using namespace std; namespace gtsam { /** * The Elimination solver */ template T optimizeSequential(const G& graph, const T& initialEstimate, const NonlinearOptimizationParameters& parameters) { // Use a variable ordering from COLAMD Ordering::shared_ptr ordering = graph.orderingCOLAMD(initialEstimate); // initial optimization state is the same in both cases tested typedef NonlinearOptimizer Optimizer; Optimizer optimizer(boost::make_shared(graph), boost::make_shared(initialEstimate), ordering); // Levenberg-Marquardt Optimizer result = optimizer.levenbergMarquardt(parameters); return *result.values(); } /** * The multifrontal solver */ template T optimizeMultiFrontal(const G& graph, const T& initialEstimate, const NonlinearOptimizationParameters& parameters) { // Use a variable ordering from COLAMD Ordering::shared_ptr ordering = graph.orderingCOLAMD(initialEstimate); // initial optimization state is the same in both cases tested typedef NonlinearOptimizer Optimizer; Optimizer optimizer(boost::make_shared(graph), boost::make_shared(initialEstimate), ordering); // Levenberg-Marquardt Optimizer result = optimizer.levenbergMarquardt(parameters); return *result.values(); } /** * The cg solver */ template T optimizeCG(const G& graph, const T& initialEstimate, const NonlinearOptimizationParameters& parameters = NonlinearOptimizationParameters()) { typedef ConjugateGradientSolver Solver; typedef boost::shared_ptr sharedSolver; typedef NonlinearOptimizer Optimizer; Ordering::shared_ptr ordering = initialEstimate.orderingArbitrary() ; sharedSolver solver = boost::make_shared(graph, initialEstimate, *ordering, boost::make_shared()); Optimizer optimizer( boost::make_shared(graph), boost::make_shared(initialEstimate), ordering, solver); // Levenberg-Marquardt Optimizer result = optimizer.levenbergMarquardt(parameters); return *result.values(); } /** * The sparse preconditioned conjucate gradient solver */ template T optimizeSPCG(const G& graph, const T& initialEstimate, const NonlinearOptimizationParameters& parameters = NonlinearOptimizationParameters()) { // initial optimization state is the same in both cases tested typedef SubgraphSolver Solver; typedef boost::shared_ptr shared_Solver; typedef NonlinearOptimizer SPCGOptimizer; shared_Solver solver = boost::make_shared(graph, initialEstimate); SPCGOptimizer optimizer( boost::make_shared(graph), boost::make_shared(initialEstimate), solver->ordering(), solver); // Levenberg-Marquardt SPCGOptimizer result = optimizer.levenbergMarquardt(parameters); return *result.values(); } /** * optimization that returns the values */ template T optimize(const G& graph, const T& initialEstimate, const NonlinearOptimizationParameters& parameters, const enum LinearSolver& solver) { switch (solver) { case SEQUENTIAL: return optimizeSequential(graph, initialEstimate, parameters); case MULTIFRONTAL: return optimizeMultiFrontal(graph, initialEstimate, parameters); case CG: return optimizeCG(graph, initialEstimate, parameters); case SPCG: throw runtime_error("optimize: SPCG not supported yet due to the specific pose constraint"); } throw runtime_error("optimize: undefined solver"); } } //namespace gtsam