diff --git a/Runtime/Scripts/Core/Cluster.cs b/Runtime/Scripts/Core/Cluster.cs index a9ea309..0a92d1e 100644 --- a/Runtime/Scripts/Core/Cluster.cs +++ b/Runtime/Scripts/Core/Cluster.cs @@ -80,7 +80,33 @@ namespace NanoBrain { [HideInInspector] public List nuclei = new(); - public readonly List<(long timestamp, string neuronName, Action action)> functions = new(); + public class TimedAction { + public TimedAction(string neuronName, Action action, long timestamp) { + this.neuronName = neuronName; + this.timestamp = timestamp; + this.action = action; + } + public string neuronName; + public long timestamp; + public Action action; + + public void AddTo(List actions) { + int ix = actions.FindIndex(f => f.neuronName == this.neuronName); + if (ix >= 0) { + // update the reset time of the existing function call + actions[ix] = this; + } + else { + // Add the function call in a sorted manner + ix = actions.FindIndex(f => f.timestamp > this.timestamp); + if (ix < 0) + actions.Add(this); + else + actions.Insert(ix, this); + } + } + } + public readonly List actions = new(); #region Init @@ -841,7 +867,7 @@ namespace NanoBrain { for (int i = 0; i < thisNucleiCount; i++) { if (this.nuclei[i] is Neuron thisNeuron && source.nuclei[i] is Neuron sourceNeuron) { - + thisNeuron.ProcessWeightsFrom(sourceNeuron, processor); } } diff --git a/Runtime/Scripts/Core/Neuron.cs b/Runtime/Scripts/Core/Neuron.cs index e87d749..b1508b6 100644 --- a/Runtime/Scripts/Core/Neuron.cs +++ b/Runtime/Scripts/Core/Neuron.cs @@ -185,14 +185,10 @@ namespace NanoBrain { public virtual float3 outputValue { get { long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); - List<(long timestamp, string, Action)> q = this.parent.functions; - // if (q.Count > 0) { - // Debug.Log($"Q size is {q.Count} {q[0].timestamp} {q[0].timestamp - now}"); - // } - while (q.Count > 0 && q[0].timestamp <= now) { - // Debug.Log($"Function call at {now}"); - (long timestamp, string neuron, Action action) = q[0]; - q.RemoveAt(0); + List actions = this.parent.actions; + while (actions.Count > 0 && actions[0].timestamp <= now) { + Action action = actions[0].action; + actions.RemoveAt(0); action(); } return _outputValue; @@ -813,45 +809,13 @@ namespace NanoBrain { if (autoResetDelay > 0) { long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); long resetTime = now + (long)(autoResetDelay * 1000.0f); - var q = this.parent.functions; - int ix = q.FindIndex(f => f.neuronName == this.name); - if (ix >= 0) { - // Debug.Log($"Update resetter at {resetTime}"); - // update the reset time of the existing function call - q[ix] = (resetTime, this.name, ResetStimulus); - } - else { - // Add the function call in a sorted manner - ix = q.FindIndex(f => f.timestamp > resetTime); - if (ix < 0) { - // Debug.Log($"Add resetter at {resetTime}"); - q.Add((resetTime, this.name, ResetStimulus)); - } - else { - Debug.Log($"Insert resetter at {resetTime}"); - q.Insert(ix, (resetTime, this.name, ResetStimulus)); - } - } - // _cts?.Cancel(); - // _cts?.Dispose(); - // _cts = new CancellationTokenSource(); - // _ = CallResetAfterDelayAsync(_cts.Token, autoResetDelay); + Cluster.TimedAction action = new(this.name, ResetStimulus, resetTime); + action.AddTo(this.parent.actions); } } - // private async Task CallResetAfterDelayAsync(CancellationToken token, float autoResetDelay) { - // try { - // await Task.Delay(TimeSpan.FromSeconds(autoResetDelay), token); - // if (!token.IsCancellationRequested) - // ResetStimulus(); - // } - // catch (TaskCanceledException) { - // // Expected when Stimulus is called again; do nothing - // } - // } - protected void ResetStimulus() { - Debug.Log("reset stimulus"); + // Debug.Log("reset stimulus"); this.bias = Vector3.zero; this.parent?.UpdateFromNucleus(this); }