# class mrpt::math::ModelSearch¶

Model search implementations: RANSAC and genetic algorithm.

The type TModelFit is a user-supplied struct/class that implements this interface:

• Types:

• Real : The numeric type to use (typ: double, float)

• Model : The type of the model to be fitted (for example: A matrix, a TLine2D, a TPlane3D, …)

• Methods:

• size_t getSampleCount() const : return the number of samples. This should not change during a model search.

• bool fitModel( const std::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.

• 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.

There are two methods provided in this class to fit a model:

• ransacSingleModel (RANSAC): Just like mrpt::math::RANSAC_Template

• geneticSingleModel (Genetic): Provides a mixture of a genetic and the ransac algorithm. Instead of selecting a set of data in each iteration, it takes more (ex. 10) and order these model using some fitness function: the average error of the inliers scaled by the number of outliers (This fitness might require some fine tuning). Than the (ex 10) new kernel for the next iteration is created as follows:

• Take the best kernels (as for the original ransac)

• 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)

• Generate some new random samples.

For an example of usage, see “samples/model_search_test/” Zoltar Gaal

mrpt::math::RANSAC_Template, another RANSAC implementation where models can be matrices only.

#include <mrpt/math/model_search.h>

class ModelSearch
{
public:
// structs

template <typename TModelFit>
struct TSpecies;

//
methods

template <typename TModelFit>
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
);

template <typename TModelFit>
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
);
};

## Methods¶

template <typename TModelFit>
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.

template <typename TModelFit>
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.