aboutsummaryrefslogtreecommitdiff
path: root/src/NeuralNet.cpp
blob: abed262ae84ddc0836d6ce0963b788420547b1e9 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <cmath>
#include <algorithm>
#include <HyperNeat/Cppn.hpp>
#include <HyperNeat/NeuralNet.hpp>
#include <HyperNeat/Utils/Map.hpp>
#include <HyperNeat/Utils/Set.hpp>
#include <HyperNeat/NeuralNetPrms.hpp>
#include <HyperNeat/Utils/Function.hpp>
#include <HyperNeat/NodeSearchPrms.hpp>

using namespace std;
using namespace hyperneat;

void
NeuralNet::create(Cppn& cppn, const NeuralNetPrms& nnPrms)
{
    class TempNeuronSynapse {
    public:
        TempNeuronSynapse() = default;
        TempNeuronSynapse(double weight, Point neuronSource)
            : _weight(weight), _neuronSource(neuronSource)
        {}

        double _weight = 0.0;
        Point  _neuronSource;
    };

    class TempNeuron {
    public:
        bool                      _isIncluded = false;
        double                    _bias       = 0.0;
        Vector<TempNeuronSynapse> _neuronSynapses;
    };

    _inputs.reserve(nnPrms._inputMap.size());
    _outputs.reserve(nnPrms._outputMap.size());

    NodeSearchPrms nsPrms(0, 2, 3, 4);
    nsPrms.importFrom(nnPrms);
    cppn.inputAt(5) = 1.0;

    Map<Point, TempNeuron> tempNeurons;

    auto findConnections = [&](const Point& source, size_t x1, size_t y1, size_t x2, size_t y2, size_t d,
            bool checkExist, Function<void(double, const Point&)> storeConn) {
        cppn.inputAt(x1) = source._x;
        cppn.inputAt(y1) = source._y;
        cppn.inputAt(x2) = 0.0;
        cppn.inputAt(y2) = 0.0;
        cppn.inputAt(d)  = 0.0;

        ValueMap newConnections;
        cppn.findNodesIn2DSection(newConnections, nsPrms, source);

        for (auto& i : newConnections) {
            if (checkExist && !tempNeurons.count(i)) {
                continue;
            }

            if (fabs(i._value) > 0.2) {
                storeConn((i._value + (i._value > 0 ? -0.2 : 0.2)) * 3.75, i);
            }
        }
    };

    {
        Set<Point>  neuronSet1;
        Set<Point>  neuronSet2;
        Set<Point>* previousNeurons = &neuronSet1;
        Set<Point>* nextNeurons     = &neuronSet2;

        for (auto& i : nnPrms._inputMap) {
            tempNeurons[i];
            previousNeurons->insert(i);
        }

        for (size_t i = 0; i < nnPrms._iterations && !previousNeurons->empty(); ++i) {
            Map<Point, TempNeuron> newNeurons;

            for (auto& j : *previousNeurons) {
                findConnections(j, 0, 1, 2, 3, 4, false, [&](double weight, const Point& target) {
                    if (tempNeurons.count(target)) {
                        auto& synapses = tempNeurons[target]._neuronSynapses;
                        synapses.emplace_back(weight, j);
                    } else {
                        auto& synapses = newNeurons[target]._neuronSynapses;
                        synapses.emplace_back(weight, j);
                        nextNeurons->insert(target);
                    }
                });
            }

            previousNeurons->clear();
            swap(nextNeurons, previousNeurons);
            tempNeurons.insert(newNeurons.begin(), newNeurons.end());
        }
    }

    nsPrms._x = 0;
    nsPrms._y = 1;

    {
        Vector<TempNeuron*>  inclusionSet1;
        Vector<TempNeuron*>  inclusionSet2;
        Vector<TempNeuron*>* crntInclusions = &inclusionSet1;
        Vector<TempNeuron*>* nextInclusions = &inclusionSet2;

        for (auto& i : nnPrms._outputMap) {
            tempNeurons[i]._isIncluded = true;
            nextInclusions->push_back(&tempNeurons[i]);

            findConnections(i, 2, 3, 0, 1, 4, true, [&](double weight, const Point& target) {
                auto& synapses = tempNeurons[i]._neuronSynapses;
                synapses.emplace_back(weight, target);
            });
        }

        while (!nextInclusions->empty()) {
            crntInclusions->clear();
            swap(crntInclusions, nextInclusions);

            for (auto& i : *crntInclusions) {
                for (auto& j : i->_neuronSynapses) {
                    auto& sourceNeuron = tempNeurons.at(j._neuronSource);

                    if (!sourceNeuron._isIncluded) {
                        nextInclusions->push_back(&sourceNeuron);
                        sourceNeuron._isIncluded = true;
                    }
                }
            }
        }
    }

    for (auto& i : nnPrms._inputMap) {
        tempNeurons[i]._isIncluded = true;
    }

    cppn.inputAt(2) = 0.0;
    cppn.inputAt(3) = 0.0;
    cppn.inputAt(4) = 0.0;

    for (auto i = tempNeurons.begin(), end = tempNeurons.end(); i != end;) {
        if (i->second._isIncluded) {
            cppn.inputAt(0) = i->first._x;
            cppn.inputAt(1) = i->first._y;
            cppn.inputAt(4) = i->first.distance(Point());
            cppn.cycle();
            i->second._bias = cppn.outputAt(1) * 3.0;
            ++i;
        } else {
            i = tempNeurons.erase(i);
        }
    }

    _neurons.resize(tempNeurons.size());

    {
        auto nIter = _neurons.begin();

        for (auto& i : tempNeurons) {
            nIter->_bias         = i.second._bias;
            nIter->_position     = i.first;
            auto& crntNrnSyns    = nIter->_synapses;
            auto& neuronSynapses = i.second._neuronSynapses;
            crntNrnSyns.reserve(neuronSynapses.size());

            for (auto& j : neuronSynapses) {
                auto src    = tempNeurons.find(j._neuronSource);
                size_t sIdx = distance(tempNeurons.begin(), src);
                crntNrnSyns.emplace_back(&_neurons[sIdx], j._weight);
            }

            ++nIter;
        }
    }

    auto relateIO = [&](Vector<double*>& ptrVec, const Vector<Point>& map, Neuron::Type type) {
        for (auto& i : map) {
            auto neuron          = tempNeurons.find(i);
            size_t nIdx          = distance(tempNeurons.begin(), neuron);
            _neurons[nIdx]._type = type;
            ptrVec.push_back(&_neurons[nIdx]._output);
        }
    };

    relateIO(_inputs, nnPrms._inputMap, Neuron::Type::INPUT);
    relateIO(_outputs, nnPrms._outputMap, Neuron::Type::OUTPUT);
}

void
NeuralNet::clear()
{
    _inputs.clear();
    _outputs.clear();
    _neurons.clear();
}

size_t
NeuralNet::getInputsCount() const
{
    return _inputs.size();
}

size_t
NeuralNet::getOutputsCount() const
{
    return _outputs.size();
}

size_t
NeuralNet::getNeuronsCount() const
{
    return _neurons.size();
}

double
NeuralNet::getAverageActivation() const
{
    double totalActivation = 0.0;

    for (auto& i : _neurons) {
        totalActivation += i._output;
    }

    return totalActivation / static_cast<double>(_neurons.size());
}

double&
NeuralNet::inputAt(size_t i)
{
    return *_inputs[i];
}

double
NeuralNet::outputAt(size_t i) const
{
    return *_outputs[i];
}

const Vector<double*>&
NeuralNet::getInputs() const
{
    return _inputs;
}

const Vector<double*>&
NeuralNet::getOutputs() const
{
    return _outputs;
}

const Vector<NeuralNet::Neuron>&
NeuralNet::getNeurons() const
{
    return _neurons;
}

void
NeuralNet::cycle()
{
    for (auto& i : _neurons) {
        i.appendInput();
    }

    for (auto& i : _neurons) {
        i.flushOutput();
    }
}

NeuralNet::Neuron::Neuron(const Point& position, Type type, double bias)
    : _position(position), _type(type), _bias(bias)
{}

void
NeuralNet::Neuron::appendInput()
{
    for (auto& i : _synapses) {
        _storedInput += *i._input * i._weight;
    }

    _storedInput += _bias;
}

void
NeuralNet::Neuron::flushOutput()
{
    _output      = 1.0 / (1.0 + exp(-_storedInput * 4.9));
    _storedInput = 0.0;
}

NeuralNet::Neuron::Synapse::Synapse(Neuron* inputNeuron, double weight)
    : _input(&inputNeuron->_output), _neuron(inputNeuron), _weight(weight)
{}