g2o
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
g2o::SparseOptimizerTerminateAction Class Reference

stop iterating based on the gain which is (oldChi - currentChi) / currentChi. More...

#include <sparse_optimizer_terminate_action.h>

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

Public Member Functions

 SparseOptimizerTerminateAction ()
 
virtual HyperGraphActionoperator() (const HyperGraph *graph, Parameters *parameters=0)
 
double gainThreshold () const
 
void setGainThreshold (double gainThreshold)
 
int maxIterations () const
 
void setMaxIterations (int maxit)
 
- Public Member Functions inherited from g2o::HyperGraphAction
virtual ~HyperGraphAction ()
 

Protected Member Functions

void setOptimizerStopFlag (const SparseOptimizer *optimizer, bool stop)
 

Protected Attributes

double _gainThreshold
 
double _lastChi
 
bool _auxTerminateFlag
 
int _maxIterations
 

Detailed Description

stop iterating based on the gain which is (oldChi - currentChi) / currentChi.

stop iterating based on the gain which is (oldChi - currentChi) / currentChi. If the gain is larger than zero and below the threshold, then the optimizer is stopped. Typically usage of this action includes adding it as a postIteration action, by calling addPostIterationAction on a sparse optimizer.

Definition at line 19 of file sparse_optimizer_terminate_action.h.

Constructor & Destructor Documentation

g2o::SparseOptimizerTerminateAction::SparseOptimizerTerminateAction ( )

Member Function Documentation

double g2o::SparseOptimizerTerminateAction::gainThreshold ( ) const
inline
int g2o::SparseOptimizerTerminateAction::maxIterations ( ) const
inline
HyperGraphAction * g2o::SparseOptimizerTerminateAction::operator() ( const HyperGraph graph,
Parameters parameters = 0 
)
virtual

re-implement to carry out an action given the graph

Reimplemented from g2o::HyperGraphAction.

Definition at line 21 of file sparse_optimizer_terminate_action.cpp.

References _gainThreshold, _lastChi, _maxIterations, g2o::SparseOptimizer::activeRobustChi2(), graph, g2o::HyperGraphAction::ParametersIteration::iteration, and setOptimizerStopFlag().

22  {
23  assert(dynamic_cast<const SparseOptimizer*>(graph) && "graph is not a SparseOptimizer");
24  assert(dynamic_cast<HyperGraphAction::ParametersIteration*>(parameters) && "error casting parameters");
25 
26  const SparseOptimizer* optimizer = static_cast<const SparseOptimizer*>(graph);
27  HyperGraphAction::ParametersIteration* params = static_cast<HyperGraphAction::ParametersIteration*>(parameters);
28 
29  const_cast<SparseOptimizer*>(optimizer)->computeActiveErrors();
30  if (params->iteration < 0)
31  {
32  // let the optimizer run for at least one iteration
33  // Hence, we reset the stop flag
34  setOptimizerStopFlag(optimizer, false);
35  } else if (params->iteration == 0) {
36  // first iteration, just store the chi2 value
37  _lastChi = optimizer->activeRobustChi2();
38  } else {
39  // compute the gain and stop the optimizer in case the
40  // gain is below the threshold or we reached the max
41  // number of iterations
42  bool stopOptimizer = false;
43  if (params->iteration < _maxIterations) {
44  double currentChi = optimizer->activeRobustChi2();
45  double gain = (_lastChi - currentChi) / currentChi;
46  _lastChi = currentChi;
47  if (gain >= 0 && gain < _gainThreshold)
48  stopOptimizer = true;
49  } else {
50  stopOptimizer = true;
51  }
52  if (stopOptimizer) { // tell the optimizer to stop
53  setOptimizerStopFlag(optimizer, true);
54  }
55  }
56  return this;
57  }
void setOptimizerStopFlag(const SparseOptimizer *optimizer, bool stop)
class G2O_CORE_API SparseOptimizer
Protocol The SLAM executable accepts such as solving the and retrieving or vertices in the graph
Definition: protocol.txt:7
void g2o::SparseOptimizerTerminateAction::setGainThreshold ( double  gainThreshold)
void g2o::SparseOptimizerTerminateAction::setMaxIterations ( int  maxit)

Definition at line 59 of file sparse_optimizer_terminate_action.cpp.

References _maxIterations.

Referenced by main().

void g2o::SparseOptimizerTerminateAction::setOptimizerStopFlag ( const SparseOptimizer optimizer,
bool  stop 
)
protected

Definition at line 64 of file sparse_optimizer_terminate_action.cpp.

References _auxTerminateFlag, and g2o::SparseOptimizer::forceStopFlag().

Referenced by operator()().

65  {
66  if (optimizer->forceStopFlag()) {
67  *(optimizer->forceStopFlag()) = stop;
68  } else {
69  _auxTerminateFlag = stop;
70  const_cast<SparseOptimizer*>(optimizer)->setForceStopFlag(&_auxTerminateFlag);
71  }
72  }
class G2O_CORE_API SparseOptimizer

Member Data Documentation

bool g2o::SparseOptimizerTerminateAction::_auxTerminateFlag
protected

Definition at line 34 of file sparse_optimizer_terminate_action.h.

Referenced by setOptimizerStopFlag().

double g2o::SparseOptimizerTerminateAction::_gainThreshold
protected

Definition at line 32 of file sparse_optimizer_terminate_action.h.

Referenced by operator()(), and setGainThreshold().

double g2o::SparseOptimizerTerminateAction::_lastChi
protected

Definition at line 33 of file sparse_optimizer_terminate_action.h.

Referenced by operator()().

int g2o::SparseOptimizerTerminateAction::_maxIterations
protected

Definition at line 35 of file sparse_optimizer_terminate_action.h.

Referenced by operator()(), and setMaxIterations().


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