NanoBrain-unitypackage/MemoryCell.cs

73 lines
1.9 KiB
C#

using System;
using UnityEngine;
using Unity.Mathematics;
using static Unity.Mathematics.math;
[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;
private float3 _memorizedValue;
public override void UpdateStateIsolated() {
// A memorycell does not have an activation function
Vector3 result = this.bias;
int n = 0;
//Applying the weight factgors
foreach (Synapse synapse in this.synapses) {
result += synapse.weight * synapse.nucleus.outputValue;
if (lengthsq(synapse.nucleus.outputValue) != 0)
n++;
}
if (this.average)
result /= n;
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
}