summaryrefslogtreecommitdiff
path: root/2015-spacebot/src/brain/neural_network.cpp
blob: c8177e8132723d0a0018646ee4159852b54ee6a0 (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
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
#include "brain/neural_network.h"
#include "brain/neuron.h"
#include <limits>

NeuralNetwork::NeuralNetwork(std::istream &&networkConfigFile, unsigned int numberOfSensors, unsigned int numberOfOutputs)
{
    _neurons.reserve(400);
    _sensors.reserve(numberOfSensors);
    _outputs.reserve(numberOfOutputs);
    
    _biasNode = std::make_shared<BiasNode>();
    
    for (unsigned int i=0; i<numberOfSensors; ++i)
    {
        auto sensor = std::make_shared<Sensor>(i);
        _sensors.push_back(sensor);
    }
    for (unsigned int i=0; i<numberOfOutputs; ++i)
    {
        auto output = findOrAddNeuron(i);
        _outputs.push_back(output);
    }

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

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

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

void NeuralNetwork::parseFile(std::istream &&file)
{
    double weight;
    char srcType;
    unsigned int srcId;
    unsigned int destId;

    while (file.get(srcType) &&
           file >> srcId &&
           file.ignore(std::numeric_limits<std::streamsize>::max(), 'n') &&
           file >> destId &&
           file >> weight &&
           file.ignore(std::numeric_limits<std::streamsize>::max(), '\n'))
    {
        std::shared_ptr<NeuralNode> source;
        std::shared_ptr<Neuron> destination;
        switch (srcType)
        {
        case 's':
            source = findOrAddSensor(srcId);
            break;
        case 'b':
            source = _biasNode;
            break;
        default:
            source = findOrAddNeuron(srcId);
        }
        destination = findOrAddNeuron(destId);
	
        addLink(source, destination, weight);
    }
	 
}

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

std::shared_ptr<Sensor> NeuralNetwork::findOrAddSensor(unsigned int id)
{
    while (_sensors.size() <= id)
    {
        auto sensor = std::make_shared<Sensor>(_sensors.size());
        _sensors.push_back(sensor);
    }

    return _sensors.at(id);
}

std::shared_ptr<Neuron> NeuralNetwork::findOrAddNeuron(unsigned int id)
{
    while (_neurons.size() <= id)
    {
        auto neuron = std::make_shared<Neuron>(_neurons.size());
        _neurons.push_back(neuron);
    }

    return _neurons.at(id);
}

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

unsigned int NeuralNetwork::findMaxOutputIndex() const
{
    bool anyNodeChanged = true;
    auto maxIterations = _neurons.size()*10;
    for (unsigned int iteration=0; anyNodeChanged && iteration<maxIterations; ++iteration)
    {
        anyNodeChanged = false;
        for (auto const& neuron : _neurons)
        {
            bool activationChanged = neuron->calculateActivation();
            anyNodeChanged = anyNodeChanged || activationChanged;
        }
    }

    int currentMaxIndex = 0;
    double currentMaxActivation = _outputs.at(0)->activation();

    for (unsigned int i=1; i<_outputs.size(); ++i)
    {
        double activation = _outputs.at(i)->activation();
        if (activation >= currentMaxActivation)
        {
            currentMaxActivation = activation;
            currentMaxIndex = i;
        }
    }
    return currentMaxIndex;
}

bool NeuralNetwork::linkExists(std::string srcIdentifier, std::string destIdentifier, double weight) const
{
    std::shared_ptr<Neuron> dest;
    
    for (auto const& node : _neurons)
    {
        if (node->identifier() == destIdentifier)
        {
            dest = node;
        }
    }

    if (!dest)
    {
        return false;
    }
    return dest->hasInputWithWeight(srcIdentifier, weight);
}