(Dis)connect perceptoids

This commit is contained in:
Pascal Serrarens 2025-12-11 17:37:01 +01:00
parent 4e8d10f1bd
commit 0adb71f306
5 changed files with 66 additions and 40 deletions

View File

@ -88,6 +88,11 @@ public class Nucleus {
receiver.SetWeight(this, 1.0f); 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) { public static void Delete(Nucleus nucleus) {
foreach (Synapse synapse in nucleus.synapses) { foreach (Synapse synapse in nucleus.synapses) {
if (synapse.nucleus.receivers.Count > 1) { if (synapse.nucleus.receivers.Count > 1) {

View File

@ -71,7 +71,7 @@ public class Perception : Nucleus {
if (sensoryNeuroids[i].isSleeping) if (sensoryNeuroids[i].isSleeping)
leastInterestingIx = i; leastInterestingIx = i;
else if (sensoryNeuroids[i] != null) { 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; leastInterestingIx = i;
} }
} }

View File

@ -163,7 +163,7 @@ public class Perceptoid : Neuroid {
break; break;
} }
} }
else if (perceptoid.receptor.position.magnitude < selectedPerceptoid.receptor.position.magnitude) else if (perceptoid.receptor.localPosition.magnitude < selectedPerceptoid.receptor.localPosition.magnitude)
selectedPerceptoid = perceptoid; selectedPerceptoid = perceptoid;
} }
if (selectedPerceptoid == null) { if (selectedPerceptoid == null) {

View File

@ -4,7 +4,7 @@ using UnityEngine;
public class Receptor { public class Receptor {
private Neuroid neuroid;
public List<Perceptoid> perceptei = new(); public List<Perceptoid> perceptei = new();
public int thingId; public int thingId;
@ -12,27 +12,13 @@ public class Receptor {
public Vector3 localPosition; public Vector3 localPosition;
public Receptor(Perceptoid perceptoid) { public Receptor(Perceptoid perceptoid) {
this.neuroid = perceptoid;
this.perceptei.Add(perceptoid); this.perceptei.Add(perceptoid);
} }
/// <summary>
/// Local position of the thing
/// </summary>
public virtual Vector3 position {
get {
return this.localPosition;
}
// set {
// this.localPosition = value;
// neuroid.UpdateState();
// }
}
public virtual void ProcessStimulus(int thingId, Vector3 localPosition) { public virtual void ProcessStimulus(int thingId, Vector3 localPosition) {
this.thingId = thingId; this.thingId = thingId;
this.localPosition = localPosition; this.localPosition = localPosition;
///neuroid.UpdateState();
Perceptoid selectedPerceptoid = null; Perceptoid selectedPerceptoid = null;
foreach (Perceptoid perceptoid in this.perceptei) { foreach (Perceptoid perceptoid in this.perceptei) {
@ -46,7 +32,7 @@ public class Receptor {
selectedPerceptoid = perceptoid; selectedPerceptoid = perceptoid;
} }
else if (selectedPerceptoid.isSleeping == false) { 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; selectedPerceptoid = perceptoid;
} }
} }

View File

@ -1,5 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
@ -437,6 +437,23 @@ public class NanoBrainInspector : Editor {
AddPerceptoid(this.currentNucleus); AddPerceptoid(this.currentNucleus);
if (GUILayout.Button("Delete this neuron")) if (GUILayout.Button("Delete this neuron"))
DeleteNeuron(this.currentNucleus); 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) { protected virtual void AddInputNeuron(Nucleus nucleus) {
Neuroid newNeuroid = new(this.brain, "New neuron"); Neuroid newNeuroid = new(this.brain, "New neuron");
newNeuroid.AddReceiver(nucleus); newNeuroid.AddReceiver(nucleus);
//Rebuild(inspectorContainer);
BuildLayers(); BuildLayers();
} }
@ -468,29 +484,48 @@ public class NanoBrainInspector : Editor {
BuildLayers(); BuildLayers();
} }
// protected virtual void AddPositionPerception(Nucleus receiver) { protected virtual void ConnectNucleus(Nucleus nucleus) {
// this.brain.perception.SendPositions(receiver); if (this.currentNucleus.brain == null)
// } return;
// protected virtual void AddVelocityPerception(Nucleus receiver) { string[] names = this.currentNucleus.brain.perceptei.Select(i => i.name).ToArray();
// this.brain.perception.SendVelocities(receiver); 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) { protected virtual void DisconnectNucleus(Nucleus nucleus) {
if (this.neuroidPositions.ContainsKey(nucleus)) { if (this.currentNucleus.brain == null)
Vector2Int nucleusPos = this.neuroidPositions[nucleus]; return;
return NodePosition(nucleusPos, layerNodeCount); string[] names = this.currentNucleus.synapses.Select(synapse => synapse.nucleus.name).ToArray();
} int selectedIndex = -1;
else { selectedIndex = EditorGUILayout.Popup("Disconnect from", selectedIndex, names);
return Vector3.zero; 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; // private Vector3 NodePosition(Nucleus nucleus, int layerNodeCount = 1) {
float margin = 10 + spacing / 2; // if (this.neuroidPositions.ContainsKey(nucleus)) {
float size = 20; // Vector2Int nucleusPos = this.neuroidPositions[nucleus];
Vector3 parentPos = new(100 + location.x * 100 - size, margin + location.y * spacing - size, 0.1f); // return NodePosition(nucleusPos, layerNodeCount);
return parentPos; // }
} // 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) { // public void CreateEdge(string fromId, string toId) {