276 lines
9.0 KiB
C#
276 lines
9.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEditor;
|
|
using Unity.Mathematics;
|
|
using static Unity.Mathematics.math;
|
|
|
|
[Serializable]
|
|
public class Neuron : Nucleus {
|
|
|
|
public Neuron(Cluster parent, string name) {
|
|
this.parent = parent;
|
|
this.name = name;
|
|
this.parent?.nuclei.Add(this);
|
|
}
|
|
public Neuron(ClusterPrefab prefab, string name) {
|
|
this.cluster = prefab;
|
|
this.name = name;
|
|
if (this.cluster != null)
|
|
this.cluster.nuclei.Add(this);
|
|
else
|
|
Debug.LogError("No prefab when adding neuron to prefab");
|
|
}
|
|
|
|
#region Serialization
|
|
|
|
//public Type type = Type.Neuron;
|
|
public enum CombinatorType {
|
|
Sum,
|
|
Product,
|
|
Max
|
|
}
|
|
public CombinatorType combinator = CombinatorType.Sum;
|
|
|
|
public enum CurvePresets {
|
|
Linear,
|
|
Power,
|
|
Sqrt,
|
|
Reciprocal,
|
|
Custom
|
|
}
|
|
[SerializeField]
|
|
public CurvePresets _curvePreset;
|
|
public CurvePresets curvePreset {
|
|
get { return _curvePreset; }
|
|
set {
|
|
// if (this.array != null && this.array.nuclei != null) {
|
|
// foreach (Neuron nucleus in this.array.nuclei.Cast<Neuron>()) {
|
|
// nucleus._curvePreset = value;
|
|
// nucleus.curve = GenerateCurve();
|
|
// }
|
|
// }
|
|
_curvePreset = value;
|
|
this.curve = GenerateCurve();
|
|
}
|
|
}
|
|
public AnimationCurve curve;
|
|
public float curveMax = 1.0f;
|
|
|
|
public AnimationCurve GenerateCurve() {
|
|
switch (this.curvePreset) {
|
|
case CurvePresets.Linear:
|
|
this.curveMax = 1;
|
|
return Presets.Linear(1);
|
|
case CurvePresets.Power:
|
|
this.curveMax = 1;
|
|
return Presets.Power(2.0f, 1);
|
|
case CurvePresets.Sqrt:
|
|
this.curveMax = 1;
|
|
return Presets.Power(0.5f, 1);
|
|
case CurvePresets.Reciprocal:
|
|
this.curveMax = 1 / 0.01f * 1;
|
|
return Presets.Reciprocal(1);
|
|
default:
|
|
this.curveMax = 1;
|
|
return this.curve;
|
|
}
|
|
}
|
|
|
|
public static class Presets {
|
|
private const int samples = 32;
|
|
public static AnimationCurve Linear(float weight) {
|
|
return AnimationCurve.Linear(0f, 0f, 1000f, weight * 1000);
|
|
}
|
|
public static AnimationCurve Power(float exponent, float weight) {
|
|
// build keyframes
|
|
Keyframe[] keys = new Keyframe[samples];
|
|
for (int i = 0; i < samples; i++) {
|
|
float t = i / (float)(samples - 1);
|
|
float v = Mathf.Pow(t, exponent) * weight;
|
|
keys[i] = new Keyframe(t, v);
|
|
}
|
|
|
|
AnimationCurve curve = new(keys);
|
|
|
|
// set tangent modes for each key to Auto (smooth). Use Linear if you prefer straight segments.
|
|
for (int i = 0; i < curve.length; i++) {
|
|
AnimationUtility.SetKeyLeftTangentMode(curve, i, AnimationUtility.TangentMode.Auto);
|
|
AnimationUtility.SetKeyRightTangentMode(curve, i, AnimationUtility.TangentMode.Auto);
|
|
}
|
|
|
|
return curve;
|
|
}
|
|
public static AnimationCurve Reciprocal(float weight) {
|
|
int samples = 128;
|
|
float xMin = 0.001f;
|
|
float xMax = 1;
|
|
var keys = new Keyframe[samples];
|
|
for (int i = 0; i < samples; i++) {
|
|
float t = i / (float)(samples - 1);
|
|
float x = Mathf.Lerp(xMin, xMax, t);
|
|
float y = 1f / x * weight;
|
|
keys[i] = new Keyframe(x, y);
|
|
}
|
|
var curve = new AnimationCurve(keys);
|
|
for (int i = 0; i < curve.length; i++) {
|
|
AnimationUtility.SetKeyLeftTangentMode(curve, i, AnimationUtility.TangentMode.Linear);
|
|
AnimationUtility.SetKeyRightTangentMode(curve, i, AnimationUtility.TangentMode.Linear);
|
|
}
|
|
return curve;
|
|
}
|
|
}
|
|
|
|
#endregion Serialization
|
|
|
|
// this clone the nucleus without the synapses and receivers
|
|
public override Nucleus ShallowCloneTo(Cluster newParent) {
|
|
Neuron clone = new(newParent, this.name);
|
|
CloneFields(clone);
|
|
return clone;
|
|
}
|
|
|
|
public override Nucleus Clone(ClusterPrefab prefab) {
|
|
Neuron clone = new(prefab, this.name);
|
|
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);
|
|
}
|
|
return clone;
|
|
}
|
|
|
|
protected virtual void CloneFields(Neuron clone) {
|
|
// clone.array = this.array;
|
|
clone.bias = this.bias;
|
|
clone.combinator = this.combinator;
|
|
clone.curve = this.curve;
|
|
clone.curvePreset = this.curvePreset;
|
|
clone.curveMax = this.curveMax;
|
|
}
|
|
|
|
public static void Delete(Nucleus nucleus) {
|
|
foreach (Synapse synapse in nucleus.synapses) {
|
|
if (synapse.nucleus is Neuron synapse_nucleus) {
|
|
if (synapse_nucleus.receivers.Count > 1) {
|
|
// there is another nucleus feeding into this input nucleus
|
|
synapse_nucleus.receivers.RemoveAll(r => r == nucleus);
|
|
}
|
|
else {
|
|
// No other links, delete it.
|
|
Neuron.Delete(synapse_nucleus);
|
|
}
|
|
}
|
|
}
|
|
foreach (Nucleus receiver in nucleus.receivers) {
|
|
if (receiver != null && receiver.synapses != null)
|
|
receiver.synapses.RemoveAll(s => s.nucleus == nucleus);
|
|
}
|
|
|
|
if (nucleus.cluster != null) {
|
|
nucleus.cluster.nuclei.RemoveAll(n => n == nucleus);
|
|
nucleus.cluster.GarbageCollection();
|
|
}
|
|
}
|
|
|
|
public override void UpdateStateIsolated() {
|
|
float3 result = Combinator();
|
|
this.outputValue = Activator(result);
|
|
}
|
|
|
|
#region Combinator
|
|
|
|
protected Func<float3> Combinator => combinator switch {
|
|
CombinatorType.Sum => CombinatorSum,
|
|
CombinatorType.Product => CombinatorProduct,
|
|
CombinatorType.Max => CombinatorMax,
|
|
_ => CombinatorSum
|
|
};
|
|
|
|
public float3 CombinatorSum() {
|
|
float3 sum = this.bias;
|
|
foreach (Synapse synapse in this.synapses)
|
|
sum += synapse.weight * synapse.nucleus.outputValue;
|
|
return sum;
|
|
//this.outputValue = Activation(sum);
|
|
}
|
|
|
|
public float3 CombinatorProduct() {
|
|
float3 product = this.bias;
|
|
foreach (Synapse synapse in this.synapses)
|
|
product *= synapse.weight * synapse.nucleus.outputValue;
|
|
return product;
|
|
//this.outputValue = Activation(product);
|
|
}
|
|
|
|
public float3 CombinatorMax() {
|
|
float3 max = this.bias;
|
|
float maxSqrLength = lengthsq(max);
|
|
|
|
//Applying the weight factors
|
|
foreach (Synapse synapse in this.synapses) {
|
|
float3 input = synapse.weight * synapse.nucleus.outputValue;
|
|
|
|
float inputSqrlength = lengthsq(input);
|
|
if (inputSqrlength > maxSqrLength) {
|
|
max = input;
|
|
maxSqrLength = inputSqrlength;
|
|
}
|
|
}
|
|
return max;
|
|
}
|
|
|
|
#endregion Combinator
|
|
|
|
#region Activator
|
|
|
|
protected Func<float3, float3> Activator => this.curvePreset switch {
|
|
CurvePresets.Linear => ActivatorLinear,
|
|
CurvePresets.Sqrt => ActivatorSqrt,
|
|
CurvePresets.Power => ActivatorPower,
|
|
CurvePresets.Reciprocal => ActivatorReciprocal,
|
|
_ => ActivatorCustom
|
|
};
|
|
|
|
protected float3 ActivatorLinear(float3 input) {
|
|
return input;
|
|
}
|
|
|
|
protected float3 ActivatorSqrt(float3 input) {
|
|
float3 result = normalize(input) * System.MathF.Sqrt(length(input));
|
|
return result;
|
|
}
|
|
|
|
protected float3 ActivatorPower(float3 input) {
|
|
float3 result = normalize(input) * System.MathF.Pow(length(input), 2);
|
|
return result;
|
|
}
|
|
|
|
protected float3 ActivatorReciprocal(float3 input) {
|
|
float magnitude = length(input);
|
|
if (magnitude == 0)
|
|
return new float3(0, 0, 0);
|
|
|
|
float3 result = normalize(input) * (1 / magnitude);
|
|
return result;
|
|
}
|
|
|
|
protected float3 ActivatorCustom(float3 input) {
|
|
float activatedValue = this.curve.Evaluate(length(input));
|
|
float3 result = normalize(input) * activatedValue;
|
|
return result;
|
|
}
|
|
|
|
#endregion Activator
|
|
|
|
public virtual void ProcessStimulus(Vector3 inputValue, string thingName = null) {
|
|
this.stale = 0;
|
|
this.bias = inputValue;
|
|
this.parent.UpdateFromNucleus(this);
|
|
}
|
|
|
|
} |