73 lines
2.0 KiB
C#

using System;
#if UNITY_MATHEMATICS
using Unity.Mathematics;
#endif
namespace NanoBrain {
/// <summary>
/// A MemoryCell stored its value for one update
/// </summary>
/// When the input for a Memory Cell changes, it will output the previous value
[Serializable]
public class MemoryCell : Neuron {
public MemoryCell(ClusterPrefab cluster, string name) : base(cluster, name) { }
public MemoryCell(Cluster parent, string name) : base(parent, name) { }
public bool staticMemory = false;
public override bool isSleeping {
get {
if (staticMemory)
return false;
return base.isSleeping;
}
}
public override Nucleus ShallowCloneTo(Cluster newParent) {
MemoryCell clone = new(newParent, this.name);
CloneFields(clone);
clone.staticMemory = this.staticMemory;
return clone;
}
#region State
private bool initialized = false;
#if UNITY_MATHEMATICS
private float3 _memorizedValue;
#else
private UnityEngine.Vector3 _memorizedValue;
#endif
public override void UpdateStateIsolated() {
// A memorycell does not have an activation function
var result = Combinator();
if (initialized)
// Output the previous, memorized value
this.outputValue = this._memorizedValue;
else {
// The first time, the result is directly set in output
this.outputValue = result;
this.initialized = true;
}
// Store the result for the next time
this._memorizedValue = result;
}
public override void UpdateNuclei() {
if (staticMemory)
// Static memory does not get stale or go to sleep
return;
base.UpdateNuclei();
}
#endregion State
}
}