-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuron.cpp
62 lines (56 loc) · 1.33 KB
/
neuron.cpp
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
#include "neuron.h"
Neuron::Neuron()
{
inputNeurons.clear();
outputNeurons.clear();
inputWeights.clear();
}
Neuron::~Neuron()
{
//Unlink neurons
for (auto &neuron:inputNeurons)
neuron->unlinkFrom(this);
for (auto &neuron:outputNeurons)
this->unlinkFrom(neuron);
}
/*
* Connects neuron to another one
*/
void Neuron::linkTo(Neuron* neuron)
{
//Link output
outputNeurons.push_back(neuron);
//Link input
neuron->inputNeurons.push_back(this);
neuron->inputWeights.push_back(Utils::getRandomDouble());
}
/*
* Disconnects neuron from another one
*/
void Neuron::unlinkFrom(Neuron* neuron)
{
//Unlink output
int i;
int foundIndex{-1};
for (i = 0; i < outputNeurons.size(); ++i)
if (outputNeurons[i] == neuron)
{
foundIndex = i;
break;
}
if (foundIndex != -1)
outputNeurons.erase(outputNeurons.begin() + foundIndex);
//Unlink input
foundIndex = -1;
for (i = 0; i < neuron->inputNeurons.size(); ++i)
if (neuron->inputNeurons[i] == this)
{
foundIndex = i;
break;
}
if (foundIndex != -1)
{
neuron->inputNeurons.erase(neuron->inputNeurons.begin() + foundIndex);
neuron->inputWeights.erase(neuron->inputWeights.begin() + foundIndex);
}
}