From 6fd23da97fa9700f59c61a966b4bf7d25fa46b34 Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Thu, 29 Feb 2024 03:15:03 +0100 Subject: initial commit --- NodeLayer.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 NodeLayer.cpp (limited to 'NodeLayer.cpp') diff --git a/NodeLayer.cpp b/NodeLayer.cpp new file mode 100644 index 0000000..b42cf11 --- /dev/null +++ b/NodeLayer.cpp @@ -0,0 +1,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(new Neuron(inputCount, zeroed))); + } + } + else + { + while (nodeCount--) + { + m_nodes.push_back(std::unique_ptr(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 NodeLayer::io(const std::vector &inputs) +{ + std::vector response; + + if (inputs.size() != m_inputCount) + { + return response; + } + + for (auto &i : m_nodes) + { + response.push_back(i->io(inputs)); + } + + return response; +} -- cgit v1.2.1