79 lines
2.3 KiB
C#
79 lines
2.3 KiB
C#
using UnityEngine;
|
|
|
|
public class Neuroid : Nucleus {
|
|
public bool average = false;
|
|
public bool inverse = false;
|
|
public float exponent = 1.0f;
|
|
|
|
// public Neuroid(NanoBrain brain, string name) : base(null, name) {
|
|
// this.brain = brain;
|
|
// if (this.brain != null)
|
|
// this.brain.neuroids.Add(this);
|
|
// else
|
|
// Debug.LogError("No neuroid network");
|
|
// }
|
|
|
|
public Neuroid(NanoBrainObj brain, string name) : base(brain, name) {
|
|
}
|
|
|
|
public void SetWeight(Neuroid input, float weight) {
|
|
//this.synapses[input] = weight;
|
|
this.SetWeight((Nucleus)input, weight);
|
|
}
|
|
|
|
// public void GetInputFrom(Neuroid input, float weight = 1.0f) {
|
|
// input.AddReceiver(this);
|
|
// //this.synapses[input] = weight;
|
|
// this.SetWeight((Nucleus)input, weight);
|
|
// }
|
|
|
|
public void SetInput(Neuroid input) {
|
|
// if (this.synapses.ContainsKey(input) == false)
|
|
// this.synapses[input] = 1.0f;
|
|
if (this.SynapseExists(input))
|
|
this.SetWeight(input, 1.0f);
|
|
UpdateState();
|
|
}
|
|
|
|
public void SetInput(Neuroid input, float weight) {
|
|
//this.synapses[input] = weight;
|
|
this.SetWeight(input, weight);
|
|
UpdateState();
|
|
}
|
|
|
|
public virtual void UpdateState() {
|
|
Vector3 result = Vector3.zero;
|
|
//foreach ((Nucleus nucleus, float weight) in this.synapses) {
|
|
foreach (Synapse synapse in this.synapses) {
|
|
Nucleus nucleus = synapse.nucleus;
|
|
if (nucleus is Neuroid neuroid && neuroid.isSleeping)
|
|
continue;
|
|
|
|
Vector3 direction = nucleus.outputValue.normalized;
|
|
float magnitude = nucleus.outputValue.magnitude;
|
|
|
|
float weight = synapse.weight;
|
|
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 (Receiver receiver in this.receivers) {
|
|
if (receiver.nucleus is Neuroid neuroid)
|
|
neuroid.SetInput(this);
|
|
}
|
|
}
|
|
|
|
// public bool IsStale() {
|
|
// return this.stale > 2;
|
|
// }
|
|
}
|
|
|