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