Removed clusterPrefab property

This commit is contained in:
Pascal Serrarens 2026-05-05 11:14:35 +02:00
parent 2ef67fe107
commit 2ff550cba4
6 changed files with 50 additions and 45 deletions

View File

@ -232,7 +232,7 @@ namespace NanoBrain {
bool connecting = GUILayout.Button("Add Output Neuron"); bool connecting = GUILayout.Button("Add Output Neuron");
if (connecting) { if (connecting) {
Nucleus newOutput = new Neuron(this.prefab, "New Output"); Nucleus newOutput = new Neuron(this.prefab.cluster, "New Output");
// Regenerate the temporary clsuter instance // Regenerate the temporary clsuter instance
// See also the constructor // See also the constructor
this.currentCluster = new(this.prefab); this.currentCluster = new(this.prefab);
@ -351,21 +351,21 @@ namespace NanoBrain {
else { else {
EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginHorizontal();
if (synapse.neuron.clusterPrefab != this.currentNucleus.clusterPrefab) { if (synapse.neuron.parent.prefab != this.currentNucleus.parent.prefab) {
// If it is a different cluster // If it is a different cluster
GUIStyle labelStyle = new(GUI.skin.label); GUIStyle labelStyle = new(GUI.skin.label);
float labelWidth = 200; float labelWidth = 200;
if (synapse.neuron.clusterPrefab != null) { if (synapse.neuron.parent.prefab != null) {
labelWidth = labelStyle.CalcSize(new GUIContent($"{synapse.neuron.clusterPrefab.name}.")).x; labelWidth = labelStyle.CalcSize(new GUIContent($"{synapse.neuron.parent.prefab.name}.")).x;
GUILayout.Label($"{synapse.neuron.clusterPrefab.name}", GUILayout.Width(labelWidth)); 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 selectedIndex = System.Array.IndexOf(options, synapse.neuron.name);
int newIndex = EditorGUILayout.Popup(selectedIndex, options); int newIndex = EditorGUILayout.Popup(selectedIndex, options);
if (newIndex != selectedIndex) { if (newIndex != selectedIndex) {
// Nucleus selectedNucleus = synapse.neuron.parent.clusterNuclei[newIndex]; // Nucleus selectedNucleus = synapse.neuron.parent.clusterNuclei[newIndex];
// Neuron newNeuron = selectedNucleus as Neuron; // 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); ChangeSynapse(synapse, newNeuron);
} }
} }
@ -445,13 +445,14 @@ namespace NanoBrain {
} }
protected virtual void AddNeuronInput(Nucleus nucleus) { protected virtual void AddNeuronInput(Nucleus nucleus) {
Neuron newNeuroid = new(this.prefab, "New neuron"); Neuron newNeuron = new (this.currentCluster, "New Neuron");
newNeuroid.AddReceiver(nucleus); //Neuron newNeuroid = new(this.prefab.cluster, "New neuron");
this.currentNucleus = newNeuroid; newNeuron.AddReceiver(nucleus);
this.currentNucleus = newNeuron;
} }
protected virtual void AddMemoryCellInput(Nucleus nucleus) { 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); newMemory.AddReceiver(nucleus);
this.currentNucleus = newMemory; this.currentNucleus = newMemory;
} }
@ -531,13 +532,13 @@ namespace NanoBrain {
} }
protected virtual void DisconnectNucleus(Neuron nucleus) { protected virtual void DisconnectNucleus(Neuron nucleus) {
if (this.currentNucleus.clusterPrefab == null) if (this.currentNucleus.parent.prefab == null)
return; return;
Neuron currentNeuron = this.currentNucleus as Neuron; Neuron currentNeuron = this.currentNucleus as Neuron;
string[] names = currentNeuron.synapses.Select(synapse => synapse.neuron.name).ToArray(); string[] names = currentNeuron.synapses.Select(synapse => synapse.neuron.name).ToArray();
int selectedIndex = -1; int selectedIndex = -1;
selectedIndex = EditorGUILayout.Popup("Disconnect from", selectedIndex, names); 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 synapse = currentNeuron.synapses[selectedIndex];
synapse.neuron.RemoveReceiver(this.currentNucleus); synapse.neuron.RemoveReceiver(this.currentNucleus);

View File

@ -72,10 +72,10 @@ namespace NanoBrain {
public Cluster(ClusterPrefab prefab, ClusterPrefab parent = null) { public Cluster(ClusterPrefab prefab, ClusterPrefab parent = null) {
this.prefab = prefab; this.prefab = prefab;
this.name = prefab.name; this.name = prefab.name;
this.clusterPrefab = parent; this.parent.prefab = parent;
if (this.clusterPrefab != null) if (this.parent.prefab != null)
this.clusterPrefab.cluster.nuclei.Add(this); this.parent.prefab.cluster.nuclei.Add(this);
ClonePrefab(); ClonePrefab();
_ = this.inputs; _ = this.inputs;
@ -108,9 +108,9 @@ namespace NanoBrain {
foreach (Synapse prefabSynapse in prefabNeuron.synapses) { foreach (Synapse prefabSynapse in prefabNeuron.synapses) {
Neuron synapseNeuron = prefabSynapse.neuron; 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 // 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; Cluster clonedCluster = this.nuclei.Find(n => n.name == prefabCluster.name) as Cluster;
if (clonedCluster == null) if (clonedCluster == null)
continue; continue;
@ -177,7 +177,7 @@ namespace NanoBrain {
Debug.Log($"create {clonedCluster.prefab.name} sibling"); Debug.Log($"create {clonedCluster.prefab.name} sibling");
Cluster sibling = new(clonedCluster.prefab, this) { Cluster sibling = new(clonedCluster.prefab, this) {
name = $"{clonedCluster.baseName}: {instanceIx}", name = $"{clonedCluster.baseName}: {instanceIx}",
clusterPrefab = this.clusterPrefab, parent = this.parent,
instanceCount = this.instanceCount, instanceCount = this.instanceCount,
}; };
siblings.Add(sibling); siblings.Add(sibling);
@ -286,9 +286,9 @@ namespace NanoBrain {
private void CloneSynapses(Neuron prefabNeuron, Neuron clonedNeuron) { private void CloneSynapses(Neuron prefabNeuron, Neuron clonedNeuron) {
foreach (Synapse prefabSynapse in prefabNeuron.synapses) { foreach (Synapse prefabSynapse in prefabNeuron.synapses) {
Neuron synapseNeuron = prefabSynapse.neuron; 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 // 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; Cluster clonedCluster = this.nuclei.Find(n => n.name == prefabCluster.name) as Cluster;
if (clonedCluster == null) if (clonedCluster == null)
continue; continue;
@ -404,7 +404,7 @@ namespace NanoBrain {
// Clusters should not be cloned, but instantiated from the prefab.... // Clusters should not be cloned, but instantiated from the prefab....
Cluster clone = new(this.prefab, parent) { Cluster clone = new(this.prefab, parent) {
name = this.name, name = this.name,
clusterPrefab = this.clusterPrefab, parent = this.parent,
instanceCount = this.instanceCount, instanceCount = this.instanceCount,
}; };
// Somehow siblingClusters should be cloned too. Believe I do this in ClonePrefab right now. // 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) { foreach (Nucleus receiver in output.receivers) {
// Debug.Log($"output {receiver.name}"); // Debug.Log($"output {receiver.name}");
// Only add receivers outside this cluster // Only add receivers outside this cluster
if (receiver.clusterPrefab != this.prefab) { if (receiver.parent.prefab != this.prefab) {
if (removeDuplicates == false || receivers.Contains(receiver) == false) if (removeDuplicates == false || receivers.Contains(receiver) == false)
// Debug.Log($" YES"); // Debug.Log($" YES");
receivers.Add(receiver); receivers.Add(receiver);
@ -812,7 +812,7 @@ namespace NanoBrain {
foreach (Nucleus receiver in output.receivers) { foreach (Nucleus receiver in output.receivers) {
// Only add receivers outside this cluster // Only add receivers outside this cluster
if (receiver.clusterPrefab != this.prefab) if (receiver.parent.prefab != this.prefab)
connections.Add((output, receiver)); connections.Add((output, receiver));
} }
} }
@ -846,7 +846,7 @@ namespace NanoBrain {
Debug.Log($"Check {this.name}.{output.name} receivers"); Debug.Log($"Check {this.name}.{output.name} receivers");
Nucleus[] receivers = output.receivers.ToArray(); Nucleus[] receivers = output.receivers.ToArray();
foreach (Nucleus receiver in receivers) { foreach (Nucleus receiver in receivers) {
if (receiver.clusterPrefab != this.prefab) { if (receiver.parent.prefab != this.prefab) {
// Replace synapse with new synapse // Replace synapse with new synapse
// to the new cluster // to the new cluster
Debug.Log($"move {receiver.name} from {this.name}.{output.name} to {newCluster.name}.{newOutput.name}"); Debug.Log($"move {receiver.name} from {this.name}.{output.name} to {newCluster.name}.{newOutput.name}");

View File

@ -12,7 +12,7 @@ namespace NanoBrain {
[Serializable] [Serializable]
public class MemoryCell : Neuron { public class MemoryCell : Neuron {
public MemoryCell(ClusterPrefab cluster, string name) : base(cluster, name) { } // public MemoryCell(ClusterPrefab cluster, string name) : base(cluster, name) { }
public MemoryCell(Cluster parent, string name) : base(parent, name) { } public MemoryCell(Cluster parent, string name) : base(parent, name) { }
public bool staticMemory = false; public bool staticMemory = false;

View File

@ -30,16 +30,16 @@ namespace NanoBrain {
/// </summary> /// </summary>
/// <param name="prefab">The Cluster Preafb in which the new Neuron should be created</param> /// <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> /// <param name="name">The name of the new Neuron</param>
public Neuron(ClusterPrefab prefab, string name) { // public Neuron(ClusterPrefab prefab, string name) {
this.clusterPrefab = prefab; // this.clusterPrefab = prefab;
this.name = name; // this.name = name;
if (this.clusterPrefab != null) { // if (this.clusterPrefab != null) {
this.clusterPrefab.cluster.nuclei.Add(this); // this.clusterPrefab.cluster.nuclei.Add(this);
this.clusterPrefab.cluster.RefreshOutputs(); // this.clusterPrefab.cluster.RefreshOutputs();
} // }
else // else
Debug.LogError("No prefab when adding neuron to prefab"); // Debug.LogError("No prefab when adding neuron to prefab");
} // }
#region Serialization #region Serialization
@ -316,7 +316,7 @@ namespace NanoBrain {
/// \copydoc NanoBrain::Nucleus::Clone /// \copydoc NanoBrain::Nucleus::Clone
public override Nucleus Clone(ClusterPrefab prefab) { public override Nucleus Clone(ClusterPrefab prefab) {
Neuron clone = new(prefab, this.name); Neuron clone = new(prefab.cluster, this.name);
CloneFields(clone); CloneFields(clone);
foreach (Synapse synapse in this.synapses) { foreach (Synapse synapse in this.synapses) {
Synapse clonedSynapse = clone.AddSynapse(synapse.neuron); Synapse clonedSynapse = clone.AddSynapse(synapse.neuron);
@ -329,7 +329,7 @@ namespace NanoBrain {
} }
protected virtual void CloneFields(Neuron clone) { protected virtual void CloneFields(Neuron clone) {
clone.clusterPrefab = this.clusterPrefab; clone.parent = this.parent;
clone.bias = this.bias; clone.bias = this.bias;
clone.combinator = this.combinator; clone.combinator = this.combinator;
clone.curve = this.curve; clone.curve = this.curve;
@ -374,10 +374,10 @@ namespace NanoBrain {
} }
if (nucleus.clusterPrefab != null) { if (nucleus.parent.prefab != null) {
nucleus.clusterPrefab.cluster.nuclei.RemoveAll(n => n == nucleus); nucleus.parent.prefab.cluster.nuclei.RemoveAll(n => n == nucleus);
nucleus.clusterPrefab.cluster.RefreshOutputs(); nucleus.parent.prefab.cluster.RefreshOutputs();
nucleus.clusterPrefab.GarbageCollection(); nucleus.parent.prefab.GarbageCollection();
} }
} }

View File

@ -23,8 +23,9 @@ public abstract class Nucleus {
/// <summary> /// <summary>
/// The cluster prefab in which the nucleus is located /// The cluster prefab in which the nucleus is located
/// </summary> /// </summary>
[SerializeReference] // [SerializeReference]
public ClusterPrefab clusterPrefab; // public ClusterPrefab clusterPrefab;
/// <summary> /// <summary>
/// The cluster instance in which the nucleus is located /// The cluster instance in which the nucleus is located
/// </summary> /// </summary>

View File

@ -82,9 +82,12 @@ namespace NanoBrain {
// This is an invariant and should be ensured before the nucleus is used // This is an invariant and should be ensured before the nucleus is used
// because output requires it. // because output requires it.
public void EnsureInitialization() { public void EnsureInitialization() {
this.cluster.prefab = this;
this.cluster.name = this.name;
this.cluster.nuclei ??= new List<Nucleus>(); this.cluster.nuclei ??= new List<Nucleus>();
if (this.cluster.nuclei.Count <= 0) 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<Nucleus>(); // nuclei ??= new List<Nucleus>();
// if (nuclei.Count == 0) // if (nuclei.Count == 0)
// new Neuron(this, "Output"); // Every cluster should have at least 1 neuron // new Neuron(this, "Output"); // Every cluster should have at least 1 neuron