-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpecies.cpp
61 lines (52 loc) · 1.75 KB
/
Species.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "Species.h"
Species::Species( int index ) {
this->Add( index );
}
void Species::Add( int index ) {
// Add to the genome array
this->genomeTracker.push_back( index );
this->representativeIndexLocation = Mathematics::RandomInRange(
0, this->genomeTracker.size() - 1
);
}
int Species::GetRepresentativeGenomeIndex() {
return this->genomeTracker[this->representativeIndexLocation];
}
void Species::CullSpecies( const std::vector<Genome*>& genomeArray ) {
// Sort all the genome indices based on the fitness of each genome
std::sort(
this->genomeTracker.begin(),
this->genomeTracker.end(),
[&]( int a, int b) {
return genomeArray[a]->fitness > genomeArray[b]->fitness;
}
);
// Get parent_length = ceil( CULLFACTOR * genomeTracker.size() );
int parentLength = ( int )std::ceil(
Globals::SPECIES_KEEP_FACTOR * this->genomeTracker.size()
);
// Empty the parent store array
this->parents.clear();
// For( i = 0 > parent_length)
for( int i = 0; i < parentLength; ++i ) {
// store genomeTracker[i] in parent store array
this->parents.push_back( this->genomeTracker[i] );
}
// empty out the genomeTracker
this->genomeTracker.clear();
}
int Species::Size() {
return this->genomeTracker.size();
}
int Species::GetGenomeIndexAt( int location ) {
if( location < 0 ) return -1;
return this->genomeTracker[location];
}
int Species::GetParentGenomeIndexAt( int location ) {
if( location < 0 || location >= this->parents.size() ) return -1;
return this->parents[location];
}
int Species::GetRandomParent() {
int rnd = Mathematics::RandomInRange( 0, this->parents.size() - 1 );
return this->parents[rnd];
}