-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection.cpp
More file actions
40 lines (33 loc) · 893 Bytes
/
Copy pathSelection.cpp
File metadata and controls
40 lines (33 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include "Random.h"
#include "Selection.h"
using namespace std;
TournamentSelection::TournamentSelection() : TournamentSelection(1)
{
}
TournamentSelection::TournamentSelection(const int& tournamentSize)
{
this->tournamentSize = tournamentSize;
}
Individual & TournamentSelection::selectIndividual(Population& population, const vector<double>& fitnessValues) const
{
vector<int> seeds = Random::randInts(0, population.getSize() - 1, this->tournamentSize);
int chosenIdx = -1;
double maxFitness = 0;
bool fitnessSet = false;
for (const auto& idx : seeds) {
if (!fitnessSet) {
chosenIdx = idx;
maxFitness = fitnessValues.at(idx);
fitnessSet = true;
}
else {
double currentFitness = fitnessValues.at(idx);
if(currentFitness > maxFitness){
chosenIdx = idx;
maxFitness = currentFitness;
}
}
}
return population.at(chosenIdx);
}