auto stimulus reset

This commit is contained in:
Pascal Serrarens 2026-08-12 17:03:50 +02:00
parent a334d9ca4d
commit a4776f9dea
2 changed files with 48 additions and 19 deletions

View File

@ -392,7 +392,7 @@ namespace NanoBrain {
foreach (Synapse synapse in neuron.synapses) { foreach (Synapse synapse in neuron.synapses) {
if (synapse.trainable) { if (synapse.trainable) {
synapse.weight = (float)randomGenerator.NextDouble() * 2.0f - 1.0f; synapse.weight = (float)randomGenerator.NextDouble() * 2.0f - 1.0f;
Debug.Log($"{neuron.name}-{synapse.neuron.name} weight = {synapse.weight}"); // Debug.Log($"{neuron.name}-{synapse.neuron.name} weight = {synapse.weight}");
} }
} }
} }
@ -859,7 +859,9 @@ namespace NanoBrain {
List<Nucleus> computeOrder = this.computeOrders[startNucleus]; List<Nucleus> computeOrder = this.computeOrders[startNucleus];
foreach (Nucleus nucleus in computeOrder) { foreach (Nucleus nucleus in computeOrder) {
if (nucleus is not Cluster) { if (nucleus is Cluster)
continue;
nucleus.UpdateStateIsolated(); nucleus.UpdateStateIsolated();
if (nucleus is Neuron neuron) { if (nucleus is Neuron neuron) {
foreach (Nucleus receiver in neuron.receivers) { foreach (Nucleus receiver in neuron.receivers) {
@ -871,7 +873,6 @@ namespace NanoBrain {
} }
} }
} }
}
/// \copydoc NanoBrain::Nucleus::UpdateStateIsolated /// \copydoc NanoBrain::Nucleus::UpdateStateIsolated
public override void UpdateStateIsolated() { public override void UpdateStateIsolated() {

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
#if UNITY_MATHEMATICS #if UNITY_MATHEMATICS
using Unity.Mathematics; using Unity.Mathematics;
@ -739,14 +741,40 @@ namespace NanoBrain {
#endregion Back propagation #endregion Back propagation
private CancellationTokenSource _cts;
/// <summary> /// <summary>
/// Process an external stimulus /// Process an external stimulus
/// </summary> /// </summary>
/// <param name="inputValue">The value of the stimulus</param> /// <param name="inputValue">The value of the stimulus</param>
public virtual void ProcessStimulus(Vector3 inputValue) { public virtual void ProcessStimulus(Vector3 inputValue, float autoResetDelay = 0) {
this.lastUpdate = Time.time; this.lastUpdate = Time.time;
this.bias = inputValue; this.bias = inputValue;
this.parent?.UpdateFromNucleus(this); this.parent?.UpdateFromNucleus(this);
if (autoResetDelay > 0) {
_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 async void ResetStimulus() {
//Debug.Log("reset stimulus");
this.bias = Vector3.zero;
this.parent?.UpdateFromNucleus(this);
} }
} }