summaryrefslogtreecommitdiff
path: root/src/brain/neural_network.cpp
blob: d6a5e15bdf4ec195d69167c17adcea6ccaff9a70 (plain)
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
#include "brain/neural_network.h"
#include "brain/neuron.h"

NeuralNetwork::NeuralNetwork(std::istream &&networkConfigFile, int numberOfSensors, int numberOfOutputs)
{
    _biasNode = std::make_shared<BiasNode>();
    
    for (int i=0; i<numberOfSensors; ++i)
    {
	_sensors[i] = std::make_shared<Sensor>(i);
    }
    for (int i=0; i<numberOfOutputs; ++i)
    {
	_outputs[i] = std::make_shared<Neuron>(i);
    }

    parseFile(std::move(networkConfigFile));
}

NeuralNetwork::NeuralNetwork(std::istream &&networkConfigFile, std::vector<bool> sensorInitialValues, int numberOfOutputs)
{
    _biasNode = std::make_shared<BiasNode>();
    
    for (int i=0; i<sensorInitialValues.size(); ++i)
    {
	auto sensor = std::make_shared<Sensor>(i);
	sensor->setActivation(sensorInitialValues.at(i) ? 1 : 0);
	_sensors[i] = sensor;
    }
    for (int i=0; i<numberOfOutputs; ++i)
    {
	_outputs[i] = std::make_shared<Neuron>(i);
    }

    parseFile(std::move(networkConfigFile));
}

void NeuralNetwork::parseFile(std::istream &&file)
{
    double weight;
    for (std::string src, dest;
	 file >> src && file >> dest && file >> weight; )
    {
	char srcType = src.at(0);
	int srcId = std::stoi(src.substr(1));
	char destType = dest.at(0);
	int destId = std::stoi(dest.substr(1));

	std::shared_ptr<NeuralNode> source;
	std::shared_ptr<Neuron> destination;
	switch (srcType)
	{
	case 's':
	    source = findOrAddSensor(srcId);
	    break;
	case 'b':
	    source = _biasNode;
	    break;
	case 'n':
	    source = findOrAddNeuron(srcId);
	    break;
	default:
	    throw 1;
	}
	switch (destType)
	{
	case 'n':
	    destination = findOrAddNeuron(destId);
	    break;
	default:
	    throw 1;
	}

	addLink(source, destination, weight);
    }
	 
}

void NeuralNetwork::addLink(std::shared_ptr<NeuralNode> source, std::shared_ptr<Neuron> destination, double weight)
{
    auto link = std::make_shared<NeuralLink>(source, weight);
    destination->addInput(link);
}

std::shared_ptr<Sensor> NeuralNetwork::findOrAddSensor(int id)
{
    bool sensorExists = _sensors.count(id) > 0;
    if (!sensorExists)
    {
	_sensors[id] = std::make_shared<Sensor>(id);
    }
    return _sensors.at(id);
}

std::shared_ptr<Neuron> NeuralNetwork::findOrAddNeuron(int id)
{
    bool isOutput = _outputs.count(id) > 0;
    if (isOutput)
    {
	return _outputs.at(id);
    }

    bool hiddenNeuronExists = _hiddenNodes.count(id) > 0;
    if (!hiddenNeuronExists)
    {
	_hiddenNodes[id] = std::make_shared<Neuron>(id);
    }
    return _hiddenNodes.at(id);
}

void NeuralNetwork::setInput(int inputIndex, double activation)
{
    _sensors.at(inputIndex)->setActivation(activation);
}

int NeuralNetwork::findMaxOutputIndex() const
{
    double currentMaxActivation = 0;
    int currentMaxIndex = 0;
    for (std::pair<const int, std::shared_ptr<Neuron>> outputPair : _outputs)
    {
	double activation = outputPair.second->activation();
	if (activation >= currentMaxActivation)
	{
	    currentMaxActivation = activation;
	    currentMaxIndex = outputPair.first;
	}
    }
    return currentMaxIndex;
}