From 8cb6b979bf3de749464dec9b8248380559a13364 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Wed, 12 Aug 2026 12:15:06 +0200 Subject: [PATCH] Add trivial mutation --- Runtime/Scripts/Core/Cluster.cs | 15 +++++++++++++++ Runtime/Scripts/Core/Synapse.cs | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/Runtime/Scripts/Core/Cluster.cs b/Runtime/Scripts/Core/Cluster.cs index d213186..75883a0 100644 --- a/Runtime/Scripts/Core/Cluster.cs +++ b/Runtime/Scripts/Core/Cluster.cs @@ -401,6 +401,21 @@ namespace NanoBrain { } } + public void GaussianAdditiveMutation(float sigma) { + foreach (Nucleus nucleus in this.nuclei) { + if (nucleus is Neuron neuron) { + foreach (Synapse synapse in neuron.synapses) { + if (synapse.trainable) { + synapse.GaussianAdditiveMutation(sigma); + //synapse.weight = (float)randomGenerator.NextDouble() * 2.0f - 1.0f; + Debug.Log($"{neuron.name}-{synapse.neuron.name} weight = {synapse.weight}"); + } + } + } + } + + } + #endregion Init #region Cluster Array diff --git a/Runtime/Scripts/Core/Synapse.cs b/Runtime/Scripts/Core/Synapse.cs index 481d663..d92af2f 100644 --- a/Runtime/Scripts/Core/Synapse.cs +++ b/Runtime/Scripts/Core/Synapse.cs @@ -110,6 +110,30 @@ namespace NanoBrain { // Check if the angle between the vectors is > 90 degrees return Vector3.Dot(a, b) < 0f; } + + public void GaussianAdditiveMutation(float sigma) { + if (this.trainable == false) + return; + + float deltaWeight = NormalDistribution.Sample(sigma); + this.weight += deltaWeight; + } + } + + public static class NormalDistribution { + private static readonly System.Random rng = new(); + + // Returns a single sample from N(0, sigma^2) + public static float Sample(float sigma) { + // u1 must be > 0 to avoid log(0) + float u1 = 1.0f - (float)rng.NextDouble(); // in (0,1] + float u2 = (float) rng.NextDouble(); // in [0,1) + + float stdNormal = + Mathf.Sqrt(-2.0f * Mathf.Log(u1)) * Mathf.Cos(2.0f * Mathf.PI * u2); + + return sigma * stdNormal; + } } [Serializable]