From 6fd23da97fa9700f59c61a966b4bf7d25fa46b34 Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Thu, 29 Feb 2024 03:15:03 +0100 Subject: initial commit --- Neuron.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Neuron.cpp (limited to 'Neuron.cpp') diff --git a/Neuron.cpp b/Neuron.cpp new file mode 100644 index 0000000..647d525 --- /dev/null +++ b/Neuron.cpp @@ -0,0 +1,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 &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); +} -- cgit v1.2.1