From 120e70515720318cdf107426ea8ef117b0ecc90d Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Mon, 17 Aug 2026 11:49:42 +0200 Subject: [PATCH] Fix calculating the weight average --- Runtime/Scripts/Core/Cluster.cs | 14 ++++++++++++++ Runtime/Scripts/Core/Neuron.cs | 11 +++++++++++ Runtime/Scripts/Core/Population.cs | 17 +++++++++++++---- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/Runtime/Scripts/Core/Cluster.cs b/Runtime/Scripts/Core/Cluster.cs index 7dbbc2a..7ee9aa6 100644 --- a/Runtime/Scripts/Core/Cluster.cs +++ b/Runtime/Scripts/Core/Cluster.cs @@ -966,6 +966,20 @@ namespace NanoBrain { } } + public void ProcessWeights(Func processor) { + if (processor is null) + throw new ArgumentNullException(nameof(processor)); + int thisNucleiCount = this.nuclei.Count; + + for (int i = 0; i < thisNucleiCount; i++) { + if (this.nuclei[i] is Neuron thisNeuron) { + + thisNeuron.ProcessWeight(processor); + } + } + + } + #region Receivers /// diff --git a/Runtime/Scripts/Core/Neuron.cs b/Runtime/Scripts/Core/Neuron.cs index 1221121..bbe8330 100644 --- a/Runtime/Scripts/Core/Neuron.cs +++ b/Runtime/Scripts/Core/Neuron.cs @@ -346,6 +346,17 @@ namespace NanoBrain { thisSynapse.weight = processor(thisSynapse.weight, sourceSynapse.weight); } } + } + + public void ProcessWeight(Func processor) { + int thisSynapseCount = this.synapses.Count; + + for (int i = 0; i < thisSynapseCount; i++) { + Synapse thisSynapse = this.synapses[i]; + if (thisSynapse.trainable) { + thisSynapse.weight = processor(thisSynapse.weight); + } + } } diff --git a/Runtime/Scripts/Core/Population.cs b/Runtime/Scripts/Core/Population.cs index 254dc73..8a5f6fc 100644 --- a/Runtime/Scripts/Core/Population.cs +++ b/Runtime/Scripts/Core/Population.cs @@ -56,11 +56,15 @@ namespace NanoBrain { return null; Cluster result = members[0].brain.Copy(); - // for (int memberIx = 1; memberIx < members.Count; memberIx++) { - // Member member = members[memberIx]; - // result.ProcessWeightsFrom(member.brain, Average); - // } + for (int memberIx = 1; memberIx < members.Count; memberIx++) { + // Debug.Log($"{((Neuron)result.nuclei[0]).synapses[0].weight}"); + Member member = members[memberIx]; + result.ProcessWeightsFrom(member.brain, Sum); + } + // Debug.Log($"R {((Neuron)result.nuclei[0]).synapses[0].weight}"); + result.ProcessWeights(w => w / members.Count); + // Debug.Log($"A {((Neuron)result.nuclei[0]).synapses[0].weight}"); return result; } @@ -136,6 +140,11 @@ namespace NanoBrain { return (a + b) / 2; } + private static float Sum(float a, float b) { + return a + b; + } + + protected List GenerateRandom() { return GenerateRandom(int.MaxValue); }