29 #ifndef math_modelsearch_impl_h
30 #define math_modelsearch_impl_h
32 #ifndef math_modelsearch_h
43 template<
typename TModelFit>
46 const typename TModelFit::Real& p_fitnessThreshold,
47 typename TModelFit::Model& p_bestModel,
50 size_t bestScore = std::string::npos;
52 size_t softIterLimit = 1;
53 size_t hardIterLimit = 100;
55 size_t nSamples = p_state.getSampleCount();
58 while ( iter < softIterLimit && iter < hardIterLimit )
60 bool degenerate =
true;
61 typename TModelFit::Model currentModel;
66 degenerate = !p_state.fitModel( ind, currentModel );
74 for(
size_t i = 0; i < nSamples; i++ )
76 if( p_state.testSample( i, currentModel ) < p_fitnessThreshold )
77 inliers.push_back( i );
82 const size_t ninliers = inliers.size();
83 bool update_estim_num_iters = (iter==0);
85 if ( ninliers > bestScore || (bestScore==std::string::npos && ninliers!=0))
88 p_bestModel = currentModel;
90 update_estim_num_iters =
true;
93 if (update_estim_num_iters)
96 double f = ninliers /
static_cast<double>( nSamples );
97 double p = 1 - pow( f, static_cast<double>( p_kernelSize ) );
98 const double eps = std::numeric_limits<double>::epsilon();
99 p = std::max( eps, p);
100 p = std::min( 1-eps, p);
101 softIterLimit = log(1-p) / log(p);
112 template<
typename TModelFit>
115 const typename TModelFit::Real& p_fitnessThreshold,
116 size_t p_populationSize,
117 size_t p_maxIteration,
118 typename TModelFit::Model& p_bestModel,
123 std::vector<Species> storage;
124 std::vector<Species*> population;
125 std::vector<Species*> sortedPopulation;
127 size_t sampleCount = p_state.getSampleCount();
128 int elderCnt = (int)p_populationSize/3;
129 int siblingCnt = (p_populationSize - elderCnt) / 2;
130 int speciesAlive = 0;
132 storage.resize( p_populationSize );
133 population.reserve( p_populationSize );
134 sortedPopulation.reserve( p_populationSize );
138 population.push_back( &*it );
139 sortedPopulation.push_back( &*it );
143 while ( iter < p_maxIteration )
152 for(; i < elderCnt; i++ )
154 population.push_back( sortedPopulation[i] );
158 int se = (int)speciesAlive;
159 for( ; i < elderCnt + siblingCnt ; i++ )
161 Species* sibling = sortedPopulation[--se];
162 population.push_back( sibling );
169 int p2 = (p1 > se / 2) ? (r2 % p1) : p1 + 1 + (r2 % (se-p1-1));
170 ASSERT_( p1 != p2 && p1 < se && p2 < se );
172 Species* a = sortedPopulation[p1];
173 Species* b = sortedPopulation[p2];
176 std::set<size_t> sampleSet;
177 sampleSet.insert( a->sample.begin(), a->sample.end() );
178 sampleSet.insert( b->sample.begin(), b->sample.end() );
180 sampleSet.insert( rand() % sampleCount );
185 for( ; i < (int)p_populationSize; i++ )
187 Species* s = sortedPopulation[i];
188 population.push_back( s );
198 if( p_state.fitModel( s.sample, s.model ) )
201 for(
size_t i = 0; i < p_state.getSampleCount(); i++ )
203 typename TModelFit::Real f = p_state.testSample( i, s.model );
204 if( f < p_fitnessThreshold )
207 s.inliers.push_back( i );
210 ASSERT_( s.inliers.size() > 0 );
212 s.fitness /= s.inliers.size();
214 s.fitness *= (sampleCount - s.inliers.size());
218 s.fitness = std::numeric_limits<typename TModelFit::Real>::max();
228 std::sort( sortedPopulation.begin(), sortedPopulation.end(), Species::compare );
233 p_bestModel = sortedPopulation[0]->model;
234 p_inliers = sortedPopulation[0]->inliers;
236 return !p_inliers.empty();
242 #endif // math_modelsearch_h