(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);
}
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) {

View File

@ -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;
}
}

View File

@ -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) {

View File

@ -4,7 +4,7 @@ using UnityEngine;
public class Receptor {
private Neuroid neuroid;
public List<Perceptoid> 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);
}
/// <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) {
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;
}
}

View File

@ -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) {