aboutsummaryrefslogtreecommitdiff
path: root/Neuron.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 /Neuron.cpp
initial commitHEADmaster
Diffstat (limited to 'Neuron.cpp')
-rw-r--r--Neuron.cpp57
1 files changed, 57 insertions, 0 deletions
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<float> &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);
+}