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



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020