-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuralnetwork.cpp
51 lines (43 loc) · 1.43 KB
/
neuralnetwork.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
#include "neuralnetwork.h"
#include <QDebug>
NeuralNetwork::NeuralNetwork(int countHiddenLayers, int countInput, int countOutput, int countHidden)
{
this->countHiddenLayers = countHiddenLayers;
this->countInput = countInput;
this->countOutput = countOutput;
this->countHidden = countHidden;
hidden.append(NeuronLayer(this->countHidden, countInput));
for(int i = 1; i < this->countHiddenLayers; i++){
hidden.append(NeuronLayer(this->countHidden, countHidden));
}
output = NeuronLayer(countOutput, countHidden);
}
Action NeuralNetwork::think(double leftDist, double rightDist, double accelerate)
{
QVector<double> inputs, outputs;
inputs.append(leftDist);
inputs.append(rightDist);
inputs.append(accelerate);
for(int i = 0; i < countHiddenLayers; i++){
outputs = hidden[i].outputs(inputs);
inputs = outputs;
}
outputs = output.outputs(inputs);
double maxValue = outputs.at(0);
int ind = 0;
for(int i = 0; i < countOutput; i++){
if(maxValue > outputs.at(i)){
ind = i;
maxValue = outputs.at(i);
}
}
return Action(ind);
}
NeuralNetwork NeuralNetwork::merge(const NeuralNetwork& other) {
NeuralNetwork res = *this;
for (int i = 0; i < hidden.size(); i++) {
res.hidden[i] = res.hidden[i].merge(other.hidden[i]);
}
res.output = res.output.merge(other.output);
return res;
}