move to float3 (to prep for SIMD)
This commit is contained in:
parent
3a67652578
commit
5f6ec71c7b
@ -1,5 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using Unity.Mathematics;
|
||||||
|
using static Unity.Mathematics.math;
|
||||||
|
|
||||||
[CreateAssetMenu(menuName = "Passer/Cluster")]
|
[CreateAssetMenu(menuName = "Passer/Cluster")]
|
||||||
public class Cluster : ScriptableObject, INucleus {
|
public class Cluster : ScriptableObject, INucleus {
|
||||||
@ -93,8 +95,8 @@ public class Cluster : ScriptableObject, INucleus {
|
|||||||
|
|
||||||
#region Dynamics
|
#region Dynamics
|
||||||
|
|
||||||
public Vector3 outputValue => this.output.outputValue;
|
public float3 outputValue => this.output.outputValue;
|
||||||
public bool isSleeping => this.outputValue.sqrMagnitude == 0;
|
public bool isSleeping => lengthsq(this.outputValue) == 0;
|
||||||
|
|
||||||
public void UpdateState() {
|
public void UpdateState() {
|
||||||
// Don't know if this is right
|
// Don't know if this is right
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using Unity.Burst;
|
||||||
|
using Unity.Collections;
|
||||||
|
using Unity.Mathematics;
|
||||||
|
|
||||||
public interface INucleus : IReceptor {
|
public interface INucleus : IReceptor {
|
||||||
|
|
||||||
@ -41,9 +44,11 @@ public interface IReceptor {
|
|||||||
|
|
||||||
#region dynamic
|
#region dynamic
|
||||||
|
|
||||||
public Vector3 outputValue { get; }
|
// float3 to prepare for SIMD
|
||||||
|
public float3 outputValue { get; }
|
||||||
|
|
||||||
public bool isSleeping { get; }
|
public bool isSleeping { get; }
|
||||||
|
|
||||||
#endregion dynamic
|
#endregion dynamic
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using Unity.Mathematics;
|
||||||
|
using static Unity.Mathematics.math;
|
||||||
|
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
public class Neuroid : Nucleus {
|
public class Neuroid : Nucleus {
|
||||||
@ -39,13 +41,13 @@ public class Neuroid : Nucleus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override void UpdateState() {
|
public override void UpdateState() {
|
||||||
Vector3 sum = Vector3.zero;
|
float3 sum = new(0, 0, 0);
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
//Applying the weight factgors
|
//Applying the weight factgors
|
||||||
foreach (Synapse synapse in this.synapses) {
|
foreach (Synapse synapse in this.synapses) {
|
||||||
sum += synapse.weight * synapse.nucleus.outputValue;
|
sum = sum + (synapse.weight * synapse.nucleus.outputValue);
|
||||||
if (synapse.nucleus.outputValue.sqrMagnitude != 0)
|
if (lengthsq(synapse.nucleus.outputValue) != 0)
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
if (average)
|
if (average)
|
||||||
@ -58,17 +60,17 @@ public class Neuroid : Nucleus {
|
|||||||
result = sum;
|
result = sum;
|
||||||
break;
|
break;
|
||||||
case CurvePresets.Sqrt:
|
case CurvePresets.Sqrt:
|
||||||
result = sum.normalized * System.MathF.Sqrt(sum.magnitude);
|
result = normalize(sum) * System.MathF.Sqrt(length(sum));
|
||||||
break;
|
break;
|
||||||
case CurvePresets.Power:
|
case CurvePresets.Power:
|
||||||
result = sum.normalized * System.MathF.Pow(sum.magnitude, 2);
|
result = normalize(sum) * System.MathF.Pow(length(sum), 2);
|
||||||
break;
|
break;
|
||||||
case CurvePresets.Reciprocal:
|
case CurvePresets.Reciprocal:
|
||||||
result = sum.normalized * (1 / sum.magnitude);
|
result = normalize(sum) * (1 / length(sum));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
float activatedValue = this.curve.Evaluate(sum.magnitude);
|
float activatedValue = this.curve.Evaluate(length(sum));
|
||||||
result = sum.normalized * activatedValue;
|
result = normalize(sum) * activatedValue;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
UpdateResult(result);
|
UpdateResult(result);
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using Unity.Mathematics;
|
||||||
|
using static Unity.Mathematics.math;
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class Nucleus : INucleus {
|
public class Nucleus : INucleus {
|
||||||
@ -76,8 +78,8 @@ public class Nucleus : INucleus {
|
|||||||
|
|
||||||
public Cluster cluster { get; set; }
|
public Cluster cluster { get; set; }
|
||||||
|
|
||||||
private Vector3 _outputValue;
|
private float3 _outputValue;
|
||||||
public Vector3 outputValue {
|
public float3 outputValue {
|
||||||
get { return _outputValue; }
|
get { return _outputValue; }
|
||||||
set {
|
set {
|
||||||
this.stale = 0;
|
this.stale = 0;
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using Unity.Mathematics;
|
||||||
|
|
||||||
public class Receptor : IReceptor {
|
public class Receptor : IReceptor {
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
@ -29,7 +30,7 @@ public class Receptor : IReceptor {
|
|||||||
public float distanceResolution = 0.1f;
|
public float distanceResolution = 0.1f;
|
||||||
public float directionResolution = 5;
|
public float directionResolution = 5;
|
||||||
|
|
||||||
public Vector3 outputValue => this.localPosition;
|
public float3 outputValue => this.localPosition;
|
||||||
|
|
||||||
public Receptor(Cluster cluster, INucleus nucleus) {
|
public Receptor(Cluster cluster, INucleus nucleus) {
|
||||||
this.AddReceiver(nucleus);
|
this.AddReceiver(nucleus);
|
||||||
|
|||||||
@ -4,6 +4,8 @@ using UnityEditor;
|
|||||||
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
|
using Unity.Mathematics;
|
||||||
|
using static Unity.Mathematics.math;
|
||||||
|
|
||||||
[CustomEditor(typeof(Cluster))]
|
[CustomEditor(typeof(Cluster))]
|
||||||
public class ClusterInspector : Editor {
|
public class ClusterInspector : Editor {
|
||||||
@ -216,7 +218,7 @@ public class ClusterInspector : Editor {
|
|||||||
// Draw selected Nucleus
|
// Draw selected Nucleus
|
||||||
Handles.color = Color.white;
|
Handles.color = Color.white;
|
||||||
Handles.DrawSolidDisc(position, Vector3.forward, size + 2);
|
Handles.DrawSolidDisc(position, Vector3.forward, size + 2);
|
||||||
DrawNucleus(this.currentNucleus, position, this.currentNucleus.outputValue.magnitude, 20);
|
DrawNucleus(this.currentNucleus, position, length(this.currentNucleus.outputValue), 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawReceivers(INucleus nucleus, Vector3 parentPos, float size) {
|
private void DrawReceivers(INucleus nucleus, Vector3 parentPos, float size) {
|
||||||
@ -227,7 +229,7 @@ public class ClusterInspector : Editor {
|
|||||||
float maxValue = 0;
|
float maxValue = 0;
|
||||||
foreach (INucleus receiver in nucleus.receivers) {
|
foreach (INucleus receiver in nucleus.receivers) {
|
||||||
if (receiver is Neuroid neuroid) {
|
if (receiver is Neuroid neuroid) {
|
||||||
float value = neuroid.outputValue.magnitude;
|
float value = length(neuroid.outputValue);
|
||||||
if (value > maxValue)
|
if (value > maxValue)
|
||||||
maxValue = value;
|
maxValue = value;
|
||||||
}
|
}
|
||||||
@ -267,7 +269,7 @@ public class ClusterInspector : Editor {
|
|||||||
drawnArrays.Add(neuroid.array);
|
drawnArrays.Add(neuroid.array);
|
||||||
neuronCount++;
|
neuronCount++;
|
||||||
|
|
||||||
float value = neuroid.outputValue.magnitude;
|
float value = length(neuroid.outputValue);
|
||||||
if (value > maxValue)
|
if (value > maxValue)
|
||||||
maxValue = value;
|
maxValue = value;
|
||||||
}
|
}
|
||||||
@ -299,7 +301,7 @@ public class ClusterInspector : Editor {
|
|||||||
Handles.color = Color.darkRed;
|
Handles.color = Color.darkRed;
|
||||||
else {
|
else {
|
||||||
if (Application.isPlaying) {
|
if (Application.isPlaying) {
|
||||||
float brightness = nucleus.outputValue.magnitude / maxValue;
|
float brightness = length(nucleus.outputValue) / maxValue;
|
||||||
Handles.color = new Color(brightness, brightness, brightness, 1f);
|
Handles.color = new Color(brightness, brightness, brightness, 1f);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -416,7 +418,7 @@ public class ClusterInspector : Editor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Application.isPlaying)
|
if (Application.isPlaying)
|
||||||
EditorGUILayout.FloatField("Output", this.currentNucleus.outputValue.magnitude);
|
EditorGUILayout.FloatField("Output", length(this.currentNucleus.outputValue));
|
||||||
else
|
else
|
||||||
EditorGUILayout.LabelField(" ");
|
EditorGUILayout.LabelField(" ");
|
||||||
|
|
||||||
@ -428,7 +430,7 @@ public class ClusterInspector : Editor {
|
|||||||
|
|
||||||
EditorGUI.BeginDisabledGroup(synapse.nucleus.isSleeping);
|
EditorGUI.BeginDisabledGroup(synapse.nucleus.isSleeping);
|
||||||
if (Application.isPlaying)
|
if (Application.isPlaying)
|
||||||
EditorGUILayout.FloatField(synapse.nucleus.name, synapse.nucleus.outputValue.magnitude * synapse.weight);
|
EditorGUILayout.FloatField(synapse.nucleus.name, length(synapse.nucleus.outputValue) * synapse.weight);
|
||||||
else {
|
else {
|
||||||
EditorGUILayout.BeginHorizontal();
|
EditorGUILayout.BeginHorizontal();
|
||||||
EditorGUILayout.LabelField(synapse.nucleus.name);
|
EditorGUILayout.LabelField(synapse.nucleus.name);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user