141 lines
4.7 KiB
C#
141 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Unity.Mathematics;
|
|
using static Unity.Mathematics.math;
|
|
|
|
[Serializable]
|
|
public class ClusterReceptor : Cluster, IReceptor {
|
|
public ClusterReceptor(ClusterPrefab prefab, Cluster parent, string name) : base(prefab, parent) {
|
|
this.name = name;
|
|
this.array = new NucleusArray(this);
|
|
if (this.name.IndexOf(":") < 0)
|
|
this.name += ": 0";
|
|
|
|
}
|
|
public ClusterReceptor(ClusterPrefab prefab, ClusterPrefab parent, string name) : base(prefab, parent) {
|
|
this.name = name;
|
|
this.array = new NucleusArray(this);
|
|
}
|
|
|
|
public string GetName() {
|
|
return this.name;
|
|
}
|
|
|
|
public override Nucleus ShallowCloneTo(Cluster parent) {
|
|
ClusterReceptor clone = new(this.prefab, parent, this.name) {
|
|
clusterPrefab = this.clusterPrefab,
|
|
//array = this.array
|
|
};
|
|
//CloneFields(clone);
|
|
// This cloned the prefab with the clusternuclei,
|
|
// but did not clone the receivers outside the cluster
|
|
RestoreExternalReceivers(clone, this.clusterPrefab, parent);
|
|
|
|
return clone;
|
|
}
|
|
|
|
public override Nucleus Clone(ClusterPrefab parent) {
|
|
ClusterReceptor clone = new(prefab, parent, this.name) {
|
|
array = this._array
|
|
};
|
|
|
|
//CloneFields(clone);
|
|
foreach (Synapse synapse in this.synapses) {
|
|
Synapse clonedSynapse = clone.AddSynapse(synapse.nucleus);
|
|
clonedSynapse.weight = synapse.weight;
|
|
}
|
|
|
|
// foreach (Nucleus receiver in this.receivers) {
|
|
// clone.AddReceiver(receiver);
|
|
// }
|
|
|
|
this._outputs = null; // Make sure the output are regenerated
|
|
foreach (Neuron output in this.outputs) {
|
|
int ix = GetNucleusIndex(this.clusterNuclei, output);
|
|
if (ix < 0 || clone.clusterNuclei[ix] is not Neuron clonedOutput)
|
|
continue;
|
|
|
|
foreach (Nucleus receiver in output.receivers)
|
|
clonedOutput.AddReceiver(receiver);
|
|
}
|
|
return clone;
|
|
}
|
|
|
|
public override List<Nucleus> CollectReceivers() {
|
|
List<Nucleus> receivers = new();
|
|
foreach (Nucleus element in this.nucleiArray) {
|
|
if (element is not Cluster clusterElement)
|
|
continue;
|
|
|
|
foreach (Nucleus outputNucleus in clusterElement.clusterNuclei) {
|
|
if (outputNucleus is not Neuron output)
|
|
continue;
|
|
|
|
// this should be clusterElement.outputs,
|
|
// but outputs is not updated when correctly and may contain old data...
|
|
foreach (Nucleus receiver in output.receivers) {
|
|
// Only add receivers outside clusterElement cluster
|
|
if (receiver.clusterPrefab != clusterElement.prefab &&
|
|
receivers.Contains(receiver) == false)
|
|
receivers.Add(receiver);
|
|
}
|
|
}
|
|
}
|
|
return receivers;
|
|
}
|
|
|
|
[SerializeReference]
|
|
private NucleusArray _array;
|
|
public NucleusArray array {
|
|
//get { return _array; }
|
|
set { _array = value; }
|
|
}
|
|
|
|
//[SerializeReference]
|
|
//private Nucleus[] _nucleusArray;
|
|
public Nucleus[] nucleiArray {
|
|
get { return _array.nuclei; }
|
|
set { _array.nuclei = value; }
|
|
}
|
|
|
|
public void AddReceptorElement(ClusterPrefab prefab) {
|
|
IReceptorHelpers.AddReceptorElement(this, prefab);
|
|
}
|
|
|
|
public void RemoveReceptorElement() {
|
|
IReceptorHelpers.RemoveReceptorElement(this);
|
|
}
|
|
|
|
public void AddArrayReceiver(Nucleus receiverToAdd, float weight = 1) {
|
|
IReceptorHelpers.AddArrayReceiver(this, receiverToAdd, weight);
|
|
}
|
|
|
|
public override void UpdateStateIsolated() {
|
|
float3 sum = this.bias;
|
|
|
|
foreach (Nucleus nucleus in this.sortedNuclei)
|
|
nucleus.UpdateStateIsolated();
|
|
|
|
this.outputValue = this.defaultOutput.outputValue;
|
|
this.stale = 0;
|
|
|
|
UpdateNuclei();
|
|
}
|
|
|
|
public override void UpdateNuclei() {
|
|
this.stale++;
|
|
if (this.stale > staleValueForSleep && lengthsq(this.bias) > 0) {
|
|
this.bias = new float3(0, 0, 0);
|
|
this.parent.UpdateFromNucleus(this);
|
|
}
|
|
|
|
foreach (Nucleus nucleus in this.clusterNuclei)
|
|
nucleus.UpdateNuclei();
|
|
}
|
|
|
|
public override void ProcessStimulus(Vector3 inputValue, int thingId = 0, string thingName = null) {
|
|
this._array ??= new NucleusArray(this.parent);
|
|
this._array.ProcessStimulus(thingId, inputValue, thingName);
|
|
}
|
|
} |