forked from peter-ch/MultiNEAT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenes.h
More file actions
398 lines (325 loc) · 10.2 KB
/
Genes.h
File metadata and controls
398 lines (325 loc) · 10.2 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#ifndef _GENES_H
#define _GENES_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: Genes.h
// Description: Definitions for the Neuron and Link gene classes.
/////////////////////////////////////////////////////////////////
#ifdef USE_BOOST_PYTHON
#include <boost/python.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/vector.hpp>
namespace py = boost::python;
#endif
#include <iostream>
#include <vector>
#include "Parameters.h"
namespace NEAT
{
//////////////////////////////////////////////
// Enumeration for all available neuron types
//////////////////////////////////////////////
enum NeuronType
{
NONE = 0,
INPUT,
BIAS,
HIDDEN,
OUTPUT
};
//////////////////////////////////////////////////////////
// Enumeration for all possible activation function types
//////////////////////////////////////////////////////////
enum ActivationFunction
{
SIGNED_SIGMOID = 0, // Sigmoid function (default) (blurred cutting plane)
UNSIGNED_SIGMOID,
TANH,
TANH_CUBIC,
SIGNED_STEP, // Treshold (0 or 1) (cutting plane)
UNSIGNED_STEP,
SIGNED_GAUSS, // Gaussian (symettry)
UNSIGNED_GAUSS,
ABS, // Absolute value |x| (another symettry)
SIGNED_SINE, // Sine wave (smooth repetition)
UNSIGNED_SINE,
LINEAR, // Linear f(x)=x (combining coordinate frames only)
RELU, // Rectifiers
SOFTPLUS
};
//////////////////////////////////
// This class defines a link gene
//////////////////////////////////
class LinkGene
{
/////////////////////
// Members
/////////////////////
private:
// These variables are initialized once and cannot be changed
// anymore
// The IDs of the neurons that this link connects
unsigned int m_FromNeuronID, m_ToNeuronID;
// The link's innovation ID
unsigned int m_InnovationID;
// This variable is modified during evolution
// The weight of the connection
double m_Weight;
// Is it recurrent?
bool m_IsRecurrent;
public:
#ifdef USE_BOOST_PYTHON
// Serialization
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & m_FromNeuronID;
ar & m_ToNeuronID;
ar & m_InnovationID;
ar & m_IsRecurrent;
ar & m_Weight;
}
#endif
double GetWeight() const
{
return m_Weight;
}
void SetWeight(const double a_Weight)
{
//TODO: Add ASSERTS and logic to check for valid values
m_Weight = a_Weight;
}
////////////////
// Constructors
////////////////
LinkGene(unsigned int a_InID, unsigned int a_OutID, unsigned int a_InnovID, double a_Wgt, bool a_Recurrent = false):
m_FromNeuronID(a_InID), m_ToNeuronID(a_OutID), m_InnovationID(a_InnovID), m_Weight(a_Wgt), m_IsRecurrent(a_Recurrent)
{}
LinkGene()
{}
// assigment operator
LinkGene& operator =(const LinkGene& a_g)
{
if (this != &a_g)
{
m_FromNeuronID = a_g.m_FromNeuronID;
m_ToNeuronID = a_g.m_ToNeuronID;
m_Weight = a_g.m_Weight;
m_IsRecurrent = a_g.m_IsRecurrent;
m_InnovationID = a_g.m_InnovationID;
}
return *this;
}
//////////////
// Destructor
//////////////
//////////////
// Methods
//////////////
// Access to static (const) variables
unsigned int FromNeuronID() const
{
return m_FromNeuronID;
}
unsigned int ToNeuronID() const
{
return m_ToNeuronID;
}
unsigned int InnovationID() const
{
return m_InnovationID;
}
bool IsRecurrent() const
{
return m_IsRecurrent;
}
bool IsLoopedRecurrent() const
{
if (m_FromNeuronID == m_ToNeuronID) return true;
else return false;
}
//overload '<', '>', '!=' and '==' used for sorting and comparison (we use the innovation ID as the criteria)
friend bool operator<(const LinkGene& a_lhs, const LinkGene& a_rhs)
{
return (a_lhs.m_InnovationID < a_rhs.m_InnovationID);
}
friend bool operator>(const LinkGene& a_lhs, const LinkGene& a_rhs)
{
return (a_lhs.m_InnovationID > a_rhs.m_InnovationID);
}
friend bool operator!=(const LinkGene& a_lhs, const LinkGene& a_rhs)
{
return (a_lhs.m_InnovationID != a_rhs.m_InnovationID);
}
friend bool operator==(const LinkGene& a_lhs, const LinkGene& a_rhs)
{
return (a_lhs.m_InnovationID == a_rhs.m_InnovationID);
}
};
////////////////////////////////////
// This class defines a neuron gene
////////////////////////////////////
class NeuronGene
{
/////////////////////
// Members
/////////////////////
private:
// These variables are initialized once and cannot be changed
// anymore
// Its unique identification number
unsigned int m_ID;
// Its type and role in the network
NeuronType m_Type;
public:
// These variables are modified during evolution
// Safe to access directly
// useful for displaying the genome
int x, y;
// Position (depth) within the network
double m_SplitY;
/////////////////////////////////////////////////////////
// Any additional properties of the neuron
// should be added here. This may include
// time constant & bias for leaky integrators,
// activation function type,
// activation function slope (or maybe other properties),
// etc...
/////////////////////////////////////////////////////////
// Additional parameters associated with the
// neuron's activation function.
// The current activation function may not use
// any of them anyway.
// A is usually used to alter the function's slope with a scalar
// B is usually used to force a bias to the neuron
// -------------------
// Sigmoid : using A, B (slope, shift)
// Step : using B (shift)
// Gauss : using A, B (slope, shift))
// Abs : using B (shift)
// Sine : using A (frequency, phase)
// Square : using A, B (high phase lenght, low phase length)
// Linear : using B (shift)
double m_A, m_B;
// Time constant value used when
// the neuron is activating in leaky integrator mode
double m_TimeConstant;
// Bias value used when the neuron is activating in
// leaky integrator mode
double m_Bias;
// The type of activation function the neuron has
ActivationFunction m_ActFunction;
#ifdef USE_BOOST_PYTHON
// Serialization
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & m_ID;
ar & m_Type;
ar & m_A;
ar & m_B;
ar & m_TimeConstant;
ar & m_Bias;
ar & x;
ar & y;
ar & m_ActFunction;
ar & m_SplitY;
}
#endif
////////////////
// Constructors
////////////////
NeuronGene(NeuronType a_type,
unsigned int a_id,
double a_splity)
:m_ID(a_id), m_Type(a_type), m_SplitY(a_splity)
{
// Initialize the node specific parameters
m_A = 0.0f;
m_B = 0.0f;
m_TimeConstant = 0.0f;
m_Bias = 0.0f;
m_ActFunction = UNSIGNED_SIGMOID;
}
NeuronGene()
{
m_A = 0.0f;
m_B = 0.0f;
m_TimeConstant = 0.0f;
m_Bias = 0.0f;
m_ActFunction = UNSIGNED_SIGMOID;
}
// assigment operator
NeuronGene& operator =(const NeuronGene& a_g)
{
if (this != &a_g)
{
m_ID = a_g.m_ID;
m_Type = a_g.m_Type;
m_SplitY = a_g.m_SplitY;
x = a_g.x;
y = a_g.y;
m_A = a_g.m_A;
m_B = a_g.m_B;
m_TimeConstant = a_g.m_TimeConstant;
m_Bias = a_g.m_Bias;
m_ActFunction = a_g.m_ActFunction;
}
return *this;
}
//////////////
// Destructor
//////////////
//////////////
// Methods
//////////////
// Accessing static (const) variables
unsigned int ID() const
{
return m_ID;
}
NeuronType Type() const
{
return m_Type;
}
double SplitY() const
{
return m_SplitY;
}
// Initializing
void Init(double a_A, double a_B, double a_TimeConstant, double a_Bias, ActivationFunction a_ActFunc)
{
m_A = a_A;
m_B = a_B;
m_TimeConstant = a_TimeConstant;
m_Bias = a_Bias;
m_ActFunction = a_ActFunc;
}
};
} // namespace NEAT
#endif