Status quo adding clusterArrays

This commit is contained in:
Pascal Serrarens 2026-04-15 17:22:54 +02:00
parent 1fc75a8143
commit b0f4b411e3
3 changed files with 48 additions and 13 deletions

View File

@ -188,6 +188,7 @@ namespace NanoBrain {
anythingChanged = true; anythingChanged = true;
} }
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
} else if (this.currentNucleus is Cluster cluster && cluster.clusterArray != null) { } else if (this.currentNucleus is Cluster cluster && cluster.clusterArray != null) {
EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginHorizontal();
EditorGUILayout.IntField("Array size", cluster.clusterArray.clusters.Count()); EditorGUILayout.IntField("Array size", cluster.clusterArray.clusters.Count());

View File

@ -331,6 +331,10 @@ namespace NanoBrain {
continue; continue;
drawnArrays.Add(clusterReceptor.nucleiArray); drawnArrays.Add(clusterReceptor.nucleiArray);
} }
// Oops...
// else if (synapse.neuron is Cluster cluster && cluster.clusterArray != null) {
// }
if (synapse.neuron is Neuron synapseNeuron) { if (synapse.neuron is Neuron synapseNeuron) {
float value = synapseNeuron.outputMagnitude * synapse.weight; float value = synapseNeuron.outputMagnitude * synapse.weight;
// Debug.Log($"{synapse.nucleus.name}: {value} {length(synapse.nucleus.outputValue)} {synapse.weight}"); // Debug.Log($"{synapse.nucleus.name}: {value} {length(synapse.nucleus.outputValue)} {synapse.weight}");
@ -475,7 +479,6 @@ namespace NanoBrain {
} }
else else
Handles.Label(labelPos, nucleus.name, style); Handles.Label(labelPos, nucleus.name, style);
} }
// Draw Cluster ring // Draw Cluster ring

View File

@ -1,12 +1,17 @@
using System;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
namespace NanoBrain { namespace NanoBrain {
[Serializable]
public class ClusterArray : Nucleus { public class ClusterArray : Nucleus {
public ClusterPrefab prefab; public ClusterPrefab prefab;
public Cluster[] clusters; public Cluster[] clusters;
public Dictionary<int, Cluster> thingClusters = new();
public ClusterArray(ClusterPrefab prefab, Cluster parent, int size, Nucleus receiver = null) { public ClusterArray(ClusterPrefab prefab, Cluster parent, int size, Nucleus receiver = null) {
this.prefab = prefab; this.prefab = prefab;
this.name = prefab.name; this.name = prefab.name;
@ -49,9 +54,10 @@ namespace NanoBrain {
public void Add(ClusterPrefab prefab) { public void Add(ClusterPrefab prefab) {
if (this.clusters.Length == 0) { if (this.clusters.Length == 0) {
Debug.LogError("Empty perceptoid array, cannot add"); Debug.LogError("Empty perceptoid array, cannot add");
return;
} }
int newLength = this.clusters.Length + 1; int newLength = this.clusters.Length + 1;
Cluster[] newArray = new Cluster[newLength]; Cluster[] newClusters = new Cluster[newLength];
string baseName = this.name; string baseName = this.name;
int colonPos = baseName.IndexOf(":"); int colonPos = baseName.IndexOf(":");
@ -59,22 +65,25 @@ namespace NanoBrain {
baseName = baseName[..colonPos]; baseName = baseName[..colonPos];
for (int i = 0; i < this.clusters.Length; i++) for (int i = 0; i < this.clusters.Length; i++)
newArray[i] = this.clusters[i]; newClusters[i] = this.clusters[i];
Cluster cluster = this.clusters[0]; Cluster cluster = this.clusters[0];
newArray[newLength - 1] = cluster.Clone(prefab) as Cluster; newClusters[newLength - 1] = cluster.Clone(prefab) as Cluster;
newArray[newLength - 1].name = $"{baseName}: {newLength - 1}"; newClusters[newLength - 1].name = $"{baseName}: {newLength - 1}";
this.clusters = newClusters;
} }
public void Remove() { public void Remove() {
int newLength = this.clusters.Length - 1; int newLength = this.clusters.Length - 1;
if (newLength == 0) { if (newLength == 0) {
Debug.LogWarning("Perceptoid array cannot be empty"); 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++) for (int i = 0; i < newLength; i++)
newArray[i] = this.clusters[i]; newClusters[i] = this.clusters[i];
// Delete the last perception // Delete the last perception
//Cluster.Delete(nucleus); //Cluster.Delete(nucleus);
this.clusters = newClusters;
} }
public override void UpdateStateIsolated() { public override void UpdateStateIsolated() {
@ -87,13 +96,23 @@ namespace NanoBrain {
Cluster selectedCluster = SelectCluster(); Cluster selectedCluster = SelectCluster();
return selectedCluster; 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() { private Cluster SelectCluster() {
// Find a sleeping cluster // Find a sleeping cluster
foreach (Cluster cluster in clusters) { foreach (Cluster cluster in clusters) {
if (cluster.defaultOutput.isSleeping) if (cluster.defaultOutput.isSleeping) {
RemoveThingCluster(cluster);
return cluster; return cluster;
} }
}
// Otherwise find the stalest cluster? // Otherwise find the stalest cluster?
Cluster stalestCluster = clusters[0]; Cluster stalestCluster = clusters[0];
@ -102,8 +121,20 @@ namespace NanoBrain {
stalestCluster = clusters[ix]; stalestCluster = clusters[ix];
} }
RemoveThingCluster(stalestCluster);
return stalestCluster; return stalestCluster;
} }
private void RemoveThingCluster(Cluster cluster) {
List<int> keysToRemove = new();
foreach (KeyValuePair<int, Cluster> kvp in thingClusters) {
if (kvp.Value == cluster)
keysToRemove.Add(kvp.Key);
}
foreach (int thingId in keysToRemove)
thingClusters.Remove(thingId);
}
} }
} }