@@ -193,6 +193,7 @@ GeneticSearch::GeneticSearch(
193
193
lastBestConf (confs[0 ]),
194
194
numGenerations(numGenerations),
195
195
maxPopulationSize(populationSize),
196
+ matingPoolSize(populationSize * 3 ),
196
197
crossOverRate(crossOverRate),
197
198
mutationRate(mutationRate),
198
199
numberElites(numberElites),
@@ -252,19 +253,40 @@ TuningConfiguration GeneticSearch::crossover(
252
253
return a;
253
254
}
254
255
256
+ std::vector<TuningConfiguration> GeneticSearch::stochasticUniversalSampling (
257
+ const std::vector<double >& fitness) const {
258
+ std::vector<TuningConfiguration> matingPool;
259
+ matingPool.reserve (matingPoolSize);
260
+
261
+ auto r = std::uniform_real_distribution<double >(0 , 1.0 / matingPoolSize)(rng);
262
+ size_t count = 0 ;
263
+ size_t i = 0 ;
264
+ while (count < matingPoolSize) {
265
+ while (r <= fitness[i]) {
266
+ matingPool.push_back (population[i]->configuration );
267
+ r += 1.0 / matingPoolSize;
268
+ ++count;
269
+ }
270
+ ++i;
271
+ }
272
+ return matingPool;
273
+ }
274
+
255
275
void GeneticSearch::breed () {
256
- auto accFitness = computeAccumulatedFitness (population);
276
+ auto matingPool =
277
+ stochasticUniversalSampling (computeAccumulatedFitness (population));
278
+
257
279
Population new_population;
258
- new_population.reserve (maxPopulationSize );
280
+ new_population.reserve (matingPoolSize );
259
281
for (size_t c = 0 ; c < numberElites; ++c) {
260
282
new_population.push_back (
261
283
make_unique<CandidateConfiguration>(population.at (c)->configuration ));
262
284
}
263
285
264
- auto select = [&]() -> const TuningConfiguration& {
265
- auto limit = std::uniform_real_distribution< double >{}(rng);
266
- auto lb = std::lower_bound (accFitness. begin ( ), accFitness. end (), limit );
267
- return population .at (std::distance (accFitness. begin (), lb))-> configuration ;
286
+ auto select = [&]() -> TuningConfiguration& {
287
+ auto idx = std::uniform_int_distribution< size_t >{
288
+ size_t ( 0 ), matingPool. size () - 1 }(rng );
289
+ return matingPool .at (idx) ;
268
290
};
269
291
auto shouldCrossOver = [&]() -> bool {
270
292
/*
0 commit comments