diff --git a/Editor/ClusterEditor.cs b/Editor/ClusterEditor.cs index ae1a535..ce75239 100644 --- a/Editor/ClusterEditor.cs +++ b/Editor/ClusterEditor.cs @@ -232,7 +232,7 @@ namespace NanoBrain { bool connecting = GUILayout.Button("Add Output Neuron"); if (connecting) { - Nucleus newOutput = new Neuron(this.prefab, "New Output"); + Nucleus newOutput = new Neuron(this.prefab.cluster, "New Output"); // Regenerate the temporary clsuter instance // See also the constructor this.currentCluster = new(this.prefab); @@ -351,21 +351,21 @@ namespace NanoBrain { else { EditorGUILayout.BeginHorizontal(); - if (synapse.neuron.clusterPrefab != this.currentNucleus.clusterPrefab) { + if (synapse.neuron.parent.prefab != this.currentNucleus.parent.prefab) { // 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.prefab != null) { + labelWidth = labelStyle.CalcSize(new GUIContent($"{synapse.neuron.parent.prefab.name}.")).x; + GUILayout.Label($"{synapse.neuron.parent.prefab.name}", GUILayout.Width(labelWidth)); } - string[] options = synapse.neuron.clusterPrefab.cluster.nuclei.Select(n => n.name).ToArray(); + string[] options = synapse.neuron.parent.prefab.cluster.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.cluster.nuclei[newIndex] as Neuron; + Neuron newNeuron = synapse.neuron.parent.prefab.cluster.nuclei[newIndex] as Neuron; ChangeSynapse(synapse, newNeuron); } } @@ -445,13 +445,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; } @@ -531,13 +532,13 @@ namespace NanoBrain { } protected virtual void DisconnectNucleus(Neuron nucleus) { - if (this.currentNucleus.clusterPrefab == null) + if (this.currentNucleus.parent.prefab == null) return; Neuron currentNeuron = this.currentNucleus as Neuron; string[] names = currentNeuron.synapses.Select(synapse => synapse.neuron.name).ToArray(); int selectedIndex = -1; selectedIndex = EditorGUILayout.Popup("Disconnect from", selectedIndex, names); - if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.clusterPrefab.cluster.nuclei.Count) { + if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.parent.prefab.cluster.nuclei.Count) { Synapse synapse = currentNeuron.synapses[selectedIndex]; synapse.neuron.RemoveReceiver(this.currentNucleus); diff --git a/Runtime/Scripts/Core/Cluster.cs b/Runtime/Scripts/Core/Cluster.cs index 657ae08..7dcf31c 100644 --- a/Runtime/Scripts/Core/Cluster.cs +++ b/Runtime/Scripts/Core/Cluster.cs @@ -72,10 +72,10 @@ namespace NanoBrain { public Cluster(ClusterPrefab prefab, ClusterPrefab parent = null) { this.prefab = prefab; this.name = prefab.name; - this.clusterPrefab = parent; + this.parent.prefab = parent; - if (this.clusterPrefab != null) - this.clusterPrefab.cluster.nuclei.Add(this); + if (this.parent.prefab != null) + this.parent.prefab.cluster.nuclei.Add(this); ClonePrefab(); _ = this.inputs; @@ -108,9 +108,9 @@ 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; + ClusterPrefab prefabCluster = synapseNeuron.parent.prefab; Cluster clonedCluster = this.nuclei.Find(n => n.name == prefabCluster.name) as Cluster; if (clonedCluster == null) continue; @@ -177,7 +177,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); @@ -286,9 +286,9 @@ namespace NanoBrain { 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) { + if (synapseNeuron.parent.prefab != null && synapseNeuron.parent.prefab != this.prefab) { // Neuron is in another cluster, find the cloned cluster first - ClusterPrefab prefabCluster = synapseNeuron.clusterPrefab; + ClusterPrefab prefabCluster = synapseNeuron.parent.prefab; Cluster clonedCluster = this.nuclei.Find(n => n.name == prefabCluster.name) as Cluster; if (clonedCluster == null) continue; @@ -404,7 +404,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. @@ -793,7 +793,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); @@ -812,7 +812,7 @@ namespace NanoBrain { 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)); } } @@ -846,7 +846,7 @@ 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}"); diff --git a/Runtime/Scripts/Core/MemoryCell.cs b/Runtime/Scripts/Core/MemoryCell.cs index 7f9fe6e..e959e16 100644 --- a/Runtime/Scripts/Core/MemoryCell.cs +++ b/Runtime/Scripts/Core/MemoryCell.cs @@ -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; diff --git a/Runtime/Scripts/Core/Neuron.cs b/Runtime/Scripts/Core/Neuron.cs index f85a2e3..1dd884f 100644 --- a/Runtime/Scripts/Core/Neuron.cs +++ b/Runtime/Scripts/Core/Neuron.cs @@ -30,16 +30,16 @@ namespace NanoBrain { /// /// The Cluster Preafb in which the new Neuron should be created /// The name of the new Neuron - 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"); - } + // 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 @@ -316,7 +316,7 @@ namespace NanoBrain { /// \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); @@ -329,7 +329,7 @@ namespace NanoBrain { } protected virtual void CloneFields(Neuron clone) { - clone.clusterPrefab = this.clusterPrefab; + clone.parent = this.parent; clone.bias = this.bias; clone.combinator = this.combinator; clone.curve = this.curve; @@ -374,10 +374,10 @@ namespace NanoBrain { } - if (nucleus.clusterPrefab != null) { - nucleus.clusterPrefab.cluster.nuclei.RemoveAll(n => n == nucleus); - nucleus.clusterPrefab.cluster.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(); } } diff --git a/Runtime/Scripts/Core/Nucleus.cs b/Runtime/Scripts/Core/Nucleus.cs index ecd9579..5c91a4a 100644 --- a/Runtime/Scripts/Core/Nucleus.cs +++ b/Runtime/Scripts/Core/Nucleus.cs @@ -23,8 +23,9 @@ public abstract class Nucleus { /// /// The cluster prefab in which the nucleus is located /// - [SerializeReference] - public ClusterPrefab clusterPrefab; + // [SerializeReference] + // public ClusterPrefab clusterPrefab; + /// /// The cluster instance in which the nucleus is located /// diff --git a/Runtime/Scripts/ScriptableObjects/ClusterPrefab.cs b/Runtime/Scripts/ScriptableObjects/ClusterPrefab.cs index df72c27..574552d 100644 --- a/Runtime/Scripts/ScriptableObjects/ClusterPrefab.cs +++ b/Runtime/Scripts/ScriptableObjects/ClusterPrefab.cs @@ -82,9 +82,12 @@ namespace NanoBrain { // This is an invariant and should be ensured before the nucleus is used // because output requires it. public void EnsureInitialization() { + this.cluster.prefab = this; + this.cluster.name = this.name; this.cluster.nuclei ??= new List(); if (this.cluster.nuclei.Count <= 0) - new Neuron(this, "Output"); // Every cluster should have at least 1 neuron + new Neuron(this.cluster, "Output"); // Every cluster should have at least 1 neuron + this.cluster.instanceCount = 1; // nuclei ??= new List(); // if (nuclei.Count == 0) // new Neuron(this, "Output"); // Every cluster should have at least 1 neuron