forked from mlpack/mlpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
species.hpp
102 lines (79 loc) · 2.09 KB
/
species.hpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* @file species.hpp
* @author Bang Liu
*
* Definition of Species class.
*/
#ifndef MLPACK_METHODS_NE_SPECIES_HPP
#define MLPACK_METHODS_NE_SPECIES_HPP
#include <cstddef>
#include <mlpack/core.hpp>
#include "link_gene.hpp"
#include "neuron_gene.hpp"
#include "genome.hpp"
namespace mlpack {
namespace ne {
/**
* This class defines a species of genomes.
*/
class Species {
public:
// Genomes.
std::vector<Genome> aGenomes;
// Default constructor.
Species() {
aSpeciesSize = 0;
aBestFitness = DBL_MAX;
aBestGenome = Genome();
aNextGenomeId = 0;
}
// Parametric constructor.
// TODO: whether randomize, random range, as parameter or not??
Species(Genome& seedGenome, size_t speciesSize) {
aSpeciesSize = speciesSize;
aBestFitness = DBL_MAX; // DBL_MAX denotes haven't evaluate yet.
// Create genomes from seed Genome and randomize weight.
for (size_t i=0; i<speciesSize; ++i) {
Genome genome = seedGenome;
genome.Id(i);
aGenomes.push_back(genome);
aGenomes[i].RandomizeWeights(-1, 1);
}
aNextGenomeId = speciesSize;
}
size_t SpeciesSize() { return aSpeciesSize; }
// Destructor.
~Species() {}
// Set/get best fitness.
double& BestFitness() { return aBestFitness; }
// Set best fitness to be the minimum of all genomes' fitness.
void SetBestFitness() {
if (aGenomes.size() == 0)
return;
aBestFitness = aGenomes[0].Fitness();
for (size_t i=0; i<aGenomes.size(); ++i) {
if (aGenomes[i].Fitness() < aBestFitness) {
aBestFitness = aGenomes[i].Fitness();
}
}
}
// Sort genomes by fitness. First is best.
static bool CompareGenome(Genome lg, Genome rg) {
return (lg.Fitness() < rg.Fitness());
}
void SortSpecies() {
std::sort(aGenomes.begin(), aGenomes.end(), CompareGenome);
}
private:
// Number of Genomes.
size_t aSpeciesSize;
// Best fitness.
double aBestFitness;
// Genome with best fitness.
Genome aBestGenome;
// Next genome id.
size_t aNextGenomeId;
};
} // namespace ne
} // namespace mlpack
# endif // MLPACK_METHODS_NE_SPECIES_HPP