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

SingleMLP::SingleMLP(unsigned inputCount, unsigned hiddenNodeCount, unsigned outputCount, NodeClass nodeClass, bool zeroed)
:   m_hiddenLayer(inputCount, hiddenNodeCount, nodeClass, zeroed),
    m_outputLayer(hiddenNodeCount, outputCount, nodeClass, zeroed)
{
    m_inputCount = inputCount;
    m_hiddenNodeCount = hiddenNodeCount;
    m_outputCount = outputCount;
    m_nodeClass = nodeClass;
}

void SingleMLP::setChromosome(const Chromosome &chromosome)
{
    if (chromosome.size() != getChromosomeSize())
    {
        return;
    }

    auto l1 = chromosome.begin() + m_hiddenLayer.getChromosomeSize();

    Chromosome chr1(chromosome.begin(), l1);
    Chromosome chr2(l1, chromosome.end());

    m_hiddenLayer.setChromosome(chr1);
    m_outputLayer.setChromosome(chr2);
}

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

    Chromosome chr1 = m_hiddenLayer.getChromosome();
    Chromosome chr2 = m_outputLayer.getChromosome();

    chromosome.insert(chromosome.end(), chr1.begin(), chr1.end());
    chromosome.insert(chromosome.end(), chr2.begin(), chr2.end());

    return chromosome;
}

unsigned SingleMLP::getChromosomeSize() const
{
    unsigned chrSize = 0;
    chrSize += m_hiddenLayer.getChromosomeSize();
    chrSize += m_outputLayer.getChromosomeSize();

    return chrSize;
}

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

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

    response = m_hiddenLayer.io(inputs);
    response = m_outputLayer.io(response);

    return response;
}