g2o
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
g2o::OptimizationAlgorithmDogleg Class Reference

Implementation of Powell's Dogleg Algorithm. More...

#include <optimization_algorithm_dogleg.h>

Inheritance diagram for g2o::OptimizationAlgorithmDogleg:
Inheritance graph
[legend]
Collaboration diagram for g2o::OptimizationAlgorithmDogleg:
Collaboration graph
[legend]

Public Types

enum  { STEP_UNDEFINED, STEP_SD, STEP_GN, STEP_DL }
 type of the step to take More...
 

Public Member Functions

 OptimizationAlgorithmDogleg (BlockSolverBase *solver)
 
virtual ~OptimizationAlgorithmDogleg ()
 
virtual SolverResult solve (int iteration, bool online=false)
 
virtual void printVerbose (std::ostream &os) const
 
int lastStep () const
 return the type of the last step taken by the algorithm More...
 
double trustRegion () const
 return the diameter of the trust region More...
 
- Public Member Functions inherited from g2o::OptimizationAlgorithmWithHessian
 OptimizationAlgorithmWithHessian (Solver *solver)
 
virtual ~OptimizationAlgorithmWithHessian ()
 
virtual bool init (bool online=false)
 
virtual bool computeMarginals (SparseBlockMatrix< MatrixXD > &spinv, const std::vector< std::pair< int, int > > &blockIndices)
 
virtual bool buildLinearStructure ()
 
virtual void updateLinearSystem ()
 
virtual bool updateStructure (const std::vector< HyperGraph::Vertex * > &vset, const HyperGraph::EdgeSet &edges)
 
Solversolver ()
 return the underlying solver used to solve the linear system More...
 
virtual void setWriteDebug (bool writeDebug)
 
virtual bool writeDebug () const
 
- Public Member Functions inherited from g2o::OptimizationAlgorithm
 OptimizationAlgorithm ()
 
virtual ~OptimizationAlgorithm ()
 
const SparseOptimizeroptimizer () const
 return the optimizer operating on More...
 
SparseOptimizeroptimizer ()
 
void setOptimizer (SparseOptimizer *optimizer)
 
const PropertyMapproperties () const
 return the properties of the solver More...
 
bool updatePropertiesFromString (const std::string &propString)
 
void printProperties (std::ostream &os) const
 

Static Public Member Functions

static const char * stepType2Str (int stepType)
 convert the type into an integer More...
 

Protected Attributes

Property< int > * _maxTrialsAfterFailure
 
Property< double > * _userDeltaInit
 
Property< double > * _initialLambda
 
Property< double > * _lamdbaFactor
 
VectorXD _hsd
 steepest decent step More...
 
VectorXD _hdl
 final dogleg step More...
 
VectorXD _auxVector
 auxilary vector used to perform multiplications or other stuff More...
 
double _currentLambda
 the damping factor to force positive definite matrix More...
 
double _delta
 trust region More...
 
int _lastStep
 type of the step taken by the algorithm More...
 
bool _wasPDInAllIterations
 the matrix we solve was positive definite in all iterations -> if not apply damping More...
 
int _lastNumTries
 
- Protected Attributes inherited from g2o::OptimizationAlgorithmWithHessian
Solver_solver
 
Property< bool > * _writeDebug
 
- Protected Attributes inherited from g2o::OptimizationAlgorithm
SparseOptimizer_optimizer
 the optimizer the solver is working on More...
 
PropertyMap _properties
 the properties of your solver, use this to store the parameters of your solver More...
 

Detailed Description

Implementation of Powell's Dogleg Algorithm.

Definition at line 40 of file optimization_algorithm_dogleg.h.

Member Enumeration Documentation

anonymous enum

Constructor & Destructor Documentation

g2o::OptimizationAlgorithmDogleg::OptimizationAlgorithmDogleg ( BlockSolverBase solver)
explicit

construct the Dogleg algorithm, which will use the given Solver for solving the linearized system.

Definition at line 41 of file optimization_algorithm_dogleg.cpp.

References _delta, _initialLambda, _lamdbaFactor, _lastStep, _maxTrialsAfterFailure, g2o::OptimizationAlgorithm::_properties, _userDeltaInit, _wasPDInAllIterations, g2o::PropertyMap::makeProperty(), STEP_UNDEFINED, and g2o::Property< T >::value().

41  :
43  {
44  _userDeltaInit = _properties.makeProperty<Property<double> >("initialDelta", 1e4);
45  _maxTrialsAfterFailure = _properties.makeProperty<Property<int> >("maxTrialsAfterFailure", 100);
46  _initialLambda = _properties.makeProperty<Property<double> >("initialLambda", 1e-7);
47  _lamdbaFactor = _properties.makeProperty<Property<double> >("lambdaFactor", 10.);
50  _wasPDInAllIterations = true;
51  }
bool _wasPDInAllIterations
the matrix we solve was positive definite in all iterations -> if not apply damping ...
Solver * solver()
return the underlying solver used to solve the linear system
PropertyMap _properties
the properties of your solver, use this to store the parameters of your solver
P * makeProperty(const std::string &name_, const typename P::ValueType &v)
Definition: property.h:119
const T & value() const
Definition: property.h:57
int _lastStep
type of the step taken by the algorithm
g2o::OptimizationAlgorithmDogleg::~OptimizationAlgorithmDogleg ( )
virtual

Definition at line 53 of file optimization_algorithm_dogleg.cpp.

54  {
55  }

Member Function Documentation

int g2o::OptimizationAlgorithmDogleg::lastStep ( ) const
inline

return the type of the last step taken by the algorithm

Definition at line 62 of file optimization_algorithm_dogleg.h.

62 { return _lastStep;}
int _lastStep
type of the step taken by the algorithm
void g2o::OptimizationAlgorithmDogleg::printVerbose ( std::ostream &  os) const
virtual

called by the optimizer if verbose. re-implement, if you want to print something

Reimplemented from g2o::OptimizationAlgorithm.

Definition at line 209 of file optimization_algorithm_dogleg.cpp.

References _currentLambda, _delta, _lastNumTries, _lastStep, _wasPDInAllIterations, and stepType2Str().

210  {
211  os
212  << "\t Delta= " << _delta
213  << "\t step= " << stepType2Str(_lastStep)
214  << "\t tries= " << _lastNumTries;
215  if (! _wasPDInAllIterations)
216  os << "\t lambda= " << _currentLambda;
217  }
static const char * stepType2Str(int stepType)
convert the type into an integer
bool _wasPDInAllIterations
the matrix we solve was positive definite in all iterations -> if not apply damping ...
double _currentLambda
the damping factor to force positive definite matrix
int _lastStep
type of the step taken by the algorithm
OptimizationAlgorithm::SolverResult g2o::OptimizationAlgorithmDogleg::solve ( int  iteration,
bool  online = false 
)
virtual

Solve one iteration. The SparseOptimizer running on-top will call this for the given number of iterations.

Parameters
iterationindicates the current iteration

Implements g2o::OptimizationAlgorithm.

Definition at line 57 of file optimization_algorithm_dogleg.cpp.

References __PRETTY_FUNCTION__, _auxVector, _currentLambda, _delta, _hdl, _hsd, _initialLambda, _lamdbaFactor, _lastNumTries, _lastStep, _maxTrialsAfterFailure, g2o::OptimizationAlgorithm::_optimizer, g2o::OptimizationAlgorithmWithHessian::_solver, _userDeltaInit, _wasPDInAllIterations, g2o::SparseOptimizer::activeRobustChi2(), g2o::Solver::b(), g2o::Solver::buildStructure(), g2o::Solver::buildSystem(), g2o::SparseOptimizer::computeActiveErrors(), g2o::SparseOptimizer::discardTop(), g2o::get_monotonic_time(), g2o::G2OBatchStatistics::globalStats(), g2o::BlockSolverBase::multiplyHessian(), OK, g2o::Solver::optimizer(), g2o::SparseOptimizer::pop(), g2o::SparseOptimizer::push(), g2o::Solver::restoreDiagonal(), g2o::Solver::setLambda(), g2o::Solver::solve(), STEP_DL, STEP_GN, STEP_SD, Terminate, g2o::G2OBatchStatistics::timeQuadraticForm, g2o::G2OBatchStatistics::timeResiduals, g2o::Property< T >::value(), g2o::Solver::vectorSize(), and g2o::Solver::x().

58  {
59  assert(_optimizer && "_optimizer not set");
60  assert(_solver->optimizer() == _optimizer && "underlying linear solver operates on different graph");
61  assert(dynamic_cast<BlockSolverBase*>(_solver) && "underlying linear solver is not a block solver");
62 
63  BlockSolverBase* blockSolver = static_cast<BlockSolverBase*>(_solver);
64 
65  if (iteration == 0 && !online) { // built up the CCS structure, here due to easy time measure
66  bool ok = _solver->buildStructure();
67  if (! ok) {
68  cerr << __PRETTY_FUNCTION__ << ": Failure while building CCS structure" << endl;
69  return OptimizationAlgorithm::Fail;
70  }
71 
72  // init some members to the current size of the problem
73  _hsd.resize(_solver->vectorSize());
74  _hdl.resize(_solver->vectorSize());
75  _auxVector.resize(_solver->vectorSize());
78  _wasPDInAllIterations = true;
79  }
80 
81  double t=get_monotonic_time();
83  G2OBatchStatistics* globalStats = G2OBatchStatistics::globalStats();
84  if (globalStats) {
85  globalStats->timeResiduals = get_monotonic_time()-t;
87  }
88 
89  double currentChi = _optimizer->activeRobustChi2();
90 
92  if (globalStats) {
93  globalStats->timeQuadraticForm = get_monotonic_time()-t;
94  }
95 
96  VectorXD::ConstMapType b(_solver->b(), _solver->vectorSize());
97 
98  // compute alpha
99  _auxVector.setZero();
100  blockSolver->multiplyHessian(_auxVector.data(), _solver->b());
101  double bNormSquared = b.squaredNorm();
102  double alpha = bNormSquared / _auxVector.dot(b);
103 
104  _hsd = alpha * b;
105  double hsdNorm = _hsd.norm();
106  double hgnNorm = -1.;
107 
108  bool solvedGaussNewton = false;
109  bool goodStep = false;
110  int& numTries = _lastNumTries;
111  numTries = 0;
112  do {
113  ++numTries;
114 
115  if (! solvedGaussNewton) {
116  const double minLambda = 1e-12;
117  const double maxLambda = 1e3;
118  solvedGaussNewton = true;
119  // apply a damping factor to enforce positive definite Hessian, if the matrix appeared
120  // to be not positive definite in at least one iteration before.
121  // We apply a damping factor to obtain a PD matrix.
122  bool solverOk = false;
123  while(!solverOk) {
124  if (! _wasPDInAllIterations)
125  _solver->setLambda(_currentLambda, true); // add _currentLambda to the diagonal
126  solverOk = _solver->solve();
127  if (! _wasPDInAllIterations)
130  if (! _wasPDInAllIterations) {
131  // simple strategy to control the damping factor
132  if (solverOk) {
133  _currentLambda = std::max(minLambda, _currentLambda / (0.5 * _lamdbaFactor->value()));
134  } else {
136  if (_currentLambda > maxLambda) {
137  _currentLambda = maxLambda;
138  return Fail;
139  }
140  }
141  }
142  }
143  if (!solverOk) {
144  return Fail;
145  }
146  hgnNorm = VectorXD::ConstMapType(_solver->x(), _solver->vectorSize()).norm();
147  }
148 
149  VectorXD::ConstMapType hgn(_solver->x(), _solver->vectorSize());
150  assert(hgnNorm >= 0. && "Norm of the GN step is not computed");
151 
152  if (hgnNorm < _delta) {
153  _hdl = hgn;
154  _lastStep = STEP_GN;
155  }
156  else if (hsdNorm > _delta) {
157  _hdl = _delta / hsdNorm * _hsd;
158  _lastStep = STEP_SD;
159  } else {
160  _auxVector = hgn - _hsd; // b - a
161  double c = _hsd.dot(_auxVector);
162  double bmaSquaredNorm = _auxVector.squaredNorm();
163  double beta;
164  if (c <= 0.)
165  beta = (-c + sqrt(c*c + bmaSquaredNorm * (_delta*_delta - _hsd.squaredNorm()))) / bmaSquaredNorm;
166  else {
167  double hsdSqrNorm = _hsd.squaredNorm();
168  beta = (_delta*_delta - hsdSqrNorm) / (c + sqrt(c*c + bmaSquaredNorm * (_delta*_delta - hsdSqrNorm)));
169  }
170  assert(beta > 0. && beta < 1 && "Error while computing beta");
171  _hdl = _hsd + beta * (hgn - _hsd);
172  _lastStep = STEP_DL;
173  assert(_hdl.norm() < _delta + 1e-5 && "Computed step does not correspond to the trust region");
174  }
175 
176  // compute the linear gain
177  _auxVector.setZero();
178  blockSolver->multiplyHessian(_auxVector.data(), _hdl.data());
179  double linearGain = -1 * (_auxVector.dot(_hdl)) + 2 * (b.dot(_hdl));
180 
181  // apply the update and see what happens
182  _optimizer->push();
183  _optimizer->update(_hdl.data());
185  double newChi = _optimizer-> activeRobustChi2();
186  double nonLinearGain = currentChi - newChi;
187  if (fabs(linearGain) < 1e-12)
188  linearGain = 1e-12;
189  double rho = nonLinearGain / linearGain;
190  //cerr << PVAR(nonLinearGain) << " " << PVAR(linearGain) << " " << PVAR(rho) << endl;
191  if (rho > 0) { // step is good and will be accepted
193  goodStep = true;
194  } else { // recover previous state
195  _optimizer->pop();
196  }
197 
198  // update trust region based on the step quality
199  if (rho > 0.75)
200  _delta = std::max(_delta, 3. * _hdl.norm());
201  else if (rho < 0.25)
202  _delta *= 0.5;
203  } while (!goodStep && numTries < _maxTrialsAfterFailure->value());
204  if (numTries == _maxTrialsAfterFailure->value() || !goodStep)
205  return Terminate;
206  return OK;
207  }
double get_monotonic_time()
Definition: timeutil.cpp:113
virtual void restoreDiagonal()=0
#define __PRETTY_FUNCTION__
Definition: macros.h:89
VectorXD _auxVector
auxilary vector used to perform multiplications or other stuff
bool _wasPDInAllIterations
the matrix we solve was positive definite in all iterations -> if not apply damping ...
void pop(SparseOptimizer::VertexContainer &vlist)
pop (restore) the estimate a subset of the variables from the stack
virtual bool setLambda(double lambda, bool backup=false)=0
void discardTop(SparseOptimizer::VertexContainer &vlist)
ignore the latest stored element on the stack, remove it from the stack but do not restore the estima...
double _currentLambda
the damping factor to force positive definite matrix
SparseOptimizer * _optimizer
the optimizer the solver is working on
static G2OBatchStatistics * globalStats()
Definition: batch_stats.h:73
SparseOptimizer * optimizer() const
the optimizer (graph) on which the solver works
Definition: solver.h:106
virtual bool solve()=0
double * x()
return x, the solution vector
Definition: solver.h:96
double * b()
return b, the right hand side of the system
Definition: solver.h:99
virtual bool buildSystem()=0
size_t vectorSize() const
return the size of the solution vector (x) and b
Definition: solver.h:103
double activeRobustChi2() const
const T & value() const
Definition: property.h:57
int _lastStep
type of the step taken by the algorithm
void push(SparseOptimizer::VertexContainer &vlist)
push the estimate of a subset of the variables onto a stack
virtual bool buildStructure(bool zeroBlocks=false)=0
const char * g2o::OptimizationAlgorithmDogleg::stepType2Str ( int  stepType)
static

convert the type into an integer

Definition at line 219 of file optimization_algorithm_dogleg.cpp.

References STEP_DL, STEP_GN, and STEP_SD.

Referenced by printVerbose().

220  {
221  switch (stepType) {
222  case STEP_SD: return "Descent";
223  case STEP_GN: return "GN";
224  case STEP_DL: return "Dogleg";
225  default: return "Undefined";
226  }
227  }
double g2o::OptimizationAlgorithmDogleg::trustRegion ( ) const
inline

return the diameter of the trust region

Definition at line 64 of file optimization_algorithm_dogleg.h.

64 { return _delta;}

Member Data Documentation

VectorXD g2o::OptimizationAlgorithmDogleg::_auxVector
protected

auxilary vector used to perform multiplications or other stuff

Definition at line 79 of file optimization_algorithm_dogleg.h.

Referenced by solve().

double g2o::OptimizationAlgorithmDogleg::_currentLambda
protected

the damping factor to force positive definite matrix

Definition at line 81 of file optimization_algorithm_dogleg.h.

Referenced by printVerbose(), and solve().

double g2o::OptimizationAlgorithmDogleg::_delta
protected

trust region

Definition at line 82 of file optimization_algorithm_dogleg.h.

Referenced by OptimizationAlgorithmDogleg(), printVerbose(), and solve().

VectorXD g2o::OptimizationAlgorithmDogleg::_hdl
protected

final dogleg step

Definition at line 78 of file optimization_algorithm_dogleg.h.

Referenced by solve().

VectorXD g2o::OptimizationAlgorithmDogleg::_hsd
protected

steepest decent step

Definition at line 77 of file optimization_algorithm_dogleg.h.

Referenced by solve().

Property<double>* g2o::OptimizationAlgorithmDogleg::_initialLambda
protected

Definition at line 74 of file optimization_algorithm_dogleg.h.

Referenced by OptimizationAlgorithmDogleg(), and solve().

Property<double>* g2o::OptimizationAlgorithmDogleg::_lamdbaFactor
protected

Definition at line 75 of file optimization_algorithm_dogleg.h.

Referenced by OptimizationAlgorithmDogleg(), and solve().

int g2o::OptimizationAlgorithmDogleg::_lastNumTries
protected

Definition at line 85 of file optimization_algorithm_dogleg.h.

Referenced by printVerbose(), and solve().

int g2o::OptimizationAlgorithmDogleg::_lastStep
protected

type of the step taken by the algorithm

Definition at line 83 of file optimization_algorithm_dogleg.h.

Referenced by OptimizationAlgorithmDogleg(), printVerbose(), and solve().

Property<int>* g2o::OptimizationAlgorithmDogleg::_maxTrialsAfterFailure
protected

Definition at line 71 of file optimization_algorithm_dogleg.h.

Referenced by OptimizationAlgorithmDogleg(), and solve().

Property<double>* g2o::OptimizationAlgorithmDogleg::_userDeltaInit
protected

Definition at line 72 of file optimization_algorithm_dogleg.h.

Referenced by OptimizationAlgorithmDogleg(), and solve().

bool g2o::OptimizationAlgorithmDogleg::_wasPDInAllIterations
protected

the matrix we solve was positive definite in all iterations -> if not apply damping

Definition at line 84 of file optimization_algorithm_dogleg.h.

Referenced by OptimizationAlgorithmDogleg(), printVerbose(), and solve().


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