Added ClusterArray

This commit is contained in:
Pascal Serrarens 2026-04-15 17:06:45 +02:00
parent 0023920ffa
commit 1fc75a8143
8 changed files with 347 additions and 194 deletions

View File

@ -188,6 +188,20 @@ namespace NanoBrain {
anythingChanged = true;
}
EditorGUILayout.EndHorizontal();
} else if (this.currentNucleus is Cluster cluster && cluster.clusterArray != null) {
EditorGUILayout.BeginHorizontal();
EditorGUILayout.IntField("Array size", cluster.clusterArray.clusters.Count());
if (GUILayout.Button("Add")) {
Undo.RecordObject(prefabAsset, "Array add " + prefabAsset.name);
cluster.clusterArray.Add(this.prefab);
anythingChanged = true;
}
if (GUILayout.Button("Del")) {
Undo.RecordObject(prefabAsset, "Array delete " + prefabAsset.name);
cluster.clusterArray.Remove();
anythingChanged = true;
}
EditorGUILayout.EndHorizontal();
}
// Synapses
@ -387,24 +401,18 @@ namespace NanoBrain {
case Nucleus.Type.MemoryCell:
AddMemoryCellInput(nucleus);
break;
// case Nucleus.Type.Selector:
// AddSelectorInput(nucleus);
// break;
case Nucleus.Type.Cluster:
AddClusterInput(nucleus);
break;
// case Nucleus.Type.Pulsar:
// AddPulsarInput(nucleus);
// break;
case Nucleus.Type.Receptor:
AddReceptorInput(nucleus);
break;
// case Nucleus.Type.ReceptorArray:
// AddReceptorArrayInput(nucleus);
// break;
case Nucleus.Type.ClusterReceptor:
AddClusterReceptorInput(nucleus);
break;
case Nucleus.Type.ClusterArray:
AddClusterArrayInput(nucleus);
break;
default:
break;
}
@ -456,6 +464,13 @@ namespace NanoBrain {
var editor = Editor.CreateEditor(subCluster.prefab);
}
protected virtual void AddClusterArrayInput(Nucleus nucleus) {
ClusterPickerWindow.ShowPicker(prefab => OnPickedClusterArray(nucleus, prefab), "Select Cluster");
}
private void OnPickedClusterArray(Nucleus nucleus, ClusterPrefab selectedPrefab) {
_ = new ClusterArray(selectedPrefab, this.prefab, 1, nucleus);
}
int selectedConnectNucleus = -1;
// Connect to another nucleus in the same cluster
protected virtual bool ConnectNucleus(ClusterPrefab cluster, Nucleus nucleusToConnect) {

View File

@ -439,12 +439,34 @@ namespace NanoBrain {
style.normal.textColor = Color.white;
}
}
else if (nucleus is Cluster cluster && cluster.clusterArray != null) {
if (expandArray) {
// Put array indices above elements
style.alignment = TextAnchor.LowerCenter;
Vector3 labelPos1 = position + Vector3.down * (size + 5); // below disc
int colonPos1 = nucleus.name.IndexOf(":");
if (colonPos1 > 0) {
string extName = nucleus.name[(colonPos1 + 2)..];
Handles.Label(labelPos1, extName, style);
}
}
else {
// draw the array size label
if (color.grayscale > 0.5f)
style.normal.textColor = Color.black;
else
style.normal.textColor = Color.white;
Handles.Label(labelPosition, cluster.clusterArray.clusters.Length.ToString(), style);
style.normal.textColor = Color.white;
}
}
if (expandArray == false || nucleus is not IReceptor) {
// put name below nucleus
Vector3 labelPos = position - Vector3.down * (size + 5); // below neuron
style.alignment = TextAnchor.UpperCenter;
nucleus.name ??= "";
int colonPos = nucleus.name.IndexOf(":");
if (colonPos > 0 && colonPos < nucleus.name.Length - 2) {
// if it is an array, we should not show the :0 of the first element

View File

@ -28,6 +28,9 @@ public class Cluster : Nucleus {
}
}
[SerializeReference]
public ClusterArray clusterArray;
#region Init
/// <summary>

View File

@ -0,0 +1,109 @@
using UnityEngine;
namespace NanoBrain {
public class ClusterArray : Nucleus {
public ClusterPrefab prefab;
public Cluster[] clusters;
public ClusterArray(ClusterPrefab prefab, Cluster parent, int size, Nucleus receiver = null) {
this.prefab = prefab;
this.name = prefab.name;
this.clusters = new Cluster[size];
for (int ix = 0; ix < size; ix++) {
Cluster cluster = new(prefab, parent);
cluster.defaultOutput.AddReceiver(receiver);
cluster.clusterArray = this;
this.clusters[ix] = cluster;
}
}
public ClusterArray(ClusterPrefab prefab, ClusterPrefab parent, int size, Nucleus receiver = null) {
this.prefab = prefab;
this.name = prefab.name;
this.clusters = new Cluster[size];
for (int ix = 0; ix < size; ix++) {
Cluster cluster = new(prefab, parent);
cluster.defaultOutput.AddReceiver(receiver);
cluster.clusterArray = this;
this.clusters[ix] = cluster;
}
}
public override Nucleus ShallowCloneTo(Cluster parent) {
ClusterArray clone = new(this.prefab, parent, this.clusters.Length) {
clusterPrefab = this.clusterPrefab,
};
return clone;
}
public override Nucleus Clone(ClusterPrefab parent) {
ClusterArray clone = new(this.prefab, parent, this.clusters.Length) {
};
return clone;
}
public void Add(ClusterPrefab prefab) {
if (this.clusters.Length == 0) {
Debug.LogError("Empty perceptoid array, cannot add");
}
int newLength = this.clusters.Length + 1;
Cluster[] newArray = new Cluster[newLength];
string baseName = this.name;
int colonPos = baseName.IndexOf(":");
if (colonPos > 0)
baseName = baseName[..colonPos];
for (int i = 0; i < this.clusters.Length; i++)
newArray[i] = this.clusters[i];
Cluster cluster = this.clusters[0];
newArray[newLength - 1] = cluster.Clone(prefab) as Cluster;
newArray[newLength - 1].name = $"{baseName}: {newLength - 1}";
}
public void Remove() {
int newLength = this.clusters.Length - 1;
if (newLength == 0) {
Debug.LogWarning("Perceptoid array cannot be empty");
}
Cluster[] newArray = new Cluster[newLength];
for (int i = 0; i < newLength; i++)
newArray[i] = this.clusters[i];
// Delete the last perception
//Cluster.Delete(nucleus);
}
public override void UpdateStateIsolated() {
// Clusters don't do anything,
// The nuclei in them do the work
// and should be called directly, not from the cluster
}
public virtual Cluster GetThingCluster() {
Cluster selectedCluster = SelectCluster();
return selectedCluster;
}
private Cluster SelectCluster() {
// Find a sleeping cluster
foreach (Cluster cluster in clusters) {
if (cluster.defaultOutput.isSleeping)
return cluster;
}
// Otherwise find the stalest cluster?
Cluster stalestCluster = clusters[0];
for (int ix = 1; ix < clusters.Length; ix++) {
if (clusters[ix].defaultOutput.stale > stalestCluster.defaultOutput.stale)
stalestCluster = clusters[ix];
}
return stalestCluster;
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 837dfcffeb99804479a0887cbfc33372

View File

@ -9,8 +9,8 @@ using System.Linq;
namespace NanoBrain {
[Serializable]
public class ClusterReceptor : Cluster, IReceptor {
[Serializable]
public class ClusterReceptor : Cluster, IReceptor {
public ClusterReceptor(ClusterPrefab prefab, Cluster parent, string name) : base(prefab, parent) {
this.name = name;
this.array = new NucleusArray(this);
@ -272,6 +272,7 @@ public class ClusterReceptor : Cluster, IReceptor {
}
}
}
}
}

View File

@ -56,6 +56,7 @@ public abstract class Nucleus {
Cluster,
Receptor,
ClusterReceptor,
ClusterArray,
}
#region Synapses

View File

@ -108,6 +108,6 @@ namespace NanoBrain {
this._array ??= new NucleusArray(this.parent);
this._array.ProcessStimulus(thingId, inputValue, thingName);
}
}
}
}