Newer
Older

Michal Kravcenko
committed
/**
* DESCRIPTION OF THE FILE
*
* @author Michal Kravčenko
* @date 13.6.18 -
*/
#include "NeuralNetwork.h"

Michal Kravcenko
committed
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
NeuralNetwork::NeuralNetwork() {
this->neurons = new std::vector<Neuron*>(0);
}
NeuralNetwork::~NeuralNetwork() {
if(this->neurons){
delete this->neurons;
this->neurons = nullptr;
}
if(this->output_neurons){
delete this->output_neurons;
this->output_neurons = nullptr;
}
if(this->input_neurons){
delete this->input_neurons;
this->input_neurons = nullptr;
}
if(this->active_eval_set){
delete this->active_eval_set;
this->active_eval_set = nullptr;
}
}
void NeuralNetwork::add_neuron(Neuron *n) {
this->neurons->push_back(n);
this->in_out_determined = false;
}
void NeuralNetwork::determine_inputs_outputs() {
if(this->output_neurons){
delete this->output_neurons;
}
if(this->input_neurons){
delete this->input_neurons;
}
this->output_neurons = new std::vector<Neuron*>(0);
this->input_neurons = new std::vector<Neuron*>(0);
if(this->active_eval_set == nullptr){
this->active_eval_set = new std::vector<Neuron*>(this->neurons->size() * 2);
}
else{
this->active_eval_set->resize(this->neurons->size() * 2);
}
for(Neuron* neuron: *this->neurons){
if(neuron->get_connections_out()->empty()){
//this neuron has no outgoing connections, it is the output neuron
this->output_neurons->push_back(neuron);
}
else if(neuron->get_connections_in()->empty()){
//this neuron has no incoming connections, it is the input neuron
this->input_neurons->push_back(neuron);
}
}
this->n_inputs = (int)this->input_neurons->size();
this->n_outputs = (int)this->output_neurons->size();
this->in_out_determined = true;
}
void NeuralNetwork::eval_single(std::vector<double> &input, std::vector<double> &output) {
if(!this->in_out_determined){
this->determine_inputs_outputs();
}
if(this->n_inputs != input.size()){
printf("Error, input size != Network input size\n");
return;
}
if(this->n_outputs != output.size()){
printf("Error, output size != Network output size\n");
return;
}
std::fill(output.begin(), output.end(), 0.0);
//reset of the potentials
for(Neuron* neuron: *this->neurons){
neuron->set_potential(0.0);
neuron->set_saturation_in(0);
neuron->set_saturation_out(0);
}
int active_set_size[2];
active_set_size[0] = 0;
active_set_size[1] = 0;
int idx1 = 0, idx2 = 1;
active_set_size[0] = this->n_inputs;
int i = 0;
auto n = this->neurons->size();
for(Neuron* neuron: *this->input_neurons){
this->active_eval_set->at(i) = neuron;
neuron->set_potential(input[i]);
++i;
}
Neuron* active_neuron;
Neuron* target_neuron;
while(active_set_size[idx1] > 0){
//we iterate through the active neurons and propagate the signal
for(i = 0; i < active_set_size[idx1]; ++i){
active_neuron = this->active_eval_set->at(i + n * idx1);
active_neuron->activate();
for(Connection* connection: *(active_neuron->get_connections_out())){
connection->pass_signal();
target_neuron = connection->get_neuron_out();
target_neuron->adjust_saturation_in(1);
if(target_neuron->is_saturated_in()){
this->active_eval_set->at(active_set_size[idx2] + n * idx2) = target_neuron;
active_set_size[idx2]++;
}
}
}
idx1 = idx2;
idx2 = (idx1 + 1) % 2;
active_set_size[idx2] = 0;
}
i = 0;
for(Neuron* neuron: *this->output_neurons){
output[i] = neuron->get_state();
++i;
}
}