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) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 
29 #ifndef math_modelsearch_h
30 #define math_modelsearch_h
31 
32 #include <mrpt/utils/utils_defs.h>
33 
34 namespace mrpt {
35  namespace math {
36 
37 
38  /** Model search implementations: RANSAC and genetic algorithm
39  *
40  * The type \a TModelFit is a user-supplied struct/class that implements this interface:
41  * - Types:
42  * - \a Real : The numeric type to use (typ: double, float)
43  * - \a Model : The type of the model to be fitted (for example: A matrix, a TLine2D, a TPlane3D, ...)
44  * - Methods:
45  * - size_t getSampleCount() const : return the number of samples. This should not change during a model search.
46  * - 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.
47  * - 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.
48  *
49  * There are two methods provided in this class to fit a model:
50  * - \a ransacSingleModel (RANSAC): Just like mrpt::math::RANSAC_Template
51  *
52  * - \a geneticSingleModel (Genetic): Provides a mixture of a genetic and the ransac algorithm.
53  * Instead of selecting a set of data in each iteration, it takes more (ex. 10) and order these model
54  * using some fitness function: the average error of the inliers scaled by the number of outliers (This
55  * fitness might require some fine tuning). Than the (ex 10) new kernel for the next iteration is created as follows:
56  * - Take the best kernels (as for the original ransac)
57  * - 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)
58  * - Generate some new random samples.
59  *
60  * For an example of usage, see "samples/model_search_test/"
61  * \sa mrpt::math::RANSAC_Template, another RANSAC implementation where models can be matrices only.
62  *
63  * \author Zoltar Gaal
64  * \ingroup ransac_grp
65  */
67  private:
68  //! Select random (unique) indices from the 0..p_size sequence
69  void pickRandomIndex( size_t p_size, size_t p_pick, vector_size_t& p_ind );
70 
71  /** Select random (unique) indices from the set.
72  * The set is destroyed during pick */
73  void pickRandomIndex( std::set<size_t> p_set, size_t p_pick, vector_size_t& p_ind );
74 
75  public:
76  template<typename TModelFit>
77  bool ransacSingleModel( const TModelFit& p_state,
78  size_t p_kernelSize,
79  const typename TModelFit::Real& p_fitnessThreshold,
80  typename TModelFit::Model& p_bestModel,
81  vector_size_t& p_inliers );
82 
83  private:
84  template<typename TModelFit>
85  struct TSpecies {
86  typename TModelFit::Model model;
89  typename TModelFit::Real fitness;
90 
91  static bool compare( const TSpecies* p_a, const TSpecies* p_b )
92  {
93  return p_a->fitness < p_b->fitness;
94  }
95  };
96 
97  public:
98  template<typename TModelFit>
99  bool geneticSingleModel( const TModelFit& p_state,
100  size_t p_kernelSize,
101  const typename TModelFit::Real& p_fitnessThreshold,
102  size_t p_populationSize,
103  size_t p_maxIteration,
104  typename TModelFit::Model& p_bestModel,
105  vector_size_t& p_inliers );
106  }; // end of class
107 
108  } // namespace math
109 } // namespace mrpt
110 
111 // Template implementations:
112 #include "model_search_impl.h"
113 
114 #endif // math_modelsearch_h



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013