Improvements

This commit is contained in:
Pascal Serrarens 2026-08-14 09:56:31 +02:00
parent dca9757548
commit ac4c5fb2c5
2 changed files with 39 additions and 21 deletions

View File

@ -90,21 +90,37 @@ namespace NanoBrain {
public long timestamp;
public Action action;
public void AddTo(List<TimedAction> actions) {
int ix = actions.FindIndex(f => f.neuronName == this.neuronName);
public static void AddTo(List<TimedAction> actions, string neuronName, Action action, long timestamp) {
int ix = -1;
int insertIx = actions.Count; // default: add at end
// Find existing item and the first item with timestamp > this.timestamp
for (int i = 0; i < actions.Count; i++) {
TimedAction item = actions[i];
if (ix < 0 && item.neuronName == neuronName) {
ix = i;
break;
}
}
if (ix >= 0) {
// update the reset time of the existing function call
actions[ix] = this;
actions[ix].action = action;
actions[ix].timestamp = timestamp;
return;
}
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);
// No existing item; compute insertion point (sorted by timestamp ascending)
for (int i = 0; i < actions.Count; i++) {
if (actions[i].timestamp > timestamp) {
insertIx = i;
break;
}
}
TimedAction newAction = new(neuronName, action, timestamp);
actions.Insert(insertIx, newAction);
}
}
public readonly List<TimedAction> actions = new();
@ -516,17 +532,18 @@ namespace NanoBrain {
/// </summary>
/// <param name="cluster">The multi-cluster instance which not no longer be mapped</param>
private void RemoveThingCluster(Cluster cluster) {
List<int> keysToRemove = new();
foreach (KeyValuePair<int, Cluster> kvp in thingClusters) {
if (kvp.Value == cluster)
keysToRemove.Add(kvp.Key);
int keyToRemove = -1;
foreach (KeyValuePair<int, Cluster> kvp in this.thingClusters) {
if (kvp.Value == cluster) {
keyToRemove = kvp.Key;
break;
}
}
foreach (int thingId in keysToRemove)
thingClusters.Remove(thingId);
if (keyToRemove >= 0)
this.thingClusters.Remove(keyToRemove);
}
public List<Neuron> GetAllInstances(Neuron nucleus) {
List<Neuron> allInstances = new();

View File

@ -37,6 +37,7 @@ namespace NanoBrain {
this.parent.nuclei ??= new();
this.parent.nuclei.Add(this);
}
this.resetStimulus = ResetStimulus;
}
#region Serialization
@ -796,6 +797,7 @@ namespace NanoBrain {
#endregion Back propagation
private CancellationTokenSource _cts;
private Action resetStimulus;
/// <summary>
/// Process an external stimulus
@ -809,12 +811,11 @@ namespace NanoBrain {
if (autoResetDelay > 0) {
long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
long resetTime = now + (long)(autoResetDelay * 1000.0f);
Cluster.TimedAction action = new(this.name, ResetStimulus, resetTime);
action.AddTo(this.parent.actions);
Cluster.TimedAction.AddTo(this.parent.actions, this.name, this.resetStimulus, resetTime);
}
}
protected void ResetStimulus() {
private void ResetStimulus() {
// Debug.Log("reset stimulus");
this.bias = Vector3.zero;
this.parent?.UpdateFromNucleus(this);