83 lines
3.3 KiB
C#
83 lines
3.3 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Perception {
|
|
public SensoryNeuroid[] sensoryNeuroids = new SensoryNeuroid[7];
|
|
//public Neuroid[] velocitySensors = new Neuroid[7];
|
|
public NeuroidNetwork neuroidNet { get; protected set; }
|
|
|
|
public HashSet<Neuroid> receivers { get; protected set; }
|
|
|
|
public Perception(NeuroidNetwork neuroidNet) {
|
|
this.neuroidNet = neuroidNet;
|
|
this.receivers = new();
|
|
}
|
|
|
|
// public void SendOutputTo(Neuroid receiver) {
|
|
// foreach (SensoryNeuroid neuroid in sensoryNeuroids) {
|
|
// if (neuroid != null) {
|
|
// neuroid.AddReceiver(receiver);
|
|
// receiver.newSynapses[neuroid] = new (neuroid, Vector3.zero, 1.0f);
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
public void SendPositions(Neuroid receiver) {
|
|
receivers.Add(receiver);
|
|
foreach (SensoryNeuroid neuroid in sensoryNeuroids) {
|
|
if (neuroid != null) {
|
|
neuroid.AddReceiver(receiver);
|
|
receiver.newSynapses[neuroid] = new (neuroid, Vector3.zero, 1.0f);
|
|
}
|
|
}
|
|
}
|
|
public void SendVelocities(Neuroid receiver) {
|
|
receivers.Add(receiver);
|
|
foreach (SensoryNeuroid neuroid in sensoryNeuroids) {
|
|
if (neuroid != null && neuroid.velocityNeuroid != null) {
|
|
neuroid.velocityNeuroid.AddReceiver(receiver);
|
|
receiver.newSynapses[neuroid] = new (neuroid, Vector3.zero, 1.0f);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ProcessStimulus(int thingId, Vector3 localPosition) {
|
|
int availableIx = -1;
|
|
SensoryNeuroid leastInterestingNeuroid = null;
|
|
for (int i = 0; i < sensoryNeuroids.Length; i++) {
|
|
if (sensoryNeuroids[i] == null || sensoryNeuroids[i].IsStale())
|
|
availableIx = i;
|
|
else if (sensoryNeuroids[i].receptor.thingId == thingId) {
|
|
sensoryNeuroids[i].receptor.position = localPosition;
|
|
return;
|
|
}
|
|
if (sensoryNeuroids[i] != null) {
|
|
if (leastInterestingNeuroid == null || leastInterestingNeuroid.receptor.position.magnitude > sensoryNeuroids[i].receptor.position.magnitude)
|
|
leastInterestingNeuroid = sensoryNeuroids[i];
|
|
}
|
|
}
|
|
if (availableIx != -1) {
|
|
if (sensoryNeuroids[availableIx] != null) {
|
|
// Debug.Log($"revived receptor {availableIx} for {thingId}");
|
|
sensoryNeuroids[availableIx].receptor.thingId = thingId;
|
|
sensoryNeuroids[availableIx].receptor.position = localPosition;
|
|
}
|
|
else {
|
|
// Debug.Log($"new receptor for {thingId}");
|
|
SensoryNeuroid neuroid = new(neuroidNet, thingId);
|
|
foreach (Neuroid receiver in receivers)
|
|
receiver.GetInputFrom(neuroid);
|
|
|
|
sensoryNeuroids[availableIx] = neuroid;
|
|
neuroid.receptor.position = localPosition;
|
|
}
|
|
}
|
|
else if (leastInterestingNeuroid != null) {
|
|
//Debug.Log($"replaced receptor {leastInterestingNeuroid.thingId} for {thingId}");
|
|
leastInterestingNeuroid.receptor.thingId = thingId;
|
|
leastInterestingNeuroid.receptor.position = localPosition;
|
|
}
|
|
|
|
//Debug.LogWarning($"No available receptor for {id}");
|
|
}
|
|
} |