g2o
sparse_optimizer.h
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef G2O_GRAPH_OPTIMIZER_CHOL_H_
28 #define G2O_GRAPH_OPTIMIZER_CHOL_H_
29 
30 #include "g2o/stuff/macros.h"
31 
32 #include "optimizable_graph.h"
33 #include "sparse_block_matrix.h"
34 #include "g2o_core_api.h"
35 #include "batch_stats.h"
36 
37 #include <map>
38 
39 namespace g2o {
40 
41  // forward declaration
42  class ActivePathCostFunction;
44  class EstimatePropagatorCost;
45 
47 
48  public:
49  enum {
50  AT_COMPUTEACTIVERROR = OptimizableGraph::AT_NUM_ELEMENTS,
51  AT_NUM_ELEMENTS, // keep as last element
52  };
53 
54  friend class ActivePathCostFunction;
55 
56  // Attention: _solver & _statistics is own by SparseOptimizer and will be
57  // deleted in its destructor.
59  virtual ~SparseOptimizer();
60 
61  // new interface for the optimizer
62  // the old functions will be dropped
70  virtual bool initializeOptimization(HyperGraph::EdgeSet& eset);
71 
80  virtual bool initializeOptimization(HyperGraph::VertexSet& vset, int level=0);
81 
89  virtual bool initializeOptimization(int level=0);
90 
94  virtual bool updateInitialization(HyperGraph::VertexSet& vset, HyperGraph::EdgeSet& eset);
95 
104  virtual void computeInitialGuess();
105 
109  virtual void computeInitialGuess(EstimatePropagatorCost& propagator);
110 
114  virtual void setToOrigin();
115 
116 
122  int optimize(int iterations, bool online = false);
123 
131  bool computeMarginals(SparseBlockMatrix<MatrixXD>& spinv, const std::vector<std::pair<int, int> >& blockIndices);
132 
140  if (vertex->hessianIndex() < 0) {
141  return false;
142  }
143  std::vector<std::pair<int, int> > index;
144  index.push_back(std::pair<int, int>(vertex->hessianIndex(), vertex->hessianIndex()));
145  return computeMarginals(spinv, index);
146  }
147 
155  std::vector<std::pair<int, int> > indices;
156  for (VertexContainer::const_iterator it = vertices.begin(); it != vertices.end(); ++it) {
157  indices.push_back(std::pair<int, int>((*it)->hessianIndex(),(*it)->hessianIndex()));
158  }
159  return computeMarginals(spinv, indices);
160  }
161 
163  // The gauge should be fixed() and then the optimization can work (if no additional dof are in
164  // the system. The default implementation returns a node with maximum dimension.
165  virtual Vertex* findGauge();
166 
167  bool gaugeFreedom();
168 
170  double activeChi2() const;
176  double activeRobustChi2() const;
177 
179  bool verbose() const {return _verbose;}
180  void setVerbose(bool verbose);
181 
185  void setForceStopFlag(bool* flag);
186  bool* forceStopFlag() const { return _forceStopFlag;};
187 
189  bool terminate() {return _forceStopFlag ? (*_forceStopFlag) : false; }
190 
192  const VertexContainer& indexMapping() const {return _ivMap;}
194  const VertexContainer& activeVertices() const { return _activeVertices;}
196  const EdgeContainer& activeEdges() const { return _activeEdges;}
197 
204  virtual bool removeVertex(HyperGraph::Vertex* v, bool detach=false);
205 
210  VertexContainer::const_iterator findActiveVertex(const OptimizableGraph::Vertex* v) const;
215  EdgeContainer::const_iterator findActiveEdge(const OptimizableGraph::Edge* e) const;
216 
218  const OptimizationAlgorithm* algorithm() const { return _algorithm;}
219  OptimizationAlgorithm* solver() { return _algorithm;}
220  void setAlgorithm(OptimizationAlgorithm* algorithm);
221 
223  void push(SparseOptimizer::VertexContainer& vlist);
225  void push(HyperGraph::VertexSet& vlist);
227  void push();
229  void pop(SparseOptimizer::VertexContainer& vlist);
231  void pop(HyperGraph::VertexSet& vlist);
233  void pop();
234 
236  void discardTop(SparseOptimizer::VertexContainer& vlist);
238  void discardTop();
240 
246  virtual void clear();
247 
251  void computeActiveErrors();
252 
257  G2O_ATTRIBUTE_DEPRECATED(void linearizeSystem())
258  {
259  // nothing needed, linearization is now done inside the solver
260  }
261 
267  void update(const double* update);
268 
272  const BatchStatisticsContainer& batchStatistics() const { return _batchStatistics;}
276  BatchStatisticsContainer& batchStatistics() { return _batchStatistics;}
277 
278  void setComputeBatchStatistics(bool computeBatchStatistics);
279 
280  bool computeBatchStatistics() const { return _computeBatchStatistics;}
281 
282  /**** callbacks ****/
284  bool addComputeErrorAction(HyperGraphAction* action);
286  bool removeComputeErrorAction(HyperGraphAction* action);
287 
288 
289 
290  protected:
292  bool _verbose;
293 
297 
298  void sortVectorContainers();
299 
301 
305  bool buildIndexMapping(SparseOptimizer::VertexContainer& vlist);
306  void clearIndexMapping();
307 
310  };
311 } // end namespace
312 
313 #endif
const BatchStatisticsContainer & batchStatistics() const
VertexContainer _ivMap
bool computeMarginals(SparseBlockMatrix< MatrixXD > &spinv, const VertexContainer &vertices)
bool terminate()
if external stop flag is given, return its state. False otherwise
VertexContainer _activeVertices
sorted according to VertexIDCompare
std::vector< Vertex * > VertexContainer
Definition: hyper_graph.h:139
std::set< Vertex * > VertexSet
Definition: hyper_graph.h:136
OptimizationAlgorithm * solver()
const EdgeContainer & activeEdges() const
the edges active in the current optimization
class G2O_CORE_API OptimizationAlgorithm
std::vector< OptimizableGraph::Edge * > EdgeContainer
vector container for edges
std::set< Edge * > EdgeSet
Definition: hyper_graph.h:135
Protocol The SLAM executable accepts such as solving the and retrieving or vertices in the explicitly state the reprensentation poses are represented by poses by VERTEX_XYZRPY In the Quaternions and other representations could be but note that it is up to the SLAM algorithm to choose the internal representation of the angles The keyword is followed by a unique vertex ID and an optional initialization of the or edges in the explicitly state the type of the constraint pose constraints are given by pose constraints by EDGE_XYZRPY The keyword is followed by a unique edge the IDs of the referenced vertices
Definition: protocol.txt:7
class G2O_CORE_API SparseOptimizer
BatchStatisticsContainer _batchStatistics
global statistics of the optimizer, e.g., timing, num-non-zeros
const VertexContainer & indexMapping() const
the index mapping of the vertices
EdgeContainer _activeEdges
sorted according to EdgeIDCompare
cost for traversing along active edges in the optimizer
BatchStatisticsContainer & batchStatistics()
A general case Vertex for optimization.
abstract Vertex, your types must derive from that one
Definition: hyper_graph.h:142
bool computeBatchStatistics() const
bool * forceStopFlag() const
#define G2O_ATTRIBUTE_DEPRECATED(func)
Definition: macros.h:96
const VertexContainer & activeVertices() const
the vertices active in the current optimization
virtual void discardTop()
discard the last backup of the estimate for all variables by removing it from the stack ...
Generic interface for a non-linear solver operating on a graph.
bool verbose() const
verbose information during optimization
const OptimizationAlgorithm * algorithm() const
the solver used by the optimizer
#define G2O_CORE_API
Definition: g2o_core_api.h:29
std::vector< G2OBatchStatistics > BatchStatisticsContainer
Definition: batch_stats.h:81
Sparse matrix which uses blocks.
OptimizationAlgorithm * _algorithm
bool computeMarginals(SparseBlockMatrix< MatrixXD > &spinv, const Vertex *vertex)
Abstract action that operates on an entire graph.