aboutsummaryrefslogtreecommitdiff
path: root/Neuron.cpp
blob: 647d52510c3905bc0758b25087ddf42502d0f3a6 (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
#include "Neuron.hpp"

Neuron::Neuron(unsigned inputCount, bool zeroed)
{
    m_inputCount = inputCount;

    while (inputCount--)
    {
        m_weights.push_back(zeroed ? 0.f : realRand(-1.f, 1.f));
    }

    m_bias = zeroed ? 0.f : realRand(-1.f, 1.f);
}

void Neuron::setChromosome(const Chromosome &chromosome)
{
    unsigned chrSize = getChromosomeSize();

    if (chrSize != chromosome.size())
    {
        return;
    }

    m_weights = Chromosome(chromosome.begin(), chromosome.end() - 1);
    m_bias = chromosome.back();
}

Chromosome Neuron::getChromosome() const
{
    Chromosome chromosome;

    for (auto &i : m_weights)
    {
        chromosome.push_back(i);
    }

    chromosome.push_back(m_bias);

    return chromosome;
}

float Neuron::io(const std::vector<float> &inputs)
{
    float response = 0.f;

    if (inputs.size() != m_inputCount)
    {
        return response;
    }

    for (unsigned i = 0; i < m_inputCount; ++i)
    {
        response += inputs[i] * m_weights[i];
    }

    return sigmoid(response - m_bias);
}