Main MRPT website > C++ reference for MRPT 1.9.9
model_search.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-2018, 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 #pragma once
11 
12 #include <cstdint>
13 #include <set>
14 #include <set>
15 
16 namespace mrpt
17 {
18 namespace math
19 {
20 /** Model search implementations: RANSAC and genetic algorithm
21  *
22  * The type \a TModelFit is a user-supplied struct/class that implements this
23  * interface:
24  * - Types:
25  * - \a Real : The numeric type to use (typ: double, float)
26  * - \a Model : The type of the model to be fitted (for example: A matrix, a
27  * TLine2D, a TPlane3D, ...)
28  * - Methods:
29  * - size_t getSampleCount() const : return the number of samples. This
30  * should not change during a model search.
31  * - bool fitModel( const std::vector<size_t>& useIndices, Model& model )
32  * const :
33  * This function fits a model to the data selected by the indices. The return
34  * value indicates the success, hence false means a degenerate case, where no
35  * model was found.
36  * - Real testSample( size_t index, const Model& model ) const : return some
37  * value that indicates how well a sample fits to the model. This way the
38  * thresholding is moved to the searching procedure and the model just tells how
39  * good a sample is.
40  *
41  * There are two methods provided in this class to fit a model:
42  * - \a ransacSingleModel (RANSAC): Just like mrpt::math::RANSAC_Template
43  *
44  * - \a geneticSingleModel (Genetic): Provides a mixture of a genetic and
45  * the ransac algorithm.
46  * Instead of selecting a set of data in each iteration, it takes more
47  * (ex. 10) and order these model
48  * using some fitness function: the average error of the inliers scaled
49  * by the number of outliers (This
50  * fitness might require some fine tuning). Than the (ex 10) new kernel
51  * for the next iteration is created as follows:
52  * - Take the best kernels (as for the original ransac)
53  * - Select two kernels ( with a higher probability for the better
54  * models) and let the new kernel be a subset of the two original kernels (
55  * additionally to leave the local minimums an additional random seed might
56  * appear - mutation)
57  * - Generate some new random samples.
58  *
59  * For an example of usage, see "samples/model_search_test/"
60  * \sa mrpt::math::RANSAC_Template, another RANSAC implementation where models
61  * can be matrices only.
62  *
63  * \author Zoltar Gaal
64  * \ingroup ransac_grp
65  */
67 {
68  private:
69  //! Select random (unique) indices from the 0..p_size sequence
70  void pickRandomIndex(
71  size_t p_size, size_t p_pick, std::vector<size_t>& p_ind);
72 
73  /** Select random (unique) indices from the set.
74  * The set is destroyed during pick */
75  void pickRandomIndex(
76  std::set<size_t> p_set, size_t p_pick, std::vector<size_t>& p_ind);
77 
78  public:
79  template <typename TModelFit>
80  bool ransacSingleModel(
81  const TModelFit& p_state, size_t p_kernelSize,
82  const typename TModelFit::Real& p_fitnessThreshold,
83  typename TModelFit::Model& p_bestModel, std::vector<size_t>& p_inliers);
84 
85  private:
86  template <typename TModelFit>
87  struct TSpecies
88  {
89  typename TModelFit::Model model;
90  std::vector<size_t> sample;
91  std::vector<size_t> inliers;
92  typename TModelFit::Real fitness;
93 
94  static bool compare(const TSpecies* p_a, const TSpecies* p_b)
95  {
96  return p_a->fitness < p_b->fitness;
97  }
98  };
99 
100  public:
101  template <typename TModelFit>
102  bool geneticSingleModel(
103  const TModelFit& p_state, size_t p_kernelSize,
104  const typename TModelFit::Real& p_fitnessThreshold,
105  size_t p_populationSize, size_t p_maxIteration,
106  typename TModelFit::Model& p_bestModel, std::vector<size_t>& p_inliers);
107 }; // end of class
108 
109 } // namespace math
110 } // namespace mrpt
111 
112 // Template implementations:
113 #include "model_search_impl.h"
mrpt::math::ModelSearch::TSpecies
Definition: model_search.h:87
mrpt::math::ModelSearch::pickRandomIndex
void pickRandomIndex(size_t p_size, size_t p_pick, std::vector< size_t > &p_ind)
Select random (unique) indices from the 0..p_size sequence.
Definition: model_search.cpp:21
mrpt::math::ModelSearch::TSpecies::fitness
TModelFit::Real fitness
Definition: model_search.h:92
mrpt::math::ModelSearch::TSpecies::model
TModelFit::Model model
Definition: model_search.h:89
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::math::ModelSearch
Model search implementations: RANSAC and genetic algorithm.
Definition: model_search.h:66
model_search_impl.h
mrpt::math::ModelSearch::TSpecies::inliers
std::vector< size_t > inliers
Definition: model_search.h:91
mrpt::math::ModelSearch::TSpecies::sample
std::vector< size_t > sample
Definition: model_search.h:90
mrpt::math::ModelSearch::geneticSingleModel
bool geneticSingleModel(const TModelFit &p_state, size_t p_kernelSize, const typename TModelFit::Real &p_fitnessThreshold, size_t p_populationSize, size_t p_maxIteration, typename TModelFit::Model &p_bestModel, std::vector< size_t > &p_inliers)
Run a generic programming version of ransac searching for a single model.
Definition: model_search_impl.h:97
mrpt::math::ModelSearch::TSpecies::compare
static bool compare(const TSpecies *p_a, const TSpecies *p_b)
Definition: model_search.h:94
mrpt::math::ModelSearch::ransacSingleModel
bool ransacSingleModel(const TModelFit &p_state, size_t p_kernelSize, const typename TModelFit::Real &p_fitnessThreshold, typename TModelFit::Model &p_bestModel, std::vector< size_t > &p_inliers)
Run the ransac algorithm searching for a single model.
Definition: model_search_impl.h:26



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST