-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCInnovation.cpp
More file actions
179 lines (138 loc) · 4.89 KB
/
CInnovation.cpp
File metadata and controls
179 lines (138 loc) · 4.89 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
#include "CInnovation.h"
//---------------------------------- ctor --------------------------------
//
// given a series of start genes and start neurons this ctor adds
// all the appropriate innovations.
//------------------------------------------------------------------------
CInnovation::CInnovation(vector<SLinkGene> start_genes,
vector<SNeuronGene> start_neurons)
{
m_NextNeuronID = 0;
m_NextInnovationNum = 0;
//add the neurons
for (int nd=0; nd<start_neurons.size(); ++nd)
{
m_vecInnovs.push_back(SInnovation(start_neurons[nd],
m_NextInnovationNum++,
m_NextNeuronID++));
}
//add the links
for (int cGen = 0; cGen<start_genes.size(); ++cGen)
{
SInnovation NewInnov(start_genes[cGen].FromNeuron,
start_genes[cGen].ToNeuron,
new_link,
m_NextInnovationNum);
m_vecInnovs.push_back(NewInnov);
++m_NextInnovationNum;
}
}
//---------------------------CheckInnovation------------------------------
//
// checks to see if this innovation has already occurred. If it has it
// returns the innovation ID. If not it returns a negative value.
//------------------------------------------------------------------------
int CInnovation::CheckInnovation(int in, int out, innov_type type)
{
//iterate through the innovations looking for a match on all
//three parameters
for (int inv=0; inv<m_vecInnovs.size(); ++inv)
{
if ( (m_vecInnovs[inv].NeuronIn == in) &&
(m_vecInnovs[inv].NeuronOut == out) &&
(m_vecInnovs[inv].InnovationType == type))
{
//found a match so assign this innovation number to id
return m_vecInnovs[inv].InnovationID;
}
}
//if no match return a negative value
return -1;
}
//--------------------------CreateNewInnovation---------------------------
//
// creates a new innovation and returns its ID
//------------------------------------------------------------------------
int CInnovation::CreateNewInnovation(int in, int out, innov_type type)
{
SInnovation new_innov(in, out, type, m_NextInnovationNum);
if (type == new_neuron)
{
new_innov.NeuronID = m_NextNeuronID;
++m_NextNeuronID;
}
m_vecInnovs.push_back(new_innov);
++m_NextInnovationNum;
return (m_NextNeuronID-1);
}
//------------------------------------------------------------------------
//
// as above but includes adding x/y position of new neuron
//------------------------------------------------------------------------
int CInnovation::CreateNewInnovation(int from,
int to,
innov_type InnovType,
neuron_type NeuronType,
double x,
double y)
{
SInnovation new_innov(from, to, InnovType, m_NextInnovationNum, NeuronType, x, y);
if (InnovType == new_neuron)
{
new_innov.NeuronID = m_NextNeuronID;
++m_NextNeuronID;
}
m_vecInnovs.push_back(new_innov);
++m_NextInnovationNum;
return (m_NextNeuronID-1);
}
//------------------------------- CreateNeuronFromID -----------------------
//
// given a neuron ID this function returns a clone of that neuron
//------------------------------------------------------------------------
SNeuronGene CInnovation::CreateNeuronFromID(int NeuronID)
{
SNeuronGene temp(hidden,0,0,0);
for (int inv=0; inv<m_vecInnovs.size(); ++inv)
{
if (m_vecInnovs[inv].NeuronID == NeuronID)
{
temp.NeuronType = m_vecInnovs[inv].NeuronType;
temp.iID = m_vecInnovs[inv].NeuronID;
temp.dSplitY = m_vecInnovs[inv].dSplitY;
temp.dSplitX = m_vecInnovs[inv].dSplitX;
return temp;
}
}
return temp;
}
//--------------------------------- Write --------------------------------
//
// writes the current innovations to a stream
//------------------------------------------------------------------------
bool CInnovation::Write(char* szFileName, const int gen)
{
ofstream stream(szFileName);
if (!stream) return false; //error
vector<SInnovation>::iterator curInnov;
stream<< "\n\n--------------------------------------- Generation: " << gen
<< " -------------------------------g-------\n";
for (curInnov = m_vecInnovs.begin(); curInnov != m_vecInnovs.end(); ++curInnov)
{
stream << "\nInnovationID: " << setw(3) << curInnov->InnovationID
<< " in: " << setw(3) << curInnov->NeuronIn
<< " out: " << setw(3) << curInnov->NeuronOut << " NeuronID: "
<< setw(3) << curInnov->NeuronID << " Type: ";
if (curInnov->InnovationType == new_link)
{
stream << "new_link";
}
else
{
stream << "NewNeuron";
}
stream<<" NeuronType: " << curInnov->NeuronType;
}//next innovation
stream<< "\n\n";
return true;
}