From 76c5372089c24416873d6586820dfb945aa0f9c4 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Mon, 9 Feb 2026 17:49:19 +0100 Subject: [PATCH] WIp pheromone selection in brain --- Cluster.cs | 5 ++ Editor/ClusterInspector.cs | 112 +++++++++++++++++++++++++++---------- Neuron.cs | 3 +- Nucleus.cs | 17 +++--- 4 files changed, 99 insertions(+), 38 deletions(-) diff --git a/Cluster.cs b/Cluster.cs index de9b6d0..d65656a 100644 --- a/Cluster.cs +++ b/Cluster.cs @@ -19,6 +19,9 @@ public class Cluster : Nucleus { ClonePrefab(); this.sortedNuclei = TopologicalSort(this.nuclei); + // Does not work because we have nuclei with the same names in an nucleusArray + // 'Pheromone steering' + //this.nucleiDict = nuclei.ToDictionary(nucleus => nucleus.name); } public Cluster(ClusterPrefab prefab, ClusterPrefab parent = null) { @@ -31,6 +34,7 @@ public class Cluster : Nucleus { ClonePrefab(); this.sortedNuclei = TopologicalSort(this.nuclei); + //this.nucleiDict = nuclei.ToDictionary(nucleus => nucleus.name); } private void ClonePrefab() { @@ -183,6 +187,7 @@ public class Cluster : Nucleus { // the nuclei sorted using topological sorting // to ensure that the cluster is computer in the right order public List sortedNuclei; + //public Dictionary nucleiDict = new(); public List _inputs = null; public virtual List inputs { diff --git a/Editor/ClusterInspector.cs b/Editor/ClusterInspector.cs index 4018cc9..4d4d941 100644 --- a/Editor/ClusterInspector.cs +++ b/Editor/ClusterInspector.cs @@ -497,6 +497,7 @@ public class ClusterInspector : Editor { } private bool showSynapses = true; + private bool showActivation = true; protected bool breakOnWake = false; void DrawInspector(VisualElement inspectorContainer) { if (inspectorContainer == null) @@ -525,34 +526,43 @@ public class ClusterInspector : Editor { }; GUILayout.Label(this.currentNucleus.GetType().ToString(), headerStyle); - this.currentNucleus.name = EditorGUILayout.TextField(this.currentNucleus.name, boldTextFieldStyle); - if (this.currentNucleus is Neuron neuroid) { - if (this.currentNucleus is MemoryCell memory) { - } - else { - EditorGUILayout.BeginHorizontal(); - EditorGUILayout.LabelField("Activation Curve", GUILayout.Width(150)); - if (neuroid.curveMax > 0) - EditorGUILayout.CurveField(neuroid.curve, Color.cyan, new Rect(0, 0, 1, neuroid.curveMax)); - else - EditorGUILayout.CurveField(neuroid.curve, Color.cyan, new Rect(0, neuroid.curveMax, 1, -neuroid.curveMax)); - neuroid.curvePreset = (Neuron.CurvePresets)EditorGUILayout.EnumPopup(neuroid.curvePreset, GUILayout.Width(100)); - EditorGUILayout.EndHorizontal(); - } - - if (neuroid.array == null || neuroid.array.nuclei == null || neuroid.array.nuclei.Count() == 0) - neuroid.array = new NucleusArray(neuroid); - EditorGUILayout.BeginHorizontal(); - EditorGUILayout.IntField("Array size", neuroid.array.nuclei.Count()); - if (GUILayout.Button("Add")) - neuroid.array.AddNucleus(this.prefab); - if (GUILayout.Button("Del")) - neuroid.array.RemoveNucleus(); - EditorGUILayout.EndHorizontal(); + string newName = EditorGUILayout.TextField(this.currentNucleus.name, boldTextFieldStyle); + if (newName != this.currentNucleus.name) { + this.currentNucleus.name = newName; + this.prefab.RefreshOutputs(); + outputsField.choices = this.prefab.outputs.Select(output => output.name).ToList(); + //outputsField.value = newName; } + //if (this.currentNucleus is Neuron neuron) { + // if (this.currentNucleus is MemoryCell memory) { + // } + // else { + // EditorGUILayout.BeginHorizontal(); + // EditorGUILayout.LabelField("Activation Curve", GUILayout.Width(150)); + // if (neuron.curveMax > 0) + // EditorGUILayout.CurveField(neuron.curve, Color.cyan, new Rect(0, 0, 1, neuron.curveMax)); + // else + // EditorGUILayout.CurveField(neuron.curve, Color.cyan, new Rect(0, neuron.curveMax, 1, -neuron.curveMax)); + // neuron.curvePreset = (Neuron.CurvePresets)EditorGUILayout.EnumPopup(neuron.curvePreset, GUILayout.Width(100)); + // EditorGUILayout.EndHorizontal(); + // } - if (Application.isPlaying) - EditorGUILayout.FloatField("Output", length(this.currentNucleus.outputValue)); + // if (neuron.array == null || neuron.array.nuclei == null || neuron.array.nuclei.Count() == 0) + // neuron.array = new NucleusArray(neuron); + // EditorGUILayout.BeginHorizontal(); + // EditorGUILayout.IntField("Array size", neuron.array.nuclei.Count()); + // if (GUILayout.Button("Add")) + // neuron.array.AddNucleus(this.prefab); + // if (GUILayout.Button("Del")) + // neuron.array.RemoveNucleus(); + // EditorGUILayout.EndHorizontal(); + //} + + + if (Application.isPlaying) { + GUIContent nameLabel = new("Output", this.currentNucleus.outputValue.ToString()); + EditorGUILayout.FloatField(nameLabel, length(this.currentNucleus.outputValue)); + } else EditorGUILayout.LabelField(" "); @@ -561,16 +571,29 @@ public class ClusterInspector : Editor { ConnectNucleus(this.prefab, this.currentNucleus); AddSynapse(this.prefab, this.currentNucleus); + this.currentNucleus.bias = EditorGUILayout.Vector3Field("Bias", this.currentNucleus.bias); + + NucleusArray array = null; if (this.currentNucleus.synapses.Count > 0) { Synapse[] synapses = this.currentNucleus.synapses.ToArray(); foreach (Synapse synapse in synapses) { if (synapse.nucleus != null) { + if (array != null) { + if (array.nuclei.Contains(synapse.nucleus)) + continue; + } + else { + if (synapse.nucleus.array != null && synapse.nucleus.array.nuclei.Length > 1) + array = synapse.nucleus.array; + } + EditorGUILayout.Space(); - //EditorGUI.BeginDisabledGroup(synapse.nucleus.isSleeping); - if (Application.isPlaying) - EditorGUILayout.FloatField(synapse.nucleus.name, length(synapse.nucleus.outputValue) * synapse.weight); - else { + if (Application.isPlaying) { + Vector3 value = synapse.nucleus.outputValue * synapse.weight; + GUIContent synapseValueLabel = new(synapse.nucleus.name, synapse.nucleus.outputValue.ToString()); + EditorGUILayout.FloatField(synapseValueLabel, length(synapse.nucleus.outputValue)); + } else { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(synapse.nucleus.name); if (GUILayout.Button("Disconnect")) @@ -588,6 +611,35 @@ public class ClusterInspector : Editor { EditorGUILayout.EndFoldoutHeaderGroup(); EditorGUILayout.Space(); + showActivation = EditorGUILayout.BeginFoldoutHeaderGroup(showActivation, "Activation"); + if (showActivation) { + + if (this.currentNucleus is Neuron neuron) { + if (this.currentNucleus is not MemoryCell memory) { + EditorGUILayout.BeginHorizontal(); + EditorGUILayout.LabelField("Activation Curve", GUILayout.Width(150)); + if (neuron.curveMax > 0) + EditorGUILayout.CurveField(neuron.curve, Color.cyan, new Rect(0, 0, 1, neuron.curveMax)); + else + EditorGUILayout.CurveField(neuron.curve, Color.cyan, new Rect(0, neuron.curveMax, 1, -neuron.curveMax)); + neuron.curvePreset = (Neuron.CurvePresets)EditorGUILayout.EnumPopup(neuron.curvePreset, GUILayout.Width(100)); + EditorGUILayout.EndHorizontal(); + } + if (neuron.array == null || neuron.array.nuclei == null || neuron.array.nuclei.Count() == 0) + neuron.array = new NucleusArray(neuron); + EditorGUILayout.BeginHorizontal(); + EditorGUILayout.IntField("Array size", neuron.array.nuclei.Count()); + if (GUILayout.Button("Add")) + neuron.array.AddNucleus(this.prefab); + if (GUILayout.Button("Del")) + neuron.array.RemoveNucleus(); + EditorGUILayout.EndHorizontal(); + } + + EditorGUILayout.Space(); + } + EditorGUILayout.EndFoldoutHeaderGroup(); + if (GUILayout.Button("Delete this neuron")) DeleteNeuron(this.currentNucleus); diff --git a/Neuron.cs b/Neuron.cs index 3992323..c6c8221 100644 --- a/Neuron.cs +++ b/Neuron.cs @@ -131,6 +131,7 @@ public class Neuron : Nucleus { public override Nucleus ShallowCloneTo(Cluster newParent) { Neuron clone = new(newParent, this.name) { array = null, + bias = this.bias, curve = this.curve, curvePreset = this.curvePreset, curveMax = this.curveMax, @@ -185,7 +186,7 @@ public class Neuron : Nucleus { } } - public float3 bias = float3(0, 0, 0); + //public float3 bias = float3(0, 0, 0); public override void UpdateStateIsolated(float3 bias_unused) { Vector3 sum = this.bias; int n = 0; diff --git a/Nucleus.cs b/Nucleus.cs index dcc30dd..fe5eae4 100644 --- a/Nucleus.cs +++ b/Nucleus.cs @@ -6,13 +6,14 @@ using static Unity.Mathematics.math; [Serializable] public abstract class Nucleus { - [SerializeField] - protected string _name; - public virtual string name { - get => _name; - set => _name = value; - } - + // [SerializeField] + // protected string _name; + // public virtual string name { + // get => _name; + // set => _name = value; + // } + public string name; + //[Obsolete] public ClusterPrefab cluster { get; set; } public Cluster parent { get; set; } @@ -39,6 +40,8 @@ public abstract class Nucleus { #region Synapses + public Vector3 bias = Vector3.zero; + [SerializeField] private List _synapses = new(); public List synapses => _synapses;