Removed clusterPrefab property

This commit is contained in:
Pascal Serrarens 2026-05-05 11:14:35 +02:00
parent 79376c30ea
commit d2ef26b6f8
7 changed files with 73 additions and 60 deletions

View File

@ -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);

View File

@ -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}");

View File

@ -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;

View File

@ -30,16 +30,16 @@ namespace NanoBrain {
/// </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.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();
}
}

View File

@ -23,8 +23,9 @@ public abstract class Nucleus {
/// <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>

View File

@ -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<Nucleus>();
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>();
// if (nuclei.Count == 0)
// new Neuron(this, "Output"); // Every cluster should have at least 1 neuron

View File

@ -13,31 +13,29 @@ MonoBehaviour:
m_Name: New Cluster Prefab
m_EditorClassIdentifier:
cluster:
name:
clusterPrefab: {fileID: 0}
name: New Cluster Prefab
parent:
rid: -2
prefab: {fileID: 0}
instanceCount: 0
prefab: {fileID: 11400000}
instanceCount: 1
nuclei:
- rid: 4201949831649034293
- rid: 4201949831649034294
- rid: 4201949831649034317
- rid: 4201949831649034319
references:
version: 2
RefIds:
- rid: -2
type: {class: , ns: , asm: }
- rid: 4201949831649034293
- rid: 4201949831649034317
type: {class: Neuron, ns: NanoBrain, asm: Assembly-CSharp}
data:
name: Output
clusterPrefab: {fileID: 11400000}
parent:
rid: -2
rid: 4201949831649034318
bias: {x: 0, y: 0, z: 0}
_synapses:
- neuron:
rid: 4201949831649034294
rid: 4201949831649034319
weight: 1
combinator: 0
_curvePreset: 0
@ -68,13 +66,23 @@ MonoBehaviour:
curveMax: 1
trace: 0
_receivers: []
- rid: 4201949831649034294
type: {class: Neuron, ns: NanoBrain, asm: Assembly-CSharp}
- rid: 4201949831649034318
type: {class: Cluster, ns: NanoBrain, asm: Assembly-CSharp}
data:
name: New neuron
clusterPrefab: {fileID: 11400000}
name: New Cluster Prefab
parent:
rid: -2
prefab: {fileID: 11400000}
instanceCount: 1
nuclei:
- rid: 4201949831649034317
- rid: 4201949831649034319
- rid: 4201949831649034319
type: {class: Neuron, ns: NanoBrain, asm: Assembly-CSharp}
data:
name: New Neuron
parent:
rid: 4201949831649034318
bias: {x: 0, y: 0, z: 0}
_synapses: []
combinator: 0
@ -106,4 +114,4 @@ MonoBehaviour:
curveMax: 1
trace: 0
_receivers:
- rid: 4201949831649034293
- rid: 4201949831649034317