aboutsummaryrefslogtreecommitdiff
path: root/NodeLayer.cpp
diff options
context:
space:
mode:
authorPaul Oliver <contact@pauloliver.dev>2024-02-29 03:15:03 +0100
committerPaul Oliver <contact@pauloliver.dev>2024-02-29 03:15:30 +0100
commit6fd23da97fa9700f59c61a966b4bf7d25fa46b34 (patch)
treed5fe3a8305a5f57e5b4cedc8300e951c74696cc5 /NodeLayer.cpp
initial commitHEADmaster
Diffstat (limited to 'NodeLayer.cpp')
-rw-r--r--NodeLayer.cpp75
1 files changed, 75 insertions, 0 deletions
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<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;
+}