Compare commits
35 Commits
7ef8e42e09
...
da370bb701
| Author | SHA1 | Date | |
|---|---|---|---|
| da370bb701 | |||
| 32b58852d4 | |||
| 33ea14bb72 | |||
| a651ec6e15 | |||
| baa7defef0 | |||
| 551b4d9cea | |||
| 7187f61b4e | |||
| c78722abce | |||
| 2ff550cba4 | |||
| 2ef67fe107 | |||
| a9a0072fb4 | |||
| 22ee17c49f | |||
| b6a3bc1892 | |||
| 517e73881a | |||
| 033ddf4d94 | |||
| ef700c082c | |||
| 7d5e1572ec | |||
| f13820139e | |||
| 1c4d36120c | |||
| 0f83945510 | |||
| 6f398ad4bf | |||
| 587f10490f | |||
| fc581a0dd8 | |||
| 837c5ce807 | |||
| 63486d10b9 | |||
| ce8e476621 | |||
| 88d5eb565d | |||
| 481829c873 | |||
| 018c99dce5 | |||
| e709ea4e60 | |||
| c1dcc83827 | |||
| af2fa77add | |||
| 04ca8dda07 | |||
| d9ba98da47 | |||
| 2219e9860c |
@ -1,2 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9197e2d322d23b5798ab4aef729815b0
|
||||
guid: 9197e2d322d23b5798ab4aef729815b0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
@ -1,2 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f05072314d39990639a2dbf99f322664
|
||||
guid: f05072314d39990639a2dbf99f322664
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
@ -64,22 +64,24 @@ namespace NanoBrain {
|
||||
public class GraphEditor : GraphView {
|
||||
|
||||
protected ClusterPrefab prefab;
|
||||
protected Nucleus currentPrefabNucleus;
|
||||
//protected Nucleus currentPrefabNucleus;
|
||||
|
||||
protected override Nucleus currentNucleus {
|
||||
get => base.currentNucleus;
|
||||
set {
|
||||
base.currentNucleus = value;
|
||||
this.currentPrefabNucleus = value != null ? this.prefab.GetNucleus(value.name) : null;
|
||||
// this.currentPrefabNucleus = value != null ? this.prefab.GetNucleus(value.name) : null;
|
||||
}
|
||||
}
|
||||
|
||||
public GraphEditor(ClusterPrefab prefab) : base(prefab.output.parent) {
|
||||
public GraphEditor(ClusterPrefab prefab) : base(prefab.cluster.defaultOutput.parent) {
|
||||
this.prefab = prefab;
|
||||
|
||||
// In a Prefab editor, no instance exists but we need it for the ClusterViewer.
|
||||
// So we create a temporary instance
|
||||
this.currentCluster = new(prefab);
|
||||
//this.currentCluster = new(prefab);
|
||||
this.currentCluster = prefab.cluster;
|
||||
this.currentCluster.Refresh();
|
||||
}
|
||||
|
||||
public void SetGraph(GameObject gameObject, VisualElement inspectorContainer) {
|
||||
@ -87,7 +89,7 @@ namespace NanoBrain {
|
||||
|
||||
if (Application.isPlaying == false)
|
||||
this.serializedBrain = new SerializedObject(this.prefab);
|
||||
this.selectedOutput = this.currentCluster.outputs[0];
|
||||
this.selectedOutput = this.currentCluster.defaultOutput;
|
||||
this.currentNucleus = this.selectedOutput;
|
||||
//this.currentCluster = this.currentNucleus.parent;
|
||||
Rebuild(inspectorContainer);
|
||||
@ -120,7 +122,7 @@ namespace NanoBrain {
|
||||
// create a SerializedObject wrapper so Unity inspector controls work (and Undo)
|
||||
SerializedObject so = new(prefabAsset);
|
||||
|
||||
foreach (Nucleus nucleus in this.prefab.nuclei) {
|
||||
foreach (Nucleus nucleus in this.prefab.cluster.nuclei) {
|
||||
nucleus.Initialize();
|
||||
}
|
||||
|
||||
@ -163,29 +165,10 @@ namespace NanoBrain {
|
||||
GUILayout.Label(nucleusType, headerStyle);
|
||||
|
||||
// Nucleus name
|
||||
Cluster cluster = this.currentPrefabNucleus as Cluster;
|
||||
if (cluster != null) {
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
if (GUILayout.Button(this.currentNucleus.parent.name))
|
||||
OnClusterClick(cluster);
|
||||
EditorGUI.BeginDisabledGroup(true);
|
||||
EditorGUILayout.TextField(this.currentNucleus.name, boldTextFieldStyle);
|
||||
EditorGUI.EndDisabledGroup();
|
||||
if (GUILayout.Button("Reimport"))
|
||||
ReimportCluster(cluster);
|
||||
EditorGUILayout.EndHorizontal();
|
||||
}
|
||||
else {
|
||||
string newName = EditorGUILayout.TextField(this.currentNucleus.name, boldTextFieldStyle);
|
||||
if (newName != this.currentNucleus.name) {
|
||||
Nucleus prefabNucleus = this.prefab.GetNucleus(this.currentNucleus.name);
|
||||
prefabNucleus.name = newName;
|
||||
// This changes it in the temporary cluster instance
|
||||
this.currentNucleus.name = newName;
|
||||
this.prefab.RefreshOutputs();
|
||||
// outputsPopup.choices = this.prefab.outputs.Select(output => output.name).ToList();
|
||||
anythingChanged = true;
|
||||
}
|
||||
string newName = EditorGUILayout.TextField(this.currentNucleus.name, boldTextFieldStyle);
|
||||
if (newName != this.currentNucleus.name) {
|
||||
this.currentNucleus.name = newName;
|
||||
anythingChanged = true;
|
||||
}
|
||||
|
||||
// Current output value
|
||||
@ -204,7 +187,7 @@ namespace NanoBrain {
|
||||
if (this.currentNucleus is MemoryCell memory)
|
||||
MemoryCellInspector(memory, ref anythingChanged);
|
||||
// Cluster
|
||||
else if (cluster != null)
|
||||
else if (this.currentNucleus is Cluster cluster)
|
||||
ClusterInspector(cluster, ref anythingChanged);
|
||||
// Other
|
||||
else
|
||||
@ -230,10 +213,8 @@ namespace NanoBrain {
|
||||
|
||||
bool connecting = GUILayout.Button("Add Output Neuron");
|
||||
if (connecting) {
|
||||
Nucleus newOutput = new Neuron(this.prefab, "New Output");
|
||||
// Regenerate the temporary clsuter instance
|
||||
// See also the constructor
|
||||
this.currentCluster = new(this.prefab);
|
||||
Nucleus newOutput = new Neuron(this.currentCluster, "New Output");
|
||||
this.currentCluster.Refresh();
|
||||
this.currentNucleus = newOutput;
|
||||
this.selectedOutput = this.currentNucleus;
|
||||
}
|
||||
@ -281,9 +262,9 @@ namespace NanoBrain {
|
||||
if (breakOnWake && this.currentNucleus is Neuron currentNeuron) {
|
||||
if (currentNeuron.isSleeping == false)
|
||||
Debug.Break();
|
||||
// trace = EditorGUILayout.Toggle("Trace", trace);
|
||||
// currentNeuron.trace = trace;
|
||||
}
|
||||
trace = EditorGUILayout.Toggle("Trace", trace);
|
||||
this.currentNucleus.trace = trace;
|
||||
}
|
||||
|
||||
protected void SynapsesInspector(ref bool anythingChanged) {
|
||||
@ -293,31 +274,30 @@ namespace NanoBrain {
|
||||
Neuron.CombinatorType newCombinator = (Neuron.CombinatorType)EditorGUILayout.EnumPopup("Combinator", neuron2.combinator);
|
||||
anythingChanged |= newCombinator != neuron2.combinator;
|
||||
neuron2.combinator = newCombinator;
|
||||
}
|
||||
|
||||
EditorGUIUtility.wideMode = true;
|
||||
float previousLabelWidth = EditorGUIUtility.labelWidth;
|
||||
EditorGUIUtility.labelWidth = 100;
|
||||
EditorGUIUtility.wideMode = true;
|
||||
float previousLabelWidth = EditorGUIUtility.labelWidth;
|
||||
EditorGUIUtility.labelWidth = 100;
|
||||
|
||||
Vector3 newBias = EditorGUILayout.Vector3Field("Bias", this.currentNucleus.bias);
|
||||
if (newBias != this.currentPrefabNucleus.bias) {
|
||||
anythingChanged |= newBias != this.currentNucleus.bias;
|
||||
this.currentPrefabNucleus.bias = newBias;
|
||||
this.currentNucleus.bias = newBias;
|
||||
Vector3 newBias = EditorGUILayout.Vector3Field("Bias", neuron2.bias);
|
||||
if (newBias != neuron2.bias) {
|
||||
anythingChanged |= newBias != neuron2.bias;
|
||||
neuron2.bias = newBias;
|
||||
}
|
||||
EditorGUIUtility.labelWidth = previousLabelWidth;
|
||||
}
|
||||
EditorGUIUtility.labelWidth = previousLabelWidth;
|
||||
|
||||
Nucleus[] array = null;
|
||||
int elementIx = -1;
|
||||
if (this.currentPrefabNucleus.synapses.Count > 0) {
|
||||
Synapse[] synapses = this.currentPrefabNucleus.synapses.ToArray();
|
||||
if (this.currentNucleus is Neuron currentNeuron && currentNeuron.synapses.Count > 0) {
|
||||
Synapse[] synapses = currentNeuron.synapses.ToArray();
|
||||
foreach (Synapse synapse in synapses) {
|
||||
if (synapse.neuron == null)
|
||||
continue;
|
||||
|
||||
if (array != null) {
|
||||
if (synapse.neuron.parent is Cluster iCluster && elementIx > 0) {
|
||||
int thisElementIx = Cluster.GetNucleusIndex(iCluster.clusterNuclei, synapse.neuron);
|
||||
int thisElementIx = Cluster.GetNucleusIndex(iCluster.nuclei, synapse.neuron);
|
||||
if (thisElementIx == elementIx)
|
||||
continue;
|
||||
else
|
||||
@ -332,7 +312,7 @@ namespace NanoBrain {
|
||||
if (synapse.neuron.parent is Cluster iReceptor) {
|
||||
array = iReceptor.siblingClusters;
|
||||
if (iReceptor is Cluster iCluster)
|
||||
elementIx = Cluster.GetNucleusIndex(iCluster.clusterNuclei, synapse.neuron);
|
||||
elementIx = Cluster.GetNucleusIndex(iCluster.nuclei, synapse.neuron);
|
||||
}
|
||||
}
|
||||
|
||||
@ -348,21 +328,19 @@ namespace NanoBrain {
|
||||
else {
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
|
||||
if (synapse.neuron.clusterPrefab != this.currentNucleus.clusterPrefab) {
|
||||
if (synapse.neuron.parent != this.currentNucleus.parent) {
|
||||
// If it is a different cluster
|
||||
GUIStyle labelStyle = new(GUI.skin.label);
|
||||
float labelWidth = 200;
|
||||
if (synapse.neuron.clusterPrefab != null) {
|
||||
labelWidth = labelStyle.CalcSize(new GUIContent($"{synapse.neuron.clusterPrefab.name}.")).x;
|
||||
GUILayout.Label($"{synapse.neuron.clusterPrefab.name}", GUILayout.Width(labelWidth));
|
||||
if (synapse.neuron.parent != null) {
|
||||
labelWidth = labelStyle.CalcSize(new GUIContent($"{synapse.neuron.parent.name}.")).x;
|
||||
GUILayout.Label($"{synapse.neuron.parent.name}", GUILayout.Width(labelWidth));
|
||||
}
|
||||
string[] options = synapse.neuron.clusterPrefab.nuclei.Select(n => n.name).ToArray();
|
||||
string[] options = synapse.neuron.parent.nuclei.Select(n => n.name).ToArray();
|
||||
int selectedIndex = System.Array.IndexOf(options, synapse.neuron.name);
|
||||
int newIndex = EditorGUILayout.Popup(selectedIndex, options);
|
||||
if (newIndex != selectedIndex) {
|
||||
// Nucleus selectedNucleus = synapse.neuron.parent.clusterNuclei[newIndex];
|
||||
// Neuron newNeuron = selectedNucleus as Neuron;
|
||||
Neuron newNeuron = synapse.neuron.clusterPrefab.nuclei[newIndex] as Neuron;
|
||||
Neuron newNeuron = synapse.neuron.parent.nuclei[newIndex] as Neuron;
|
||||
ChangeSynapse(synapse, newNeuron);
|
||||
}
|
||||
}
|
||||
@ -370,13 +348,12 @@ namespace NanoBrain {
|
||||
GUILayout.Label(synapse.neuron.name);
|
||||
|
||||
bool disconnecting = GUILayout.Button("Disconnect", GUILayout.Width(80));
|
||||
if (disconnecting && synapse.neuron is Neuron synapseNeuron) {
|
||||
synapseNeuron.RemoveReceiver(this.currentNucleus);
|
||||
this.prefab.GarbageCollection();
|
||||
if (disconnecting) {
|
||||
synapse.neuron.RemoveReceiver(this.currentNucleus);
|
||||
this.currentCluster.Refresh();
|
||||
anythingChanged = true;
|
||||
}
|
||||
EditorGUILayout.EndHorizontal();
|
||||
|
||||
}
|
||||
|
||||
EditorGUI.indentLevel++;
|
||||
@ -443,13 +420,14 @@ namespace NanoBrain {
|
||||
}
|
||||
|
||||
protected virtual void AddNeuronInput(Nucleus nucleus) {
|
||||
Neuron newNeuroid = new(this.prefab, "New neuron");
|
||||
newNeuroid.AddReceiver(nucleus);
|
||||
this.currentNucleus = newNeuroid;
|
||||
Neuron newNeuron = new(this.currentCluster, "New Neuron");
|
||||
//Neuron newNeuroid = new(this.prefab.cluster, "New neuron");
|
||||
newNeuron.AddReceiver(nucleus);
|
||||
this.currentNucleus = newNeuron;
|
||||
}
|
||||
|
||||
protected virtual void AddMemoryCellInput(Nucleus nucleus) {
|
||||
MemoryCell newMemory = new(this.prefab, "New memory cell");
|
||||
MemoryCell newMemory = new(this.prefab.cluster, "New memory cell");
|
||||
newMemory.AddReceiver(nucleus);
|
||||
this.currentNucleus = newMemory;
|
||||
}
|
||||
@ -458,7 +436,7 @@ namespace NanoBrain {
|
||||
ClusterPickerWindow.ShowPicker(brain => OnClusterPicked(nucleus, brain), "Select Cluster");
|
||||
}
|
||||
private void OnClusterPicked(Nucleus nucleus, ClusterPrefab selectedPrefab) {
|
||||
Cluster subclusterInstance = new(selectedPrefab, this.prefab);
|
||||
Cluster subclusterInstance = new(selectedPrefab, this.currentCluster);
|
||||
subclusterInstance.defaultOutput.AddReceiver(nucleus);
|
||||
}
|
||||
|
||||
@ -493,11 +471,12 @@ namespace NanoBrain {
|
||||
if (cluster == null)
|
||||
return false;
|
||||
|
||||
IEnumerable<Nucleus> synapseNuclei = this.currentNucleus.synapses
|
||||
Neuron currentNeuron = this.currentNucleus as Neuron;
|
||||
IEnumerable<Nucleus> synapseNuclei = currentNeuron.synapses
|
||||
.Where(synapse => synapse.neuron != null)
|
||||
.Select(synapse => synapse.neuron);
|
||||
|
||||
IEnumerable<Nucleus> nuclei = cluster.nuclei
|
||||
IEnumerable<Nucleus> nuclei = cluster.cluster.nuclei
|
||||
.Except(synapseNuclei);
|
||||
IEnumerable<string> nucleiNames = nuclei
|
||||
.Select(n => {
|
||||
@ -513,10 +492,13 @@ namespace NanoBrain {
|
||||
EditorGUILayout.EndHorizontal();
|
||||
if (connecting) {
|
||||
Nucleus nucleus = nuclei.ElementAt(selectedConnectNucleus);
|
||||
if (nucleus is Cluster subCluster)
|
||||
if (nucleus is Cluster subCluster) {
|
||||
subCluster.AddArrayReceiver(this.currentNucleus);
|
||||
else if (nucleus is Neuron neuron)
|
||||
}
|
||||
else if (nucleus is Neuron neuron) {
|
||||
neuron.AddReceiver(this.currentNucleus);
|
||||
}
|
||||
this.currentCluster.Refresh();
|
||||
}
|
||||
return connecting;
|
||||
}
|
||||
@ -537,10 +519,10 @@ namespace NanoBrain {
|
||||
|
||||
// this.prefab.nuclei.Remove(nucleus);
|
||||
// Neuron.Delete(nucleus);
|
||||
this.prefab.RefreshOutputs();
|
||||
this.prefab.cluster.RefreshOutputs();
|
||||
|
||||
|
||||
this.currentNucleus = this.prefab.output;
|
||||
this.currentNucleus = this.prefab.cluster.defaultOutput;
|
||||
this.selectedOutput = this.currentNucleus;
|
||||
}
|
||||
|
||||
@ -558,11 +540,6 @@ namespace NanoBrain {
|
||||
AddInput(selectedType, this.currentNucleus);
|
||||
}
|
||||
return connecting;
|
||||
// if (selectedType == Nucleus.Type.None)
|
||||
// return false;
|
||||
|
||||
// AddInput(selectedType, this.currentNucleus);
|
||||
// return true;
|
||||
}
|
||||
|
||||
protected virtual void ChangeSynapse(Synapse synapse, Neuron newNucleus) {
|
||||
@ -596,8 +573,8 @@ namespace NanoBrain {
|
||||
// }
|
||||
// else {
|
||||
// it is a neuron in a subcluster
|
||||
synapseNeuron.RemoveReceiver(this.currentPrefabNucleus);
|
||||
newNucleus.AddReceiver(this.currentPrefabNucleus);
|
||||
synapseNeuron.RemoveReceiver(this.currentNucleus);
|
||||
newNucleus.AddReceiver(this.currentNucleus);
|
||||
// }
|
||||
}
|
||||
else {
|
||||
@ -606,19 +583,6 @@ namespace NanoBrain {
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void DisconnectNucleus(Neuron nucleus) {
|
||||
if (this.currentNucleus.clusterPrefab == null)
|
||||
return;
|
||||
string[] names = this.currentNucleus.synapses.Select(synapse => synapse.neuron.name).ToArray();
|
||||
int selectedIndex = -1;
|
||||
selectedIndex = EditorGUILayout.Popup("Disconnect from", selectedIndex, names);
|
||||
if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.clusterPrefab.nuclei.Count) {
|
||||
Synapse synapse = this.currentNucleus.synapses[selectedIndex];
|
||||
Neuron synapseNeuron = synapse.neuron as Neuron;
|
||||
synapseNeuron.RemoveReceiver(this.currentNucleus);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Synapses
|
||||
|
||||
#endregion Inspector
|
||||
|
||||
@ -1,2 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1fc1fb7db9f7ad54a87d31313e7f457d
|
||||
guid: 1fc1fb7db9f7ad54a87d31313e7f457d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
@ -106,6 +106,8 @@ namespace NanoBrain {
|
||||
|
||||
public void SetGraph(GameObject gameObject) {
|
||||
this.gameObject = gameObject;
|
||||
if (this.currentCluster == null)
|
||||
return;
|
||||
|
||||
if (Application.isPlaying == false)
|
||||
this.serializedBrain = new SerializedObject(this.currentCluster.prefab);
|
||||
@ -128,7 +130,7 @@ namespace NanoBrain {
|
||||
}
|
||||
|
||||
public void OnIMGUI() {
|
||||
if (Application.isPlaying == false)
|
||||
if (Application.isPlaying == false && serializedBrain != null)
|
||||
serializedBrain.Update();
|
||||
|
||||
Handles.BeginGUI();
|
||||
@ -211,7 +213,8 @@ namespace NanoBrain {
|
||||
}
|
||||
|
||||
private void DescendGraph(Dag.Node receiver, ref int ix, Dag dag) {
|
||||
foreach (Synapse synapse in receiver.nucleus.synapses) {
|
||||
Neuron receiverNeuron = receiver.nucleus as Neuron;
|
||||
foreach (Synapse synapse in receiverNeuron.synapses) {
|
||||
Nucleus nucleus = synapse.neuron;
|
||||
if (nucleus.parent != null && nucleus.parent != currentNucleus.parent) {
|
||||
nucleus = nucleus.parent;
|
||||
@ -384,6 +387,9 @@ namespace NanoBrain {
|
||||
}
|
||||
|
||||
protected void DrawSynapses(Nucleus nucleus, Vector3 parentPos, float size) {
|
||||
if (nucleus is not Neuron neuron)
|
||||
return;
|
||||
|
||||
if (this.selectedSynapseNeuron != null) {
|
||||
DrawClusterSynapses(this.selectedSynapseNeuron, parentPos, size);
|
||||
return;
|
||||
@ -396,7 +402,7 @@ namespace NanoBrain {
|
||||
float maxValue = 0;
|
||||
int neuronCount = 0;
|
||||
List<string> drawnNeuronNames = new();
|
||||
foreach (Synapse synapse in nucleus.synapses) {
|
||||
foreach (Synapse synapse in neuron.synapses) {
|
||||
if (synapse.neuron == null)
|
||||
continue;
|
||||
|
||||
@ -423,7 +429,7 @@ namespace NanoBrain {
|
||||
int row = 0;
|
||||
//List<Neuron> drawnNeurons = new();
|
||||
drawnNeuronNames = new();
|
||||
foreach (Synapse synapse in nucleus.synapses) {
|
||||
foreach (Synapse synapse in neuron.synapses) {
|
||||
if (synapse.neuron is null)
|
||||
continue;
|
||||
|
||||
@ -482,14 +488,15 @@ namespace NanoBrain {
|
||||
maxValue = 1;
|
||||
float brightness = siblingNeuron.outputMagnitude / maxValue;
|
||||
color = new Color(brightness, brightness, brightness, 1f);
|
||||
} DrawNucleus(siblingNeuron, position, size, color);
|
||||
}
|
||||
DrawNucleus(siblingNeuron, position, size, color);
|
||||
GUIStyle style = new(EditorStyles.label) {
|
||||
alignment = TextAnchor.UpperCenter,
|
||||
normal = { textColor = Color.white },
|
||||
fontStyle = FontStyle.Bold,
|
||||
};
|
||||
Vector3 labelPos = position - Vector3.down * (size + 5); // below neuron
|
||||
string name = $"{sibling.baseName}.{nucleus.name}";
|
||||
string name = $"{sibling.baseName}\n{nucleus.name}";
|
||||
Handles.Label(labelPos, name, style);
|
||||
row++;
|
||||
}
|
||||
@ -497,6 +504,9 @@ namespace NanoBrain {
|
||||
}
|
||||
|
||||
protected void DrawOutputs(Vector2 parentPos, float size) {
|
||||
if (this.currentCluster == null)
|
||||
return;
|
||||
|
||||
// Determine the maximum value in this layer
|
||||
// This is used to 'scale' the output value colors of the nuclei
|
||||
float maxValue = 0;
|
||||
@ -603,18 +613,12 @@ namespace NanoBrain {
|
||||
if (nucleus.parent != null && currentNucleus != null && nucleus.parent != currentNucleus.parent && nucleus.parent is Cluster parentCluster1) {
|
||||
// This neuron is part of another cluster
|
||||
parentCluster1.name ??= "";
|
||||
string baseName = "";
|
||||
int colonPos = parentCluster1.name.IndexOf(":");
|
||||
string baseName;
|
||||
if (colonPos > 0 && colonPos < parentCluster1.name.Length - 2)
|
||||
baseName = parentCluster1.name[..colonPos] + ".";
|
||||
baseName = parentCluster1.name[..colonPos] + "\n";
|
||||
else
|
||||
baseName = parentCluster1.name + ".";
|
||||
// if (colonPos > 0 && colonPos < parentCluster1.name.Length - 2) {
|
||||
// // if it is an array, we should not show the :0 of the first element
|
||||
// //baseName = baseName[..colonPos];
|
||||
// Handles.Label(labelPos, baseName + nucleus.name, style);
|
||||
// }
|
||||
// else
|
||||
baseName = parentCluster1.name + "\n";
|
||||
Handles.Label(labelPos, baseName + nucleus.name, style);
|
||||
}
|
||||
else {
|
||||
@ -849,22 +853,34 @@ namespace NanoBrain {
|
||||
|
||||
void OnSceneGUI(SceneView sceneView) {
|
||||
if (this.gameObject != null) {
|
||||
// if (this.currentNucleus is IReceptor receptor) {
|
||||
// foreach (Nucleus nucleus in receptor.nucleiArray) {
|
||||
// if (nucleus is Neuron neuron) {
|
||||
// Vector3 worldVector = this.gameObject.transform.TransformVector(neuron.outputValue);
|
||||
// Handles.color = Color.yellow;
|
||||
// Handles.DrawLine(this.gameObject.transform.position, this.gameObject.transform.position + worldVector);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
if (this.currentNucleus is Neuron currentNeuron) {
|
||||
Vector3 worldVector = this.gameObject.transform.TransformVector(currentNeuron.outputValue);
|
||||
Handles.color = Color.yellow;
|
||||
Handles.DrawLine(this.gameObject.transform.position, this.gameObject.transform.position + worldVector);
|
||||
|
||||
Handles.color = Color.yellow;
|
||||
if (this.selectedSynapseNeuron != null) {
|
||||
foreach (Cluster sibling in this.selectedSynapseNeuron.parent.siblingClusters) {
|
||||
Neuron siblingNeuron = sibling.GetNucleus(this.selectedSynapseNeuron.name) as Neuron;
|
||||
Vector3 worldVector = this.gameObject.transform.TransformVector(siblingNeuron.outputValue);
|
||||
Handles.DrawLine(this.gameObject.transform.position, this.gameObject.transform.position + worldVector);
|
||||
}
|
||||
// if (this.currentNucleus is Cluster cluster) {
|
||||
// foreach (Cluster sibling in cluster.siblingClusters) {
|
||||
|
||||
// }
|
||||
// }
|
||||
// // if (this.currentNucleus is IReceptor receptor) {
|
||||
// // foreach (Nucleus nucleus in receptor.nucleiArray) {
|
||||
// // if (nucleus is Neuron neuron) {
|
||||
// // Vector3 worldVector = this.gameObject.transform.TransformVector(neuron.outputValue);
|
||||
// // Handles.color = Color.yellow;
|
||||
// // Handles.DrawLine(this.gameObject.transform.position, this.gameObject.transform.position + worldVector);
|
||||
// // }
|
||||
// // }
|
||||
}
|
||||
else {
|
||||
if (this.currentNucleus is Neuron currentNeuron) {
|
||||
Vector3 worldVector = this.gameObject.transform.TransformVector(currentNeuron.outputValue);
|
||||
Handles.DrawLine(this.gameObject.transform.position, this.gameObject.transform.position + worldVector);
|
||||
}
|
||||
}
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,2 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4fe58945c76d153edacc220597474ad2
|
||||
guid: 4fe58945c76d153edacc220597474ad2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
@ -1,2 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c7539a20f7894542ca347730cd8417b1
|
||||
guid: c7539a20f7894542ca347730cd8417b1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
83
Editor/Neuron_Drawer.cs
Normal file
83
Editor/Neuron_Drawer.cs
Normal file
@ -0,0 +1,83 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using Unity.Mathematics;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Collections;
|
||||
|
||||
namespace NanoBrain {
|
||||
[CustomPropertyDrawer(typeof(Neuron))]
|
||||
class Neuron_Drawer : PropertyDrawer {
|
||||
public static void Insepctor(SerializedObject serializedObject, string propertyName ) {
|
||||
EditorGUILayout.PropertyField(serializedObject.FindProperty(propertyName));
|
||||
}
|
||||
|
||||
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
|
||||
// Draw foldout + properties
|
||||
label = EditorGUI.BeginProperty(position, label, property);
|
||||
|
||||
// Begin indent block
|
||||
int indent = EditorGUI.indentLevel;
|
||||
EditorGUI.indentLevel = 0;
|
||||
|
||||
object instance = GetTargetObjectOfProperty(property);
|
||||
|
||||
float lineHeight = EditorGUIUtility.singleLineHeight;
|
||||
Rect r = new(position.x, position.y, position.width, lineHeight);
|
||||
if (instance != null) {
|
||||
FieldInfo field = typeof(Neuron).GetField("_outputValue", BindingFlags.NonPublic | BindingFlags.Instance);
|
||||
if (field != null) {
|
||||
float3 val = (float3)field.GetValue(instance);
|
||||
EditorGUI.Vector3Field(r, $"Neuron: {label}", val);
|
||||
}
|
||||
}
|
||||
|
||||
EditorGUI.indentLevel = indent;
|
||||
EditorGUI.EndProperty();
|
||||
}
|
||||
|
||||
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
|
||||
// height for 1 line
|
||||
return (EditorGUIUtility.singleLineHeight * 1) + (EditorGUIUtility.standardVerticalSpacing * 0);
|
||||
}
|
||||
|
||||
public static object GetTargetObjectOfProperty(SerializedProperty prop) {
|
||||
var path = prop.propertyPath.Replace(".Array.data[", "[");
|
||||
object obj = prop.serializedObject.targetObject;
|
||||
var elements = path.Split('.');
|
||||
foreach (var element in elements) {
|
||||
if (element.Contains("[")) {
|
||||
var elementName = element.Substring(0, element.IndexOf("["));
|
||||
var index = Convert.ToInt32(element.Substring(element.IndexOf("[")).Replace("[", "").Replace("]", ""));
|
||||
obj = GetValue_Imp(obj, elementName, index);
|
||||
}
|
||||
else {
|
||||
obj = GetValue_Imp(obj, element);
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
static object GetValue_Imp(object source, string name) {
|
||||
if (source == null)
|
||||
return null;
|
||||
|
||||
Type t = source.GetType();
|
||||
FieldInfo f = t.GetField(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
|
||||
if (f != null)
|
||||
return f.GetValue(source);
|
||||
PropertyInfo p = t.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
|
||||
return p?.GetValue(source, null);
|
||||
}
|
||||
static object GetValue_Imp(object source, string name, int index) {
|
||||
if (GetValue_Imp(source, name) is not IEnumerable enumerable)
|
||||
return null;
|
||||
IEnumerator en = enumerable.GetEnumerator();
|
||||
for (int i = 0; i <= index; i++) {
|
||||
if (!en.MoveNext())
|
||||
return null;
|
||||
}
|
||||
return en.Current;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Editor/Neuron_Drawer.cs.meta
Normal file
11
Editor/Neuron_Drawer.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aa0e340763ca6299e93d514b271ae38d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -1,8 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a4c7dfe43bdf504e29c5c97919d7a1c0
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
fileFormatVersion: 2
|
||||
guid: a4c7dfe43bdf504e29c5c97919d7a1c0
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6a602cec2c4009925b1d19ed36a98c6a
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
fileFormatVersion: 2
|
||||
guid: 6a602cec2c4009925b1d19ed36a98c6a
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9b84f664459d02b90894e460de42c219
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
fileFormatVersion: 2
|
||||
guid: 9b84f664459d02b90894e460de42c219
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
//#if !UNITY_5_6_OR_NEWER
|
||||
#if !UNITY_5_6_OR_NEWER
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
@ -268,4 +268,4 @@ namespace LinearAlgebra.Test {
|
||||
|
||||
}
|
||||
}
|
||||
//#endif
|
||||
#endif
|
||||
@ -45,7 +45,7 @@ namespace NanoBrain {
|
||||
/// <param name="name">The name of the Neuron for which the weights are updated</param>
|
||||
/// <param name="weight">The new Synapse weight</param>
|
||||
public static void UpdateWeight(Cluster brain, string name, float weight) {
|
||||
Nucleus root = brain.defaultOutput;
|
||||
Neuron root = brain.defaultOutput;
|
||||
foreach (Synapse synapse in root.synapses) {
|
||||
if (synapse.neuron.name == name) {
|
||||
if (synapse.weight != weight) {
|
||||
|
||||
@ -1,2 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 92f34a5e4027a1dc39efd8ce63cf6aba
|
||||
guid: 92f34a5e4027a1dc39efd8ce63cf6aba
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
@ -41,7 +41,7 @@ namespace NanoBrain {
|
||||
public Dictionary<int, Cluster> thingClusters = new();
|
||||
|
||||
[SerializeReference]
|
||||
public List<Nucleus> clusterNuclei = new();
|
||||
public List<Nucleus> nuclei = new();
|
||||
// the nuclei sorted using topological sorting
|
||||
// to ensure that the cluster is computer in the right order
|
||||
public List<Nucleus> sortedNuclei;
|
||||
@ -58,10 +58,10 @@ namespace NanoBrain {
|
||||
this.name = prefab.name;
|
||||
|
||||
this.parent = parent;
|
||||
this.parent?.clusterNuclei.Add(this);
|
||||
this.parent?.nuclei.Add(this);
|
||||
ClonePrefab();
|
||||
_ = this.inputs;
|
||||
this.sortedNuclei = TopologicalSort(this.clusterNuclei);
|
||||
this.sortedNuclei = TopologicalSort(this.nuclei);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -72,14 +72,15 @@ namespace NanoBrain {
|
||||
public Cluster(ClusterPrefab prefab, ClusterPrefab parent = null) {
|
||||
this.prefab = prefab;
|
||||
this.name = prefab.name;
|
||||
this.clusterPrefab = parent;
|
||||
if (parent != null)
|
||||
this.parent = parent.cluster;
|
||||
|
||||
if (this.clusterPrefab != null)
|
||||
this.clusterPrefab.nuclei.Add(this);
|
||||
// if (this.parent.prefab != null)
|
||||
// this.parent.prefab.cluster.nuclei.Add(this);
|
||||
|
||||
ClonePrefab();
|
||||
_ = this.inputs;
|
||||
this.sortedNuclei = TopologicalSort(this.clusterNuclei);
|
||||
this.sortedNuclei = TopologicalSort(this.nuclei);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -88,15 +89,13 @@ namespace NanoBrain {
|
||||
/// Strange that this does not take any parameters or return values.
|
||||
/// Where which the clone be found???
|
||||
private void ClonePrefab() {
|
||||
Nucleus[] prefabNuclei = this.prefab.nuclei.ToArray();
|
||||
Nucleus[] prefabNuclei = this.prefab.cluster.nuclei.ToArray();
|
||||
|
||||
// first clone the nuclei without their connections
|
||||
foreach (Nucleus nucleus in prefabNuclei) {
|
||||
nucleus.ShallowCloneTo(this);
|
||||
}
|
||||
Nucleus[] clonedNuclei = this.clusterNuclei.ToArray();
|
||||
// foreach (Nucleus n in clonedNuclei)
|
||||
// n.name += "(c)";
|
||||
Nucleus[] clonedNuclei = this.nuclei.ToArray();
|
||||
|
||||
// Now clone the connections
|
||||
for (int nucleusIx = 0; nucleusIx < prefabNuclei.Length; nucleusIx++) {
|
||||
@ -110,10 +109,10 @@ namespace NanoBrain {
|
||||
|
||||
foreach (Synapse prefabSynapse in prefabNeuron.synapses) {
|
||||
Neuron synapseNeuron = prefabSynapse.neuron;
|
||||
if (synapseNeuron.clusterPrefab != null && synapseNeuron.clusterPrefab != this.prefab) {
|
||||
if (synapseNeuron.parent.prefab != null && synapseNeuron.parent.prefab != this.prefab) {
|
||||
// Neuron is in another cluster, find the cloned cluster first
|
||||
ClusterPrefab prefabCluster = synapseNeuron.clusterPrefab;
|
||||
Cluster clonedCluster = this.clusterNuclei.Find(n => n.name == prefabCluster.name) as Cluster;
|
||||
Cluster prefabCluster = synapseNeuron.parent;
|
||||
Cluster clonedCluster = this.nuclei.Find(n => n.name == prefabCluster.name) as Cluster;
|
||||
if (clonedCluster == null)
|
||||
continue;
|
||||
|
||||
@ -122,7 +121,7 @@ namespace NanoBrain {
|
||||
if (neuronIx < 0)
|
||||
// Could not find the neuron in the prefab cluster
|
||||
continue;
|
||||
if (clonedCluster.clusterNuclei[neuronIx] is not Neuron clonedSender)
|
||||
if (clonedCluster.nuclei[neuronIx] is not Neuron clonedSender)
|
||||
// Could not find the neuron in the cloned cluster
|
||||
continue;
|
||||
|
||||
@ -141,28 +140,6 @@ namespace NanoBrain {
|
||||
// Debug.Log($"Add synapse {clonedSender.name} -> {clonedNeuron.name}");
|
||||
}
|
||||
}
|
||||
|
||||
// // Copy the receivers, which will also create the synapses
|
||||
// foreach (Nucleus receiver in prefabNeuron.receivers.ToArray()) {
|
||||
// int ix = GetNucleusIndex(prefabNuclei, receiver);
|
||||
// if (ix < 0)
|
||||
// continue;
|
||||
|
||||
// if (clonedNuclei[ix] is not Nucleus clonedReceiver)
|
||||
// continue;
|
||||
|
||||
// // Find the synapse for the weight
|
||||
// float weight = 1;
|
||||
// foreach (Synapse synapse in receiver.synapses) {
|
||||
// // Find the weight for this synapse
|
||||
// if (synapse.neuron == prefabNucleus) {
|
||||
// weight = synapse.weight;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// clonedNeuron.AddReceiver(clonedReceiver, weight);
|
||||
// }
|
||||
}
|
||||
|
||||
if (Application.isPlaying) {
|
||||
@ -179,7 +156,7 @@ namespace NanoBrain {
|
||||
Debug.Log($"create {clonedCluster.prefab.name} sibling");
|
||||
Cluster sibling = new(clonedCluster.prefab, this) {
|
||||
name = $"{clonedCluster.baseName}: {instanceIx}",
|
||||
clusterPrefab = this.clusterPrefab,
|
||||
parent = this.parent,
|
||||
instanceCount = this.instanceCount,
|
||||
};
|
||||
siblings.Add(sibling);
|
||||
@ -189,134 +166,47 @@ namespace NanoBrain {
|
||||
foreach (Cluster sibling in siblings)
|
||||
sibling.siblingClusters = siblingClusters;
|
||||
}
|
||||
}
|
||||
/*
|
||||
for (int nucleusIx = 0; nucleusIx < clonedNuclei.Length; nucleusIx++) {
|
||||
Nucleus prefabNucleus = prefabNuclei[nucleusIx];
|
||||
if (prefabNucleus is not Cluster prefabCluster)
|
||||
continue;
|
||||
|
||||
if (prefabCluster.instanceCount <= 1)
|
||||
continue;
|
||||
|
||||
Cluster clonedNucleus = clonedNuclei[nucleusIx] as Cluster;
|
||||
if (prefabCluster == prefabCluster.siblingClusters[0]) {
|
||||
// We clone the array only for the first entry
|
||||
//NucleusArray clonedArray = new(prefabReceptor.nucleiArray.Length);
|
||||
Cluster[] clonedArray = new Cluster[prefabCluster.siblingClusters.Length];
|
||||
int arrayIx = 0;
|
||||
foreach (Cluster prefabArrayNucleus in prefabCluster.siblingClusters) {
|
||||
int arrayNucleusIx = GetNucleusIndex(prefabNuclei, prefabArrayNucleus);
|
||||
if (arrayNucleusIx >= 0) {
|
||||
Cluster clonedArrayNucleus = clonedNuclei[arrayNucleusIx] as Cluster;
|
||||
clonedArray[arrayIx] = clonedArrayNucleus;
|
||||
}
|
||||
else {
|
||||
Debug.LogError($" Could not find prefab nucleus {prefabNucleus.name} in the clones");
|
||||
}
|
||||
arrayIx++;
|
||||
}
|
||||
clonedNucleus.siblingClusters = clonedArray;
|
||||
}
|
||||
else {
|
||||
// The others will refer to the array created for the first nucleus in the array
|
||||
int firstNucleusIx = GetNucleusIndex(prefabNuclei, prefabCluster.siblingClusters[0]);
|
||||
Cluster clonedFirstNucleus = clonedNuclei[firstNucleusIx] as Cluster;
|
||||
clonedNucleus.siblingClusters = clonedFirstNucleus.siblingClusters;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// Collect the subclusters
|
||||
List<Cluster> subClusters = new();
|
||||
foreach (Nucleus nucleus in prefabNuclei) {
|
||||
foreach (Synapse synapse in nucleus.synapses) {
|
||||
Nucleus synapseNucleus = synapse.neuron;
|
||||
Cluster subCluster = synapseNucleus.parent;
|
||||
if (subCluster is null ||
|
||||
synapseNucleus.clusterPrefab == this.clusterPrefab) {
|
||||
|
||||
continue;
|
||||
}
|
||||
// if (synapseNucleus is not Cluster subCluster)
|
||||
// continue;
|
||||
if (subClusters.Contains(subCluster))
|
||||
continue;
|
||||
subClusters.Add(subCluster);
|
||||
}
|
||||
}
|
||||
// Create the subcluster instances
|
||||
foreach (Cluster subCluster in subClusters) {
|
||||
for (int ix = 0; ix < subCluster.instanceCount; ix++) {
|
||||
// create the new instance
|
||||
Cluster clusterInstance = new(subCluster.prefab);
|
||||
// connect it
|
||||
foreach ((Neuron sender, Nucleus receiver) in subCluster.CollectConnections()) {
|
||||
int receiverIx = GetNucleusIndex(prefabNuclei, receiver);
|
||||
if (receiverIx < 0)
|
||||
continue;
|
||||
|
||||
if (clonedNuclei[receiverIx] is not Nucleus clonedReceiver)
|
||||
continue;
|
||||
|
||||
// Find the synapse for the weight
|
||||
float weight = 1;
|
||||
foreach (Synapse synapse in receiver.synapses) {
|
||||
// Find the weight for this synapse
|
||||
if (synapse.neuron == sender) {
|
||||
weight = synapse.weight;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (clusterInstance.GetNucleus(sender.name) is not Neuron clonedSender)
|
||||
continue;
|
||||
|
||||
clonedSender.AddReceiver(clonedReceiver, weight);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// foreach (Nucleus nucleus in this.clusterNuclei) {
|
||||
// if (nucleus is Cluster clonedSubCluster)
|
||||
// RestoreAllExternalReceivers(clonedSubCluster, this.prefab, this);
|
||||
// }
|
||||
}
|
||||
|
||||
private void CloneSynapses(Neuron prefabNeuron, Neuron clonedNeuron) {
|
||||
foreach (Synapse prefabSynapse in prefabNeuron.synapses) {
|
||||
Neuron synapseNeuron = prefabSynapse.neuron;
|
||||
if (synapseNeuron.clusterPrefab != null && synapseNeuron.clusterPrefab != this.prefab) {
|
||||
// Neuron is in another cluster, find the cloned cluster first
|
||||
ClusterPrefab prefabCluster = synapseNeuron.clusterPrefab;
|
||||
Cluster clonedCluster = this.clusterNuclei.Find(n => n.name == prefabCluster.name) as Cluster;
|
||||
if (clonedCluster == null)
|
||||
continue;
|
||||
|
||||
// Now find the neuron in that cloned cluster
|
||||
int neuronIx = GetNucleusIndex(prefabCluster.nuclei, prefabSynapse.neuron.name);
|
||||
if (neuronIx < 0)
|
||||
// Could not find the neuron in the prefab cluster
|
||||
continue;
|
||||
if (clonedCluster.clusterNuclei[neuronIx] is not Neuron clonedSender)
|
||||
// Could not find the neuron in the cloned cluster
|
||||
continue;
|
||||
|
||||
clonedSender.AddReceiver(clonedNeuron, prefabSynapse.weight);
|
||||
//Debug.Log($"Add synapse {clonedCluster.name}.{clonedSender.name} -> {clonedNeuron.name} [{clonedSender.receivers.Count}]");
|
||||
}
|
||||
else {
|
||||
Neuron clonedSender = this.clusterNuclei.Find(n => n.name == prefabSynapse.neuron.name) as Neuron;
|
||||
// Copy the receivers which will also create the synapse
|
||||
clonedSender.AddReceiver(clonedNeuron, prefabSynapse.weight);
|
||||
// Debug.Log($"Add synapse {clonedSender.name} -> {clonedNeuron.name}");
|
||||
// Ensure that all neurons are computed to initialize bias
|
||||
foreach (Nucleus clonedNucleus in clonedNuclei) {
|
||||
if (clonedNucleus is not Cluster)
|
||||
clonedNucleus.UpdateStateIsolated();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// private void CloneSynapses(Neuron prefabNeuron, Neuron clonedNeuron) {
|
||||
// foreach (Synapse prefabSynapse in prefabNeuron.synapses) {
|
||||
// Neuron synapseNeuron = prefabSynapse.neuron;
|
||||
// if (synapseNeuron.parent.prefab != null && synapseNeuron.parent.prefab != this.prefab) {
|
||||
// // Neuron is in another cluster, find the cloned cluster first
|
||||
// ClusterPrefab prefabCluster = synapseNeuron.parent.prefab;
|
||||
// Cluster clonedCluster = this.nuclei.Find(n => n.name == prefabCluster.name) as Cluster;
|
||||
// if (clonedCluster == null)
|
||||
// continue;
|
||||
|
||||
// // Now find the neuron in that cloned cluster
|
||||
// int neuronIx = GetNucleusIndex(prefabCluster.cluster.nuclei, prefabSynapse.neuron.name);
|
||||
// if (neuronIx < 0)
|
||||
// // Could not find the neuron in the prefab cluster
|
||||
// continue;
|
||||
// if (clonedCluster.nuclei[neuronIx] is not Neuron clonedSender)
|
||||
// // Could not find the neuron in the cloned cluster
|
||||
// continue;
|
||||
|
||||
// clonedSender.AddReceiver(clonedNeuron, prefabSynapse.weight);
|
||||
// //Debug.Log($"Add synapse {clonedCluster.name}.{clonedSender.name} -> {clonedNeuron.name} [{clonedSender.receivers.Count}]");
|
||||
// }
|
||||
// else {
|
||||
// Neuron clonedSender = this.nuclei.Find(n => n.name == prefabSynapse.neuron.name) as Neuron;
|
||||
// // Copy the receivers which will also create the synapse
|
||||
// clonedSender.AddReceiver(clonedNeuron, prefabSynapse.weight);
|
||||
// // Debug.Log($"Add synapse {clonedSender.name} -> {clonedNeuron.name}");
|
||||
// }
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Sort the nuclei in a correct evaluation order
|
||||
/// </summary>
|
||||
@ -378,20 +268,20 @@ namespace NanoBrain {
|
||||
public override Nucleus Clone(ClusterPrefab parent) {
|
||||
Cluster clone = new(this.prefab, parent);
|
||||
|
||||
foreach (Synapse synapse in this.synapses) {
|
||||
Synapse clonedSynapse = clone.AddSynapse(synapse.neuron);
|
||||
clonedSynapse.weight = synapse.weight;
|
||||
}
|
||||
// foreach (Synapse synapse in this.synapses) {
|
||||
// Synapse clonedSynapse = clone.AddSynapse(synapse.neuron);
|
||||
// clonedSynapse.weight = synapse.weight;
|
||||
// }
|
||||
|
||||
foreach (Nucleus nucleus in this.clusterNuclei) {
|
||||
foreach (Nucleus nucleus in this.nuclei) {
|
||||
if (nucleus is Neuron output) {
|
||||
foreach (Nucleus receiver in output.receivers) {
|
||||
int ix = GetNucleusIndex(this.clusterNuclei, output);
|
||||
int ix = GetNucleusIndex(this.nuclei, output);
|
||||
Debug.Log($"{output.name} -> {receiver.name}: {ix}");
|
||||
if (ix < 0)
|
||||
continue;
|
||||
|
||||
if (clone.clusterNuclei[ix] is not Neuron clonedOutput)
|
||||
if (clone.nuclei[ix] is not Neuron clonedOutput)
|
||||
continue;
|
||||
|
||||
clonedOutput.AddReceiver(receiver);
|
||||
@ -406,7 +296,7 @@ namespace NanoBrain {
|
||||
// Clusters should not be cloned, but instantiated from the prefab....
|
||||
Cluster clone = new(this.prefab, parent) {
|
||||
name = this.name,
|
||||
clusterPrefab = this.clusterPrefab,
|
||||
parent = this.parent,
|
||||
instanceCount = this.instanceCount,
|
||||
};
|
||||
// Somehow siblingClusters should be cloned too. Believe I do this in ClonePrefab right now.
|
||||
@ -416,23 +306,26 @@ namespace NanoBrain {
|
||||
|
||||
private static void CopyAllExternalReceivers(Cluster sourceCluster, Cluster sibling, ClusterPrefab prefabParent, Cluster clonedParent) {
|
||||
|
||||
for (int nucleusIx = 0; nucleusIx < sourceCluster.clusterNuclei.Count; nucleusIx++) {
|
||||
Nucleus sourceNucleus = sourceCluster.clusterNuclei[nucleusIx];
|
||||
for (int nucleusIx = 0; nucleusIx < sourceCluster.nuclei.Count; nucleusIx++) {
|
||||
Nucleus sourceNucleus = sourceCluster.nuclei[nucleusIx];
|
||||
if (sourceNucleus is not Neuron sourceNeuron)
|
||||
continue;
|
||||
|
||||
if (sibling.clusterNuclei[nucleusIx] is not Neuron clonedNeuron)
|
||||
if (sibling.nuclei[nucleusIx] is not Neuron clonedNeuron)
|
||||
continue;
|
||||
|
||||
// copy the receivers (and thus synapses) from the source to the sibling
|
||||
foreach (Nucleus receiver in sourceNeuron.receivers) {
|
||||
int ix = GetNucleusIndex(clonedParent.clusterNuclei, receiver);
|
||||
if (ix < 0 || ix >= clonedParent.clusterNuclei.Count)
|
||||
if (receiver is not Neuron receiverNeuron)
|
||||
continue;
|
||||
|
||||
int ix = GetNucleusIndex(clonedParent.nuclei, receiver);
|
||||
if (ix < 0 || ix >= clonedParent.nuclei.Count)
|
||||
continue;
|
||||
|
||||
// Find the synapse for the weight
|
||||
float weight = 1;
|
||||
foreach (Synapse synapse in receiver.synapses) {
|
||||
foreach (Synapse synapse in receiverNeuron.synapses) {
|
||||
// Find the weight for this synapse
|
||||
if (synapse.neuron == sourceNucleus) {
|
||||
weight = synapse.weight;
|
||||
@ -530,42 +423,43 @@ namespace NanoBrain {
|
||||
}
|
||||
}
|
||||
|
||||
public virtual Cluster GetThingCluster() {
|
||||
Cluster selectedCluster = SelectCluster();
|
||||
return selectedCluster;
|
||||
}
|
||||
public virtual Cluster GetThingCluster(int thingId, string thingName = null) {
|
||||
if (thingClusters.TryGetValue(thingId, out Cluster cluster))
|
||||
return cluster;
|
||||
// public virtual Cluster GetThingCluster() {
|
||||
// Cluster selectedCluster = SelectCluster();
|
||||
// return selectedCluster;
|
||||
// }
|
||||
// public virtual Cluster GetThingCluster(int thingId, string thingName = null) {
|
||||
// if (thingClusters.TryGetValue(thingId, out Cluster cluster))
|
||||
// return cluster;
|
||||
|
||||
Cluster selectedCluster = SelectCluster();
|
||||
selectedCluster.name = baseName + ": " + thingName;
|
||||
thingClusters[thingId] = selectedCluster;
|
||||
return selectedCluster;
|
||||
}
|
||||
// Cluster selectedCluster = SelectCluster();
|
||||
// selectedCluster.name = baseName + ": " + thingName;
|
||||
// thingClusters[thingId] = selectedCluster;
|
||||
// return selectedCluster;
|
||||
// }
|
||||
|
||||
private Cluster SelectCluster() {
|
||||
if (this.siblingClusters == null)
|
||||
return this;
|
||||
// private Cluster SelectCluster() {
|
||||
// if (this.siblingClusters == null)
|
||||
// return this;
|
||||
|
||||
// Find a sleeping cluster
|
||||
foreach (Cluster cluster in this.siblingClusters) {
|
||||
if (cluster.defaultOutput.isSleeping) {
|
||||
RemoveThingCluster(cluster);
|
||||
return cluster;
|
||||
}
|
||||
}
|
||||
// // Find a sleeping cluster
|
||||
// // foreach (Cluster cluster in this.siblingClusters) {
|
||||
// // if (cluster.defaultOutput.isSleeping) {
|
||||
// // RemoveThingCluster(cluster);
|
||||
// // return cluster;
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// Otherwise find longest unused cluster
|
||||
Cluster unusedCluster = this.siblingClusters[0];
|
||||
for (int ix = 1; ix < this.siblingClusters.Length; ix++) {
|
||||
if (this.siblingClusters[ix].defaultOutput.lastUpdate < unusedCluster.defaultOutput.lastUpdate)
|
||||
unusedCluster = this.siblingClusters[ix];
|
||||
}
|
||||
// // Find longest unused cluster
|
||||
// // Note this uses the default output...
|
||||
// Cluster unusedCluster = this.siblingClusters[0];
|
||||
// for (int ix = 1; ix < this.siblingClusters.Length; ix++) {
|
||||
// if (this.siblingClusters[ix].defaultOutput.lastUpdate < unusedCluster.defaultOutput.lastUpdate)
|
||||
// unusedCluster = this.siblingClusters[ix];
|
||||
// }
|
||||
|
||||
RemoveThingCluster(unusedCluster);
|
||||
return unusedCluster;
|
||||
}
|
||||
// RemoveThingCluster(unusedCluster);
|
||||
// return unusedCluster;
|
||||
// }
|
||||
|
||||
private void RemoveThingCluster(Cluster cluster) {
|
||||
List<int> keysToRemove = new();
|
||||
@ -603,36 +497,34 @@ namespace NanoBrain {
|
||||
get {
|
||||
if (this._inputs == null) {
|
||||
this._inputs = new();
|
||||
foreach (Nucleus nucleus in this.clusterNuclei) {
|
||||
foreach (Nucleus nucleus in this.nuclei) {
|
||||
if (nucleus is not Neuron neuron)
|
||||
continue;
|
||||
|
||||
// inputs have no synapses
|
||||
if (nucleus.synapses.Count == 0)
|
||||
if (neuron.synapses.Count == 0)
|
||||
this._inputs.Add(nucleus);
|
||||
}
|
||||
ComputeOrders();
|
||||
RefreshComputeOrders();
|
||||
}
|
||||
return this._inputs;
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<Nucleus, List<Nucleus>> computeOrders = new();
|
||||
private void ComputeOrders() {
|
||||
foreach (Nucleus nucleus in this.clusterNuclei) {
|
||||
// if (nucleus is Cluster cluster) {
|
||||
// List<Synapse> synapses = this.CollectSynapsesTo(cluster);
|
||||
// foreach (Synapse synapse in synapses) {
|
||||
// computeOrders[synapse.neuron] = TopologicalSort2(synapse.neuron);
|
||||
// Debug.Log($"{this.baseName}: Order for {cluster.baseName}.{synapse.neuron.name}");
|
||||
// }
|
||||
// // List<Nucleus> receivers = cluster.CollectReceivers();
|
||||
// // foreach (Nucleus receiver in receivers)
|
||||
// // computeOrders[receiver] = TopologicalSort2(receiver);
|
||||
// }
|
||||
// else {
|
||||
computeOrders[nucleus] = TopologicalSort2(nucleus);
|
||||
Debug.Log($"{this.baseName} Order for {nucleus.name}");
|
||||
// }
|
||||
private Dictionary<Nucleus, List<Nucleus>> _computeOrders;
|
||||
public Dictionary<Nucleus, List<Nucleus>> computeOrders {
|
||||
get {
|
||||
if (_computeOrders == null || _computeOrders.Count == 0) {
|
||||
_computeOrders = new();
|
||||
foreach (Nucleus nucleus in this.nuclei)
|
||||
_computeOrders[nucleus] = TopologicalSort2(nucleus);
|
||||
}
|
||||
return _computeOrders;
|
||||
}
|
||||
}
|
||||
public void RefreshComputeOrders() {
|
||||
this._computeOrders = null;
|
||||
}
|
||||
|
||||
private List<Nucleus> TopologicalSort2(Nucleus startNode) {
|
||||
Dictionary<Nucleus, int> inDegree = new();
|
||||
@ -698,17 +590,17 @@ namespace NanoBrain {
|
||||
|
||||
public virtual Neuron defaultOutput {//=> this.nuclei[0] as Nucleus;
|
||||
get {
|
||||
if (this.clusterNuclei.Count > 0)
|
||||
return this.clusterNuclei[0] as Neuron;
|
||||
if (this.nuclei.Count > 0)
|
||||
return this.nuclei[0] as Neuron;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
protected List<Neuron> _outputs = null;
|
||||
public List<Neuron> outputs {
|
||||
get {
|
||||
if (this._outputs == null) {
|
||||
if (this._outputs == null || this._outputs.Count == 0) {
|
||||
this._outputs = new();
|
||||
foreach (Nucleus nucleus in this.clusterNuclei) {
|
||||
foreach (Nucleus nucleus in this.nuclei) {
|
||||
if (nucleus is Neuron neuron && neuron.receivers.Count == 0)
|
||||
this._outputs.Add(neuron);
|
||||
}
|
||||
@ -721,7 +613,7 @@ namespace NanoBrain {
|
||||
}
|
||||
|
||||
public bool TryGetNucleus(string nucleusName, out Nucleus foundNucleus) {
|
||||
foreach (Nucleus receptor in this.clusterNuclei) {
|
||||
foreach (Nucleus receptor in this.nuclei) {
|
||||
if (receptor is Nucleus nucleus)
|
||||
if (nucleus.name == nucleusName) {
|
||||
foundNucleus = nucleus;
|
||||
@ -737,7 +629,7 @@ namespace NanoBrain {
|
||||
if (dotPosition >= 0) {
|
||||
string clusterName = nucleusName[..dotPosition];
|
||||
string clusterName0 = clusterName + ": 0";
|
||||
foreach (Nucleus nucleus in this.clusterNuclei) {
|
||||
foreach (Nucleus nucleus in this.nuclei) {
|
||||
if (nucleus is Cluster cluster) {
|
||||
if (cluster.name == clusterName || cluster.name == clusterName0) {
|
||||
string subNucleusName = nucleusName[(dotPosition + 1)..];
|
||||
@ -749,9 +641,9 @@ namespace NanoBrain {
|
||||
}
|
||||
else {
|
||||
string nucleusName0 = nucleusName + ": 0";
|
||||
foreach (Nucleus nucleus in this.clusterNuclei) {
|
||||
if (nucleus is Cluster) { //IReceptor receptor) {
|
||||
if (nucleus.name == nucleusName | nucleus.name == nucleusName0)
|
||||
foreach (Nucleus nucleus in this.nuclei) {
|
||||
if (nucleus is Cluster) {
|
||||
if (nucleus.name == nucleusName || nucleus.name == nucleusName0)
|
||||
return nucleus;
|
||||
}
|
||||
else if (nucleus.name == nucleusName)
|
||||
@ -761,17 +653,70 @@ namespace NanoBrain {
|
||||
}
|
||||
}
|
||||
|
||||
public Neuron GetNeuron(string neuronName) {
|
||||
foreach (Nucleus nucleus in this.nuclei) {
|
||||
if (nucleus is Neuron neuron && neuron.name == neuronName)
|
||||
return neuron;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Neuron GetNeuron(int thingId, string neuronName, string thingName = null) {
|
||||
if (this.siblingClusters == null || this.siblingClusters.Length <= 1)
|
||||
return this.GetNeuron(neuronName);
|
||||
|
||||
// See if we are already using a cluster for thingId
|
||||
if (thingClusters.TryGetValue(thingId, out Cluster cluster))
|
||||
return cluster.GetNeuron(neuronName);
|
||||
|
||||
// Find the cluster with the lowest value neuron
|
||||
Neuron lowestNeuron = null;
|
||||
foreach (Cluster sibling in this.siblingClusters) {
|
||||
Neuron neuron = sibling.GetNeuron(neuronName);
|
||||
if (lowestNeuron == null || neuron.outputMagnitude < lowestNeuron.outputMagnitude)
|
||||
lowestNeuron = neuron;
|
||||
}
|
||||
Cluster selectedCluster = lowestNeuron.parent;
|
||||
RemoveThingCluster(selectedCluster);
|
||||
selectedCluster.name = baseName + ": " + thingName;
|
||||
thingClusters[thingId] = selectedCluster;
|
||||
return lowestNeuron;
|
||||
/*
|
||||
// Find a sleeping cluster
|
||||
// foreach (Cluster cluster in this.siblingClusters) {
|
||||
// if (cluster.defaultOutput.isSleeping) {
|
||||
// RemoveThingCluster(cluster);
|
||||
// return cluster;
|
||||
// }
|
||||
// }
|
||||
|
||||
// Find longest unused cluster
|
||||
// Note this uses the default output...
|
||||
Cluster unusedCluster = this.siblingClusters[0];
|
||||
for (int ix = 1; ix < this.siblingClusters.Length; ix++) {
|
||||
if (this.siblingClusters[ix].defaultOutput.lastUpdate < unusedCluster.defaultOutput.lastUpdate)
|
||||
unusedCluster = this.siblingClusters[ix];
|
||||
}
|
||||
|
||||
RemoveThingCluster(unusedCluster);
|
||||
//return unusedCluster;
|
||||
|
||||
Cluster cluster = GetThingCluster(thingId, thingName);
|
||||
Neuron neuron = cluster?.GetNeuron(neuronName);
|
||||
return neuron;
|
||||
*/
|
||||
}
|
||||
public bool DeleteNucleus(Nucleus nucleus) {
|
||||
if (this.clusterNuclei.Contains(nucleus) == false) {
|
||||
if (this.nuclei.Contains(nucleus) == false) {
|
||||
// Try to find the nucleus by name
|
||||
if (TryGetNucleus(nucleus.name, out nucleus) == false)
|
||||
return false;
|
||||
}
|
||||
|
||||
Neuron.Delete(nucleus);
|
||||
int nucleusIx = this.clusterNuclei.IndexOf(nucleus);
|
||||
this.clusterNuclei.Remove(nucleus);
|
||||
this.prefab.nuclei.RemoveAt(nucleusIx);
|
||||
//int nucleusIx = this.nuclei.IndexOf(nucleus);
|
||||
this.nuclei.Remove(nucleus);
|
||||
//this.prefab.cluster.nuclei.RemoveAt(nucleusIx);
|
||||
RefreshOutputs();
|
||||
|
||||
return true;
|
||||
@ -781,7 +726,7 @@ namespace NanoBrain {
|
||||
|
||||
public virtual List<Nucleus> CollectReceivers(bool removeDuplicates = false) {
|
||||
List<Nucleus> receivers = new();
|
||||
foreach (Nucleus outputNucleus in this.clusterNuclei) {
|
||||
foreach (Nucleus outputNucleus in this.nuclei) {
|
||||
if (outputNucleus is not Neuron output)
|
||||
continue;
|
||||
|
||||
@ -789,7 +734,7 @@ namespace NanoBrain {
|
||||
foreach (Nucleus receiver in output.receivers) {
|
||||
// Debug.Log($"output {receiver.name}");
|
||||
// Only add receivers outside this cluster
|
||||
if (receiver.clusterPrefab != this.prefab) {
|
||||
if (receiver.parent.prefab != this.prefab) {
|
||||
if (removeDuplicates == false || receivers.Contains(receiver) == false)
|
||||
// Debug.Log($" YES");
|
||||
receivers.Add(receiver);
|
||||
@ -802,13 +747,13 @@ namespace NanoBrain {
|
||||
public List<(Neuron, Nucleus)> CollectConnections() {
|
||||
List<(Neuron, Nucleus)> connections = new();
|
||||
|
||||
foreach (Nucleus outputNucleus in this.clusterNuclei) {
|
||||
foreach (Nucleus outputNucleus in this.nuclei) {
|
||||
if (outputNucleus is not Neuron output)
|
||||
continue;
|
||||
|
||||
foreach (Nucleus receiver in output.receivers) {
|
||||
// Only add receivers outside this cluster
|
||||
if (receiver.clusterPrefab != this.prefab)
|
||||
if (receiver.parent.prefab != this.prefab)
|
||||
connections.Add((output, receiver));
|
||||
}
|
||||
}
|
||||
@ -817,10 +762,10 @@ namespace NanoBrain {
|
||||
public List<Synapse> CollectSynapsesTo(Cluster otherCluster) {
|
||||
List<Synapse> collectedSynapses = new();
|
||||
|
||||
foreach (Nucleus nucleus in this.clusterNuclei) {
|
||||
foreach (Nucleus nucleus in this.nuclei) {
|
||||
if (nucleus is not Neuron neuron)
|
||||
continue;
|
||||
foreach (Synapse synapse in nucleus.synapses) {
|
||||
foreach (Synapse synapse in neuron.synapses) {
|
||||
if (synapse.neuron.parent == otherCluster)
|
||||
collectedSynapses.Add(synapse);
|
||||
}
|
||||
@ -830,7 +775,7 @@ namespace NanoBrain {
|
||||
|
||||
public void MoveReceivers(Cluster newCluster) {
|
||||
Debug.Log($"Move receivers for {this.name} to {newCluster.name}");
|
||||
foreach (Nucleus outputNucleus in this.clusterNuclei) {
|
||||
foreach (Nucleus outputNucleus in this.nuclei) {
|
||||
if (outputNucleus is not Neuron output)
|
||||
continue;
|
||||
|
||||
@ -842,11 +787,13 @@ namespace NanoBrain {
|
||||
Debug.Log($"Check {this.name}.{output.name} receivers");
|
||||
Nucleus[] receivers = output.receivers.ToArray();
|
||||
foreach (Nucleus receiver in receivers) {
|
||||
if (receiver.clusterPrefab != this.prefab) {
|
||||
if (receiver.parent.prefab != this.prefab) {
|
||||
// Replace synapse with new synapse
|
||||
// to the new cluster
|
||||
Debug.Log($"move {receiver.name} from {this.name}.{output.name} to {newCluster.name}.{newOutput.name}");
|
||||
Synapse synapse = receiver.GetSynapse(output);
|
||||
if (receiver is not Neuron receiverNeuron)
|
||||
continue;
|
||||
Synapse synapse = receiverNeuron.GetSynapse(output);
|
||||
newOutput.AddReceiver(receiver, synapse.weight);
|
||||
output.RemoveReceiver(receiver);
|
||||
}
|
||||
@ -867,41 +814,45 @@ namespace NanoBrain {
|
||||
}
|
||||
|
||||
List<Nucleus> computeOrder = this.computeOrders[startNucleus];
|
||||
//if (startNucleus.trace)
|
||||
Debug.Log($"Update from {startNucleus.name}");
|
||||
foreach (Nucleus nucleus in computeOrder) {
|
||||
if (nucleus is not Cluster) {
|
||||
nucleus.UpdateStateIsolated();
|
||||
//if (startNucleus.trace && nucleus is Neuron neuron)
|
||||
Debug.Log($" {nucleus.name}");
|
||||
if (nucleus is Neuron neuron) {
|
||||
foreach (Nucleus receiver in neuron.receivers) {
|
||||
if (receiver.parent != this) {
|
||||
Debug.Log($" External: {receiver.parent.name}.{receiver.name}");
|
||||
//Debug.Log($" External: {receiver.parent.name}.{receiver.name}");
|
||||
receiver.parent.UpdateFromNucleus(receiver);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// continue in parent
|
||||
//this.parent?.UpdateFromNucleus(this);
|
||||
|
||||
UpdateNuclei();
|
||||
//UpdateNuclei();
|
||||
}
|
||||
|
||||
public override void UpdateStateIsolated() {
|
||||
throw new Exception("Cluster should not be updated!");
|
||||
}
|
||||
|
||||
public override void UpdateNuclei() {
|
||||
foreach (Nucleus nucleus in this.clusterNuclei)
|
||||
nucleus.UpdateNuclei();
|
||||
}
|
||||
// Don't think this does anything anymore...
|
||||
// public override void UpdateNuclei() {
|
||||
// foreach (Nucleus nucleus in this.nuclei)
|
||||
// nucleus.UpdateNuclei();
|
||||
// }
|
||||
|
||||
#endregion Update
|
||||
|
||||
public void Refresh() {
|
||||
// This should not be needed, but somehow somewhere the parent is changed...
|
||||
foreach (Nucleus nucleus in this.nuclei) {
|
||||
// if (nucleus is not Neuron neuron)
|
||||
// continue;
|
||||
nucleus.parent = this;
|
||||
}
|
||||
RefreshOutputs();
|
||||
RefreshComputeOrders();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,2 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f13cdc4a175a9f379a00317ae68d8bea
|
||||
guid: f13cdc4a175a9f379a00317ae68d8bea
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
@ -12,7 +12,7 @@ namespace NanoBrain {
|
||||
[Serializable]
|
||||
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 bool staticMemory = false;
|
||||
|
||||
@ -1,2 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 29633aa3fe5cd9dcc8d886051f45d4d8
|
||||
guid: 29633aa3fe5cd9dcc8d886051f45d4d8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
@ -23,26 +23,91 @@ namespace NanoBrain {
|
||||
public Neuron(Cluster parent, string name) {
|
||||
this.parent = parent;
|
||||
this.name = name;
|
||||
this.parent?.clusterNuclei.Add(this);
|
||||
this.parent?.nuclei.Add(this);
|
||||
}
|
||||
/// <summary>
|
||||
/// Create a new Neuron in a Cluster Prefab
|
||||
/// </summary>
|
||||
/// <param name="prefab">The Cluster Preafb in which the new Neuron should be created</param>
|
||||
/// <param name="name">The name of the new Neuron</param>
|
||||
public Neuron(ClusterPrefab prefab, string name) {
|
||||
this.clusterPrefab = prefab;
|
||||
this.name = name;
|
||||
if (this.clusterPrefab != null) {
|
||||
this.clusterPrefab.nuclei.Add(this);
|
||||
this.clusterPrefab.RefreshOutputs();
|
||||
}
|
||||
else
|
||||
Debug.LogError("No prefab when adding neuron to prefab");
|
||||
}
|
||||
// public Neuron(ClusterPrefab prefab, string name) {
|
||||
// this.clusterPrefab = prefab;
|
||||
// this.name = name;
|
||||
// if (this.clusterPrefab != null) {
|
||||
// this.clusterPrefab.cluster.nuclei.Add(this);
|
||||
// this.clusterPrefab.cluster.RefreshOutputs();
|
||||
// }
|
||||
// else
|
||||
// Debug.LogError("No prefab when adding neuron to prefab");
|
||||
// }
|
||||
|
||||
#region Serialization
|
||||
|
||||
/// <summary>
|
||||
/// The bias
|
||||
/// </summary>
|
||||
/// The bias which a value which is always added to the combined value of the neuron
|
||||
/// It does not have a synapse and therefore no weight of source nucleus
|
||||
public Vector3 bias = Vector3.zero;
|
||||
|
||||
#region Synapses
|
||||
|
||||
[SerializeField]
|
||||
private List<Synapse> _synapses = new();
|
||||
/// <summary>
|
||||
/// The synapses of the nucleus
|
||||
/// </summary>
|
||||
public List<Synapse> synapses => _synapses;
|
||||
|
||||
/// <summary>
|
||||
/// Add a new synapse to this nuclues
|
||||
/// </summary>
|
||||
/// <param name="sendingNucleus">The nucleus from which the signals may originate</param>
|
||||
/// <param name="weight">The weight applied to the input. Default value = 1</param>
|
||||
/// <returns>The created Synapse</returns>
|
||||
/// This will add a new input to this nucleus with the given weight.
|
||||
public Synapse AddSynapse(Neuron sendingNucleus, float weight = 1) {
|
||||
Synapse synapse = new(sendingNucleus, weight);
|
||||
this.synapses.Add(synapse);
|
||||
return synapse;
|
||||
}
|
||||
|
||||
// public Synapse AddSynapse(ClusterPrefab clusterPrefab, string neuronName, float weight = 1) {
|
||||
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Find a synapse
|
||||
/// </summary>
|
||||
/// <param name="sender">The sender of the input to the Synapse</param>
|
||||
/// <returns>The found Synapse or null when the sender has no synapse to this nucleus.</returns>
|
||||
public Synapse GetSynapse(Nucleus sender) {
|
||||
foreach (Synapse synapse in this.synapses)
|
||||
if (synapse.neuron == sender)
|
||||
return synapse;
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove a synapse from a Nucleus
|
||||
/// </summary>
|
||||
/// <param name="sendingNucleus">Remote the synapse connecting to this Nucleus</param>
|
||||
public void RemoveSynapse(Nucleus sendingNucleus) {
|
||||
this.synapses.RemoveAll(synapse => synapse.neuron == sendingNucleus);
|
||||
}
|
||||
|
||||
#endregion Synapses
|
||||
|
||||
/// <summary>
|
||||
/// Set the bias, recalculate the output and update all Nuclei receiving from this Nucleus
|
||||
/// </summary>
|
||||
/// <param name="inputValue"></param>
|
||||
public virtual void SetBias(Vector3 inputValue) {
|
||||
this.bias = inputValue;
|
||||
this.lastUpdate = Time.time;
|
||||
this.parent?.UpdateFromNucleus(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The type of combinators
|
||||
/// </summary>
|
||||
@ -52,8 +117,6 @@ namespace NanoBrain {
|
||||
Sum,
|
||||
/// Multiply the weighted values
|
||||
Product,
|
||||
/// Take the maximum of all the weighted values
|
||||
Max,
|
||||
}
|
||||
/// <summary>
|
||||
/// The type of combinator used for this Neuron
|
||||
@ -211,16 +274,11 @@ namespace NanoBrain {
|
||||
public float outputSqrMagnitude => _outputValue.sqrMagnitude;
|
||||
|
||||
#endif
|
||||
public bool isFiring {
|
||||
get {
|
||||
SleepCheck();
|
||||
return this.outputMagnitude > 0.5f;
|
||||
}
|
||||
}
|
||||
public bool isFiring => this.outputMagnitude > 0.5f;
|
||||
public Action WhenFiring;
|
||||
|
||||
|
||||
public virtual bool isSleeping => Time.time - this.lastUpdate > this.timeToSleep; //this.outputMagnitude == 0;
|
||||
public bool persistOutput = false;
|
||||
public virtual bool isSleeping => !persistOutput && (Time.time - this.lastUpdate > this.timeToSleep);
|
||||
public void SleepCheck() {
|
||||
if (this.isSleeping) {
|
||||
#if UNITY_MATHEMATICS
|
||||
@ -231,20 +289,27 @@ namespace NanoBrain {
|
||||
}
|
||||
}
|
||||
|
||||
[NonSerialized]
|
||||
/// <summary>
|
||||
/// Toggle for printing debugging trace data
|
||||
/// </summary>
|
||||
//public bool trace = false;
|
||||
|
||||
//[NonSerialized]
|
||||
public float lastUpdate = 0;
|
||||
public readonly float timeToSleep = 1f;
|
||||
|
||||
/// \copydoc NanoBrain::Nucleus::ShallowCloneTo
|
||||
public override Nucleus ShallowCloneTo(Cluster newParent) {
|
||||
Neuron clone = new(newParent, this.name);
|
||||
Neuron clone = new(newParent, this.name) {
|
||||
// prefabNucleus = this
|
||||
};
|
||||
CloneFields(clone);
|
||||
return clone;
|
||||
}
|
||||
|
||||
/// \copydoc NanoBrain::Nucleus::Clone
|
||||
public override Nucleus Clone(ClusterPrefab prefab) {
|
||||
Neuron clone = new(prefab, this.name);
|
||||
Neuron clone = new(prefab.cluster, this.name);
|
||||
CloneFields(clone);
|
||||
foreach (Synapse synapse in this.synapses) {
|
||||
Synapse clonedSynapse = clone.AddSynapse(synapse.neuron);
|
||||
@ -257,8 +322,8 @@ namespace NanoBrain {
|
||||
}
|
||||
|
||||
protected virtual void CloneFields(Neuron clone) {
|
||||
clone.clusterPrefab = this.clusterPrefab;
|
||||
clone.bias = this.bias;
|
||||
clone.persistOutput = this.persistOutput;
|
||||
clone.combinator = this.combinator;
|
||||
clone.curve = this.curve;
|
||||
clone.curvePreset = this.curvePreset;
|
||||
@ -268,8 +333,8 @@ namespace NanoBrain {
|
||||
public static void Delete(Nucleus nucleus) {
|
||||
if (nucleus == null)
|
||||
return;
|
||||
if (nucleus.synapses != null) {
|
||||
foreach (Synapse synapse in nucleus.synapses) {
|
||||
if (nucleus is Neuron neuron) {
|
||||
foreach (Synapse synapse in neuron.synapses) {
|
||||
if (synapse.neuron is Neuron synapse_nucleus) {
|
||||
if (synapse_nucleus.receivers.Count > 1) {
|
||||
// there is another nucleus feeding into this input nucleus
|
||||
@ -281,45 +346,43 @@ namespace NanoBrain {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (nucleus is Neuron neuron) {
|
||||
foreach (Nucleus receiver in neuron.receivers) {
|
||||
if (receiver != null && receiver.synapses != null)
|
||||
receiver.synapses.RemoveAll(s => s.neuron == nucleus);
|
||||
if (receiver is not Neuron receiverNeuron)
|
||||
continue;
|
||||
if (receiver != null && receiverNeuron.synapses != null)
|
||||
receiverNeuron.synapses.RemoveAll(s => s.neuron == nucleus);
|
||||
}
|
||||
}
|
||||
else if (nucleus is Cluster cluster) {
|
||||
// remove all receivers for this cluster
|
||||
foreach (Nucleus clusterNucleus in cluster.clusterNuclei) {
|
||||
foreach (Nucleus clusterNucleus in cluster.nuclei) {
|
||||
if (clusterNucleus is Neuron output) {
|
||||
foreach (Nucleus receiver in output.receivers) {
|
||||
receiver.synapses.RemoveAll(s => s.neuron == output);
|
||||
if (receiver is not Neuron receiverNeuron)
|
||||
continue;
|
||||
receiverNeuron.synapses.RemoveAll(s => s.neuron == output);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (nucleus.clusterPrefab != null) {
|
||||
nucleus.clusterPrefab.nuclei.RemoveAll(n => n == nucleus);
|
||||
nucleus.clusterPrefab.RefreshOutputs();
|
||||
nucleus.clusterPrefab.GarbageCollection();
|
||||
if (nucleus.parent.prefab != null) {
|
||||
nucleus.parent.prefab.cluster.nuclei.RemoveAll(n => n == nucleus);
|
||||
nucleus.parent.prefab.cluster.RefreshOutputs();
|
||||
nucleus.parent.prefab.GarbageCollection();
|
||||
}
|
||||
}
|
||||
|
||||
public override void UpdateStateIsolated() {
|
||||
CheckSleepingSynapses();
|
||||
var result = Combinator();
|
||||
this.outputValue = Activator(result);
|
||||
this.outputValue = ApplyActivator(result);
|
||||
this.lastUpdate = Time.time;
|
||||
}
|
||||
|
||||
protected void CheckSleepingSynapses() {
|
||||
foreach (Synapse synapse in this.synapses) {
|
||||
if (synapse.isSleeping) {
|
||||
synapse.neuron.outputValue = Vector3.zero;
|
||||
}
|
||||
}
|
||||
foreach (Synapse synapse in this.synapses)
|
||||
synapse.neuron.SleepCheck();
|
||||
}
|
||||
|
||||
#region Combinator
|
||||
@ -329,42 +392,27 @@ namespace NanoBrain {
|
||||
protected Func<float3> Combinator => combinator switch {
|
||||
CombinatorType.Sum => CombinatorSum,
|
||||
CombinatorType.Product => CombinatorProduct,
|
||||
CombinatorType.Max => CombinatorMax,
|
||||
_ => CombinatorSum
|
||||
};
|
||||
|
||||
public float3 CombinatorSum() {
|
||||
float3 sum = this.bias;
|
||||
foreach (Synapse synapse in this.synapses)
|
||||
foreach (Synapse synapse in this.synapses) {
|
||||
synapse.neuron.SleepCheck();
|
||||
sum += synapse.weight * synapse.neuron.outputValue;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public float3 CombinatorProduct() {
|
||||
float3 product = this.bias;
|
||||
foreach (Synapse synapse in this.synapses) {
|
||||
synapse.neuron.SleepCheck();
|
||||
product *= synapse.weight * synapse.neuron.outputValue;
|
||||
}
|
||||
return product;
|
||||
}
|
||||
|
||||
public float3 CombinatorMax() {
|
||||
float3 max = this.bias;
|
||||
float maxLength = length(max);
|
||||
|
||||
//Applying the weight factors
|
||||
foreach (Synapse synapse in this.synapses) {
|
||||
float3 input = synapse.weight * synapse.neuron.outputValue;
|
||||
|
||||
float inputLength = length(input);
|
||||
if (inputLength > maxLength) {
|
||||
max = input;
|
||||
maxLength = inputLength;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
protected Func<Vector3> Combinator => combinator switch {
|
||||
@ -413,6 +461,20 @@ namespace NanoBrain {
|
||||
|
||||
#if UNITY_MATHEMATICS
|
||||
|
||||
// This does not allocate memory and seems faster than the solution below
|
||||
float3 ApplyActivator(float3 x) {
|
||||
switch (curvePreset) {
|
||||
case ActivationType.Linear: return ActivatorLinear(x);
|
||||
case ActivationType.Sqrt: return ActivatorSqrt(x);
|
||||
case ActivationType.Power: return ActivatorPower(x);
|
||||
case ActivationType.Reciprocal: return ActivatorReciprocal(x);
|
||||
case ActivationType.Tanh: return ActivatorTanh(x);
|
||||
case ActivationType.Binary: return ActivatorBinary(x);
|
||||
case ActivationType.Normalized: return ActivatorNormalized(x);
|
||||
default: return ActivatorCustom(x);
|
||||
}
|
||||
}
|
||||
|
||||
public Func<float3, float3> Activator => this.curvePreset switch {
|
||||
ActivationType.Linear => ActivatorLinear,
|
||||
ActivationType.Sqrt => ActivatorSqrt,
|
||||
@ -524,25 +586,40 @@ namespace NanoBrain {
|
||||
}
|
||||
|
||||
public virtual void AddReceiver(Nucleus receiverToAdd, float weight = 1) {
|
||||
this._receivers.Add(receiverToAdd);
|
||||
receiverToAdd.AddSynapse(this, weight);
|
||||
if (receiverToAdd is not Neuron receiverNeuron)
|
||||
return;
|
||||
this._receivers.Add(receiverNeuron);
|
||||
receiverNeuron.AddSynapse(this, weight);
|
||||
//Debug.Log($"Add synapse {this.clusterPrefab.name}.{this.name} -> {receiverToAdd.name} --- [{this.receivers.Count}]");
|
||||
|
||||
}
|
||||
|
||||
public virtual void RemoveReceiver(Nucleus receiverToRemove) {
|
||||
this._receivers.RemoveAll(receiver => receiver == receiverToRemove);
|
||||
receiverToRemove.synapses.RemoveAll(synapse => synapse.neuron == this);
|
||||
if (receiverToRemove is not Neuron receiverNeuron)
|
||||
return;
|
||||
this._receivers.RemoveAll(receiver => receiver == receiverNeuron);
|
||||
receiverNeuron.synapses.RemoveAll(synapse => synapse.neuron == this);
|
||||
|
||||
// Nucleus prefabReceiver = receiverToRemove.prefabNucleus;
|
||||
// if (this.prefabNucleus is Neuron prefabNeuron && prefabReceiver != null) {
|
||||
// prefabNeuron.receivers.RemoveAll(receiver => receiver == prefabReceiver);
|
||||
// prefabReceiver.synapses.RemoveAll(synapse => synapse.neuron == prefabNeuron);
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
#endregion Receivers
|
||||
|
||||
public override void ProcessStimulus(Vector3 inputValue) {
|
||||
;
|
||||
/// <summary>
|
||||
/// Process an external stimulus
|
||||
/// </summary>
|
||||
/// <param name="inputValue">The value of the stimulus</param>
|
||||
/// <param name="thingId">The id of the thing causing the stimulus</param>
|
||||
/// <param name="thingName">The name of the thing causing the stimulus</param>
|
||||
public virtual void ProcessStimulus(Vector3 inputValue) {
|
||||
this.lastUpdate = Time.time;
|
||||
this.bias = inputValue;
|
||||
this.parent.UpdateFromNucleus(this);
|
||||
this.parent?.UpdateFromNucleus(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,2 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 750748f3f0e7d472fbf88ab02987074c
|
||||
guid: 750748f3f0e7d472fbf88ab02987074c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
@ -17,11 +17,15 @@ public abstract class Nucleus {
|
||||
/// </summary>
|
||||
public string name;
|
||||
|
||||
// [NonSerialized]
|
||||
// public Nucleus prefabNucleus;
|
||||
|
||||
/// <summary>
|
||||
/// The cluster prefab in which the nucleus is located
|
||||
/// </summary>
|
||||
[SerializeReference]
|
||||
public ClusterPrefab clusterPrefab;
|
||||
// [SerializeReference]
|
||||
// public ClusterPrefab clusterPrefab;
|
||||
|
||||
/// <summary>
|
||||
/// The cluster instance in which the nucleus is located
|
||||
/// </summary>
|
||||
@ -31,7 +35,7 @@ public abstract class Nucleus {
|
||||
/// <summary>
|
||||
/// Toggle for printing debugging trace data
|
||||
/// </summary>
|
||||
public bool trace = false;
|
||||
//public bool trace = false;
|
||||
|
||||
/// <summary>
|
||||
/// Function to make a partial clone of this nucleus
|
||||
@ -61,60 +65,60 @@ public abstract class Nucleus {
|
||||
|
||||
public virtual void Initialize() {}
|
||||
|
||||
#region Synapses
|
||||
// #region Synapses
|
||||
|
||||
/// <summary>
|
||||
/// The bias of the nucleus
|
||||
/// </summary>
|
||||
/// The bias which a value which is always added to the combined value of the nucleus
|
||||
/// It does not have a synapse and therefore no weight of source nucleus
|
||||
public Vector3 bias = Vector3.zero;
|
||||
// /// <summary>
|
||||
// /// The bias of the nucleus
|
||||
// /// </summary>
|
||||
// /// The bias which a value which is always added to the combined value of the nucleus
|
||||
// /// It does not have a synapse and therefore no weight of source nucleus
|
||||
// //public Vector3 bias = Vector3.zero;
|
||||
|
||||
[SerializeField]
|
||||
private List<Synapse> _synapses = new();
|
||||
/// <summary>
|
||||
/// The synapses of the nucleus
|
||||
/// </summary>
|
||||
public List<Synapse> synapses => _synapses;
|
||||
// [SerializeField]
|
||||
// private List<Synapse> _synapses = new();
|
||||
// /// <summary>
|
||||
// /// The synapses of the nucleus
|
||||
// /// </summary>
|
||||
// public List<Synapse> synapses => _synapses;
|
||||
|
||||
/// <summary>
|
||||
/// Add a new synapse to this nuclues
|
||||
/// </summary>
|
||||
/// <param name="sendingNucleus">The nucleus from which the signals may originate</param>
|
||||
/// <param name="weight">The weight applied to the input. Default value = 1</param>
|
||||
/// <returns>The created Synapse</returns>
|
||||
/// This will add a new input to this nucleus with the given weight.
|
||||
public Synapse AddSynapse(Neuron sendingNucleus, float weight = 1) {
|
||||
Synapse synapse = new(sendingNucleus, weight);
|
||||
this.synapses.Add(synapse);
|
||||
return synapse;
|
||||
}
|
||||
// /// <summary>
|
||||
// /// Add a new synapse to this nuclues
|
||||
// /// </summary>
|
||||
// /// <param name="sendingNucleus">The nucleus from which the signals may originate</param>
|
||||
// /// <param name="weight">The weight applied to the input. Default value = 1</param>
|
||||
// /// <returns>The created Synapse</returns>
|
||||
// /// This will add a new input to this nucleus with the given weight.
|
||||
// public Synapse AddSynapse(Neuron sendingNucleus, float weight = 1) {
|
||||
// Synapse synapse = new(sendingNucleus, weight);
|
||||
// this.synapses.Add(synapse);
|
||||
// return synapse;
|
||||
// }
|
||||
|
||||
// public Synapse AddSynapse(ClusterPrefab clusterPrefab, string neuronName, float weight = 1) {
|
||||
// // public Synapse AddSynapse(ClusterPrefab clusterPrefab, string neuronName, float weight = 1) {
|
||||
|
||||
// }
|
||||
// // }
|
||||
|
||||
/// <summary>
|
||||
/// Find a synapse
|
||||
/// </summary>
|
||||
/// <param name="sender">The sender of the input to the Synapse</param>
|
||||
/// <returns>The found Synapse or null when the sender has no synapse to this nucleus.</returns>
|
||||
public Synapse GetSynapse(Nucleus sender) {
|
||||
foreach (Synapse synapse in this.synapses)
|
||||
if (synapse.neuron == sender)
|
||||
return synapse;
|
||||
return null;
|
||||
}
|
||||
// /// <summary>
|
||||
// /// Find a synapse
|
||||
// /// </summary>
|
||||
// /// <param name="sender">The sender of the input to the Synapse</param>
|
||||
// /// <returns>The found Synapse or null when the sender has no synapse to this nucleus.</returns>
|
||||
// public Synapse GetSynapse(Nucleus sender) {
|
||||
// foreach (Synapse synapse in this.synapses)
|
||||
// if (synapse.neuron == sender)
|
||||
// return synapse;
|
||||
// return null;
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Remove a synapse from a Nucleus
|
||||
/// </summary>
|
||||
/// <param name="sendingNucleus">Remote the synapse connecting to this Nucleus</param>
|
||||
public void RemoveSynapse(Nucleus sendingNucleus) {
|
||||
this.synapses.RemoveAll(synapse => synapse.neuron == sendingNucleus);
|
||||
}
|
||||
// /// <summary>
|
||||
// /// Remove a synapse from a Nucleus
|
||||
// /// </summary>
|
||||
// /// <param name="sendingNucleus">Remote the synapse connecting to this Nucleus</param>
|
||||
// public void RemoveSynapse(Nucleus sendingNucleus) {
|
||||
// this.synapses.RemoveAll(synapse => synapse.neuron == sendingNucleus);
|
||||
// }
|
||||
|
||||
#endregion Synapses
|
||||
// #endregion Synapses
|
||||
|
||||
#region Update
|
||||
|
||||
@ -129,23 +133,14 @@ public abstract class Nucleus {
|
||||
public virtual void UpdateNuclei() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the bias, recalculate the output and update all Nuclei receiving from this Nucleus
|
||||
/// </summary>
|
||||
/// <param name="inputValue"></param>
|
||||
public virtual void SetBias(Vector3 inputValue) {
|
||||
this.bias = inputValue;
|
||||
this.parent.UpdateFromNucleus(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Process an external stimulus
|
||||
/// </summary>
|
||||
/// <param name="inputValue">The value of the stimulus</param>
|
||||
/// <param name="thingId">The id of the thing causing the stimulus</param>
|
||||
/// <param name="thingName">The name of the thing causing the stimulus</param>
|
||||
public virtual void ProcessStimulus(Vector3 inputValue) { //, int thingId = 0, string thingName = "") {
|
||||
}
|
||||
// /// <summary>
|
||||
// /// Set the bias, recalculate the output and update all Nuclei receiving from this Nucleus
|
||||
// /// </summary>
|
||||
// /// <param name="inputValue"></param>
|
||||
// public virtual void SetBias(Vector3 inputValue) {
|
||||
// this.bias = inputValue;
|
||||
// this.parent.UpdateFromNucleus(this);
|
||||
// }
|
||||
|
||||
#endregion Update
|
||||
|
||||
|
||||
@ -1,2 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4310eea6ab77628b085387a226c1c386
|
||||
guid: 4310eea6ab77628b085387a226c1c386
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
@ -1,2 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 334a58eafccd60cbdb32f719e9e861c6
|
||||
guid: 334a58eafccd60cbdb32f719e9e861c6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
||||
@ -8,134 +8,146 @@ namespace NanoBrain {
|
||||
/// </summary>
|
||||
[CreateAssetMenu(menuName = "Passer/Cluster")]
|
||||
public class ClusterPrefab : ScriptableObject {
|
||||
/// The nuclei in this cluster
|
||||
[SerializeReference]
|
||||
// This list should not include any clusters...
|
||||
public List<Nucleus> nuclei = new();
|
||||
|
||||
/// <summary>
|
||||
/// The output of this cluster
|
||||
/// </summary>
|
||||
/// <deprecated>This only returens the first(default) nucleus. Use outputs[0] instead</deprecated>
|
||||
public virtual Nucleus output => this.nuclei[0] as Nucleus;
|
||||
public Cluster cluster;
|
||||
/*
|
||||
/// The nuclei in this cluster
|
||||
[SerializeReference]
|
||||
// This list should not include any clusters...
|
||||
public List<Nucleus> nuclei = new();
|
||||
|
||||
/// <summary>
|
||||
/// The nuclei in this cluster which are meant for receiving signals from outside the cluster
|
||||
/// </summary>
|
||||
/// <remark>This is currently the nuclei which do not have any incoming synapse</remark>
|
||||
public List<Nucleus> _inputs = null;
|
||||
public virtual List<Nucleus> inputs {
|
||||
get {
|
||||
if (this._inputs == null) {
|
||||
this._inputs = new();
|
||||
foreach (Nucleus receptor in this.nuclei) {
|
||||
if (receptor is Nucleus nucleus) {
|
||||
// inputs have no incoming synapses yet.
|
||||
if (nucleus.synapses.Count == 0)
|
||||
this._inputs.Add(nucleus);
|
||||
/// <summary>
|
||||
/// The output of this cluster
|
||||
/// </summary>
|
||||
/// <deprecated>This only returens the first(default) nucleus. Use outputs[0] instead</deprecated>
|
||||
public virtual Nucleus output => this.nuclei[0] as Nucleus;
|
||||
|
||||
/// <summary>
|
||||
/// The nuclei in this cluster which are meant for receiving signals from outside the cluster
|
||||
/// </summary>
|
||||
/// <remark>This is currently the nuclei which do not have any incoming synapse</remark>
|
||||
public List<Nucleus> _inputs = null;
|
||||
public virtual List<Nucleus> inputs {
|
||||
get {
|
||||
if (this._inputs == null) {
|
||||
this._inputs = new();
|
||||
foreach (Nucleus receptor in this.nuclei) {
|
||||
if (receptor is Nucleus nucleus) {
|
||||
// inputs have no incoming synapses yet.
|
||||
if (nucleus.synapses.Count == 0)
|
||||
this._inputs.Add(nucleus);
|
||||
}
|
||||
}
|
||||
}
|
||||
return this._inputs;
|
||||
}
|
||||
}
|
||||
return this._inputs;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// The nuclei in this cluster which are meant for sending signals onward
|
||||
/// </summary>
|
||||
private List<Nucleus> _outputs = null;
|
||||
public List<Nucleus> outputs {
|
||||
get {
|
||||
if (this._outputs == null)
|
||||
RefreshOutputs();
|
||||
return this._outputs;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Redetermine the outpus in the cluster
|
||||
/// </summary>
|
||||
public void RefreshOutputs() {
|
||||
this._outputs = new();
|
||||
foreach (Nucleus nucleus in this.nuclei) {
|
||||
if (nucleus is Neuron neuron && neuron.receivers.Count == 0)
|
||||
this._outputs.Add(nucleus);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The nuclei in this cluster which are meant for sending signals onward
|
||||
/// </summary>
|
||||
private List<Nucleus> _outputs = null;
|
||||
public List<Nucleus> outputs {
|
||||
get {
|
||||
if (this._outputs == null)
|
||||
RefreshOutputs();
|
||||
return this._outputs;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Redetermine the outpus in the cluster
|
||||
/// </summary>
|
||||
public void RefreshOutputs() {
|
||||
this._outputs = new();
|
||||
foreach (Nucleus nucleus in this.nuclei) {
|
||||
if (nucleus is Neuron neuron && neuron.receivers.Count == 0)
|
||||
this._outputs.Add(nucleus);
|
||||
}
|
||||
}
|
||||
*/
|
||||
/// <summary>
|
||||
/// Retrieve a nucleus in this cluster
|
||||
/// </summary>
|
||||
/// <param name="nucleusName">The name of the nucleus</param>
|
||||
/// <returns>The Nucleus with the given name or null if no such Nucleus could be found</returns>
|
||||
public Nucleus GetNucleus(string nucleusName) {
|
||||
foreach (Nucleus nucleus in this.nuclei) {
|
||||
if (nucleus.name == nucleusName)
|
||||
return nucleus;
|
||||
}
|
||||
return null;
|
||||
return cluster.GetNucleus(nucleusName);
|
||||
// foreach (Nucleus nucleus in this.nuclei) {
|
||||
// if (nucleus.name == nucleusName)
|
||||
// return nucleus;
|
||||
// }
|
||||
// return null;
|
||||
}
|
||||
|
||||
// Call this function to ensure that there is at least one nucleus
|
||||
// This is an invariant and should be ensured before the nucleus is used
|
||||
// because output requires it.
|
||||
public void EnsureInitialization() {
|
||||
nuclei ??= new List<Nucleus>();
|
||||
if (nuclei.Count == 0)
|
||||
new Neuron(this, "Output"); // Every cluster should have at least 1 neuron
|
||||
this.cluster.prefab = this;
|
||||
this.cluster.name = this.name;
|
||||
this.cluster.nuclei ??= new List<Nucleus>();
|
||||
if (this.cluster.nuclei.Count <= 0)
|
||||
new Neuron(this.cluster, "Output"); // Every cluster should have at least 1 neuron
|
||||
this.cluster.instanceCount = 1;
|
||||
// nuclei ??= new List<Nucleus>();
|
||||
// if (nuclei.Count == 0)
|
||||
// new Neuron(this, "Output"); // Every cluster should have at least 1 neuron
|
||||
}
|
||||
|
||||
public void GarbageCollection() {
|
||||
HashSet<Nucleus> visitedNuclei = new();
|
||||
foreach (Nucleus output in this.outputs)
|
||||
MarkNuclei(visitedNuclei, output);
|
||||
//Debug.Log($"Garbage collection found {visitedNuclei.Count} Nuclei");
|
||||
this.nuclei.RemoveAll(nucleus => visitedNuclei.Contains(nucleus) == false);
|
||||
// HashSet<Nucleus> visitedNuclei = new();
|
||||
// foreach (Nucleus output in this.outputs)
|
||||
// MarkNuclei(visitedNuclei, output);
|
||||
// //Debug.Log($"Garbage collection found {visitedNuclei.Count} Nuclei");
|
||||
// this.nuclei.RemoveAll(nucleus => visitedNuclei.Contains(nucleus) == false);
|
||||
}
|
||||
|
||||
public void MarkNuclei(HashSet<Nucleus> visitedNuclei, Nucleus nucleus) {
|
||||
if (nucleus is null)
|
||||
return;
|
||||
// public void MarkNuclei(HashSet<Nucleus> visitedNuclei, Nucleus nucleus) {
|
||||
// if (nucleus is null)
|
||||
// return;
|
||||
|
||||
if (nucleus.parent != null && nucleus.parent.prefab != this)
|
||||
visitedNuclei.Add(nucleus.parent);
|
||||
else
|
||||
visitedNuclei.Add(nucleus);
|
||||
if (nucleus.synapses != null) {
|
||||
HashSet<Synapse> visitedSynapses = new();
|
||||
foreach (Synapse synapse in nucleus.synapses) {
|
||||
if (synapse != null && synapse.neuron != null) {
|
||||
visitedSynapses.Add(synapse);
|
||||
if (synapse.neuron is Nucleus synapse_nucleus)
|
||||
MarkNuclei(visitedNuclei, synapse_nucleus);
|
||||
}
|
||||
}
|
||||
nucleus.synapses.RemoveAll(synapse => visitedSynapses.Contains(synapse) == false);
|
||||
}
|
||||
if (nucleus is Neuron neuron && neuron.receivers != null) {
|
||||
HashSet<Nucleus> visitedReceivers = new();
|
||||
foreach (Nucleus receiver in neuron.receivers) {
|
||||
if (receiver != null && receiver != null) {
|
||||
visitedReceivers.Add(receiver);
|
||||
visitedNuclei.Add(receiver);
|
||||
}
|
||||
}
|
||||
neuron.receivers.RemoveAll(receiver => visitedReceivers.Contains(receiver) == false);
|
||||
}
|
||||
}
|
||||
// if (nucleus.parent != null && nucleus.parent.prefab != this)
|
||||
// visitedNuclei.Add(nucleus.parent);
|
||||
// else
|
||||
// visitedNuclei.Add(nucleus);
|
||||
// if (nucleus is Neuron neuron) {
|
||||
// if (neuron.synapses != null) {
|
||||
// HashSet<Synapse> visitedSynapses = new();
|
||||
// foreach (Synapse synapse in neuron.synapses) {
|
||||
// if (synapse != null && synapse.neuron != null) {
|
||||
// visitedSynapses.Add(synapse);
|
||||
// if (synapse.neuron is Nucleus synapse_nucleus)
|
||||
// MarkNuclei(visitedNuclei, synapse_nucleus);
|
||||
// }
|
||||
// }
|
||||
// neuron.synapses.RemoveAll(synapse => visitedSynapses.Contains(synapse) == false);
|
||||
// }
|
||||
// if (neuron.receivers != null) {
|
||||
// HashSet<Nucleus> visitedReceivers = new();
|
||||
// foreach (Nucleus receiver in neuron.receivers) {
|
||||
// if (receiver != null && receiver != null) {
|
||||
// visitedReceivers.Add(receiver);
|
||||
// visitedNuclei.Add(receiver);
|
||||
// }
|
||||
// }
|
||||
// neuron.receivers.RemoveAll(receiver => visitedReceivers.Contains(receiver) == false);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
public virtual void UpdateNuclei() {
|
||||
foreach (Nucleus nucleus in this.nuclei)
|
||||
nucleus.UpdateNuclei();
|
||||
}
|
||||
// public virtual void UpdateNuclei() {
|
||||
// foreach (Nucleus nucleus in this.nuclei)
|
||||
// nucleus.UpdateNuclei();
|
||||
// }
|
||||
|
||||
public int GetNucleusIndex(Nucleus receiver) {
|
||||
int ix = 0;
|
||||
foreach (Nucleus nucleus in this.nuclei) {
|
||||
if (receiver == nucleus)
|
||||
return ix;
|
||||
ix++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
// public int GetNucleusIndex(Nucleus receiver) {
|
||||
// int ix = 0;
|
||||
// foreach (Nucleus nucleus in this.nuclei) {
|
||||
// if (receiver == nucleus)
|
||||
// return ix;
|
||||
// ix++;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user