Main MRPT website > C++ reference
MRPT logo
model_search.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (c) 2005-2013, Individual contributors, see AUTHORS file |
7  | Copyright (c) 2005-2013, MAPIR group, University of Malaga |
8  | Copyright (c) 2012-2013, University of Almeria |
9  | All rights reserved. |
10  | |
11  | Redistribution and use in source and binary forms, with or without |
12  | modification, are permitted provided that the following conditions are |
13  | met: |
14  | * Redistributions of source code must retain the above copyright |
15  | notice, this list of conditions and the following disclaimer. |
16  | * Redistributions in binary form must reproduce the above copyright |
17  | notice, this list of conditions and the following disclaimer in the |
18  | documentation and/or other materials provided with the distribution. |
19  | * Neither the name of the copyright holders nor the |
20  | names of its contributors may be used to endorse or promote products |
21  | derived from this software without specific prior written permission.|
22  | |
23  | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24  | 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
25  | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR|
26  | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE |
27  | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL|
28  | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR|
29  | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
30  | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
31  | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
32  | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
33  | POSSIBILITY OF SUCH DAMAGE. |
34  +---------------------------------------------------------------------------+ */
35 
36 #ifndef math_modelsearch_h
37 #define math_modelsearch_h
38 
39 #include <mrpt/utils/utils_defs.h>
40 
41 namespace mrpt {
42  namespace math {
43 
44 
45  /** Model search implementations: RANSAC and genetic algorithm
46  *
47  * The type \a TModelFit is a user-supplied struct/class that implements this interface:
48  * - Types:
49  * - \a Real : The numeric type to use (typ: double, float)
50  * - \a Model : The type of the model to be fitted (for example: A matrix, a TLine2D, a TPlane3D, ...)
51  * - Methods:
52  * - size_t getSampleCount() const : return the number of samples. This should not change during a model search.
53  * - bool fitModel( const vector_size_t& useIndices, Model& model ) const : This function fits a model to the data selected by the indices. The return value indicates the success, hence false means a degenerate case, where no model was found.
54  * - Real testSample( size_t index, const Model& model ) const : return some value that indicates how well a sample fits to the model. This way the thresholding is moved to the searching procedure and the model just tells how good a sample is.
55  *
56  * There are two methods provided in this class to fit a model:
57  * - \a ransacSingleModel (RANSAC): Just like mrpt::math::RANSAC_Template
58  *
59  * - \a geneticSingleModel (Genetic): Provides a mixture of a genetic and the ransac algorithm.
60  * Instead of selecting a set of data in each iteration, it takes more (ex. 10) and order these model
61  * using some fitness function: the average error of the inliers scaled by the number of outliers (This
62  * fitness might require some fine tuning). Than the (ex 10) new kernel for the next iteration is created as follows:
63  * - Take the best kernels (as for the original ransac)
64  * - Select two kernels ( with a higher probability for the better models) and let the new kernel be a subset of the two original kernels ( additionally to leave the local minimums an additional random seed might appear - mutation)
65  * - Generate some new random samples.
66  *
67  * For an example of usage, see "samples/model_search_test/"
68  * \sa mrpt::math::RANSAC_Template, another RANSAC implementation where models can be matrices only.
69  *
70  * \author Zoltar Gaal
71  * \ingroup ransac_grp
72  */
74  private:
75  //! Select random (unique) indices from the 0..p_size sequence
76  void pickRandomIndex( size_t p_size, size_t p_pick, vector_size_t& p_ind );
77 
78  /** Select random (unique) indices from the set.
79  * The set is destroyed during pick */
80  void pickRandomIndex( std::set<size_t> p_set, size_t p_pick, vector_size_t& p_ind );
81 
82  public:
83  template<typename TModelFit>
84  bool ransacSingleModel( const TModelFit& p_state,
85  size_t p_kernelSize,
86  const typename TModelFit::Real& p_fitnessThreshold,
87  typename TModelFit::Model& p_bestModel,
88  vector_size_t& p_inliers );
89 
90  private:
91  template<typename TModelFit>
92  struct TSpecies {
93  typename TModelFit::Model model;
96  typename TModelFit::Real fitness;
97 
98  static bool compare( const TSpecies* p_a, const TSpecies* p_b )
99  {
100  return p_a->fitness < p_b->fitness;
101  }
102  };
103 
104  public:
105  template<typename TModelFit>
106  bool geneticSingleModel( const TModelFit& p_state,
107  size_t p_kernelSize,
108  const typename TModelFit::Real& p_fitnessThreshold,
109  size_t p_populationSize,
110  size_t p_maxIteration,
111  typename TModelFit::Model& p_bestModel,
112  vector_size_t& p_inliers );
113  }; // end of class
114 
115  } // namespace math
116 } // namespace mrpt
117 
118 // Template implementations:
119 #include "model_search_impl.h"
120 
121 #endif // math_modelsearch_h
Model search implementations: RANSAC and genetic algorithm.
Definition: model_search.h:73
std::vector< size_t > vector_size_t
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
static bool compare(const TSpecies *p_a, const TSpecies *p_b)
Definition: model_search.h:98



Page generated by Doxygen 1.8.14 for MRPT 1.0.2 SVN: at lun oct 28 00:52:41 CET 2019 Hosted on:
SourceForge.net Logo