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

NodeLayer::NodeLayer(unsigned inputCount, unsigned nodeCount, NodeClass nodeClass, bool zeroed)
:   m_inputCount(inputCount), m_nodeCount(nodeCount), m_nodeClass(nodeClass)
{
    if (nodeClass == NEURON)
    {
        while (nodeCount--)
        {
            m_nodes.push_back(std::unique_ptr<Node>(new Neuron(inputCount, zeroed)));
        }
    }
    else
    {
        while (nodeCount--)
        {
            m_nodes.push_back(std::unique_ptr<Node>(new MemCell(inputCount, zeroed)));
        }
    }
}

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

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

    chrSize /= m_nodeCount;

    for (unsigned i = 0; i < m_nodeCount; ++i)
    {
        auto l1 = chromosome.begin() + i * chrSize;
        auto l2 = l1 + chrSize;
        Chromosome chrPart(l1, l2);
        m_nodes[i]->setChromosome(chrPart);
    }
}

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

    for (auto &i : m_nodes)
    {
        Chromosome chrPart = i->getChromosome();
        chromosome.insert(chromosome.end(), chrPart.begin(), chrPart.end());
    }

    return chromosome;
}

unsigned NodeLayer::getChromosomeSize() const
{
    return m_nodes.front()->getChromosomeSize() * m_nodeCount;
}

std::vector<float> NodeLayer::io(const std::vector<float> &inputs)
{
    std::vector<float> response;

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

    for (auto &i : m_nodes)
    {
        response.push_back(i->io(inputs));
    }

    return response;
}