-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenotype.h
192 lines (138 loc) · 5.86 KB
/
genotype.h
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
#ifndef NEATGENOTYPE_H
#define NEATGENOTYPE_H
//-----------------------------------------------------------------------
//
// Name: genotype.h
//
// Author: Mat Buckland 2002 ([email protected])
//
// Desc: genome definition used in the implementation of
// Kenneth Owen Stanley's and Risto Miikkulainen's NEAT idea.
//
//-----------------------------------------------------------------------
#include <vector>
#include "phenotype.h"
#include "utils.h"
#include "CInnovation.h"
#include "Genes.h"
using namespace std;
class Cga;
class CInnovation;
//------------------------------------------------------------------------
//
// CGenome class definition. A genome basically consists of a vector of
// link genes, a vector of neuron genes and a fitness score.
//------------------------------------------------------------------------
class CGenome
{
private:
//its identification number
int m_GenomeID;
//all the neurons which make up this genome
vector<SNeuronGene> m_vecNeurons;
//and all the the links
vector<SLinkGene> m_vecLinks;
//pointer to its phenotype
CNeuralNet* m_pPhenotype;
//the depth of this genomes network
int m_iNetDepth;
//its raw fitness score
double m_dFitness;
//its fitness score after it has been placed into a
//species and adjusted accordingly
double m_dAdjustedFitness;
//the number of offspring this individual is required to spawn
//for the next generation
double m_dAmountToSpawn;
//keep a record of the number of inputs and outputs
int m_iNumInputs,
m_iNumOutPuts;
//keeps a track of which species this genome is in (only used
//for display purposes)
int m_iSpecies;
//returns true if the specified link is already part of the genome
bool DuplicateLink(int NeuronIn, int NeuronOut);
//given a neuron id this function just finds its position in
//m_vecNeurons
int GetElementPos(int neuron_id);
//tests if the passed ID is the same as any existing neuron IDs. Used
//in AddNeuron
bool AlreadyHaveThisNeuronID(const int ID);
public:
CGenome();
//this constructor creates a minimal genome where there are output &
//input neurons and every input neuron is connected to each output neuron
CGenome(int id, int inputs, int outputs);
//this constructor creates a genome from a vector of SLinkGenes
//a vector of SNeuronGenes and an ID number
CGenome(int id,
vector<SNeuronGene> neurons,
vector<SLinkGene> genes,
int inputs,
int outputs);
~CGenome();
//copy constructor
CGenome(const CGenome& g);
//assignment operator
CGenome& operator =(const CGenome& g);
//create a neural network from the genome
CNeuralNet* CreatePhenotype();
//delete the neural network
void DeletePhenotype();
//initializes all the weights to small random values. This
//is used mainly when ceated a population from a genome file
void InitializeWeights();
//add a link to the genome dependent upon the mutation rate
void AddLink(double MutationRate,
double ChanceOfRecurrent,
CInnovation &innovation,
int NumTrysToFindLoop,
int NumTrysToAddLink);
//and a neuron
void AddNeuron(double MutationRate,
CInnovation &innovation,
int NumTrysToFindOldLink);
//this function mutates the connection weights
void MutateWeights(double mut_rate,
double prob_new_mut,
double dMaxPertubation);
//perturbs the activation responses of the neurons
void MutateActivationResponse(double mut_rate,
double MaxPertubation);
//calculates the compatibility score between this genome and
//another genome
double GetCompatibilityScore(const CGenome &genome);
void SortGenes();
bool Write(ostream &file);
//create a genome from a data file
bool CreateFromFile(const char* szFileName);
//overload '<' used for sorting. From fittest to poorest.
friend bool operator<(const CGenome& lhs, const CGenome& rhs)
{
return (lhs.m_dFitness > rhs.m_dFitness);
}
//---------------------------------accessor methods
int ID()const{return m_GenomeID;}
void SetID(const int val){m_GenomeID = val;}
int Depth(){return m_iNetDepth;}
void SetDepth(int val){m_iNetDepth = val;}
int NumGenes()const{return m_vecLinks.size();}
int NumNeurons()const{return m_vecNeurons.size();}
int NumInputs()const{return m_iNumInputs;}
int NumOutputs()const{return m_iNumOutPuts;}
double AmountToSpawn()const{return m_dAmountToSpawn;}
void SetAmountToSpawn(double num){m_dAmountToSpawn = num;}
void SetFitness(const double num){m_dFitness = num;}
void SetAdjFitness(const double num){m_dAdjustedFitness = num;}
double Fitness()const{return m_dFitness;}
double GetAdjFitness()const{return m_dAdjustedFitness;}
int GetSpecies()const{return m_iSpecies;}
void SetSpecies(int spc){m_iSpecies = spc;}
CNeuralNet* Phenotype(){return m_pPhenotype;}
double SplitY(const int val)const{return m_vecNeurons[val].dSplitY;}
vector<SLinkGene> LinkGenes()const{return m_vecLinks;}
vector<SNeuronGene> NeuronGenes()const{return m_vecNeurons;}
vector<SLinkGene>::iterator StartOfGenes(){return m_vecLinks.begin();}
vector<SLinkGene>::iterator EndOfGenes(){return m_vecLinks.end();}
};
#endif