WIP: NaN error because zero distance

This commit is contained in:
Pascal Serrarens 2025-11-25 16:19:26 +01:00
parent 6ded599bb8
commit d91057cf97
6 changed files with 18 additions and 17 deletions

View File

@ -48,7 +48,7 @@
<Analyzer Include="/home/pascal/Unity/Hub/Editor/6000.2.13f1/Editor/Data/Tools/Unity.SourceGenerators/Unity.UIToolkit.SourceGenerator.dll" />
</ItemGroup>
<ItemGroup>
<Compile Include="Assets/NanoBrain/Editor/GraphEditorWindow.cs" />
<Compile Include="Assets/NanoBrain/Editor/NeuroidWindow.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="UnityEngine">

View File

@ -3,7 +3,7 @@ using UnityEngine;
using System.Linq;
using System.Collections.Generic;
public class Layer {
public class NeuroidLayer {
public int ix = 0;
public List<Neuroid> neuroids = new();
}
@ -13,7 +13,7 @@ public class GraphEditorWindow : EditorWindow {
private List<Neuroid> allNeuroids;
private Dictionary<Neuroid, Vector2Int> neuroidPositions = new();
private List<Layer> layers = new();
private List<NeuroidLayer> layers = new();
private void OnEnable() {
EditorApplication.update += EditorUpdate;
@ -33,7 +33,7 @@ public class GraphEditorWindow : EditorWindow {
// While there are unvisited neuroid
while (neuroids.Any(neuroid => !neuronVisited.Contains(neuroid))) {
// Create the next layer
Layer currentLayer = new() { ix = layerIx };
NeuroidLayer currentLayer = new() { ix = layerIx };
int neuroidIx = 0;
foreach (Neuroid neuroid in neuroids) {
@ -82,7 +82,7 @@ public class GraphEditorWindow : EditorWindow {
if (currentNeuroid == null)
return;
foreach (Layer layer in layers)
foreach (NeuroidLayer layer in layers)
DrawLayer(layer);
// int column = 100;
// int row = 200;
@ -119,7 +119,7 @@ public class GraphEditorWindow : EditorWindow {
// }
// }
private void DrawLayer(Layer layer) {
private void DrawLayer(NeuroidLayer layer) {
int column = layer.ix * 100;
int nodeCount = layer.neuroids.Count;
float maxValue = 0;
@ -161,7 +161,7 @@ public class GraphEditorWindow : EditorWindow {
private void HandleMouseHover(Neuroid neuroid, Rect rect) {
// Draw the tooltip
GUIContent tooltip = new($"{neuroid.name}\n Value: {neuroid.outputValue}");
GUIContent tooltip = new($"{neuroid.name}\n synapse count {neuroid.synapses.Count} \n Value: {neuroid.outputValue}");
Vector2 mousePosition = Event.current.mousePosition;
// Display tooltip with some offset
@ -197,8 +197,8 @@ public class GraphEditorWindow : EditorWindow {
}
[MenuItem("Window/Graph Visualizer")]
[MenuItem("Window/Neuroid Visualizer")]
public static void ShowWindow() {
GetWindow<GraphEditorWindow>("Graph Visualizer");
GetWindow<GraphEditorWindow>("Neuroid Visualizer");
}
}

View File

@ -98,6 +98,10 @@ public class Neuroid {
}
Vector3 Activation(Vector3 sum) {
if (synapses.Count == 0 && mode == Mode.Average)
Debug.LogWarning($"{this.id} {this.name} has zero synapses for average");
if (float.IsNaN(sum.magnitude))
Debug.LogWarning($"{this.id} {this.name} sum is nan");
return mode switch {
Mode.Sum => sum,
Mode.Average => sum / synapses.Count,

View File

@ -373,12 +373,12 @@ MonoBehaviour:
m_EditorClassIdentifier: Assembly-CSharp::SwarmControl
speed: 0.5
inertia: 0.1
alignmentForce: 0
alignmentForce: 1
cohesionForce: 10
separationForce: 5
separationDistance: 0.5
bodyForce: 20
boundaryForce: 5
boundaryForce: 2
spaceSize: {x: 10, y: 10, z: 10}
boundaryWidth: {x: 1, y: 1, z: 1}
--- !u!114 &301943979

View File

@ -104,12 +104,9 @@ public class Boid : MonoBehaviour
// outside distances per axis (0 if inside on that axis)
Vector3 outside = Vector3.Max(Vector3.zero, Vector3.Max(below, above));
Vector3 dir = Vector3.zero;
dir.x = (below.x > 0f) ? 1f : ((above.x > 0f) ? -1f : 0f);
dir.y = (below.y > 0f) ? 1f : ((above.y > 0f) ? -1f : 0f);
dir.z = (below.z > 0f) ? 1f : ((above.z > 0f) ? -1f : 0f);
outside = Vector3.Scale(outside, dir);
float magnitude = outside.magnitude;
Vector3 direction = (sc.transform.position - this.transform.position).normalized;
outside = direction * magnitude;
boundary.SetInput(id, outside, sc.boundaryForce);
Debug.Log($"boundary {this.transform.position} {outside} force = {outside * sc.boundaryForce}");