Add static memory option
This commit is contained in:
parent
d562c7192b
commit
c011e04650
@ -396,7 +396,7 @@ public class ClusterInspector : Editor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void DrawNucleus(Nucleus nucleus, Vector3 position, float maxValue, float size, Color color) {
|
private void DrawNucleus(Nucleus nucleus, Vector3 position, float maxValue, float size, Color color) {
|
||||||
if (nucleus is MemoryCell memory) {
|
if (nucleus is MemoryCell) {
|
||||||
Handles.color = Color.white;
|
Handles.color = Color.white;
|
||||||
Handles.DrawWireDisc(position + Vector3.right * 10, Vector3.forward, size);
|
Handles.DrawWireDisc(position + Vector3.right * 10, Vector3.forward, size);
|
||||||
}
|
}
|
||||||
@ -413,17 +413,17 @@ public class ClusterInspector : Editor {
|
|||||||
normal = { textColor = Color.white },
|
normal = { textColor = Color.white },
|
||||||
fontStyle = FontStyle.Bold,
|
fontStyle = FontStyle.Bold,
|
||||||
};
|
};
|
||||||
if (nucleus is Nucleus neuron) {
|
//if (nucleus is Nucleus neuron) {
|
||||||
if (neuron.array == null || neuron.array.nuclei == null || neuron.array.nuclei.Count() == 0)
|
if (nucleus.array == null || nucleus.array.nuclei == null || nucleus.array.nuclei.Count() == 0)
|
||||||
neuron.array = new NucleusArray(neuron);
|
nucleus.array = new NucleusArray(nucleus);
|
||||||
|
|
||||||
if ((!expandArray || neuron.array.nuclei.First() != this.currentNucleus) && neuron.array.nuclei.Count() > 1) {
|
if ((!expandArray || nucleus.array.nuclei.First() != this.currentNucleus) && nucleus.array.nuclei.Count() > 1) {
|
||||||
Handles.Label(labelPosition, neuron.array.nuclei.Count().ToString(), style);
|
Handles.Label(labelPosition, nucleus.array.nuclei.Count().ToString(), style);
|
||||||
}
|
}
|
||||||
if (expandArray && neuron.array.nuclei.First() == this.currentNucleus) {
|
if (expandArray && nucleus.array.nuclei.First() == this.currentNucleus) {
|
||||||
int arrayIx = 0;
|
int arrayIx = 0;
|
||||||
foreach (Nucleus n in neuron.array.nuclei) {
|
foreach (Nucleus n in nucleus.array.nuclei) {
|
||||||
if (n == neuron)
|
if (n == nucleus)
|
||||||
break;
|
break;
|
||||||
arrayIx++;
|
arrayIx++;
|
||||||
}
|
}
|
||||||
@ -441,17 +441,18 @@ public class ClusterInspector : Editor {
|
|||||||
Handles.Label(labelPos, nucleus.name, style);
|
Handles.Label(labelPos, nucleus.name, style);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nucleus is Cluster cluster) {
|
if (nucleus is Cluster) {
|
||||||
Handles.color = Color.white;
|
Handles.color = Color.white;
|
||||||
Handles.DrawWireDisc(position, Vector3.forward, size + 10);
|
Handles.DrawWireDisc(position, Vector3.forward, size + 10);
|
||||||
}
|
}
|
||||||
}
|
// }
|
||||||
else {
|
// else {
|
||||||
style.alignment = TextAnchor.UpperCenter;
|
// style.alignment = TextAnchor.UpperCenter;
|
||||||
Vector3 labelPos = position - Vector3.down * (size + 10); // below disc along up axis
|
// Vector3 labelPos = position - Vector3.down * (size + 10); // below disc along up axis
|
||||||
Handles.Label(labelPos, nucleus.name, style);
|
// Handles.Label(labelPos, nucleus.name, style);
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
// Tooltip
|
||||||
Rect neuronRect = new(position.x - size, position.y - size, size * 2, size * 2);
|
Rect neuronRect = new(position.x - size, position.y - size, size * 2, size * 2);
|
||||||
int id = GUIUtility.GetControlID(FocusType.Passive);
|
int id = GUIUtility.GetControlID(FocusType.Passive);
|
||||||
Event e = Event.current;
|
Event e = Event.current;
|
||||||
@ -533,31 +534,6 @@ public class ClusterInspector : Editor {
|
|||||||
outputsField.choices = this.prefab.outputs.Select(output => output.name).ToList();
|
outputsField.choices = this.prefab.outputs.Select(output => output.name).ToList();
|
||||||
//outputsField.value = newName;
|
//outputsField.value = newName;
|
||||||
}
|
}
|
||||||
//if (this.currentNucleus is Neuron neuron) {
|
|
||||||
// if (this.currentNucleus is MemoryCell memory) {
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// EditorGUILayout.BeginHorizontal();
|
|
||||||
// EditorGUILayout.LabelField("Activation Curve", GUILayout.Width(150));
|
|
||||||
// if (neuron.curveMax > 0)
|
|
||||||
// EditorGUILayout.CurveField(neuron.curve, Color.cyan, new Rect(0, 0, 1, neuron.curveMax));
|
|
||||||
// else
|
|
||||||
// EditorGUILayout.CurveField(neuron.curve, Color.cyan, new Rect(0, neuron.curveMax, 1, -neuron.curveMax));
|
|
||||||
// neuron.curvePreset = (Neuron.CurvePresets)EditorGUILayout.EnumPopup(neuron.curvePreset, GUILayout.Width(100));
|
|
||||||
// EditorGUILayout.EndHorizontal();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (neuron.array == null || neuron.array.nuclei == null || neuron.array.nuclei.Count() == 0)
|
|
||||||
// neuron.array = new NucleusArray(neuron);
|
|
||||||
// EditorGUILayout.BeginHorizontal();
|
|
||||||
// EditorGUILayout.IntField("Array size", neuron.array.nuclei.Count());
|
|
||||||
// if (GUILayout.Button("Add"))
|
|
||||||
// neuron.array.AddNucleus(this.prefab);
|
|
||||||
// if (GUILayout.Button("Del"))
|
|
||||||
// neuron.array.RemoveNucleus();
|
|
||||||
// EditorGUILayout.EndHorizontal();
|
|
||||||
//}
|
|
||||||
|
|
||||||
|
|
||||||
if (Application.isPlaying) {
|
if (Application.isPlaying) {
|
||||||
GUIContent nameLabel = new("Output", this.currentNucleus.outputValue.ToString());
|
GUIContent nameLabel = new("Output", this.currentNucleus.outputValue.ToString());
|
||||||
@ -566,6 +542,12 @@ public class ClusterInspector : Editor {
|
|||||||
else
|
else
|
||||||
EditorGUILayout.LabelField(" ");
|
EditorGUILayout.LabelField(" ");
|
||||||
|
|
||||||
|
if (this.currentNucleus is MemoryCell memory) {
|
||||||
|
memory.staticMemory = EditorGUILayout.Toggle("Static Memory", memory.staticMemory);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Synapses
|
||||||
|
|
||||||
showSynapses = EditorGUILayout.BeginFoldoutHeaderGroup(showSynapses, "Synapses");
|
showSynapses = EditorGUILayout.BeginFoldoutHeaderGroup(showSynapses, "Synapses");
|
||||||
if (showSynapses) {
|
if (showSynapses) {
|
||||||
ConnectNucleus(this.prefab, this.currentNucleus);
|
ConnectNucleus(this.prefab, this.currentNucleus);
|
||||||
@ -616,7 +598,7 @@ public class ClusterInspector : Editor {
|
|||||||
if (showActivation) {
|
if (showActivation) {
|
||||||
|
|
||||||
if (this.currentNucleus is Neuron neuron) {
|
if (this.currentNucleus is Neuron neuron) {
|
||||||
if (this.currentNucleus is not MemoryCell memory) {
|
if (this.currentNucleus is not MemoryCell) {
|
||||||
EditorGUILayout.BeginHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
||||||
EditorGUILayout.LabelField("Activation Curve", GUILayout.Width(150));
|
EditorGUILayout.LabelField("Activation Curve", GUILayout.Width(150));
|
||||||
if (neuron.curveMax > 0)
|
if (neuron.curveMax > 0)
|
||||||
@ -650,13 +632,6 @@ public class ClusterInspector : Editor {
|
|||||||
EditCluster(subCluster);
|
EditCluster(subCluster);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (this.gameObject != null) {
|
|
||||||
// Vector3 worldVector = this.gameObject.transform.TransformVector(this.currentNucleus.outputValue);
|
|
||||||
// //Debug.DrawRay(this.gameObject.transform.position, worldVector, Color.yellow);
|
|
||||||
// Handles.color = Color.yellow;
|
|
||||||
// Handles.DrawLine(this.gameObject.transform.position, this.gameObject.transform.position + worldVector);
|
|
||||||
// }
|
|
||||||
|
|
||||||
EditorGUILayout.Space();
|
EditorGUILayout.Space();
|
||||||
breakOnWake = EditorGUILayout.Toggle("Break on wake", breakOnWake);
|
breakOnWake = EditorGUILayout.Toggle("Break on wake", breakOnWake);
|
||||||
if (breakOnWake) {
|
if (breakOnWake) {
|
||||||
@ -700,22 +675,22 @@ public class ClusterInspector : Editor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void AddInput(int selectedInputType, Nucleus nucleus) {
|
// protected virtual void AddInput(int selectedInputType, Nucleus nucleus) {
|
||||||
switch (selectedInputType) {
|
// switch (selectedInputType) {
|
||||||
case 0: // Neuron
|
// case 0: // Neuron
|
||||||
AddNeuronInput(nucleus);
|
// AddNeuronInput(nucleus);
|
||||||
break;
|
// break;
|
||||||
case 1: // MemoryCell
|
// case 1: // MemoryCell
|
||||||
AddMemoryCellInput(nucleus);
|
// AddMemoryCellInput(nucleus);
|
||||||
break;
|
// break;
|
||||||
case 2: // Selector
|
// case 2: // Selector
|
||||||
AddSelectorInput(nucleus);
|
// AddSelectorInput(nucleus);
|
||||||
break;
|
// break;
|
||||||
case 3: // Cluster
|
// case 3: // Cluster
|
||||||
AddClusterInput(nucleus);
|
// AddClusterInput(nucleus);
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
protected virtual void AddNeuronInput(Nucleus nucleus) {
|
protected virtual void AddNeuronInput(Nucleus nucleus) {
|
||||||
//Neuron newNeuroid = new(this.cluster, "New neuron");
|
//Neuron newNeuroid = new(this.cluster, "New neuron");
|
||||||
|
|||||||
@ -9,27 +9,30 @@ public class MemoryCell : Neuron {
|
|||||||
public MemoryCell(ClusterPrefab cluster, string name) : base(cluster, name) { }
|
public MemoryCell(ClusterPrefab cluster, string name) : base(cluster, name) { }
|
||||||
public MemoryCell(Cluster parent, string name) : base(parent, 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) {
|
public override Nucleus ShallowCloneTo(Cluster newParent) {
|
||||||
MemoryCell clone = new(newParent, this.name) {
|
MemoryCell clone = new(newParent, this.name);
|
||||||
array = this.array,
|
CloneFields(clone);
|
||||||
curve = this.curve,
|
clone.staticMemory = this.staticMemory;
|
||||||
curvePreset = this.curvePreset,
|
|
||||||
curveMax = this.curveMax,
|
|
||||||
average = this.average
|
|
||||||
};
|
|
||||||
return clone;
|
return clone;
|
||||||
}
|
}
|
||||||
|
|
||||||
#region State
|
#region State
|
||||||
|
|
||||||
private float3 _memorizedValue;
|
private bool initialized = false;
|
||||||
private float _memorizedTime;
|
|
||||||
|
|
||||||
// public override void UpdateStateIsolated() {
|
private float3 _memorizedValue;
|
||||||
// float3 bias = new(0, 0, 0);
|
|
||||||
// UpdateStateIsolated(bias);
|
public override void UpdateStateIsolated() {
|
||||||
// }
|
|
||||||
public override void UpdateStateIsolated() { //float3 bias) {
|
|
||||||
// A memorycell does not have an activation function
|
// A memorycell does not have an activation function
|
||||||
Vector3 result = this.bias;
|
Vector3 result = this.bias;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
@ -44,11 +47,25 @@ public class MemoryCell : Neuron {
|
|||||||
if (this.average)
|
if (this.average)
|
||||||
result /= n;
|
result /= n;
|
||||||
|
|
||||||
this.outputValue = this._memorizedValue;
|
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
|
// Store the result for the next time
|
||||||
this._memorizedValue = result;
|
this._memorizedValue = result;
|
||||||
this._memorizedTime = Time.time;
|
}
|
||||||
|
|
||||||
|
public override void UpdateNuclei() {
|
||||||
|
if (staticMemory)
|
||||||
|
// Static memory does not get stale or go to sleep
|
||||||
|
return;
|
||||||
|
|
||||||
|
base.UpdateNuclei();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion State
|
#endregion State
|
||||||
|
|||||||
@ -27,7 +27,7 @@ public abstract class Nucleus {
|
|||||||
public bool isFiring => length(_outputValue) > 0.5f;
|
public bool isFiring => length(_outputValue) > 0.5f;
|
||||||
public Action WhenFiring;
|
public Action WhenFiring;
|
||||||
|
|
||||||
public bool isSleeping => lengthsq(this.outputValue) == 0;
|
public virtual bool isSleeping => lengthsq(this.outputValue) == 0;
|
||||||
[NonSerialized]
|
[NonSerialized]
|
||||||
public int stale = 1000;
|
public int stale = 1000;
|
||||||
public readonly int staleValueForSleep = 20;
|
public readonly int staleValueForSleep = 20;
|
||||||
@ -99,12 +99,6 @@ public abstract class Nucleus {
|
|||||||
#region Update
|
#region Update
|
||||||
|
|
||||||
public abstract void UpdateStateIsolated();
|
public abstract void UpdateStateIsolated();
|
||||||
// UpdateStateIsolated(new float3(0, 0, 0));
|
|
||||||
// }
|
|
||||||
|
|
||||||
//public abstract void UpdateStateIsolated(float3 bias);
|
|
||||||
// public virtual void UpdateStateIsolated(float3 bias) {
|
|
||||||
// }
|
|
||||||
|
|
||||||
public virtual void UpdateNuclei() {
|
public virtual void UpdateNuclei() {
|
||||||
this.stale++;
|
this.stale++;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user