aboutsummaryrefslogtreecommitdiff
path: root/Neuron.cpp
diff options
context:
space:
mode:
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);
+}