forked from mlpack/mlpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
population.hpp
119 lines (91 loc) · 2.65 KB
/
population.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* @file population.hpp
* @author Bang Liu
*
* Definition of Population class.
*/
#ifndef MLPACK_METHODS_NE_POPULATION_HPP
#define MLPACK_METHODS_NE_POPULATION_HPP
#include <cstddef>
#include <mlpack/core.hpp>
#include "link_gene.hpp"
#include "neuron_gene.hpp"
#include "genome.hpp"
#include "species.hpp"
namespace mlpack {
namespace ne {
/**
* This class defines a population consist of multiple species.
*/
class Population {
public:
// Species contained by this population.
std::vector<Species> aSpecies;
// Default constructor.
Population() {
aNumSpecies = 0;
aPopulationSize = 0;
aBestFitness = DBL_MAX;
aBestGenome = Genome();
aNextSpeciesId = 0;
}
// Parametric constructor.
Population(Genome& seedGenome, ssize_t populationSize) {
aPopulationSize = populationSize;
aNumSpecies = 1;
aBestGenome = seedGenome;
aBestFitness = DBL_MAX;
Species species(seedGenome, populationSize);
species.Id(0);
aSpecies.push_back(species);
aNextSpeciesId = 1;
}
// Destructor.
~Population() {}
// Set species number.
void NumSpecies(ssize_t numSpecies) { aNumSpecies = numSpecies; }
// Get species number.
ssize_t NumSpecies() const { return aNumSpecies; }
// Set population size.
void PopulationSize(ssize_t populationSize) { aPopulationSize = populationSize; }
// Get population size.
ssize_t PopulationSize() const { return aPopulationSize; }
// Set best fitness.
void BestFitness(double bestFitness) { aBestFitness = bestFitness; }
// Get best fitness.
double BestFitness() const { return aBestFitness; }
// Set best genome.
void BestGenome(Genome& bestGenome) { aBestGenome = bestGenome; }
// Get best genome.
Genome BestGenome() const { return aBestGenome; }
// Set next species id.
void NextSpeciesId(ssize_t nextSpeciesId) { aNextSpeciesId = nextSpeciesId; }
// Get next species id.
ssize_t NextSpeciesId() const { return aNextSpeciesId; }
// Add species.
void AddSpecies(Species& species) {
aSpecies.push_back(species);
aPopulationSize += species.SpeciesSize();
++aNumSpecies; // TODO: do we really need numSpecies? Maybe NumSpecies() is enough.
++aNextSpeciesId;
}
// Remove species.
void RemoveSpecies(ssize_t idx) {
aSpecies.erase(aSpecies.begin() + idx);
--aNumSpecies;
}
private:
// Number of species.
ssize_t aNumSpecies;
// Number of genomes including all species.
ssize_t aPopulationSize;
// Best fitness.
double aBestFitness;
// Best genome.
Genome aBestGenome;
// Next species id.
ssize_t aNextSpeciesId;
};
} // namespace ne
} // namespace mlpack
# endif // MLPACK_METHODS_NE_POPULATION_HPP