reset stimulus during output getter

This commit is contained in:
Pascal Serrarens 2026-08-13 17:51:33 +02:00
parent abd337f1dd
commit 6acf07f487
2 changed files with 51 additions and 21 deletions

View File

@ -80,6 +80,8 @@ namespace NanoBrain {
[HideInInspector] [HideInInspector]
public List<Nucleus> nuclei = new(); public List<Nucleus> nuclei = new();
public readonly List<(long timestamp, string neuronName, Action action)> functions = new();
#region Init #region Init
public Cluster() { public Cluster() {

View File

@ -174,8 +174,6 @@ namespace NanoBrain {
#if UNITY_MATHEMATICS #if UNITY_MATHEMATICS
private readonly List<Action> fs = new();
/// <summary> /// <summary>
/// The output value of the neuron /// The output value of the neuron
/// </summary> /// </summary>
@ -185,11 +183,20 @@ namespace NanoBrain {
/// The output value of the neuron /// The output value of the neuron
/// </summary> /// </summary>
public virtual float3 outputValue { public virtual float3 outputValue {
get { get {
foreach (var f in fs) long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
f(); 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);
action();
}
return _outputValue; return _outputValue;
} }
set { set {
_outputValue = value; _outputValue = value;
if (this.isFiring) if (this.isFiring)
@ -804,26 +811,47 @@ namespace NanoBrain {
this.parent?.UpdateFromNucleus(this); this.parent?.UpdateFromNucleus(this);
if (autoResetDelay > 0) { if (autoResetDelay > 0) {
_cts?.Cancel(); long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
_cts?.Dispose(); long resetTime = now + (long)(autoResetDelay * 1000.0f);
_cts = new CancellationTokenSource(); var q = this.parent.functions;
_ = CallResetAfterDelayAsync(_cts.Token, autoResetDelay); 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);
} }
} }
private async Task CallResetAfterDelayAsync(CancellationToken token, float autoResetDelay) { // private async Task CallResetAfterDelayAsync(CancellationToken token, float autoResetDelay) {
try { // try {
await Task.Delay(TimeSpan.FromSeconds(autoResetDelay), token); // await Task.Delay(TimeSpan.FromSeconds(autoResetDelay), token);
if (!token.IsCancellationRequested) // if (!token.IsCancellationRequested)
ResetStimulus(); // ResetStimulus();
} // }
catch (TaskCanceledException) { // catch (TaskCanceledException) {
// Expected when Stimulus is called again; do nothing // // Expected when Stimulus is called again; do nothing
} // }
} // }
protected void ResetStimulus() { protected void ResetStimulus() {
//Debug.Log("reset stimulus"); Debug.Log("reset stimulus");
this.bias = Vector3.zero; this.bias = Vector3.zero;
this.parent?.UpdateFromNucleus(this); this.parent?.UpdateFromNucleus(this);
} }