12 #ifndef math_modelsearch_h 27 template <
typename TModelFit>
29 const TModelFit& p_state,
size_t p_kernelSize,
30 const typename TModelFit::Real& p_fitnessThreshold,
31 typename TModelFit::Model& p_bestModel, std::vector<size_t>& p_inliers)
33 size_t bestScore = std::string::npos;
35 size_t softIterLimit = 1;
36 size_t hardIterLimit = 100;
38 size_t nSamples = p_state.getSampleCount();
39 std::vector<size_t> ind(p_kernelSize);
41 while (iter < softIterLimit && iter < hardIterLimit)
43 bool degenerate =
true;
44 typename TModelFit::Model currentModel;
49 degenerate = !p_state.fitModel(ind, currentModel);
51 if (i > 100)
return false;
54 std::vector<size_t> inliers;
56 for (
size_t j = 0; j < nSamples; j++)
58 if (p_state.testSample(j, currentModel) < p_fitnessThreshold)
64 const size_t ninliers = inliers.size();
65 bool update_estim_num_iters =
69 if (ninliers > bestScore ||
70 (bestScore == std::string::npos && ninliers != 0))
73 p_bestModel = currentModel;
75 update_estim_num_iters =
true;
78 if (update_estim_num_iters)
82 double f = ninliers /
static_cast<double>(nSamples);
83 double p = 1 - std::pow(f, static_cast<double>(p_kernelSize));
84 const double eps = std::numeric_limits<double>::epsilon();
86 p = std::min(1 -
eps, p);
98 template <
typename TModelFit>
100 const TModelFit& p_state,
size_t p_kernelSize,
101 const typename TModelFit::Real& p_fitnessThreshold,
size_t p_populationSize,
102 size_t p_maxIteration,
typename TModelFit::Model& p_bestModel,
103 std::vector<size_t>& p_inliers)
107 std::vector<Species> storage;
108 std::vector<Species*> population;
109 std::vector<Species*> sortedPopulation;
111 size_t sampleCount = p_state.getSampleCount();
112 int elderCnt = (int)p_populationSize / 3;
113 int siblingCnt = (p_populationSize - elderCnt) / 2;
114 int speciesAlive = 0;
116 storage.resize(p_populationSize);
117 population.reserve(p_populationSize);
118 sortedPopulation.reserve(p_populationSize);
119 for (
auto& d : storage)
122 population.push_back(&d);
123 sortedPopulation.push_back(&d);
127 while (iter < p_maxIteration)
136 for (; i < elderCnt; i++)
138 population.push_back(sortedPopulation[i]);
142 int se = (int)speciesAlive;
143 for (; i < elderCnt + siblingCnt; i++)
145 Species* sibling = sortedPopulation[--se];
146 population.push_back(sibling);
155 (p1 > se / 2) ? (r2 % p1) : p1 + 1 + (r2 % (se - p1 - 1));
156 ASSERT_(p1 != p2 && p1 < se && p2 < se);
158 Species* a = sortedPopulation[p1];
159 Species* b = sortedPopulation[p2];
162 std::set<size_t> sampleSet;
163 sampleSet.insert(a->sample.begin(), a->sample.end());
164 sampleSet.insert(b->sample.begin(), b->sample.end());
167 sampleSet.insert(rand() % sampleCount);
172 for (; i < (int)p_populationSize; i++)
174 Species* s = sortedPopulation[i];
175 population.push_back(s);
182 for (
typename std::vector<Species*>::iterator it = population.begin();
183 it != population.end(); it++)
186 if (p_state.fitModel(s.sample, s.model))
189 for (
size_t i = 0; i < p_state.getSampleCount(); i++)
191 typename TModelFit::Real f = p_state.testSample(i, s.model);
192 if (f < p_fitnessThreshold)
195 s.inliers.push_back(i);
200 s.fitness /= s.inliers.size();
202 s.fitness *= (sampleCount - s.inliers.size());
207 std::numeric_limits<typename TModelFit::Real>::max();
218 sortedPopulation.begin(), sortedPopulation.end(), Species::compare);
223 p_bestModel = sortedPopulation[0]->model;
224 p_inliers = sortedPopulation[0]->inliers;
226 return !p_inliers.empty();
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.
#define ASSERT_(f)
Defines an assertion mechanism.
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.
int round(const T value)
Returns the closer integer (int) to x.