Main MRPT website > C++ reference for MRPT 1.9.9
CLevMarqGSO.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #ifndef CLEVMARQGSO_H
11 #define CLEVMARQGSO_H
12 
14 #include <mrpt/obs/CRawlog.h>
15 #include <mrpt/obs/CSensoryFrame.h>
17 #include <mrpt/utils/CConfigFile.h>
19 #include <mrpt/utils/CStream.h>
20 #include <mrpt/utils/CTicTac.h>
22 #include <mrpt/utils/TColor.h>
24 #include <mrpt/opengl/CDisk.h>
25 #include <mrpt/opengl/CSphere.h>
27 #include <mrpt/utils/TColor.h>
28 #include <mrpt/poses/CPose2D.h>
29 #include <mrpt/poses/CPose3D.h>
30 
31 #include <mrpt/graphslam/levmarq.h>
33 
34 #include <iostream>
35 #include <string>
36 #include <map>
37 #include <cmath> // fabs function
38 
39 namespace mrpt
40 {
41 namespace graphslam
42 {
43 namespace optimizers
44 {
45 /**\brief Levenberg-Marquardt non-linear graph slam optimization scheme.
46  *
47  * ## Description
48  *
49  * Current decider optimizes the graph according to the
50  * graphslam::optimize_spa_levmarq method. Refer to the latter for more
51  * details on the implementation.
52  *
53  * ### .ini Configuration Parameters
54  *
55  * \htmlinclude graphslam-engine_config_params_preamble.txt
56  *
57  * - \b class_verbosity
58  * + \a Section : OptimizerParameters
59  * + \a Default value : 1 (LVL_INFO)
60  * + \a Required : FALSE
61  *
62  * - \b optimization_on_second_thread
63  * + \a Section : OptimizerParameters
64  * + \a Default value : FALSE
65  * + \a Required : FALSE
66  * + \a Description : Specify whether to use a second thread to optimize
67  * the graph.
68  *
69  * - \b LC_min_nodeid_diff
70  * + \a Section : GeneralConfiguration
71  * + \a Default value : 30
72  * + \a Required : FALSE
73  * + \a Description : Minimum NodeID difference for an edge to be considered
74  * a loop closure.
75  *
76  * - \b optimization_distance
77  * + \a Section : OptimizerParameters
78  * + \a Default value : 5
79  * + \a Required : FALSE
80  * + \a Description : Positions of the nodes within the specified distance
81  from the current
82  * graph node are optimized according to the corresponding constraints between
83  * them
84  *
85  * - \b verbose
86  * + \a Section : OptimizerParameters
87  * + \a Default value : FALSE
88  * + \a Required : FALSE
89  * + \a Description : Refers to the Levenberg-Marquardt optimization.
90  * procedure
91  *
92  * - \b profiler
93  * + \a Section : OptimizerParameters
94  * + \a Default value : FALSE
95  * + \a Required : FALSE
96  * + \a Description : Refers to the Levenberg-Marquardt optimization.
97  * procedure
98  *
99  * - \b max_iterations
100  * + \a Section : OptimizerParameters
101  * + \a Default value : 100
102  * + \a Required : FALSE
103  * + \a Description : Refers to the Levenberg-Marquardt optimization. Sets
104  * the maximum number of iterations of the optimization scheme.
105  *
106  * - \b scale_hessian
107  * + \a Section : OptimizerParameters
108  * + \a Default value : 0.2
109  * + \a Required : FALSE
110  * + \a Description : Refers to the Levenberg-Marquardt optimization.
111  *
112  * - \b tau
113  * + \a Section : OptimizerParameters
114  * + \a Default value : 1e-3
115  * + \a Required : FALSE
116  * + \a Description : Refers to the Levenberg-Marquardt optimization.
117  *
118  * \note For a detailed description of the optimization parameters of the
119  * Levenberg-Marquardt scheme, refer to
120  *
121  http://reference.mrpt.org/devel/group__mrpt__graphslam__grp.html#ga022f4a70be5ec7c432f46374e4bb9d66
122  *
123  * \note For a detailed description of the graph visualization parameters
124  * refer to
125  *
126  http://reference.mrpt.org/devel/group__mrpt__opengl__grp.html#ga30efc9f6fcb49801e989d174e0f65a61
127 
128  *
129  * \ingroup mrpt_graphslam_grp
130  */
131 template <class GRAPH_T = typename mrpt::graphs::CNetworkOfPoses2DInf>
134 {
135  public:
136  // Public methods
137  //////////////////////////////////////////////////////////////
138  /**\brief Handy typedefs */
139  /**\{*/
140  typedef typename GRAPH_T::constraint_t constraint_t;
141  typedef typename GRAPH_T::constraint_t::type_value
142  pose_t; // type of underlying poses (2D/3D)
143  typedef mrpt::math::CMatrixFixedNumeric<double, constraint_t::state_length,
144  constraint_t::state_length>
148  /**\}*/
149 
150  CLevMarqGSO();
151  ~CLevMarqGSO();
152 
153  bool updateState(
155  mrpt::obs::CSensoryFrame::Ptr observations,
156  mrpt::obs::CObservation::Ptr observation);
157 
158  void initializeVisuals();
159  void updateVisuals();
160  /**\brief Get a list of the window events that happened since the last
161  * call.
162  */
164  const std::map<std::string, bool>& events_occurred);
165  /**\brief Struct for holding the optimization-related variables in a
166  * compact form
167  */
169  {
170  public:
173 
174  void loadFromConfigFile(
176  const std::string& section);
177  void dumpToTextStream(mrpt::utils::CStream& out) const;
178 
180  // True if optimization procedure is to run in a multithreading fashion
182 
183  /**\brief optimize only for the nodes found in a certain distance from
184  * the current position. Optimize for the entire graph if set to -1
185  */
190  /**\brief Keystroke to toggle the optimization distance on/off */
192  /**\brief Keystroke to manually trigger a full graph optimization */
194 
195  // nodeID difference for an edge to be considered loop closure
197 
198  // Map of TPairNodesID to their corresponding edge as recorded in the
199  // last update of the optimizer state
200  typename GRAPH_T::edges_map_t last_pair_nodes_to_edge;
201  };
202 
203  /**\brief struct for holding the graph visualization-related variables in a
204  * compact form
205  */
207  {
208  public:
211 
212  void loadFromConfigFile(
214  const std::string& section);
215  void dumpToTextStream(mrpt::utils::CStream& out) const;
216 
219  // textMessage parameters
220  std::string keystroke_graph_toggle; // see Ctor for initialization
221  std::string keystroke_graph_autofit; // see Ctor for initialization
224  };
225 
226  void loadParams(const std::string& source_fname);
227  void printParams() const;
228  void getDescriptiveReport(std::string* report_str) const;
229 
230  bool justFullyOptimizedGraph() const;
231 
232  // Public members
233  // ////////////////////////////
234  /** Parameters relevant to the optimizatio nfo the graph. */
236  /** Parameters relevant to the visualization of the graph. */
238 
239  protected:
240  // protected methods
241  // ////////////////////////////
242 
243  /**\brief Feedback of the Levenberg-Marquardt graph optimization procedure.
244  *
245  */
246  static void levMarqFeedback(
247  const GRAPH_T& graph, const size_t iter, const size_t max_iter,
248  const double cur_sq_error);
249 
250  /**\brief Optimize the given graph.
251  *
252  * Wrapper around the graphslam::optimize_spa_levmarq method
253  * \sa optimize_spa_levmarq, optimizeGraph
254  *
255  * \param[in] full_update Impose that method optimizes the whole graph
256  *
257  */
258  void _optimizeGraph(bool is_full_update = false);
259  /** \brief Wrapper around _optimizeGraph which first locks the section and
260  * then calls the _optimizeGraph method.
261  *
262  * Used in multithreaded optimization
263  * \sa _optimizeGraph()
264  */
265  void optimizeGraph();
266  /**\brief Check if a loop closure edge was added in the graph.
267  *
268  * Match the previously registered edges in the graph with the current. If
269  * there is a node difference *in any new edge* greater than
270  * \b LC_min_nodeid_diff (see .ini parameter) then new constraint is
271  * considered a Loop Closure
272  *
273  * \return True if \b any of the newly added edges is considered a loop
274  * closure
275  */
276  bool checkForLoopClosures();
277  /**\brief Decide whether to issue a full graph optimization
278  *
279  * In case N consecutive full optimizations have been issued, skip some of
280  * the next as they slow down the overall execution and they don't reduce
281  * the overall error
282  *
283  * \return True for issuing a full graph optimization, False otherwise
284  */
286  /**\brief Initialize objects relateed to the Graph Visualization
287  */
288  void initGraphVisualization();
289  /**\brief Called internally for updating the visualization scene for the
290  * graph
291  * building procedure
292  */
293  inline void updateGraphVisualization();
294  /**\brief Toggle the graph visualization on and off.
295  */
297  /**\brief Set the camera parameters of the CDisplayWindow3D so that the
298  * whole
299  * graph is viewed in the window.
300  *
301  * \warning Method assumes that the COpenGLinstance *is not locked* prior to
302  * this
303  * call, so make sure you have issued
304  * CDisplayWindow3D::unlockAccess3DScene() before calling this method.
305  */
306  inline void fitGraphInView();
307 
308  /**\brief Initialize the Disk/Sphere used for visualizing the optimization
309  * distance.
310  */
311  /**\{*/
313  /**\brief Setup the corresponding Disk/Sphere instance.
314  *
315  * Method overloads are used to overcome the C++ specialization
316  * restrictions
317  *
318  * \return Disk/Sphere instance for 2D/3D SLAM respectively
319  */
320  /**\{*/
322  const mrpt::poses::CPose2D& p_unused);
324  const mrpt::poses::CPose3D& p_unused);
325  /**\}*/
326 
327  /**\}*/
328 
329  /**\brief Update the position of the disk indicating the distance in which
330  * Levenberg-Marquardt graph optimization is executed
331  */
332  inline void updateOptDistanceVisualization();
333  /**\brief toggle the optimization distance object on and off
334  */
336  /**\brief Get a list of the nodeIDs whose position is within a certain
337  * distance to the specified nodeID
338  */
339  void getNearbyNodesOf(
340  std::set<mrpt::utils::TNodeID>* nodes_set,
341  const mrpt::utils::TNodeID& cur_nodeID, double distance);
342 
343  // protected members
344  //////////////////////////////////////////////////////////////
345 
350 
351  // start optimizing the graph after a certain number of nodes has been
352  // added (when m_graph->nodeCount() > m_last_total_num_of_nodes)
354 
355  // Use second thread for graph optimization
356  std::thread m_thread_optimize;
357 
358  /**\brief Enumeration that defines the behaviors towards using or ignoring a
359  * newly added loop closure to fully optimize the graph
360  */
362  {
366  };
367  /**\brief Should I fully optimize the graph on loop closure?
368  */
370  /**\name Smart Full-Optimization Command
371  *
372  * Instead of issuing a full optimization every time a loop closure is
373  * detected, ignore current loop closure when enough consecutive loop
374  * closures have already been utilised.
375  * This avoids the added computational cost that is needed for optimizing
376  * the graph without reducing the accuracy of the overall operation
377  */
378  /**\{*/
379 
380  /**\brief Number of maximum cosecutive loop closures that are allowed to be
381  * issued.
382  *
383  * \sa m_curr_used_consec_lcs, m_max_ignored_consec_lcs
384  */
386  /**\brief Number of consecutive loop closures that are currently registered
387  *
388  * \sa m_max_used_consec_lcs
389  */
391  /**\brief Number of consecutive loop closures to ignore after \b
392  * m_max_used_consec_lcs have already been issued.
393  *
394  * \sa m_curr_ignored_consec_lcs, m_max_used_consec_lcs
395  */
397  /**\brief Consecutive Loop Closures that have currently been ignored
398  *
399  * \sa m_max_ignored_consec_lcs
400  */
402 
403  /**\}*/
404 
405  /**\brief Indicates whether a full graph optimization was just issued.
406  */
408 
409  /**\brief Minimum number of nodes before we try optimizing the graph */
411 };
412 }
413 }
414 } // end of namespaces
415 
416 #include "CLevMarqGSO_impl.h"
417 
418 #endif /* end of include guard: CLEVMARQGSO_H */
void initGraphVisualization()
Initialize objects relateed to the Graph Visualization.
static void levMarqFeedback(const GRAPH_T &graph, const size_t iter, const size_t max_iter, const double cur_sq_error)
Feedback of the Levenberg-Marquardt graph optimization procedure.
bool checkForFullOptimization()
Decide whether to issue a full graph optimization.
void loadFromConfigFile(const mrpt::utils::CConfigFileBase &source, const std::string &section)
This method load the options from a ".ini"-like file or memory-stored string list.
mrpt::opengl::CRenderizable::Ptr initOptDistanceVisualizationInternal(const mrpt::poses::CPose2D &p_unused)
Setup the corresponding Disk/Sphere instance.
void updateGraphVisualization()
Called internally for updating the visualization scene for the graph building procedure.
void toggleGraphVisualization()
Toggle the graph visualization on and off.
Struct for holding the optimization-related variables in a compact form.
Definition: CLevMarqGSO.h:168
FullOptimizationPolicy
Enumeration that defines the behaviors towards using or ignoring a newly added loop closure to fully ...
Definition: CLevMarqGSO.h:361
void optimizeGraph()
Wrapper around _optimizeGraph which first locks the section and then calls the _optimizeGraph method...
bool m_just_fully_optimized_graph
Indicates whether a full graph optimization was just issued.
Definition: CLevMarqGSO.h:407
std::shared_ptr< CRenderizable > Ptr
Definition: CRenderizable.h:45
bool justFullyOptimizedGraph() const
Used by the caller to query for possible full graph optimization on the latest optimizer run...
mrpt::graphslam::CRegistrationDeciderOrOptimizer< GRAPH_T > grandpa
Definition: CLevMarqGSO.h:146
std::string keystroke_optimize_graph
Keystroke to manually trigger a full graph optimization.
Definition: CLevMarqGSO.h:193
double optimization_distance
optimize only for the nodes found in a certain distance from the current position.
Definition: CLevMarqGSO.h:186
void getNearbyNodesOf(std::set< mrpt::utils::TNodeID > *nodes_set, const mrpt::utils::TNodeID &cur_nodeID, double distance)
Get a list of the nodeIDs whose position is within a certain distance to the specified nodeID...
void printParams() const
Print the problem parameters - relevant to the decider/optimizer to the screen in a unified/compact w...
void toggleOptDistanceVisualization()
toggle the optimization distance object on and off
This class allows loading and storing values and vectors of different types from a configuration text...
OptimizationParams opt_params
Parameters relevant to the optimizatio nfo the graph.
Definition: CLevMarqGSO.h:235
void dumpToTextStream(mrpt::utils::CStream &out) const
This method should clearly display all the contents of the structure in textual form, sending it to a CStream.
void initOptDistanceVisualization()
Initialize the Disk/Sphere used for visualizing the optimization distance.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
A numeric matrix of compile-time fixed size.
uint64_t TNodeID
The type for node IDs in graphs of different types.
mrpt::graphslam::optimizers::CGraphSlamOptimizer< GRAPH_T > parent
Definition: CLevMarqGSO.h:147
void initializeVisuals()
Initialize visual objects in CDisplayWindow (e.g.
void loadParams(const std::string &source_fname)
Load the necessary for the decider/optimizer configuration parameters.
Interface for implementing node/edge registration deciders or optimizer classes.
GRAPH_T::constraint_t constraint_t
Handy typedefs.
Definition: CLevMarqGSO.h:140
FullOptimizationPolicy m_optimization_policy
Should I fully optimize the graph on loop closure?
Definition: CLevMarqGSO.h:369
size_t m_min_nodes_for_optimization
Minimum number of nodes before we try optimizing the graph.
Definition: CLevMarqGSO.h:410
A RGB color - 8bit.
Definition: TColor.h:25
std::shared_ptr< CSensoryFrame > Ptr
Definition: CSensoryFrame.h:56
void notifyOfWindowEvents(const std::map< std::string, bool > &events_occurred)
Get a list of the window events that happened since the last call.
size_t m_max_ignored_consec_lcs
Number of consecutive loop closures to ignore after m_max_used_consec_lcs have already been issued...
Definition: CLevMarqGSO.h:396
void dumpToTextStream(mrpt::utils::CStream &out) const
This method should clearly display all the contents of the structure in textual form, sending it to a CStream.
std::shared_ptr< CObservation > Ptr
Definition: CObservation.h:43
mrpt::math::CMatrixFixedNumeric< double, constraint_t::state_length, constraint_t::state_length > InfMat
Definition: CLevMarqGSO.h:145
GLsizei const GLchar ** string
Definition: glext.h:4101
GRAPH_T::constraint_t::type_value pose_t
Definition: CLevMarqGSO.h:142
bool checkForLoopClosures()
Check if a loop closure edge was added in the graph.
size_t m_curr_ignored_consec_lcs
Consecutive Loop Closures that have currently been ignored.
Definition: CLevMarqGSO.h:401
std::string keystroke_optimization_distance
Keystroke to toggle the optimization distance on/off.
Definition: CLevMarqGSO.h:191
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Interface for implementing graphSLAM optimizer classes.
std::shared_ptr< CActionCollection > Ptr
void updateOptDistanceVisualization()
Update the position of the disk indicating the distance in which Levenberg-Marquardt graph optimizati...
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle...
Definition: CPose2D.h:40
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
void loadFromConfigFile(const mrpt::utils::CConfigFileBase &source, const std::string &section)
This method load the options from a ".ini"-like file or memory-stored string list.
size_t m_max_used_consec_lcs
Number of maximum cosecutive loop closures that are allowed to be issued.
Definition: CLevMarqGSO.h:385
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
GraphVisualizationParams viz_params
Parameters relevant to the visualization of the graph.
Definition: CLevMarqGSO.h:237
size_t m_curr_used_consec_lcs
Number of consecutive loop closures that are currently registered.
Definition: CLevMarqGSO.h:390
Levenberg-Marquardt non-linear graph slam optimization scheme.
Definition: CLevMarqGSO.h:132
bool updateState(mrpt::obs::CActionCollection::Ptr action, mrpt::obs::CSensoryFrame::Ptr observations, mrpt::obs::CObservation::Ptr observation)
Generic method for fetching the incremental action/observation readings from the calling function...
void fitGraphInView()
Set the camera parameters of the CDisplayWindow3D so that the whole graph is viewed in the window...
void getDescriptiveReport(std::string *report_str) const
Fill the provided string with a detailed report of the decider/optimizer state.
struct for holding the graph visualization-related variables in a compact form
Definition: CLevMarqGSO.h:206
double distance(const TPoint2D &p1, const TPoint2D &p2)
Gets the distance between two points in a 2D space.
Definition: geometry.cpp:1885
This is a virtual base class for sets of options than can be loaded from and/or saved to configuratio...
void updateVisuals()
Update the relevant visual features in CDisplayWindow.
void _optimizeGraph(bool is_full_update=false)
Optimize the given graph.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019