From baa7defef0f80961140a23431a3879de6254946f Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Tue, 5 May 2026 17:37:59 +0200 Subject: [PATCH] Pheromones WIP --- Editor/ClusterEditor.cs | 25 +++---------------------- Runtime/Scripts/Core/Cluster.cs | 20 +++++++++++++++++--- Runtime/Scripts/Core/Neuron.cs | 11 +++++++---- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/Editor/ClusterEditor.cs b/Editor/ClusterEditor.cs index 3086b0a..b67650c 100644 --- a/Editor/ClusterEditor.cs +++ b/Editor/ClusterEditor.cs @@ -81,6 +81,7 @@ namespace NanoBrain { // So we create a temporary instance //this.currentCluster = new(prefab); this.currentCluster = prefab.cluster; + this.currentCluster.Refresh(); } public void SetGraph(GameObject gameObject, VisualElement inspectorContainer) { @@ -227,10 +228,8 @@ namespace NanoBrain { bool connecting = GUILayout.Button("Add Output Neuron"); if (connecting) { - Nucleus newOutput = new Neuron(this.prefab.cluster, "New Output"); - // Regenerate the temporary clsuter instance - // See also the constructor - this.currentCluster = new(this.prefab); + Nucleus newOutput = new Neuron(this.currentCluster, "New Output"); + this.currentCluster.Refresh(); this.currentNucleus = newOutput; this.selectedOutput = this.currentNucleus; } @@ -358,8 +357,6 @@ namespace NanoBrain { int selectedIndex = System.Array.IndexOf(options, synapse.neuron.name); int newIndex = EditorGUILayout.Popup(selectedIndex, options); if (newIndex != selectedIndex) { - // Nucleus selectedNucleus = synapse.neuron.parent.clusterNuclei[newIndex]; - // Neuron newNeuron = selectedNucleus as Neuron; Neuron newNeuron = synapse.neuron.parent.prefab.cluster.nuclei[newIndex] as Neuron; ChangeSynapse(synapse, newNeuron); } @@ -523,22 +520,6 @@ namespace NanoBrain { return connecting; } - // protected virtual void DisconnectNucleus(Neuron nucleus) { - // if (this.currentNucleus.parent.prefab == null) - // return; - // Neuron currentNeuron = this.currentNucleus as Neuron; - // string[] names = currentNeuron.synapses.Select(synapse => synapse.neuron.name).ToArray(); - // int selectedIndex = -1; - // selectedIndex = EditorGUILayout.Popup("Disconnect from", selectedIndex, names); - // if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.parent.prefab.cluster.nuclei.Count) { - // Synapse synapse = currentNeuron.synapses[selectedIndex]; - // synapse.neuron.RemoveReceiver(this.currentNucleus); - - // // synapse = currentNeuron.synapses[selectedIndex]; - // // synapse.neuron.RemoveReceiver(this.currentPrefabNucleus); - // } - // } - protected virtual void DeleteNucleus(Nucleus nucleus) { if (nucleus == null) return; diff --git a/Runtime/Scripts/Core/Cluster.cs b/Runtime/Scripts/Core/Cluster.cs index 9106984..8077e04 100644 --- a/Runtime/Scripts/Core/Cluster.cs +++ b/Runtime/Scripts/Core/Cluster.cs @@ -618,7 +618,7 @@ namespace NanoBrain { if (neuron.synapses.Count == 0) this._inputs.Add(nucleus); } - RefreshComputeOrders(); + RefreshComputeOrders(); } return this._inputs; } @@ -766,6 +766,14 @@ namespace NanoBrain { } } + public Neuron GetNeuron(string neuronName) { + foreach (Nucleus nucleus in this.nuclei) { + if (nucleus is Neuron neuron && neuron.name == neuronName) + return neuron; + } + return null; + } + public bool DeleteNucleus(Nucleus nucleus) { if (this.nuclei.Contains(nucleus) == false) { // Try to find the nucleus by name @@ -875,12 +883,12 @@ namespace NanoBrain { List computeOrder = this.computeOrders[startNucleus]; //if (startNucleus.trace) - Debug.Log($"Update from {startNucleus.name}"); + // Debug.Log($"Update from {startNucleus.name}"); foreach (Nucleus nucleus in computeOrder) { if (nucleus is not Cluster) { nucleus.UpdateStateIsolated(); //if (startNucleus.trace && nucleus is Neuron neuron) - Debug.Log($" {nucleus.name}"); + // Debug.Log($" {nucleus.name}"); if (nucleus is Neuron neuron) { foreach (Nucleus receiver in neuron.receivers) { if (receiver.parent != this) { @@ -910,6 +918,12 @@ namespace NanoBrain { #endregion Update public void Refresh() { + // This should not be needed, but somehow somewhere the parent is changed... + foreach (Nucleus nucleus in this.nuclei) { + if (nucleus is not Neuron neuron) + continue; + neuron.parent = this; + } RefreshOutputs(); RefreshComputeOrders(); } diff --git a/Runtime/Scripts/Core/Neuron.cs b/Runtime/Scripts/Core/Neuron.cs index 02fa2df..b5dcc66 100644 --- a/Runtime/Scripts/Core/Neuron.cs +++ b/Runtime/Scripts/Core/Neuron.cs @@ -104,6 +104,7 @@ namespace NanoBrain { /// public virtual void SetBias(Vector3 inputValue) { this.bias = inputValue; + this.lastUpdate = Time.time; this.parent?.UpdateFromNucleus(this); } @@ -277,7 +278,7 @@ namespace NanoBrain { #endif public bool isFiring { get { - SleepCheck(); + //SleepCheck(); return this.outputMagnitude > 0.5f; } } @@ -384,13 +385,15 @@ namespace NanoBrain { var result = Combinator(); this.outputValue = Activator(result); this.lastUpdate = Time.time; + // Debug.Log($"Update Neuron {this.name}"); } protected void CheckSleepingSynapses() { foreach (Synapse synapse in this.synapses) { - if (synapse.isSleeping) { - synapse.neuron.outputValue = Vector3.zero; - } + synapse.neuron.SleepCheck(); + // if (synapse.isSleeping) { + // synapse.neuron.outputValue = Vector3.zero; + // } } }