From 1e2726ecd46f76abbf94cb9f4ceb706d191ca664 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Mon, 9 Feb 2026 13:04:14 +0100 Subject: [PATCH] Foraging is working, but not correct yet --- Cluster.cs | 2 +- Editor/ClusterInspector.cs | 54 ++++++++++++++++++-------------------- Editor/NanoBrain_Editor.cs | 2 +- Neuron.cs | 2 +- Nucleus.cs | 3 ++- 5 files changed, 31 insertions(+), 32 deletions(-) diff --git a/Cluster.cs b/Cluster.cs index e94417b..1868d91 100644 --- a/Cluster.cs +++ b/Cluster.cs @@ -271,7 +271,7 @@ public class Cluster : Nucleus { public override void UpdateNuclei() { this.stale++; - if (this.stale > 5) + if (this.stale > staleValueForSleep) _outputValue = Vector3.zero; foreach (Nucleus nucleus in this.nuclei) diff --git a/Editor/ClusterInspector.cs b/Editor/ClusterInspector.cs index 0e3f10c..d4712bc 100644 --- a/Editor/ClusterInspector.cs +++ b/Editor/ClusterInspector.cs @@ -17,20 +17,20 @@ public class ClusterInspector : Editor { #region Start public override VisualElement CreateInspectorGUI() { - ClusterPrefab cluster = target as ClusterPrefab; - if (cluster != null) - cluster.EnsureInitialization(); + ClusterPrefab prefab = target as ClusterPrefab; + if (prefab != null) + prefab.EnsureInitialization(); serializedObject.Update(); VisualElement root = new(); - CreateInspector(root, cluster); + CreateInspector(root, prefab, prefab.output); serializedObject.ApplyModifiedProperties(); return root; } - public static GraphView CreateInspector(VisualElement root, ClusterPrefab cluster) { + public static GraphView CreateInspector(VisualElement root, ClusterPrefab cluster, Nucleus output) { root.style.paddingLeft = 0; root.style.paddingRight = 0; root.style.paddingTop = 0; @@ -61,14 +61,14 @@ public class ClusterInspector : Editor { mainContainer.Add(inspectorContainer); root.Add(mainContainer); - graph.SetGraph(null, cluster.output, inspectorContainer); + graph.SetGraph(null, output, inspectorContainer); return graph; } public class GraphView : VisualElement { - readonly ClusterPrefab cluster; + readonly ClusterPrefab prefab; SerializedObject serializedBrain; Nucleus currentNucleus; GameObject gameObject; @@ -80,7 +80,7 @@ public class ClusterInspector : Editor { PopupField outputsField; public GraphView(ClusterPrefab prefab) { - this.cluster = prefab; + this.prefab = prefab; name = "content"; style.flexGrow = 1; @@ -100,7 +100,7 @@ public class ClusterInspector : Editor { } }; - List names = this.cluster.outputs.Select(output => output.name).ToList(); + List names = this.prefab.outputs.Select(output => output.name).ToList(); outputsField = new(names, names.First()) { style = { flexGrow = 1 } }; @@ -120,13 +120,18 @@ public class ClusterInspector : Editor { } void OnOutputChanged(string outputName) { - this.currentNucleus = this.cluster.GetNucleus(outputName); + if (this.currentNucleus.parent != null) + // Get nucleus in the parent instance + this.currentNucleus = this.currentNucleus.parent.GetNucleus(outputName); + else + // Get nucleus in the prefab + this.currentNucleus = this.prefab.GetNucleus(outputName); } void OnAddClusterOutput() { - Nucleus newOutput = new Neuron(this.cluster, "New Output"); + Nucleus newOutput = new Neuron(this.prefab, "New Output"); - outputsField.choices = this.cluster.outputs.Select(output => output.name).ToList(); + outputsField.choices = this.prefab.outputs.Select(output => output.name).ToList(); outputsField.value = newOutput.name; this.currentNucleus = newOutput; @@ -150,7 +155,7 @@ public class ClusterInspector : Editor { this.gameObject = gameObject; //this.cluster = brain; if (Application.isPlaying == false) - this.serializedBrain = new SerializedObject(this.cluster); + this.serializedBrain = new SerializedObject(this.prefab); this.currentNucleus = nucleus; Rebuild(inspectorContainer); } @@ -165,7 +170,7 @@ public class ClusterInspector : Editor { if (currentWrapper != null) DestroyImmediate(currentWrapper); - currentWrapper = CreateInstance().Init(this.currentNucleus, cluster); + currentWrapper = CreateInstance().Init(this.currentNucleus, prefab); DrawInspector(inspectorContainer); } @@ -277,13 +282,6 @@ public class ClusterInspector : Editor { DrawNucleus(nucleus, pos, maxValue, size); row++; } - // GUIStyle style = new(EditorStyles.label) { - // alignment = TextAnchor.UpperCenter, - // normal = { textColor = Color.white }, - // fontStyle = FontStyle.Bold, - // }; - // Vector3 labelPos = new Vector3(150, yMax, 0) - Vector3.down * (size + 25); // below disc along up axis - // Handles.Label(labelPos, this.currentNucleus.name, style); } else { Handles.color = Color.white; @@ -547,7 +545,7 @@ public class ClusterInspector : Editor { EditorGUILayout.BeginHorizontal(); EditorGUILayout.IntField("Array size", neuroid.array.nuclei.Count()); if (GUILayout.Button("Add")) - neuroid.array.AddNucleus(this.cluster); + neuroid.array.AddNucleus(this.prefab); if (GUILayout.Button("Del")) neuroid.array.RemoveNucleus(); EditorGUILayout.EndHorizontal(); @@ -560,8 +558,8 @@ public class ClusterInspector : Editor { showSynapses = EditorGUILayout.BeginFoldoutHeaderGroup(showSynapses, "Synapses"); if (showSynapses) { - ConnectNucleus(this.cluster, this.currentNucleus); - AddSynapse(this.cluster, this.currentNucleus); + ConnectNucleus(this.prefab, this.currentNucleus); + AddSynapse(this.prefab, this.currentNucleus); if (this.currentNucleus.synapses.Count > 0) { Synapse[] synapses = this.currentNucleus.synapses.ToArray(); @@ -645,7 +643,7 @@ public class ClusterInspector : Editor { protected virtual void AddInputNeuron(Nucleus nucleus) { //Neuron newNeuroid = new(this.cluster, "New neuron"); - Neuron newNeuroid = new(this.cluster, "New neuron"); + Neuron newNeuroid = new(this.prefab, "New neuron"); newNeuroid.AddReceiver(nucleus); this.currentNucleus = newNeuroid; BuildLayers(); @@ -667,14 +665,14 @@ public class ClusterInspector : Editor { } protected void AddSelectorInput(Nucleus nucleus) { - Selector newSelector = new(this.cluster, "New Selector"); + Selector newSelector = new(this.prefab, "New Selector"); newSelector.AddReceiver(nucleus); this.currentNucleus = newSelector; BuildLayers(); } protected virtual void AddInputMemoryCell(Nucleus nucleus) { - MemoryCell newMemory = new(this.cluster, "New memory cell"); + MemoryCell newMemory = new(this.prefab, "New memory cell"); newMemory.AddReceiver(nucleus); this.currentNucleus = newMemory; BuildLayers(); @@ -685,7 +683,7 @@ public class ClusterInspector : Editor { } private void OnClusterPicked(Nucleus nucleus, ClusterPrefab prefab) { - Cluster subclusterInstance = new(prefab, this.cluster); + Cluster subclusterInstance = new(prefab, this.prefab); subclusterInstance.AddReceiver(nucleus); // This does not work somehow // this.currentNucleus = subclusterInstance; diff --git a/Editor/NanoBrain_Editor.cs b/Editor/NanoBrain_Editor.cs index 658c02c..138c901 100644 --- a/Editor/NanoBrain_Editor.cs +++ b/Editor/NanoBrain_Editor.cs @@ -27,7 +27,7 @@ public class NanoBrainComponent_Editor : Editor { VisualElement root = new(); - ClusterInspector.CreateInspector(root, brain.prefab); + ClusterInspector.CreateInspector(root, brain.prefab, brain.output); if (Application.isPlaying == false) serializedObject.ApplyModifiedProperties(); diff --git a/Neuron.cs b/Neuron.cs index 4b86845..a7b56fe 100644 --- a/Neuron.cs +++ b/Neuron.cs @@ -226,7 +226,7 @@ public class Neuron : Nucleus { result = normalize(sum) * activatedValue; break; } - if (this.stale > 5) + if (this.stale > staleValueForSleep) this.outputValue = new float3(0,0,0); else this.outputValue = result; diff --git a/Nucleus.cs b/Nucleus.cs index 25bebb1..ff61523 100644 --- a/Nucleus.cs +++ b/Nucleus.cs @@ -32,6 +32,7 @@ public abstract class Nucleus { public bool isSleeping => lengthsq(this.outputValue) == 0; [NonSerialized] public int stale = 1000; + public readonly int staleValueForSleep = 20; public abstract Nucleus ShallowCloneTo(Cluster parent); public abstract Nucleus Clone(ClusterPrefab prefab); @@ -98,7 +99,7 @@ public abstract class Nucleus { public virtual void UpdateNuclei() { this.stale++; - if (this.stale > 5) { + if (this.stale > staleValueForSleep) { //Debug.Log($"{this.name} goes to sleep, stale = {this.stale}"); _outputValue = Vector3.zero; }