2012-03-23 11:38:57 +08:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
* 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
|
|
|
|
|
|
|
|
|
|
* -------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @file DoglegOptimizer.cpp
|
|
|
|
|
* @brief
|
|
|
|
|
* @author Richard Roberts
|
|
|
|
|
* @created Feb 26, 2012
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <gtsam/nonlinear/DoglegOptimizer.h>
|
|
|
|
|
|
|
|
|
|
#include <gtsam/linear/GaussianMultifrontalSolver.h>
|
|
|
|
|
#include <gtsam/linear/GaussianSequentialSolver.h>
|
|
|
|
|
#include <gtsam/nonlinear/DoglegOptimizerImpl.h>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
|
|
namespace gtsam {
|
|
|
|
|
|
2012-04-05 07:20:42 +08:00
|
|
|
/* ************************************************************************* */
|
2012-04-05 10:45:47 +08:00
|
|
|
NonlinearOptimizer::SharedState DoglegOptimizer::iterate(const NonlinearOptimizer::SharedState& _current) const {
|
|
|
|
|
|
|
|
|
|
const DoglegState& current = dynamic_cast<const DoglegState&>(*_current);
|
2012-03-23 11:38:57 +08:00
|
|
|
|
|
|
|
|
// Linearize graph
|
2012-04-05 10:45:47 +08:00
|
|
|
const Ordering& ordering = this->ordering(current.values);
|
|
|
|
|
GaussianFactorGraph::shared_ptr linear = graph_->linearize(current.values, ordering);
|
2012-03-23 11:38:57 +08:00
|
|
|
|
|
|
|
|
// Check whether to use QR
|
|
|
|
|
bool useQR;
|
2012-04-05 07:20:42 +08:00
|
|
|
if(params_->factorization == DoglegParams::LDL)
|
2012-03-23 11:38:57 +08:00
|
|
|
useQR = false;
|
2012-04-05 07:20:42 +08:00
|
|
|
else if(params_->factorization == DoglegParams::QR)
|
2012-03-23 11:38:57 +08:00
|
|
|
useQR = true;
|
|
|
|
|
else
|
|
|
|
|
throw runtime_error("Optimization parameter is invalid: DoglegParams::factorization");
|
|
|
|
|
|
|
|
|
|
// Pull out parameters we'll use
|
2012-04-05 07:20:42 +08:00
|
|
|
const bool dlVerbose = (params_->dlVerbosity > DoglegParams::SILENT);
|
2012-03-23 11:38:57 +08:00
|
|
|
|
|
|
|
|
// Do Dogleg iteration with either Multifrontal or Sequential elimination
|
|
|
|
|
DoglegOptimizerImpl::IterationResult result;
|
|
|
|
|
|
2012-04-05 07:20:42 +08:00
|
|
|
if(params_->elimination == DoglegParams::MULTIFRONTAL) {
|
2012-03-23 11:38:57 +08:00
|
|
|
GaussianBayesTree::shared_ptr bt = GaussianMultifrontalSolver(*linear, useQR).eliminate();
|
2012-04-05 10:45:47 +08:00
|
|
|
result = DoglegOptimizerImpl::Iterate(current.Delta, DoglegOptimizerImpl::ONE_STEP_PER_ITERATION, *bt, *graph_, current.values, ordering, current.error, dlVerbose);
|
2012-03-23 11:38:57 +08:00
|
|
|
|
2012-04-05 07:20:42 +08:00
|
|
|
} else if(params_->elimination == DoglegParams::SEQUENTIAL) {
|
2012-03-23 11:38:57 +08:00
|
|
|
GaussianBayesNet::shared_ptr bn = GaussianSequentialSolver(*linear, useQR).eliminate();
|
2012-04-05 10:45:47 +08:00
|
|
|
result = DoglegOptimizerImpl::Iterate(current.Delta, DoglegOptimizerImpl::ONE_STEP_PER_ITERATION, *bn, *graph_, current.values, ordering, current.error, dlVerbose);
|
2012-03-23 11:38:57 +08:00
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
throw runtime_error("Optimization parameter is invalid: DoglegParams::elimination");
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-05 10:45:47 +08:00
|
|
|
// Maybe show output
|
|
|
|
|
if(params_->verbosity >= NonlinearOptimizerParams::DELTA) result.dx_d.print("delta");
|
2012-04-05 07:20:42 +08:00
|
|
|
|
2012-04-05 10:45:47 +08:00
|
|
|
// Create new state with new values and new error
|
|
|
|
|
SharedState newState = boost::make_shared<DoglegState>();
|
|
|
|
|
newState->values = current.values.retract(result.dx_d, ordering);
|
2012-04-05 07:20:42 +08:00
|
|
|
newState->error = result.f_error;
|
2012-04-05 10:45:47 +08:00
|
|
|
newState->iterations = current.iterations + 1;
|
2012-04-02 08:26:42 +08:00
|
|
|
newState->Delta = result.Delta;
|
2012-03-23 11:38:57 +08:00
|
|
|
|
2012-04-02 08:26:42 +08:00
|
|
|
return newState;
|
2012-03-23 11:38:57 +08:00
|
|
|
}
|
|
|
|
|
|
2012-04-05 07:20:42 +08:00
|
|
|
/* ************************************************************************* */
|
|
|
|
|
NonlinearOptimizer::SharedState DoglegOptimizer::initialState(const Values& initialValues) const {
|
|
|
|
|
SharedState initial = boost::make_shared<DoglegState>();
|
2012-04-05 10:45:47 +08:00
|
|
|
defaultInitialState(initialValues, *initial);
|
2012-04-05 07:20:42 +08:00
|
|
|
initial->Delta = params_->deltaInitial;
|
|
|
|
|
return initial;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-23 11:38:57 +08:00
|
|
|
} /* namespace gtsam */
|