diff --git a/Runtime/Scripts/Core/Cluster.cs b/Runtime/Scripts/Core/Cluster.cs index 3264d8a..50ab0a0 100644 --- a/Runtime/Scripts/Core/Cluster.cs +++ b/Runtime/Scripts/Core/Cluster.cs @@ -382,14 +382,21 @@ namespace NanoBrain { } // Otherwise find the stalest cluster? - Cluster stalestCluster = this.siblingClusters[0]; - for (int ix = 1; ix < this.siblingClusters.Length; ix++) { - if (this.siblingClusters[ix].defaultOutput.stale > stalestCluster.defaultOutput.stale) - stalestCluster = this.siblingClusters[ix]; - } + // Cluster stalestCluster = this.siblingClusters[0]; + // for (int ix = 1; ix < this.siblingClusters.Length; ix++) { + // if (this.siblingClusters[ix].defaultOutput.stale > stalestCluster.defaultOutput.stale) + // stalestCluster = this.siblingClusters[ix]; + // } - RemoveThingCluster(stalestCluster); - return stalestCluster; + // Otherwise find longest unused cluster + Cluster unusedCluster = this.siblingClusters[0]; + for (int ix = 1; ix < this.siblingClusters.Length; ix++) { + if (this.siblingClusters[ix].defaultOutput.lastUpdate < unusedCluster.defaultOutput.lastUpdate) + unusedCluster = this.siblingClusters[ix]; + } + + RemoveThingCluster(unusedCluster); + return unusedCluster; } private void RemoveThingCluster(Cluster cluster) { diff --git a/Runtime/Scripts/Core/Neuron.cs b/Runtime/Scripts/Core/Neuron.cs index 16d4a1a..c622bcf 100644 --- a/Runtime/Scripts/Core/Neuron.cs +++ b/Runtime/Scripts/Core/Neuron.cs @@ -213,11 +213,23 @@ namespace NanoBrain { public Action WhenFiring; - public virtual bool isSleeping => this.outputMagnitude == 0; + public virtual bool isSleeping => Time.time - this.lastUpdate > this.timeToSleep; //this.outputMagnitude == 0; + public void SleepCheck() { + if (this.isSleeping) { +#if UNITY_MATHEMATICS + this.bias = new float3(0, 0, 0); +#else + this.bias = new Vector3(0,0,0); +#endif + } + } + // [NonSerialized] + // public int stale = 1000; [NonSerialized] - public int stale = 1000; - public readonly int staleValueForSleep = 20; + public float lastUpdate = 0; + // public readonly int staleValueForSleep = 20; + public readonly float timeToSleep = 1f; /// \copydoc NanoBrain::Nucleus::ShallowCloneTo public override Nucleus ShallowCloneTo(Cluster newParent) { @@ -288,8 +300,18 @@ namespace NanoBrain { } public override void UpdateStateIsolated() { + CheckSleepingSynapses(); var result = Combinator(); this.outputValue = Activator(result); + this.lastUpdate = Time.time; + } + + protected void CheckSleepingSynapses() { + foreach (Synapse synapse in this.synapses) { + if (synapse.isSleeping) { + synapse.neuron.outputValue = Vector3.zero; + } + } } #region Combinator @@ -524,7 +546,8 @@ namespace NanoBrain { } public void ProcessStimulusDirect(Vector3 inputValue) { //}, int thingId = 0, string thingName = null) { - this.stale = 0; + // this.stale = 0; + this.lastUpdate = Time.time; this.bias = inputValue; this.parent.UpdateFromNucleus(this); } diff --git a/Runtime/Scripts/Core/Synapse.cs b/Runtime/Scripts/Core/Synapse.cs index 63bacf7..e71130d 100644 --- a/Runtime/Scripts/Core/Synapse.cs +++ b/Runtime/Scripts/Core/Synapse.cs @@ -28,6 +28,12 @@ namespace NanoBrain { this.neuron = nucleus; this.weight = weight; } + + public bool isSleeping { + get { + return this.neuron.isSleeping; + } + } } } \ No newline at end of file