From b0f4b411e3230b4fd404293b4f0bb6de87a0fed8 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Wed, 15 Apr 2026 17:22:54 +0200 Subject: [PATCH] Status quo adding clusterArrays --- Editor/ClusterInspector.cs | 1 + Editor/ClusterViewer.cs | 15 ++++++---- Runtime/Scripts/Core/ClusterArray.cs | 45 +++++++++++++++++++++++----- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/Editor/ClusterInspector.cs b/Editor/ClusterInspector.cs index c90c5cf..4e4d684 100644 --- a/Editor/ClusterInspector.cs +++ b/Editor/ClusterInspector.cs @@ -188,6 +188,7 @@ 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()); diff --git a/Editor/ClusterViewer.cs b/Editor/ClusterViewer.cs index a9e5d96..213c25a 100644 --- a/Editor/ClusterViewer.cs +++ b/Editor/ClusterViewer.cs @@ -203,11 +203,11 @@ namespace NanoBrain { float yMin = 10 + margin - size / 2; float yMax = 400 - margin + size; Vector3[] verts = new Vector3[4] { - new(xMin, yMin, 0), - new(xMax, yMin, 0), - new(xMax, yMax, 0), - new(xMin, yMax, 0) - }; + new(xMin, yMin, 0), + new(xMax, yMin, 0), + new(xMax, yMax, 0), + new(xMin, yMax, 0) + }; Handles.color = Color.black; Handles.DrawAAConvexPolygon(verts); int row = 0; @@ -331,6 +331,10 @@ namespace NanoBrain { continue; drawnArrays.Add(clusterReceptor.nucleiArray); } + // Oops... + // else if (synapse.neuron is Cluster cluster && cluster.clusterArray != null) { + + // } if (synapse.neuron is Neuron synapseNeuron) { float value = synapseNeuron.outputMagnitude * synapse.weight; // Debug.Log($"{synapse.nucleus.name}: {value} {length(synapse.nucleus.outputValue)} {synapse.weight}"); @@ -475,7 +479,6 @@ namespace NanoBrain { } else Handles.Label(labelPos, nucleus.name, style); - } // Draw Cluster ring diff --git a/Runtime/Scripts/Core/ClusterArray.cs b/Runtime/Scripts/Core/ClusterArray.cs index fec9b23..58a9e14 100644 --- a/Runtime/Scripts/Core/ClusterArray.cs +++ b/Runtime/Scripts/Core/ClusterArray.cs @@ -1,12 +1,17 @@ +using System; +using System.Collections.Generic; using UnityEngine; namespace NanoBrain { + [Serializable] public class ClusterArray : Nucleus { public ClusterPrefab prefab; public Cluster[] clusters; + public Dictionary thingClusters = new(); + public ClusterArray(ClusterPrefab prefab, Cluster parent, int size, Nucleus receiver = null) { this.prefab = prefab; this.name = prefab.name; @@ -49,9 +54,10 @@ namespace NanoBrain { public void Add(ClusterPrefab prefab) { if (this.clusters.Length == 0) { Debug.LogError("Empty perceptoid array, cannot add"); + return; } int newLength = this.clusters.Length + 1; - Cluster[] newArray = new Cluster[newLength]; + Cluster[] newClusters = new Cluster[newLength]; string baseName = this.name; int colonPos = baseName.IndexOf(":"); @@ -59,22 +65,25 @@ namespace NanoBrain { baseName = baseName[..colonPos]; for (int i = 0; i < this.clusters.Length; i++) - newArray[i] = this.clusters[i]; + newClusters[i] = this.clusters[i]; Cluster cluster = this.clusters[0]; - newArray[newLength - 1] = cluster.Clone(prefab) as Cluster; - newArray[newLength - 1].name = $"{baseName}: {newLength - 1}"; + newClusters[newLength - 1] = cluster.Clone(prefab) as Cluster; + newClusters[newLength - 1].name = $"{baseName}: {newLength - 1}"; + this.clusters = newClusters; } public void Remove() { int newLength = this.clusters.Length - 1; if (newLength == 0) { Debug.LogWarning("Perceptoid array cannot be empty"); + return; } - Cluster[] newArray = new Cluster[newLength]; + Cluster[] newClusters = new Cluster[newLength]; for (int i = 0; i < newLength; i++) - newArray[i] = this.clusters[i]; + newClusters[i] = this.clusters[i]; // Delete the last perception //Cluster.Delete(nucleus); + this.clusters = newClusters; } public override void UpdateStateIsolated() { @@ -87,12 +96,22 @@ namespace NanoBrain { 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(); + thingClusters[thingId] = selectedCluster; + return selectedCluster; + } private Cluster SelectCluster() { // Find a sleeping cluster foreach (Cluster cluster in clusters) { - if (cluster.defaultOutput.isSleeping) + if (cluster.defaultOutput.isSleeping) { + RemoveThingCluster(cluster); return cluster; + } } // Otherwise find the stalest cluster? @@ -102,8 +121,20 @@ namespace NanoBrain { stalestCluster = clusters[ix]; } + RemoveThingCluster(stalestCluster); return stalestCluster; } + + private void RemoveThingCluster(Cluster cluster) { + List keysToRemove = new(); + foreach (KeyValuePair kvp in thingClusters) { + if (kvp.Value == cluster) + keysToRemove.Add(kvp.Key); + } + + foreach (int thingId in keysToRemove) + thingClusters.Remove(thingId); + } } } \ No newline at end of file