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;
}
EditorGUILayout.EndHorizontal();
} else if (this.currentNucleus is Cluster cluster && cluster.clusterArray != null) {
EditorGUILayout.BeginHorizontal();
EditorGUILayout.IntField("Array size", cluster.clusterArray.clusters.Count());

View File

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

View File

@ -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<int, Cluster> 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<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);
}
}
}