-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphenotype.cpp
More file actions
420 lines (321 loc) · 11.4 KB
/
phenotype.cpp
File metadata and controls
420 lines (321 loc) · 11.4 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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include "phenotype.h"
//------------------------------------Sigmoid function------------------------
//
//----------------------------------------------------------------------------
float Sigmoid(float netinput, float response)
{
return ( 1 / ( 1 + exp(-netinput / response)));
}
//--------------------------------- ctor ---------------------------------
//
//------------------------------------------------------------------------
CNeuralNet::CNeuralNet(vector<SNeuron*> neurons,
int depth):m_vecpNeurons(neurons),
m_iDepth(depth)
{}
//--------------------------------- dtor ---------------------------------
//
//------------------------------------------------------------------------
CNeuralNet::~CNeuralNet()
{
//delete any live neurons
for (int i=0; i<m_vecpNeurons.size(); ++i)
{
if (m_vecpNeurons[i])
{
delete m_vecpNeurons[i];
m_vecpNeurons[i] = NULL;
}
}
}
//----------------------------------Update--------------------------------
// takes a list of doubles as inputs into the network then steps through
// the neurons calculating each neurons next output.
//
// finally returns a std::vector of doubles as the output from the net.
//------------------------------------------------------------------------
vector<double> CNeuralNet::Update(const vector<double> &inputs,
const run_type type)
{
//create a vector to put the outputs into
vector<double> outputs;
//if the mode is snapshot then we require all the neurons to be
//iterated through as many times as the network is deep. If the
//mode is set to active the method can return an output after
//just one iteration
int FlushCount = 0;
if (type == snapshot)
{
FlushCount = m_iDepth;
}
else
{
FlushCount = 1;
}
//iterate through the network FlushCount times
for (int i=0; i<FlushCount; ++i)
{
//clear the output vector
outputs.clear();
//this is an index into the current neuron
int cNeuron = 0;
//first set the outputs of the 'input' neurons to be equal
//to the values passed into the function in inputs
while (m_vecpNeurons[cNeuron]->NeuronType == input)
{
m_vecpNeurons[cNeuron]->dOutput = inputs[cNeuron];
++cNeuron;
}
//set the output of the bias to 1
m_vecpNeurons[cNeuron++]->dOutput = 1;
//then we step through the network a neuron at a time
while (cNeuron < m_vecpNeurons.size())
{
//this will hold the sum of all the inputs x weights
double sum = 0;
//sum this neuron's inputs by iterating through all the links into
//the neuron
for (int lnk=0; lnk<m_vecpNeurons[cNeuron]->vecLinksIn.size(); ++lnk)
{
//get this link's weight
double Weight = m_vecpNeurons[cNeuron]->vecLinksIn[lnk].dWeight;
//get the output from the neuron this link is coming from
double NeuronOutput =
m_vecpNeurons[cNeuron]->vecLinksIn[lnk].pIn->dOutput;
//add to sum
sum += Weight * NeuronOutput;
}
//now put the sum through the activation function and assign the
//value to this neuron's output
m_vecpNeurons[cNeuron]->dOutput =
Sigmoid(sum, m_vecpNeurons[cNeuron]->dActivationResponse);
if (m_vecpNeurons[cNeuron]->NeuronType == output)
{
//add to our outputs
outputs.push_back(m_vecpNeurons[cNeuron]->dOutput);
}
//next neuron
++cNeuron;
}
}//next iteration through the network
#if 1
//the network needs to be flushed if this type of update is performed otherwise
//it is possible for dependencies to be built on the order the training data is
//presented
if (type == snapshot)
{
for (int n=0; n<m_vecpNeurons.size(); ++n)
{
m_vecpNeurons[n]->dOutput = 0;
}
}
#endif
//return the outputs
return outputs;
}
//----------------------------- TidyXSplits -----------------------------
//
// This is a fix to prevent neurons overlapping when they are displayed
//-----------------------------------------------------------------------
void TidyXSplits(vector<SNeuron*> &neurons)
{
//stores the index of any neurons with identical splitY values
vector<int> SameLevelNeurons;
//stores all the splitY values already checked
vector<double> DepthsChecked;
//for each neuron find all neurons of identical ySplit level
for (int n=0; n<neurons.size(); ++n)
{
double ThisDepth = neurons[n]->dSplitY;
//check to see if we have already adjusted the neurons at this depth
bool bAlreadyChecked = false;
for (int i=0; i<DepthsChecked.size(); ++i)
{
if (DepthsChecked[i] == ThisDepth)
{
bAlreadyChecked = true;
break;
}
}
//add this depth to the depths checked.
DepthsChecked.push_back(ThisDepth);
//if this depth has not already been adjusted
if (!bAlreadyChecked)
{
//clear this storage and add the neuron's index we are checking
SameLevelNeurons.clear();
SameLevelNeurons.push_back(n);
//find all the neurons with this splitY depth
for (int i=n+1; i<neurons.size(); ++i)
{
if (neurons[i]->dSplitY == ThisDepth)
{
//add the index to this neuron
SameLevelNeurons.push_back(i);
}
}
//calculate the distance between each neuron
double slice = 1.0/(SameLevelNeurons.size()+1);
//separate all neurons at this level
for (int i=0; i<SameLevelNeurons.size(); ++i)
{
int idx = SameLevelNeurons[i];
neurons[idx]->dSplitX = (i+1) * slice;
}
}
}//next neuron to check
}
//----------------------------- DrawNet ----------------------------------
//
// creates a representation of the ANN on a device context
//
//------------------------------------------------------------------------
void CNeuralNet::DrawNet(HDC &surface, int Left, int Right, int Top, int Bottom)
{
//the border width
const int border = 10;
//max line thickness
const int MaxThickness = 5;
TidyXSplits(m_vecpNeurons);
//go through the neurons and assign x/y coords
int spanX = Right - Left;
int spanY = Top - Bottom - (2*border);
for (int cNeuron=0; cNeuron<m_vecpNeurons.size(); ++cNeuron)
{
m_vecpNeurons[cNeuron]->iPosX = Left + spanX*m_vecpNeurons[cNeuron]->dSplitX;
m_vecpNeurons[cNeuron]->iPosY = (Top - border) - (spanY * m_vecpNeurons[cNeuron]->dSplitY);
}
//create some pens and brushes to draw with
HPEN GreyPen = CreatePen(PS_SOLID, 1, RGB(200, 200, 200));
HPEN RedPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
HPEN GreenPen = CreatePen(PS_SOLID, 1, RGB(0, 200, 0));
HPEN OldPen = NULL;
//create a solid brush
HBRUSH RedBrush = CreateSolidBrush(RGB(255, 0, 0));
HBRUSH OldBrush = NULL;
OldPen = (HPEN) SelectObject(surface, RedPen);
OldBrush = (HBRUSH)SelectObject(surface, GetStockObject(HOLLOW_BRUSH));
//radius of neurons
int radNeuron = spanX/60;
int radLink = radNeuron * 1.5;
//now we have an X,Y pos for every neuron we can get on with the
//drawing. First step through each neuron in the network and draw
//the links
for (int cNeuron=0; cNeuron<m_vecpNeurons.size(); ++cNeuron)
{
//grab this neurons position as the start position of each
//connection
int StartX = m_vecpNeurons[cNeuron]->iPosX;
int StartY = m_vecpNeurons[cNeuron]->iPosY;
//is this a bias neuron? If so, draw the link in green
bool bBias = false;
if (m_vecpNeurons[cNeuron]->NeuronType == bias)
{
bBias = true;
}
//now iterate through each outgoing link to grab the end points
for (int cLnk=0; cLnk<m_vecpNeurons[cNeuron]->vecLinksOut.size(); ++ cLnk)
{
int EndX = m_vecpNeurons[cNeuron]->vecLinksOut[cLnk].pOut->iPosX;
int EndY = m_vecpNeurons[cNeuron]->vecLinksOut[cLnk].pOut->iPosY;
//if link is forward draw a straight line
if( (!m_vecpNeurons[cNeuron]->vecLinksOut[cLnk].bRecurrent) && !bBias)
{
int thickness = (int)(fabs(m_vecpNeurons[cNeuron]->vecLinksOut[cLnk].dWeight));
Clamp(thickness, 0, MaxThickness);
HPEN Pen;
//create a yellow pen for inhibitory weights
if (m_vecpNeurons[cNeuron]->vecLinksOut[cLnk].dWeight <= 0)
{
Pen = CreatePen(PS_SOLID, thickness, RGB(240, 230, 170));
}
//grey for excitory
else
{
Pen = CreatePen(PS_SOLID, thickness, RGB(200, 200, 200));
}
HPEN tempPen = (HPEN)SelectObject(surface, Pen);
//draw the link
MoveToEx(surface, StartX, StartY, NULL);
LineTo(surface, EndX, EndY);
SelectObject(surface, tempPen);
DeleteObject(Pen);
}
else if( (!m_vecpNeurons[cNeuron]->vecLinksOut[cLnk].bRecurrent) && bBias)
{
SelectObject(surface, GreenPen);
//draw the link
MoveToEx(surface, StartX, StartY, NULL);
LineTo(surface, EndX, EndY);
}
//recurrent link draw in red
else
{
if ((StartX == EndX) && (StartY == EndY))
{
int thickness = (int)(fabs(m_vecpNeurons[cNeuron]->vecLinksOut[cLnk].dWeight));
Clamp(thickness, 0, MaxThickness);
HPEN Pen;
//blue for inhibitory
if (m_vecpNeurons[cNeuron]->vecLinksOut[cLnk].dWeight <= 0)
{
Pen = CreatePen(PS_SOLID, thickness, RGB(0,0,255));
}
//red for excitory
else
{
Pen = CreatePen(PS_SOLID, thickness, RGB(255, 0, 0));
}
HPEN tempPen = (HPEN)SelectObject(surface, Pen);
//we have a recursive link to the same neuron draw an ellipse
int x = m_vecpNeurons[cNeuron]->iPosX ;
int y = m_vecpNeurons[cNeuron]->iPosY - (1.5 * radNeuron);
Ellipse(surface, x-radLink, y-radLink, x+radLink, y+radLink);
SelectObject(surface, tempPen);
DeleteObject(Pen);
}
else
{
int thickness = (int)(fabs(m_vecpNeurons[cNeuron]->vecLinksOut[cLnk].dWeight));
Clamp(thickness, 0, MaxThickness);
HPEN Pen;
//blue for inhibitory
if (m_vecpNeurons[cNeuron]->vecLinksOut[cLnk].dWeight <= 0)
{
Pen = CreatePen(PS_SOLID, thickness, RGB(0,0,255));
}
//red for excitory
else
{
Pen = CreatePen(PS_SOLID, thickness, RGB(255, 0, 0));
}
HPEN tempPen = (HPEN)SelectObject(surface, Pen);
//draw the link
MoveToEx(surface, StartX, StartY, NULL);
LineTo(surface, EndX, EndY);
SelectObject(surface, tempPen);
DeleteObject(Pen);
}
}
}
}
//now draw the neurons and their IDs
SelectObject(surface, RedBrush);
SelectObject(surface, GetStockObject(BLACK_PEN));
for (int cNeuron=0; cNeuron<m_vecpNeurons.size(); ++cNeuron)
{
int x = m_vecpNeurons[cNeuron]->iPosX;
int y = m_vecpNeurons[cNeuron]->iPosY;
//display the neuron
Ellipse(surface, x-radNeuron, y-radNeuron, x+radNeuron, y+radNeuron);
}
//cleanup
SelectObject(surface, OldPen);
SelectObject(surface, OldBrush);
DeleteObject(RedPen);
DeleteObject(GreyPen);
DeleteObject(GreenPen);
DeleteObject(OldPen);
DeleteObject(RedBrush);
DeleteObject(OldBrush);
}