73 lines
2.0 KiB
C#
73 lines
2.0 KiB
C#
using UnityEngine;
|
|
|
|
public class Neuroid : Nucleus {
|
|
public int stale = 0;
|
|
|
|
public bool average = false;
|
|
public bool inverse = false;
|
|
public float exponent = 1.0f;
|
|
|
|
public Neuroid(NanoBrain brain, string name) : base(name) {
|
|
this.brain = brain;
|
|
if (this.brain != null)
|
|
this.brain.neuroids.Add(this);
|
|
else
|
|
Debug.LogError("No neuroid network");
|
|
}
|
|
|
|
// public void AddSynapse(Neuroid input) {
|
|
// input.AddReceiver(this);
|
|
// this.synapses[input] = 1.0f;
|
|
// }
|
|
|
|
public void SetWeight(Neuroid input, float weight) {
|
|
this.synapses[input] = weight;
|
|
}
|
|
|
|
public void GetInputFrom(Neuroid input, float weight = 1.0f) {
|
|
input.AddReceiver(this);
|
|
this.synapses[input] = weight;
|
|
}
|
|
|
|
public void SetInput(Neuroid input) {
|
|
if (this.synapses.ContainsKey(input) == false)
|
|
this.synapses[input] = 1.0f;
|
|
UpdateState();
|
|
}
|
|
|
|
public void SetInput(Neuroid input, float weight) {
|
|
this.synapses[input] = weight;
|
|
UpdateState();
|
|
}
|
|
|
|
public virtual void UpdateState() {
|
|
Vector3 result = Vector3.zero;
|
|
foreach ((Nucleus nucleus, float weight) in this.synapses) {
|
|
if (nucleus is Neuroid neuroid && neuroid.IsStale())
|
|
continue;
|
|
|
|
Vector3 direction = nucleus.outputValue.normalized;
|
|
float magnitude = nucleus.outputValue.magnitude;
|
|
|
|
magnitude = weight * Mathf.Pow(magnitude, exponent);
|
|
if (inverse && magnitude > 0)
|
|
magnitude = 1 / magnitude;
|
|
result += direction * magnitude;
|
|
|
|
}
|
|
if (average && this.synapses.Count > 0)
|
|
result /= this.synapses.Count;
|
|
|
|
this.outputValue = result;
|
|
this.stale = 0;
|
|
|
|
foreach (Neuroid receiver in this.receivers)
|
|
receiver.SetInput(this);
|
|
}
|
|
|
|
public bool IsStale() {
|
|
return this.stale > 2;
|
|
}
|
|
}
|
|
|