This commit is contained in:
Pascal Serrarens 2026-08-14 09:12:17 +02:00
parent 6acf07f487
commit dca9757548
2 changed files with 35 additions and 45 deletions

View File

@ -80,7 +80,33 @@ 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(); 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<TimedAction> 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<TimedAction> actions = new();
#region Init #region Init

View File

@ -185,14 +185,10 @@ namespace NanoBrain {
public virtual float3 outputValue { public virtual float3 outputValue {
get { get {
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
List<(long timestamp, string, Action)> q = this.parent.functions; List<Cluster.TimedAction> actions = this.parent.actions;
// if (q.Count > 0) { while (actions.Count > 0 && actions[0].timestamp <= now) {
// Debug.Log($"Q size is {q.Count} {q[0].timestamp} {q[0].timestamp - now}"); Action action = actions[0].action;
// } actions.RemoveAt(0);
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(); action();
} }
return _outputValue; return _outputValue;
@ -813,45 +809,13 @@ namespace NanoBrain {
if (autoResetDelay > 0) { if (autoResetDelay > 0) {
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
long resetTime = now + (long)(autoResetDelay * 1000.0f); long resetTime = now + (long)(autoResetDelay * 1000.0f);
var q = this.parent.functions; Cluster.TimedAction action = new(this.name, ResetStimulus, resetTime);
int ix = q.FindIndex(f => f.neuronName == this.name); action.AddTo(this.parent.actions);
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) {
// 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() { 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);
} }