From 0adb71f3060b132ae6e740457778285245b3bdb1 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 11 Dec 2025 17:37:01 +0100 Subject: [PATCH] (Dis)connect perceptoids --- Assets/NanoBrain/Nucleus.cs | 5 ++ Assets/NanoBrain/Perception.cs | 2 +- Assets/NanoBrain/Perceptoid.cs | 2 +- Assets/NanoBrain/Receptor.cs | 18 +---- .../VisualEditor/Editor/NanoBrainInspector.cs | 79 +++++++++++++------ 5 files changed, 66 insertions(+), 40 deletions(-) diff --git a/Assets/NanoBrain/Nucleus.cs b/Assets/NanoBrain/Nucleus.cs index 4593628..ae4b0c6 100644 --- a/Assets/NanoBrain/Nucleus.cs +++ b/Assets/NanoBrain/Nucleus.cs @@ -88,6 +88,11 @@ public class Nucleus { receiver.SetWeight(this, 1.0f); } + public void RemoveReceiver(Nucleus receiverNucleus) { + this.receivers.RemoveAll(receiver => receiver.nucleus == receiverNucleus); + receiverNucleus.synapses.RemoveAll(synapse => synapse.nucleus == this); + } + public static void Delete(Nucleus nucleus) { foreach (Synapse synapse in nucleus.synapses) { if (synapse.nucleus.receivers.Count > 1) { diff --git a/Assets/NanoBrain/Perception.cs b/Assets/NanoBrain/Perception.cs index ee77960..46ca2ab 100644 --- a/Assets/NanoBrain/Perception.cs +++ b/Assets/NanoBrain/Perception.cs @@ -71,7 +71,7 @@ public class Perception : Nucleus { if (sensoryNeuroids[i].isSleeping) leastInterestingIx = i; else if (sensoryNeuroids[i] != null) { - if (leastInterestingIx == -1 || sensoryNeuroids[leastInterestingIx].receptor.position.magnitude > sensoryNeuroids[i].receptor.position.magnitude) + if (leastInterestingIx == -1 || sensoryNeuroids[leastInterestingIx].receptor.localPosition.magnitude > sensoryNeuroids[i].receptor.localPosition.magnitude) leastInterestingIx = i; } } diff --git a/Assets/NanoBrain/Perceptoid.cs b/Assets/NanoBrain/Perceptoid.cs index 2802128..02b3ccd 100644 --- a/Assets/NanoBrain/Perceptoid.cs +++ b/Assets/NanoBrain/Perceptoid.cs @@ -163,7 +163,7 @@ public class Perceptoid : Neuroid { break; } } - else if (perceptoid.receptor.position.magnitude < selectedPerceptoid.receptor.position.magnitude) + else if (perceptoid.receptor.localPosition.magnitude < selectedPerceptoid.receptor.localPosition.magnitude) selectedPerceptoid = perceptoid; } if (selectedPerceptoid == null) { diff --git a/Assets/NanoBrain/Receptor.cs b/Assets/NanoBrain/Receptor.cs index 92b7d67..4c0f770 100644 --- a/Assets/NanoBrain/Receptor.cs +++ b/Assets/NanoBrain/Receptor.cs @@ -4,7 +4,7 @@ using UnityEngine; public class Receptor { - private Neuroid neuroid; + public List perceptei = new(); public int thingId; @@ -12,27 +12,13 @@ public class Receptor { public Vector3 localPosition; public Receptor(Perceptoid perceptoid) { - this.neuroid = perceptoid; this.perceptei.Add(perceptoid); } - /// - /// Local position of the thing - /// - public virtual Vector3 position { - get { - return this.localPosition; - } - // set { - // this.localPosition = value; - // neuroid.UpdateState(); - // } - } public virtual void ProcessStimulus(int thingId, Vector3 localPosition) { this.thingId = thingId; this.localPosition = localPosition; - ///neuroid.UpdateState(); Perceptoid selectedPerceptoid = null; foreach (Perceptoid perceptoid in this.perceptei) { @@ -46,7 +32,7 @@ public class Receptor { selectedPerceptoid = perceptoid; } else if (selectedPerceptoid.isSleeping == false) { - if (perceptoid.receptor.position.magnitude < selectedPerceptoid.receptor.position.magnitude) + if (perceptoid.receptor.localPosition.magnitude < selectedPerceptoid.receptor.localPosition.magnitude) selectedPerceptoid = perceptoid; } } diff --git a/Assets/NanoBrain/VisualEditor/Editor/NanoBrainInspector.cs b/Assets/NanoBrain/VisualEditor/Editor/NanoBrainInspector.cs index f86e0f3..7897927 100644 --- a/Assets/NanoBrain/VisualEditor/Editor/NanoBrainInspector.cs +++ b/Assets/NanoBrain/VisualEditor/Editor/NanoBrainInspector.cs @@ -1,5 +1,5 @@ using System.Collections.Generic; - +using System.Linq; using UnityEditor; using UnityEngine; @@ -437,6 +437,23 @@ public class NanoBrainInspector : Editor { AddPerceptoid(this.currentNucleus); if (GUILayout.Button("Delete this neuron")) DeleteNeuron(this.currentNucleus); + // if (GUILayout.Button("Connect to...")) + // ConnectNucleus(this.currentNucleus); + + ConnectNucleus(this.currentNucleus); + DisconnectNucleus(this.currentNucleus); + // GUIStyle toggleButton = new("Button"); + // if (connecting) { + // toggleButton.normal = toggleButton.active; + // } + // if (GUILayout.Button(connecting ? "Connecting..." : "Connect to...", toggleButton)) { + // connecting = !connecting; + // if (connecting) { + // names = this.currentNucleus.brain.perceptei.Select(i => i.name).ToArray(); + // } + // } + // if (connecting) + // ConnectNucleus(this.currentNucleus); }); @@ -446,7 +463,6 @@ public class NanoBrainInspector : Editor { protected virtual void AddInputNeuron(Nucleus nucleus) { Neuroid newNeuroid = new(this.brain, "New neuron"); newNeuroid.AddReceiver(nucleus); - //Rebuild(inspectorContainer); BuildLayers(); } @@ -468,29 +484,48 @@ public class NanoBrainInspector : Editor { BuildLayers(); } - // protected virtual void AddPositionPerception(Nucleus receiver) { - // this.brain.perception.SendPositions(receiver); - // } - // protected virtual void AddVelocityPerception(Nucleus receiver) { - // this.brain.perception.SendVelocities(receiver); - // } + protected virtual void ConnectNucleus(Nucleus nucleus) { + if (this.currentNucleus.brain == null) + return; + string[] names = this.currentNucleus.brain.perceptei.Select(i => i.name).ToArray(); + int selectedIndex = -1; + selectedIndex = EditorGUILayout.Popup("Connect to", selectedIndex, names); + if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.brain.perceptei.Count) { + Nucleus n = this.currentNucleus.brain.perceptei[selectedIndex]; + n.AddReceiver(this.currentNucleus); + } + } - private Vector3 NodePosition(Nucleus nucleus, int layerNodeCount = 1) { - if (this.neuroidPositions.ContainsKey(nucleus)) { - Vector2Int nucleusPos = this.neuroidPositions[nucleus]; - return NodePosition(nucleusPos, layerNodeCount); - } - else { - return Vector3.zero; + protected virtual void DisconnectNucleus(Nucleus nucleus) { + if (this.currentNucleus.brain == null) + return; + string[] names = this.currentNucleus.synapses.Select(synapse => synapse.nucleus.name).ToArray(); + int selectedIndex = -1; + selectedIndex = EditorGUILayout.Popup("Disconnect from", selectedIndex, names); + if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.brain.perceptei.Count) { + Synapse synapse =this.currentNucleus.synapses[selectedIndex]; + //n.AddReceiver(this.currentNucleus); + synapse.nucleus.RemoveReceiver(this.currentNucleus); + //BuildLayers(); } } - private Vector3 NodePosition(Vector2Int location, int layerNodeCount = 1) { - float spacing = 400f / layerNodeCount; - float margin = 10 + spacing / 2; - float size = 20; - Vector3 parentPos = new(100 + location.x * 100 - size, margin + location.y * spacing - size, 0.1f); - return parentPos; - } + + // private Vector3 NodePosition(Nucleus nucleus, int layerNodeCount = 1) { + // if (this.neuroidPositions.ContainsKey(nucleus)) { + // Vector2Int nucleusPos = this.neuroidPositions[nucleus]; + // return NodePosition(nucleusPos, layerNodeCount); + // } + // else { + // return Vector3.zero; + // } + // } + // private Vector3 NodePosition(Vector2Int location, int layerNodeCount = 1) { + // float spacing = 400f / layerNodeCount; + // float margin = 10 + spacing / 2; + // float size = 20; + // Vector3 parentPos = new(100 + location.x * 100 - size, margin + location.y * spacing - size, 0.1f); + // return parentPos; + // } // public void CreateEdge(string fromId, string toId) {