From 6c30009564c727a5d4b9fdafd797c2383a34f85f Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Wed, 6 May 2026 17:51:14 +0200 Subject: [PATCH] Improvements --- NanoBrain/Runtime/Scripts/Core/Cluster.cs | 10 ++--- NanoBrain/Runtime/Scripts/Core/Neuron.cs | 49 +++++++++-------------- Runtime/Scripts/Ant.cs | 2 - 3 files changed, 25 insertions(+), 36 deletions(-) diff --git a/NanoBrain/Runtime/Scripts/Core/Cluster.cs b/NanoBrain/Runtime/Scripts/Core/Cluster.cs index 329fd3a..be51790 100644 --- a/NanoBrain/Runtime/Scripts/Core/Cluster.cs +++ b/NanoBrain/Runtime/Scripts/Core/Cluster.cs @@ -827,7 +827,7 @@ namespace NanoBrain { } } } - UpdateNuclei(); + //UpdateNuclei(); } public override void UpdateStateIsolated() { @@ -835,10 +835,10 @@ namespace NanoBrain { } // Don't think this does anything anymore... - public override void UpdateNuclei() { - foreach (Nucleus nucleus in this.nuclei) - nucleus.UpdateNuclei(); - } + // public override void UpdateNuclei() { + // foreach (Nucleus nucleus in this.nuclei) + // nucleus.UpdateNuclei(); + // } #endregion Update diff --git a/NanoBrain/Runtime/Scripts/Core/Neuron.cs b/NanoBrain/Runtime/Scripts/Core/Neuron.cs index 4869615..59a4640 100644 --- a/NanoBrain/Runtime/Scripts/Core/Neuron.cs +++ b/NanoBrain/Runtime/Scripts/Core/Neuron.cs @@ -117,8 +117,6 @@ namespace NanoBrain { Sum, /// Multiply the weighted values Product, - /// Take the maximum of all the weighted values - Max, } /// /// The type of combinator used for this Neuron @@ -377,20 +375,14 @@ namespace NanoBrain { } public override void UpdateStateIsolated() { - CheckSleepingSynapses(); var result = Combinator(); - this.outputValue = Activator(result); + this.outputValue = ApplyActivator(result); this.lastUpdate = Time.time; - // Debug.Log($"Update Neuron {this.name}"); } protected void CheckSleepingSynapses() { - foreach (Synapse synapse in this.synapses) { + foreach (Synapse synapse in this.synapses) synapse.neuron.SleepCheck(); - // if (synapse.isSleeping) { - // synapse.neuron.outputValue = Vector3.zero; - // } - } } #region Combinator @@ -400,42 +392,27 @@ namespace NanoBrain { protected Func Combinator => combinator switch { CombinatorType.Sum => CombinatorSum, CombinatorType.Product => CombinatorProduct, - CombinatorType.Max => CombinatorMax, _ => CombinatorSum }; public float3 CombinatorSum() { float3 sum = this.bias; - foreach (Synapse synapse in this.synapses) + foreach (Synapse synapse in this.synapses) { + synapse.neuron.SleepCheck(); sum += synapse.weight * synapse.neuron.outputValue; + } return sum; } public float3 CombinatorProduct() { float3 product = this.bias; foreach (Synapse synapse in this.synapses) { + synapse.neuron.SleepCheck(); product *= synapse.weight * synapse.neuron.outputValue; } return product; } - public float3 CombinatorMax() { - float3 max = this.bias; - float maxLength = length(max); - - //Applying the weight factors - foreach (Synapse synapse in this.synapses) { - float3 input = synapse.weight * synapse.neuron.outputValue; - - float inputLength = length(input); - if (inputLength > maxLength) { - max = input; - maxLength = inputLength; - } - } - return max; - } - #else protected Func Combinator => combinator switch { @@ -484,6 +461,20 @@ namespace NanoBrain { #if UNITY_MATHEMATICS + // This does not allocate memory and seems faster than the solution below + float3 ApplyActivator(float3 x) { + switch (curvePreset) { + case ActivationType.Linear: return ActivatorLinear(x); + case ActivationType.Sqrt: return ActivatorSqrt(x); + case ActivationType.Power: return ActivatorPower(x); + case ActivationType.Reciprocal: return ActivatorReciprocal(x); + case ActivationType.Tanh: return ActivatorTanh(x); + case ActivationType.Binary: return ActivatorBinary(x); + case ActivationType.Normalized: return ActivatorNormalized(x); + default: return ActivatorCustom(x); + } + } + public Func Activator => this.curvePreset switch { ActivationType.Linear => ActivatorLinear, ActivationType.Sqrt => ActivatorSqrt, diff --git a/Runtime/Scripts/Ant.cs b/Runtime/Scripts/Ant.cs index c4c1265..166c62c 100644 --- a/Runtime/Scripts/Ant.cs +++ b/Runtime/Scripts/Ant.cs @@ -193,8 +193,6 @@ namespace CreatureControl { float angle = Vector3.Angle(Vector3.forward, smellDirection); if (angle < smellAngle && smellDirection.magnitude > 0.01) { - // float intensity = pheromone.StrengthAt(distance); - // Vector3 smell = smellDirection.normalized * intensity; Vector3 smell = Vector3.zero; int id = thing.GetInstanceID();