forked from peter-ch/MultiNEAT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInnovation.cpp
More file actions
301 lines (232 loc) · 9.07 KB
/
Innovation.cpp
File metadata and controls
301 lines (232 loc) · 9.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
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
///////////////////////////////////////////////////////////////////////////////////////////
// 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: Innovation.cpp
// Description: Implementation for the Innovation and InnovationDatabase classes.
///////////////////////////////////////////////////////////////////////////////
#include <fstream>
#include <string>
#include "Innovation.h"
#include "Genes.h"
#include "Genome.h"
#include "assert.h"
namespace NEAT
{
// Creates an empty database
InnovationDatabase::InnovationDatabase()
{
m_NextInnovationNum = 1; // innovations start at 1
m_NextNeuronID = 1; // neuron IDs start at 1
m_Innovations.clear();
}
// Creates an empty database but this time sets the next innov number and neuron ID
InnovationDatabase::InnovationDatabase(int a_LastInnovationNum, int a_LastNeuronID)
{
ASSERT((a_LastInnovationNum > 0) && (a_LastNeuronID > 0));
m_NextInnovationNum = a_LastInnovationNum;
m_NextNeuronID = a_LastNeuronID;
m_Innovations.clear();
}
// Initializes an empty database
void InnovationDatabase::Init(int a_LastInnovationNum, int a_LastNeuronID)
{
Flush();
m_NextNeuronID = a_LastNeuronID;
m_NextInnovationNum = a_LastInnovationNum;
}
// Initializes a database from a given genome
void InnovationDatabase::Init(const Genome& a_Genome)
{
m_Innovations.clear();
for(unsigned int i=0; i<a_Genome.NumLinks(); i++)
{
Innovation t_innov( a_Genome.GetLinkByIndex(i).InnovationID(), NEW_LINK, a_Genome.GetLinkByIndex(i).FromNeuronID(), a_Genome.GetLinkByIndex(i).ToNeuronID(), NONE, -1);
m_Innovations.push_back(t_innov);
}
m_NextNeuronID = a_Genome.GetLastNeuronID();
m_NextInnovationNum = a_Genome.GetLastInnovationID();
}
void InnovationDatabase::Init(std::ifstream& a_DataFile)
{
m_Innovations.clear();
m_NextInnovationNum = 0;
m_NextNeuronID = 0;
std::string t_str;
// search for InnovationDatabaseStart
do
{
a_DataFile >> t_str;
}
while (t_str != "InnovationDatabaseStart");
// Read the last innov numbers
a_DataFile >> t_str;
a_DataFile >> m_NextInnovationNum;
a_DataFile >> t_str;
a_DataFile >> m_NextNeuronID;
// Read the database until InnovationDatabaseEnd is encountered
do
{
a_DataFile >> t_str;
if (t_str == "Innovation")
{
// Read in the innovation
int t_id, t_from, t_to, t_innovtype, t_neurontype, t_nid;
a_DataFile >> t_id;
a_DataFile >> t_innovtype;
a_DataFile >> t_from;
a_DataFile >> t_to;
a_DataFile >> t_neurontype;
a_DataFile >> t_nid;
m_Innovations.push_back( Innovation(t_id, static_cast<InnovationType>(t_innovtype), t_from, t_to, static_cast<NeuronType>(t_neurontype), t_nid) );
}
}
while( t_str != "InnovationDatabaseEnd");
}
// The file is assumed to be opened
void InnovationDatabase::Save(FILE *a_file)
{
fprintf(a_file, "InnovationDatabaseStart\n");
fprintf(a_file, "NextInnovNum: %d\n", m_NextInnovationNum);
fprintf(a_file, "NextNeuronID: %d\n", m_NextNeuronID);
// Now save all innovations
for(unsigned int i=0; i<m_Innovations.size(); i++)
{
fprintf(a_file, "Innovation %d %d %d %d %d %d\n", m_Innovations[i].ID(), static_cast<int>(m_Innovations[i].InnovType()), m_Innovations[i].FromNeuronID(), m_Innovations[i].ToNeuronID(), static_cast<int>(m_Innovations[i].GetNeuronType()), m_Innovations[i].NeuronID());
}
fprintf(a_file, "InnovationDatabaseEnd\n\n");
}
// Checks the database if the innovation has already occured
// Returns the innovation id if true or -1 if false
// If it is a NEW_LINK innovation, in & out specify the neuron IDs being connected
// If it is a NEW_NEURON innovation, in & out specify the connection that was split
int InnovationDatabase::CheckInnovation(int a_In, int a_Out, InnovationType a_Type) const
{
ASSERT((a_In > 0) && (a_Out > 0));
ASSERT((a_Type == NEW_NEURON) || (a_Type == NEW_LINK));
// search the list for a match
for(unsigned int i=0; i < m_Innovations.size(); i++)
{
if ((m_Innovations[i].FromNeuronID() == a_In) && (m_Innovations[i].ToNeuronID() == a_Out) && (m_Innovations[i].InnovType() == a_Type))
{
// match found?
return m_Innovations[i].ID();
}
}
// not found
return -1;
}
int InnovationDatabase::CheckLastInnovation(int a_In, int a_Out, InnovationType a_Type) const
{
ASSERT((a_In > 0) && (a_Out > 0));
ASSERT((a_Type == NEW_NEURON) || (a_Type == NEW_LINK));
int t_ID = -1;
// search the list for a match
for(unsigned int i=0; i < m_Innovations.size(); i++)
{
if ((m_Innovations[i].FromNeuronID() == a_In) && (m_Innovations[i].ToNeuronID() == a_Out) && (m_Innovations[i].InnovType() == a_Type))
{
// match found?
t_ID = m_Innovations[i].ID();
}
}
return t_ID;
}
// returns a list of indexes in the database of identical innovations
std::vector<int> InnovationDatabase::CheckAllInnovations(int a_In, int a_Out, InnovationType a_Type) const
{
ASSERT((a_In > 0) && (a_Out > 0));
ASSERT((a_Type == NEW_NEURON) || (a_Type == NEW_LINK));
std::vector<int> t_idxs;
t_idxs.clear();
// search the list for a match
for(unsigned int i=0; i < m_Innovations.size(); i++)
{
if ((m_Innovations[i].FromNeuronID() == a_In) && (m_Innovations[i].ToNeuronID() == a_Out) && (m_Innovations[i].InnovType() == a_Type))
{
// match found?
t_idxs.push_back( i );
}
}
return t_idxs;
}
// Returns the neuron ID given the in and out neurons
// If not found, returns -1
int InnovationDatabase::FindNeuronID(int a_In, int a_Out) const
{
ASSERT((a_In > 0) && (a_Out > 0));
// search the list for a match
for(unsigned int i=0; i < m_Innovations.size(); i++)
{
if ((m_Innovations[i].FromNeuronID() == a_In) && (m_Innovations[i].ToNeuronID() == a_Out) && (m_Innovations[i].InnovType() == NEW_NEURON))
{
// match found?
return m_Innovations[i].NeuronID();
}
}
// Not found
return -1;
}
int InnovationDatabase::FindLastNeuronID(int a_In, int a_Out) const
{
ASSERT((a_In > 0) && (a_Out > 0));
int t_ID = -1;
// search the list for a match
for(unsigned int i=0; i < m_Innovations.size(); i++)
{
if ((m_Innovations[i].FromNeuronID() == a_In) && (m_Innovations[i].ToNeuronID() == a_Out) && (m_Innovations[i].InnovType() == NEW_NEURON))
{
// match found?
t_ID = m_Innovations[i].NeuronID();
}
}
return t_ID;
}
// Adds a new link innovation and returns its ID
// Increments the m_NextInnovationNum internally
int InnovationDatabase::AddLinkInnovation(int a_In, int a_Out)
{
ASSERT((a_In > 0) && (a_Out > 0));
m_Innovations.push_back( Innovation(m_NextInnovationNum, NEW_LINK, a_In, a_Out, NONE, -1) );
m_NextInnovationNum++;
return (m_NextInnovationNum - 1);
}
// Adds a new neuron innovation and returns the new neuron ID
// in and out specify the connection that was split
// type specifies the type of neuron
// Increments the m_NextNeuronID and m_NextInnovationNum internally
int InnovationDatabase::AddNeuronInnovation(int a_In, int a_Out, NeuronType a_NType)
{
ASSERT((a_In > 0) && (a_Out > 0));
ASSERT(!((a_NType == INPUT) || (a_NType == BIAS) || (a_NType == OUTPUT)));
m_Innovations.push_back( Innovation(m_NextInnovationNum, NEW_NEURON, a_In, a_Out, a_NType, m_NextNeuronID) );
m_NextInnovationNum++;
m_NextNeuronID++;
return (m_NextNeuronID - 1);
}
// Clears all innovations in the database
void InnovationDatabase::Flush()
{
m_Innovations.clear();
}
} // namespace NEAT