g2o
sparse_optimizer_terminate_action.cpp
Go to the documentation of this file.
2 
3 #include "sparse_optimizer.h"
4 
5 #include <limits>
6 
7 namespace g2o {
8 
11  _gainThreshold(1e-6), _lastChi(0.), _auxTerminateFlag(false),
12  _maxIterations(std::numeric_limits<int>::max())
13  {
14  }
15 
17  {
19  }
20 
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);
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  }
58 
60  {
61  _maxIterations = maxit;
62  }
63 
65  {
66  if (optimizer->forceStopFlag()) {
67  *(optimizer->forceStopFlag()) = stop;
68  } else {
69  _auxTerminateFlag = stop;
70  const_cast<SparseOptimizer*>(optimizer)->setForceStopFlag(&_auxTerminateFlag);
71  }
72  }
73 
74 } // end namespace
void setOptimizerStopFlag(const SparseOptimizer *optimizer, bool stop)
bool * forceStopFlag() const
double activeRobustChi2() const
virtual HyperGraphAction * operator()(const HyperGraph *graph, Parameters *parameters=0)
Protocol The SLAM executable accepts such as solving the and retrieving or vertices in the graph
Definition: protocol.txt:7
Abstract action that operates on an entire graph.