forked from peter-ch/MultiNEAT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecies.h
More file actions
200 lines (145 loc) · 6.07 KB
/
Species.h
File metadata and controls
200 lines (145 loc) · 6.07 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#ifndef _SPECIES_H
#define _SPECIES_H
///////////////////////////////////////////////////////////////////////////////////////////
// MultiNEAT - Python/C++ NeuroEvolution of Augmenting Topologies Library
//
// Copyright (C) 2012 Peter Chervenski
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see < http://www.gnu.org/licenses/ >.
//
// Contact info:
//
// Peter Chervenski < spookey@abv.bg >
// Shane Ryan < shane.mcdonald.ryan@gmail.com >
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// File: Species.h
// Description: Definition for the Species class.
///////////////////////////////////////////////////////////////////////////////
#include <vector>
#include "Innovation.h"
#include "Genome.h"
#include "Genes.h"
namespace NEAT
{
// forward
class Population;
//////////////////////////////////////////////
// The Species class
//////////////////////////////////////////////
class Species
{
/////////////////////
// Members
/////////////////////
private:
// ID of the species
unsigned int m_ID;
// Keep a local copy of the representative
Genome m_Representative;
// This tell us if this is the best species in the population
bool m_BestSpecies;
// This tell us if this is the worst species in the population
bool m_WorstSpecies;
// age of species
unsigned int m_Age;
// how many of this species should be spawned for
// the next population
double m_OffspringRqd;
public:
// best fitness found so far by this species
double m_BestFitness;
// Keep a local copy of the best genome
// Useful in co-evolution
Genome m_BestGenome;
// generations since fitness has improved, we can use
// this info to kill off a species if required
unsigned int m_GensNoImprovement;
// Color. Useful for displaying
// Safe to access directly.
int m_R, m_G, m_B;
////////////////////////////
// Constructors
////////////////////////////
// initializes a species with a leader genome and an ID number
Species(const Genome& a_Seed, int a_id);
// assignment operator
Species& operator=(const Species& a_g);
// comparison operator (nessesary for boost::python)
// todo: implement a better comparison technique
bool operator==(Species const& other) const { return m_ID == other.m_ID; }
////////////////////////////
// Destructor
////////////////////////////
////////////////////////////
// Methods
////////////////////////////
// Access
double GetBestFitness() const { return m_BestFitness; }
void SetBestSpecies(bool t) { m_BestSpecies = t; }
void SetWorstSpecies(bool t) { m_WorstSpecies = t; }
void IncreaseAge() { m_Age++; }
void ResetAge() { m_Age = 0; m_GensNoImprovement = 0; }
void IncreaseGensNoImprovement() { m_GensNoImprovement++; }
void SetOffspringRqd(double a_ofs) { m_OffspringRqd = a_ofs; }
double GetOffspringRqd() const { return m_OffspringRqd; }
unsigned int NumIndividuals() { return m_Individuals.size(); }
void ClearIndividuals() { m_Individuals.clear(); }
int ID() { return m_ID; }
int GensNoImprovement() { return m_GensNoImprovement; }
int Age() { return m_Age; }
Genome GetIndividualByIdx(int a_idx) const { return (m_Individuals[a_idx]); }
bool IsBestSpecies() const { return m_BestSpecies; }
bool IsWorstSpecies() const { return m_WorstSpecies; }
void SetRepresentative(Genome& a_G) { m_Representative = a_G; }
// returns the leader (the member having the best fitness, representing the species)
Genome GetLeader() const;
Genome GetRepresentative() const;
// adds a new member to the species and updates variables
void AddIndividual(Genome& a_New);
// returns an individual randomly selected from the best N%
Genome GetIndividual(Parameters& a_Parameters, RNG& a_RNG) const;
// returns a completely random individual
Genome GetRandomIndividual(RNG& a_RNG) const;
// calculates how many babies this species will spawn in total
void CountOffspring();
// this method performs fitness sharing
// it also boosts the fitness if young and penalizes if old
// applies extreme penalty for stagnating species over SpeciesDropoffAge generations.
void AdjustFitness(Parameters& a_Parameters);
// Sorts the individuals
void SortIndividuals();
///////////////////////////////////////////////////
// New stuff
// each species CONTAINS the individuals
std::vector<Genome> m_Individuals;
// Reproduction.
void Reproduce(Population& a_Pop, Parameters& a_Parameters, RNG& a_RNG);
void MutateGenome( bool t_baby_is_clone, Population &a_Pop, Genome &t_baby, Parameters& a_Parameters, RNG& a_RNG);
// Removes all individuals
void Clear()
{
m_Individuals.clear();
}
////////////////////////////////////////
// Real-time methods
double m_AverageFitness;
// Computes an estimate of the average fitness
void CalculateAverageFitness();
// A second version that returns the baby only
Genome ReproduceOne(Population& a_Pop, Parameters& a_Parameters, RNG& a_RNG);
void RemoveIndividual(unsigned int a_idx);
};
} // namespace NEAT
#endif