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

double Neuron::sigmoid(double input) const
{
    const double slope = 4.924273;
    return (1/(1+(std::exp(-(slope*input)))));
}

bool Neuron::calculateActivation()
{
    double newActivation = 0;
    for (auto const& link : _inputLinks)
    {
        newActivation += link.weightedActivation();
    }
    newActivation = sigmoid(newActivation);

    const double errorMargin = 0.000001;
    bool activationChanged = std::abs(newActivation - _activation) > errorMargin;
    _activation = newActivation;
    return activationChanged;
}

void Neuron::addInput(NeuralLink&& link)
{
    _inputLinks.push_back(std::move(link));
}

bool Neuron::hasInputWithWeight(std::string srcIdentifier, double weight) const
{
    for (auto const& link : _inputLinks)
    {
        if (link.inputIdentifier() == srcIdentifier && link.weight() == weight)
        {
            return true;
        }
    }
    return false;
}