Added Cluster & INucleus
This commit is contained in:
parent
1b5a86ce55
commit
c64ccb246c
@ -51,8 +51,10 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Assets/NanoBrain/VisualEditor/Editor/NanoBrainEditor.cs" />
|
<Compile Include="Assets/NanoBrain/VisualEditor/Editor/NanoBrainEditor.cs" />
|
||||||
<Compile Include="Assets/NanoBrain/VisualEditor/Editor/NanoBrainInspector.cs" />
|
<Compile Include="Assets/NanoBrain/VisualEditor/Editor/NanoBrainInspector.cs" />
|
||||||
|
<Compile Include="Assets/NanoBrain/VisualEditor/Editor/BrainPickerWindow.cs" />
|
||||||
<Compile Include="Assets/Scenes/Boids/Scripts/Editor/SwarmControl_Editor.cs" />
|
<Compile Include="Assets/Scenes/Boids/Scripts/Editor/SwarmControl_Editor.cs" />
|
||||||
<Compile Include="Assets/NanoBrain/Editor/NeuroidWindow.cs" />
|
<Compile Include="Assets/NanoBrain/Editor/NeuroidWindow.cs" />
|
||||||
|
<Compile Include="Assets/NanoBrain/VisualEditor/Editor/ClusterInspector.cs" />
|
||||||
<Compile Include="Assets/NanoBrain/VisualEditor/Editor/NanoBrainComponent_Editor.cs" />
|
<Compile Include="Assets/NanoBrain/VisualEditor/Editor/NanoBrainComponent_Editor.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@ -71,12 +71,14 @@
|
|||||||
<Compile Include="Assets/NanoBrain/VisualEditor/NanoBrainComponent.cs" />
|
<Compile Include="Assets/NanoBrain/VisualEditor/NanoBrainComponent.cs" />
|
||||||
<Compile Include="Assets/NanoBrain/LinearAlgebra/src/Vector2Int.cs" />
|
<Compile Include="Assets/NanoBrain/LinearAlgebra/src/Vector2Int.cs" />
|
||||||
<Compile Include="Assets/Scenes/Boids/Scripts/SwarmControl.cs" />
|
<Compile Include="Assets/Scenes/Boids/Scripts/SwarmControl.cs" />
|
||||||
|
<Compile Include="Assets/NanoBrain/INucleus.cs" />
|
||||||
<Compile Include="Assets/NanoBrain/LinearAlgebra/src/Spherical.cs" />
|
<Compile Include="Assets/NanoBrain/LinearAlgebra/src/Spherical.cs" />
|
||||||
<Compile Include="Assets/NanoBrain/LinearAlgebra/test/Vector3FloatTest.cs" />
|
<Compile Include="Assets/NanoBrain/LinearAlgebra/test/Vector3FloatTest.cs" />
|
||||||
<Compile Include="Assets/NanoBrain/LinearAlgebra/test/QuaternionTest.cs" />
|
<Compile Include="Assets/NanoBrain/LinearAlgebra/test/QuaternionTest.cs" />
|
||||||
<Compile Include="Assets/Scenes/Boids/Scripts/Boid.cs" />
|
<Compile Include="Assets/Scenes/Boids/Scripts/Boid.cs" />
|
||||||
<Compile Include="Assets/NanoBrain/LinearAlgebra/src/SwingTwist.cs" />
|
<Compile Include="Assets/NanoBrain/LinearAlgebra/src/SwingTwist.cs" />
|
||||||
<Compile Include="Assets/NanoBrain/LinearAlgebra/test/SphericalTest.cs" />
|
<Compile Include="Assets/NanoBrain/LinearAlgebra/test/SphericalTest.cs" />
|
||||||
|
<Compile Include="Assets/NanoBrain/Cluster.cs" />
|
||||||
<Compile Include="Assets/NanoBrain/LinearAlgebra/test/Vector3IntTest.cs" />
|
<Compile Include="Assets/NanoBrain/LinearAlgebra/test/Vector3IntTest.cs" />
|
||||||
<Compile Include="Assets/NanoBrain/Neuroid.cs" />
|
<Compile Include="Assets/NanoBrain/Neuroid.cs" />
|
||||||
<Compile Include="Assets/NanoBrain/LinearAlgebra/src/Angle.cs" />
|
<Compile Include="Assets/NanoBrain/LinearAlgebra/src/Angle.cs" />
|
||||||
|
|||||||
85
Assets/NanoBrain/Cluster.cs
Normal file
85
Assets/NanoBrain/Cluster.cs
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
[CreateAssetMenu(menuName = "Passer/Cluster")]
|
||||||
|
public class Cluster : ScriptableObject, INucleus {
|
||||||
|
|
||||||
|
private string _name;
|
||||||
|
public string name {
|
||||||
|
get { return _name; }
|
||||||
|
set { _name = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public Cluster cluster => this;
|
||||||
|
|
||||||
|
public List<Nucleus> nuclei = new();
|
||||||
|
|
||||||
|
public Nucleus output => this.nuclei[0];
|
||||||
|
|
||||||
|
private readonly List<Synapse> _synapses = new(); // = inputs, compare receptors in NanoBrain
|
||||||
|
public List<Synapse> synapses => _synapses;
|
||||||
|
|
||||||
|
private void OnEnable() {
|
||||||
|
nuclei ??= new List<Nucleus>();
|
||||||
|
if (nuclei.Count == 0)
|
||||||
|
new Neuroid(this, "Output"); // Every cluster should have at least 1 neuroid
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddReceiver(INucleus receiver) {
|
||||||
|
output.AddReceiver(receiver);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveReceiver(INucleus receiver) {
|
||||||
|
output.RemoveReceiver(receiver);
|
||||||
|
}
|
||||||
|
public List<Receiver> receivers {
|
||||||
|
get => output.receivers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GarbageCollection() {
|
||||||
|
HashSet<INucleus> visitedNuclei = new();
|
||||||
|
MarkNuclei(visitedNuclei, this.output);
|
||||||
|
//Debug.Log($"Garbage collection found {visitedNuclei.Count} Nuclei");
|
||||||
|
this.nuclei.RemoveAll(nucleus => visitedNuclei.Contains(nucleus) == false);
|
||||||
|
//this.perceptei.RemoveAll(perceptoid => visitedNuclei.Contains(perceptoid) == false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void MarkNuclei(HashSet<INucleus> visitedNuclei, INucleus nucleus) {
|
||||||
|
if (nucleus is null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
visitedNuclei.Add(nucleus);
|
||||||
|
if (nucleus.synapses != null) {
|
||||||
|
HashSet<Synapse> visitedSynapses = new();
|
||||||
|
foreach (Synapse synapse in nucleus.synapses) {
|
||||||
|
if (synapse != null && synapse.nucleus != null) {
|
||||||
|
visitedSynapses.Add(synapse);
|
||||||
|
MarkNuclei(visitedNuclei, synapse.nucleus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nucleus.synapses.RemoveAll(synapse => visitedSynapses.Contains(synapse) == false);
|
||||||
|
}
|
||||||
|
if (nucleus.receivers != null) {
|
||||||
|
HashSet<Receiver> visitedReceivers = new();
|
||||||
|
foreach (Receiver receiver in nucleus.receivers) {
|
||||||
|
if (receiver != null && receiver.nucleus != null) {
|
||||||
|
visitedReceivers.Add(receiver);
|
||||||
|
visitedNuclei.Add(receiver.nucleus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nucleus.receivers.RemoveAll(receiver => visitedReceivers.Contains(receiver) == false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Dynamics
|
||||||
|
|
||||||
|
public Vector3 outputValue => this.output.outputValue;
|
||||||
|
public bool isSleeping => this.outputValue.sqrMagnitude == 0;
|
||||||
|
|
||||||
|
public void UpdateState() {
|
||||||
|
// Don't know if this is right
|
||||||
|
this.output.UpdateState();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Dynamics
|
||||||
|
}
|
||||||
2
Assets/NanoBrain/Cluster.cs.meta
Normal file
2
Assets/NanoBrain/Cluster.cs.meta
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 60a957541c24c57e78018c202ebb1d9b
|
||||||
@ -1,300 +1,302 @@
|
|||||||
using UnityEditor;
|
/*
|
||||||
using UnityEngine;
|
using UnityEditor;
|
||||||
using System.Linq;
|
using UnityEngine;
|
||||||
using System.Collections.Generic;
|
using System.Linq;
|
||||||
|
using System.Collections.Generic;
|
||||||
public class NeuroidLayer {
|
|
||||||
public int ix = 0;
|
public class NeuroidLayer {
|
||||||
public List<Nucleus> neuroids = new();
|
public int ix = 0;
|
||||||
}
|
public List<Nucleus> neuroids = new();
|
||||||
|
}
|
||||||
public class GraphEditorWindow : EditorWindow {
|
|
||||||
private Nucleus currentNucleus;
|
public class GraphEditorWindow : EditorWindow {
|
||||||
private List<Neuroid> allNeuroids;
|
private Nucleus currentNucleus;
|
||||||
private Dictionary<Nucleus, Vector2Int> neuroidPositions = new();
|
private List<Neuroid> allNeuroids;
|
||||||
|
private Dictionary<Nucleus, Vector2Int> neuroidPositions = new();
|
||||||
private List<NeuroidLayer> layers = new();
|
|
||||||
|
private List<NeuroidLayer> layers = new();
|
||||||
private void OnEnable() {
|
|
||||||
EditorApplication.update += EditorUpdate;
|
private void OnEnable() {
|
||||||
Selection.selectionChanged += OnSelectionChange;
|
EditorApplication.update += EditorUpdate;
|
||||||
SelectNeuron();
|
Selection.selectionChanged += OnSelectionChange;
|
||||||
}
|
SelectNeuron();
|
||||||
|
}
|
||||||
private void AddToLayer(NeuroidLayer layer, Nucleus nucleus) {
|
|
||||||
layer.neuroids.Add(nucleus);
|
private void AddToLayer(NeuroidLayer layer, Nucleus nucleus) {
|
||||||
nucleus.layerIx = layer.ix;
|
layer.neuroids.Add(nucleus);
|
||||||
// Store its position
|
nucleus.layerIx = layer.ix;
|
||||||
Vector2Int neuroidPosition = new(layer.ix, layer.neuroids.Count - 1);
|
// Store its position
|
||||||
neuroidPositions[nucleus] = neuroidPosition;
|
Vector2Int neuroidPosition = new(layer.ix, layer.neuroids.Count - 1);
|
||||||
|
neuroidPositions[nucleus] = neuroidPosition;
|
||||||
}
|
|
||||||
|
}
|
||||||
private void BuildLayers() {
|
|
||||||
// A temporary list to track what's been added to layers
|
private void BuildLayers() {
|
||||||
this.layers = new();
|
// A temporary list to track what's been added to layers
|
||||||
int layerIx = 0;
|
this.layers = new();
|
||||||
|
int layerIx = 0;
|
||||||
Nucleus selectedNucleus = this.currentNucleus;
|
|
||||||
if (selectedNucleus == null)
|
Nucleus selectedNucleus = this.currentNucleus;
|
||||||
return;
|
if (selectedNucleus == null)
|
||||||
NeuroidLayer currentLayer = new() { ix = layerIx };
|
return;
|
||||||
|
NeuroidLayer currentLayer = new() { ix = layerIx };
|
||||||
//foreach (Nucleus outputNeuroid in selectedNucleus.receivers) {
|
|
||||||
foreach (Receiver receiver in selectedNucleus.receivers) {
|
//foreach (Nucleus outputNeuroid in selectedNucleus.receivers) {
|
||||||
Nucleus outputNeuroid = receiver.nucleus;
|
foreach (Receiver receiver in selectedNucleus.receivers) {
|
||||||
if (outputNeuroid != null) {
|
Nucleus outputNeuroid = receiver.nucleus;
|
||||||
AddToLayer(currentLayer, outputNeuroid);
|
if (outputNeuroid != null) {
|
||||||
Debug.Log($"layer {layerIx} nucleus {outputNeuroid.name}");
|
AddToLayer(currentLayer, outputNeuroid);
|
||||||
}
|
Debug.Log($"layer {layerIx} nucleus {outputNeuroid.name}");
|
||||||
}
|
}
|
||||||
if (currentLayer.neuroids.Count > 0) {
|
}
|
||||||
this.layers.Add(currentLayer);
|
if (currentLayer.neuroids.Count > 0) {
|
||||||
layerIx++;
|
this.layers.Add(currentLayer);
|
||||||
currentLayer = new() { ix = layerIx };
|
layerIx++;
|
||||||
}
|
currentLayer = new() { ix = layerIx };
|
||||||
|
}
|
||||||
AddToLayer(currentLayer, selectedNucleus);
|
|
||||||
this.layers.Add(currentLayer);
|
AddToLayer(currentLayer, selectedNucleus);
|
||||||
Debug.Log($"layer {layerIx} nucleus {selectedNucleus.name}");
|
this.layers.Add(currentLayer);
|
||||||
|
Debug.Log($"layer {layerIx} nucleus {selectedNucleus.name}");
|
||||||
layerIx++;
|
|
||||||
currentLayer = new() { ix = layerIx };
|
layerIx++;
|
||||||
|
currentLayer = new() { ix = layerIx };
|
||||||
int six = 0;
|
|
||||||
// foreach (Synapse synapse in selectedNucleus.synapses.Values) {
|
int six = 0;
|
||||||
// Debug.Log($"Synapse {six}");
|
// foreach (Synapse synapse in selectedNucleus.synapses.Values) {
|
||||||
// Nucleus input = synapse.neuroid;
|
// Debug.Log($"Synapse {six}");
|
||||||
//foreach ((Nucleus input, Synapse synapse) in selectedNucleus.synapses) {
|
// Nucleus input = synapse.neuroid;
|
||||||
//foreach ((Nucleus input, float weight) in selectedNucleus.synapses) {
|
//foreach ((Nucleus input, Synapse synapse) in selectedNucleus.synapses) {
|
||||||
foreach (Synapse synapse in selectedNucleus.synapses) {
|
//foreach ((Nucleus input, float weight) in selectedNucleus.synapses) {
|
||||||
Nucleus input = synapse.nucleus;
|
foreach (Synapse synapse in selectedNucleus.synapses) {
|
||||||
if (input != null) {
|
Nucleus input = synapse.nucleus;
|
||||||
AddToLayer(currentLayer, input);
|
if (input != null) {
|
||||||
Debug.Log($"layer {layerIx} nucleus {input.name}");
|
AddToLayer(currentLayer, input);
|
||||||
}
|
Debug.Log($"layer {layerIx} nucleus {input.name}");
|
||||||
six++;
|
}
|
||||||
}
|
six++;
|
||||||
if (currentLayer.neuroids.Count > 0) {
|
}
|
||||||
this.layers.Add(currentLayer);
|
if (currentLayer.neuroids.Count > 0) {
|
||||||
}
|
this.layers.Add(currentLayer);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private void BuildLayers_old(List<Neuroid> neuroids) {
|
|
||||||
if (neuroids == null)
|
private void BuildLayers_old(List<Neuroid> neuroids) {
|
||||||
return;
|
if (neuroids == null)
|
||||||
|
return;
|
||||||
// A temporary list to track what's been added to layers
|
|
||||||
this.layers = new();
|
// A temporary list to track what's been added to layers
|
||||||
HashSet<Neuroid> neuronVisited = new();
|
this.layers = new();
|
||||||
int layerIx = 0;
|
HashSet<Neuroid> neuronVisited = new();
|
||||||
|
int layerIx = 0;
|
||||||
// While there are unvisited neuroid
|
|
||||||
while (neuroids.Any(neuroid => !neuronVisited.Contains(neuroid))) {
|
// While there are unvisited neuroid
|
||||||
// Create the next layer
|
while (neuroids.Any(neuroid => !neuronVisited.Contains(neuroid))) {
|
||||||
NeuroidLayer currentLayer = new() { ix = layerIx };
|
// Create the next layer
|
||||||
int neuroidIx = 0;
|
NeuroidLayer currentLayer = new() { ix = layerIx };
|
||||||
|
int neuroidIx = 0;
|
||||||
foreach (Neuroid neuroid in neuroids) {
|
|
||||||
// Skip neurons we already processed
|
foreach (Neuroid neuroid in neuroids) {
|
||||||
if (neuronVisited.Contains(neuroid))
|
// Skip neurons we already processed
|
||||||
continue;
|
if (neuronVisited.Contains(neuroid))
|
||||||
|
continue;
|
||||||
// if (neuroid.IsStale()) {
|
|
||||||
// Debug.Log($"neuron {neuroid.name} is stale {neuroid.stale}");
|
// if (neuroid.IsStale()) {
|
||||||
// neuronVisited.Add(neuroid);
|
// Debug.Log($"neuron {neuroid.name} is stale {neuroid.stale}");
|
||||||
// continue;
|
// neuronVisited.Add(neuroid);
|
||||||
// }
|
// continue;
|
||||||
|
// }
|
||||||
// If the output neuroid is visited
|
|
||||||
// Note: this does not yet work for multiple outputs yet (see the use of First())
|
// If the output neuroid is visited
|
||||||
// if (neuroid.receivers.Count == 0 // make sure the root neuroids are processed directly
|
// Note: this does not yet work for multiple outputs yet (see the use of First())
|
||||||
// || (neuronVisited.Contains(neuroid.receivers.First()) && neuroid.receivers.First().layerIx == layerIx - 1)) {
|
// if (neuroid.receivers.Count == 0 // make sure the root neuroids are processed directly
|
||||||
if (neuroid.receivers.Count == 0 // make sure the root neuroids are processed directly
|
// || (neuronVisited.Contains(neuroid.receivers.First()) && neuroid.receivers.First().layerIx == layerIx - 1)) {
|
||||||
|| (neuronVisited.Contains(neuroid.receivers.First().nucleus) && neuroid.receivers.First().nucleus.layerIx == layerIx - 1)) {
|
if (neuroid.receivers.Count == 0 // make sure the root neuroids are processed directly
|
||||||
// Add it to the next layer
|
|| (neuronVisited.Contains(neuroid.receivers.First().nucleus) && neuroid.receivers.First().nucleus.layerIx == layerIx - 1)) {
|
||||||
currentLayer.neuroids.Add(neuroid);
|
// Add it to the next layer
|
||||||
neuroid.layerIx = layerIx;
|
currentLayer.neuroids.Add(neuroid);
|
||||||
// Register it as visited
|
neuroid.layerIx = layerIx;
|
||||||
neuronVisited.Add(neuroid);
|
// Register it as visited
|
||||||
// Store its position
|
neuronVisited.Add(neuroid);
|
||||||
Vector2Int neuroidPosition = new(layerIx, neuroidIx);
|
// Store its position
|
||||||
neuroidPositions[neuroid] = neuroidPosition;
|
Vector2Int neuroidPosition = new(layerIx, neuroidIx);
|
||||||
neuroidIx++;
|
neuroidPositions[neuroid] = neuroidPosition;
|
||||||
Debug.Log($"Layer {layerIx} neuron {neuroidIx} name {neuroid.name}");
|
neuroidIx++;
|
||||||
}
|
Debug.Log($"Layer {layerIx} neuron {neuroidIx} name {neuroid.name}");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (currentLayer.neuroids.Count > 0) {
|
|
||||||
this.layers.Add(currentLayer);
|
if (currentLayer.neuroids.Count > 0) {
|
||||||
layerIx++;
|
this.layers.Add(currentLayer);
|
||||||
}
|
layerIx++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private void OnDisable() {
|
|
||||||
EditorApplication.update -= EditorUpdate;
|
private void OnDisable() {
|
||||||
Selection.selectionChanged -= OnSelectionChange;
|
EditorApplication.update -= EditorUpdate;
|
||||||
}
|
Selection.selectionChanged -= OnSelectionChange;
|
||||||
|
}
|
||||||
private void OnSelectionChange() {
|
|
||||||
SelectNeuron();
|
private void OnSelectionChange() {
|
||||||
Repaint();
|
SelectNeuron();
|
||||||
}
|
Repaint();
|
||||||
|
}
|
||||||
private void EditorUpdate() {
|
|
||||||
if (EditorApplication.isPlaying)
|
private void EditorUpdate() {
|
||||||
Repaint();
|
if (EditorApplication.isPlaying)
|
||||||
}
|
Repaint();
|
||||||
|
}
|
||||||
private void OnGUI() {
|
|
||||||
GUILayout.Label("Graph Visualizer", EditorStyles.boldLabel);
|
private void OnGUI() {
|
||||||
|
GUILayout.Label("Graph Visualizer", EditorStyles.boldLabel);
|
||||||
DrawGraph();
|
|
||||||
}
|
DrawGraph();
|
||||||
|
}
|
||||||
private void DrawGraph() {
|
|
||||||
if (currentNucleus == null)
|
private void DrawGraph() {
|
||||||
return;
|
if (currentNucleus == null)
|
||||||
|
return;
|
||||||
foreach (NeuroidLayer layer in layers)
|
|
||||||
DrawLayer(layer);
|
foreach (NeuroidLayer layer in layers)
|
||||||
}
|
DrawLayer(layer);
|
||||||
|
}
|
||||||
private void DrawLayer(NeuroidLayer layer) {
|
|
||||||
int column = layer.ix * 100;
|
private void DrawLayer(NeuroidLayer layer) {
|
||||||
int nodeCount = layer.neuroids.Count;
|
int column = layer.ix * 100;
|
||||||
float maxValue = 0;
|
int nodeCount = layer.neuroids.Count;
|
||||||
foreach (Nucleus nucleus in layer.neuroids) {
|
float maxValue = 0;
|
||||||
if (nucleus is Neuroid neuroid) {
|
foreach (Nucleus nucleus in layer.neuroids) {
|
||||||
float value = neuroid.outputValue.magnitude;
|
if (nucleus is Neuroid neuroid) {
|
||||||
if (value > maxValue)
|
float value = neuroid.outputValue.magnitude;
|
||||||
maxValue = value;
|
if (value > maxValue)
|
||||||
}
|
maxValue = value;
|
||||||
}
|
}
|
||||||
float spacing = 400f / nodeCount;
|
}
|
||||||
float margin = 100 + spacing / 2;
|
float spacing = 400f / nodeCount;
|
||||||
foreach (Nucleus layerNucleus in layer.neuroids) {
|
float margin = 100 + spacing / 2;
|
||||||
if (layerNucleus is Neuroid layerNeuroid) {
|
foreach (Nucleus layerNucleus in layer.neuroids) {
|
||||||
Vector2Int layerNeuroidPos = this.neuroidPositions[layerNeuroid];
|
if (layerNucleus is Neuroid layerNeuroid) {
|
||||||
Vector3 parentPos = new(100 + layerNeuroidPos.x * 100, margin + layerNeuroidPos.y * spacing, 0.1f);
|
Vector2Int layerNeuroidPos = this.neuroidPositions[layerNeuroid];
|
||||||
|
Vector3 parentPos = new(100 + layerNeuroidPos.x * 100, margin + layerNeuroidPos.y * spacing, 0.1f);
|
||||||
int i = 0;
|
|
||||||
float inputSpacing = 400f / layerNeuroid.synapses.Count;
|
int i = 0;
|
||||||
float inputMargin = 100 + inputSpacing / 2;
|
float inputSpacing = 400f / layerNeuroid.synapses.Count;
|
||||||
// foreach (Synapse synapse in layerNeuroid.synapses.Values) {
|
float inputMargin = 100 + inputSpacing / 2;
|
||||||
// if (synapse.neuroid != null) {
|
// foreach (Synapse synapse in layerNeuroid.synapses.Values) {
|
||||||
// if (this.neuroidPositions.ContainsKey(synapse.neuroid)) {
|
// if (synapse.neuroid != null) {
|
||||||
|
// if (this.neuroidPositions.ContainsKey(synapse.neuroid)) {
|
||||||
// Vector2Int inputNeuroidPos = this.neuroidPositions[synapse.neuroid];
|
|
||||||
//foreach ((Nucleus neuroid, Synapse synapse) in layerNeuroid.synapses) {
|
// Vector2Int inputNeuroidPos = this.neuroidPositions[synapse.neuroid];
|
||||||
//foreach ((Nucleus neuroid, float weight) in layerNeuroid.synapses) {
|
//foreach ((Nucleus neuroid, Synapse synapse) in layerNeuroid.synapses) {
|
||||||
foreach (Synapse synapse in layerNeuroid.synapses) {
|
//foreach ((Nucleus neuroid, float weight) in layerNeuroid.synapses) {
|
||||||
Nucleus neuroid = synapse.nucleus;
|
foreach (Synapse synapse in layerNeuroid.synapses) {
|
||||||
float weight = synapse.weight;
|
Nucleus neuroid = synapse.nucleus;
|
||||||
if (neuroid != null) {
|
float weight = synapse.weight;
|
||||||
if (this.neuroidPositions.ContainsKey(neuroid)) {
|
if (neuroid != null) {
|
||||||
Vector2Int inputNeuroidPos = this.neuroidPositions[neuroid];
|
if (this.neuroidPositions.ContainsKey(neuroid)) {
|
||||||
if (inputNeuroidPos.x == layerNeuroidPos.x + 1) {
|
Vector2Int inputNeuroidPos = this.neuroidPositions[neuroid];
|
||||||
Vector3 pos = new(100 + inputNeuroidPos.x * 100, inputMargin + inputNeuroidPos.y * inputSpacing, 0.0f);
|
if (inputNeuroidPos.x == layerNeuroidPos.x + 1) {
|
||||||
|
Vector3 pos = new(100 + inputNeuroidPos.x * 100, inputMargin + inputNeuroidPos.y * inputSpacing, 0.0f);
|
||||||
//float brightness = synapse.weight / 10.0f;
|
|
||||||
float brightness = weight / 10.0f;
|
//float brightness = synapse.weight / 10.0f;
|
||||||
Handles.color = new Color(brightness, brightness, brightness);
|
float brightness = weight / 10.0f;
|
||||||
Handles.DrawLine(parentPos, pos);
|
Handles.color = new Color(brightness, brightness, brightness);
|
||||||
}
|
Handles.DrawLine(parentPos, pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
float size = 20;
|
|
||||||
if (layerNeuroid.isSleeping)
|
float size = 20;
|
||||||
Handles.color = Color.black;
|
if (layerNeuroid.isSleeping)
|
||||||
else {
|
Handles.color = Color.black;
|
||||||
float brightness = layerNeuroid.outputValue.magnitude / maxValue;
|
else {
|
||||||
Handles.color = new Color(brightness, brightness, brightness);
|
float brightness = layerNeuroid.outputValue.magnitude / maxValue;
|
||||||
}
|
Handles.color = new Color(brightness, brightness, brightness);
|
||||||
Handles.DrawSolidDisc(parentPos, Vector3.forward, size);
|
}
|
||||||
Vector3 labelPos = parentPos - Vector3.down * (size + 0.2f); // below disc along up axis
|
Handles.DrawSolidDisc(parentPos, Vector3.forward, size);
|
||||||
GUIStyle style = new GUIStyle(EditorStyles.label) {
|
Vector3 labelPos = parentPos - Vector3.down * (size + 0.2f); // below disc along up axis
|
||||||
alignment = TextAnchor.UpperCenter,
|
GUIStyle style = new GUIStyle(EditorStyles.label) {
|
||||||
normal = { textColor = Color.white },
|
alignment = TextAnchor.UpperCenter,
|
||||||
fontStyle = FontStyle.Bold
|
normal = { textColor = Color.white },
|
||||||
};
|
fontStyle = FontStyle.Bold
|
||||||
Handles.Label(labelPos, layerNeuroid.name, style);
|
};
|
||||||
|
Handles.Label(labelPos, layerNeuroid.name, style);
|
||||||
Rect neuronRect = new(parentPos.x - size, parentPos.y - size, size * 2, size * 2);
|
|
||||||
Event e = Event.current;
|
Rect neuronRect = new(parentPos.x - size, parentPos.y - size, size * 2, size * 2);
|
||||||
if (e != null && neuronRect.Contains(e.mousePosition)) {
|
Event e = Event.current;
|
||||||
HandleMouseHover(layerNeuroid, neuronRect);
|
if (e != null && neuronRect.Contains(e.mousePosition)) {
|
||||||
// Process click
|
HandleMouseHover(layerNeuroid, neuronRect);
|
||||||
if (e.type == EventType.MouseDown && e.button == 0) {
|
// Process click
|
||||||
// Consume the event so the scene doesn't also handle it
|
if (e.type == EventType.MouseDown && e.button == 0) {
|
||||||
e.Use();
|
// Consume the event so the scene doesn't also handle it
|
||||||
HandleDiscClicked(layerNeuroid);
|
e.Use();
|
||||||
}
|
HandleDiscClicked(layerNeuroid);
|
||||||
}
|
}
|
||||||
i++;
|
}
|
||||||
}
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private void HandleMouseHover(Neuroid neuroid, Rect rect) {
|
|
||||||
GUIContent tooltip;
|
private void HandleMouseHover(Neuroid neuroid, Rect rect) {
|
||||||
// if (neuroid is SensoryNeuroid sensoryNeuroid) {
|
GUIContent tooltip;
|
||||||
// tooltip = new(
|
// if (neuroid is SensoryNeuroid sensoryNeuroid) {
|
||||||
// $"{sensoryNeuroid.name}" +
|
// tooltip = new(
|
||||||
// $"\nThing {sensoryNeuroid.receptor.thingType}" +
|
// $"{sensoryNeuroid.name}" +
|
||||||
// $"\nValue: {neuroid.outputValue}");
|
// $"\nThing {sensoryNeuroid.receptor.thingType}" +
|
||||||
// }
|
// $"\nValue: {neuroid.outputValue}");
|
||||||
// else {
|
// }
|
||||||
tooltip = new(
|
// else {
|
||||||
$"{neuroid.name}" +
|
tooltip = new(
|
||||||
$"\nsynapse count {neuroid.synapses.Count}" +
|
$"{neuroid.name}" +
|
||||||
$"\nValue: {neuroid.outputValue}");
|
$"\nsynapse count {neuroid.synapses.Count}" +
|
||||||
// }
|
$"\nValue: {neuroid.outputValue}");
|
||||||
|
// }
|
||||||
Vector2 mousePosition = Event.current.mousePosition;
|
|
||||||
|
Vector2 mousePosition = Event.current.mousePosition;
|
||||||
// Display tooltip with some offset
|
|
||||||
Vector2 tooltipSize = GUI.skin.box.CalcSize(tooltip);
|
// Display tooltip with some offset
|
||||||
Rect tooltipRect = new Rect(mousePosition.x + 10, mousePosition.y + 10, tooltipSize.x, tooltipSize.y);
|
Vector2 tooltipSize = GUI.skin.box.CalcSize(tooltip);
|
||||||
|
Rect tooltipRect = new Rect(mousePosition.x + 10, mousePosition.y + 10, tooltipSize.x, tooltipSize.y);
|
||||||
GUI.Box(tooltipRect, tooltip);
|
|
||||||
}
|
GUI.Box(tooltipRect, tooltip);
|
||||||
|
}
|
||||||
private void HandleDiscClicked(Nucleus nucleus) {
|
|
||||||
this.currentNucleus = nucleus;
|
private void HandleDiscClicked(Nucleus nucleus) {
|
||||||
BuildLayers();
|
this.currentNucleus = nucleus;
|
||||||
}
|
BuildLayers();
|
||||||
|
}
|
||||||
// Update node colors based on selected GameObjects
|
|
||||||
private void SelectNeuron() {
|
// Update node colors based on selected GameObjects
|
||||||
GameObject[] selectedObjects = Selection.gameObjects;
|
private void SelectNeuron() {
|
||||||
if (selectedObjects.Length == 0)
|
GameObject[] selectedObjects = Selection.gameObjects;
|
||||||
return;
|
if (selectedObjects.Length == 0)
|
||||||
|
return;
|
||||||
GameObject selectedObject = selectedObjects[0];
|
|
||||||
Boid boid = selectedObject.GetComponent<Boid>();
|
GameObject selectedObject = selectedObjects[0];
|
||||||
if (boid == null)
|
Boid boid = selectedObject.GetComponent<Boid>();
|
||||||
return;
|
if (boid == null)
|
||||||
|
return;
|
||||||
// Nucleus neuroid = boid.behaviour;
|
|
||||||
// this.currentNucleus = neuroid;
|
// Nucleus neuroid = boid.behaviour;
|
||||||
// if (neuroid == null)
|
// this.currentNucleus = neuroid;
|
||||||
// this.allNeuroids = new();
|
// if (neuroid == null)
|
||||||
// else
|
// this.allNeuroids = new();
|
||||||
// this.allNeuroids = neuroid.brain.neuroids;
|
// else
|
||||||
|
// this.allNeuroids = neuroid.brain.neuroids;
|
||||||
|
|
||||||
// Debug.Log($"Neuroncount = {this.allNeuroids.Count}");
|
|
||||||
// BuildLayers();
|
// Debug.Log($"Neuroncount = {this.allNeuroids.Count}");
|
||||||
// Debug.Log($"Layercount = {this.layers.Count}");
|
// BuildLayers();
|
||||||
|
// Debug.Log($"Layercount = {this.layers.Count}");
|
||||||
}
|
|
||||||
|
}
|
||||||
[MenuItem("Window/Neuroid Visualizer")]
|
|
||||||
public static void ShowWindow() {
|
[MenuItem("Window/Neuroid Visualizer")]
|
||||||
GetWindow<GraphEditorWindow>("Neuroid Visualizer");
|
public static void ShowWindow() {
|
||||||
}
|
GetWindow<GraphEditorWindow>("Neuroid Visualizer");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
32
Assets/NanoBrain/INucleus.cs
Normal file
32
Assets/NanoBrain/INucleus.cs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public interface INucleus {
|
||||||
|
|
||||||
|
#region static struct
|
||||||
|
|
||||||
|
public string name { get; set; }
|
||||||
|
|
||||||
|
// Cluster
|
||||||
|
public Cluster cluster { get; }
|
||||||
|
|
||||||
|
// Receivers
|
||||||
|
public List<Receiver> receivers { get; }
|
||||||
|
public void AddReceiver(INucleus receiver);
|
||||||
|
public void RemoveReceiver(INucleus receiverNucleus);
|
||||||
|
|
||||||
|
// Senders
|
||||||
|
public List<Synapse> synapses { get; }
|
||||||
|
|
||||||
|
#endregion static struct
|
||||||
|
|
||||||
|
#region dynamic state
|
||||||
|
|
||||||
|
public bool isSleeping { get; }
|
||||||
|
|
||||||
|
public Vector3 outputValue { get; }
|
||||||
|
|
||||||
|
public void UpdateState();
|
||||||
|
|
||||||
|
#endregion dynamic state
|
||||||
|
}
|
||||||
2
Assets/NanoBrain/INucleus.cs.meta
Normal file
2
Assets/NanoBrain/INucleus.cs.meta
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 6a8a0e8965cea660abff254cab8a4723
|
||||||
@ -1,112 +1,112 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
public class Neuroid : Nucleus {
|
public class Neuroid : Nucleus {
|
||||||
public enum CurvePresets {
|
public enum CurvePresets {
|
||||||
Linear,
|
Linear,
|
||||||
Power,
|
Power,
|
||||||
Sqrt,
|
Sqrt,
|
||||||
Reciprocal,
|
Reciprocal,
|
||||||
Custom
|
Custom
|
||||||
}
|
}
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
private CurvePresets _curvePreset;
|
private CurvePresets _curvePreset;
|
||||||
public CurvePresets curvePreset {
|
public CurvePresets curvePreset {
|
||||||
get { return _curvePreset; }
|
get { return _curvePreset; }
|
||||||
set {
|
set {
|
||||||
_curvePreset = value;
|
_curvePreset = value;
|
||||||
this.curve = GenerateCurve();
|
this.curve = GenerateCurve();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public AnimationCurve curve;
|
public AnimationCurve curve;
|
||||||
public float curveMax = 1.0f;
|
public float curveMax = 1.0f;
|
||||||
|
|
||||||
public AnimationCurve GenerateCurve() {
|
public AnimationCurve GenerateCurve() {
|
||||||
switch (this.curvePreset) {
|
switch (this.curvePreset) {
|
||||||
case CurvePresets.Linear:
|
case CurvePresets.Linear:
|
||||||
this.curveMax = 1;
|
this.curveMax = 1;
|
||||||
return Synapse.Presets.Linear(1);
|
return Synapse.Presets.Linear(1);
|
||||||
case CurvePresets.Power:
|
case CurvePresets.Power:
|
||||||
this.curveMax = 1;
|
this.curveMax = 1;
|
||||||
return Synapse.Presets.Power(2.0f, 1);
|
return Synapse.Presets.Power(2.0f, 1);
|
||||||
case CurvePresets.Sqrt:
|
case CurvePresets.Sqrt:
|
||||||
this.curveMax = 1;
|
this.curveMax = 1;
|
||||||
return Synapse.Presets.Power(0.5f, 1);
|
return Synapse.Presets.Power(0.5f, 1);
|
||||||
case CurvePresets.Reciprocal:
|
case CurvePresets.Reciprocal:
|
||||||
this.curveMax = 1 / 0.01f * 1;
|
this.curveMax = 1 / 0.01f * 1;
|
||||||
return Synapse.Presets.Reciprocal(1);
|
return Synapse.Presets.Reciprocal(1);
|
||||||
default:
|
default:
|
||||||
this.curveMax = 1;
|
this.curveMax = 1;
|
||||||
return this.curve;
|
return this.curve;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool average = false;
|
public bool average = false;
|
||||||
public bool inverse = false;
|
public bool inverse = false;
|
||||||
public float exponent = 1.0f;
|
public float exponent = 1.0f;
|
||||||
|
|
||||||
|
|
||||||
public Neuroid(NanoBrain brain, string name) : base(name) {
|
public Neuroid(Cluster brain, string name) : base(name) {
|
||||||
this.brain = brain;
|
this.cluster = brain;
|
||||||
if (this.brain != null) {
|
if (this.cluster != null) {
|
||||||
this.brain.nuclei.Add(this);
|
this.cluster.nuclei.Add(this);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
Debug.LogError("No neuroid network");
|
Debug.LogError("No neuroid network");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Neuroid(string name) : base(name) { }
|
public Neuroid(string name) : base(name) { }
|
||||||
|
|
||||||
public void SetWeight(Neuroid input, float weight) {
|
public void SetWeight(Neuroid input, float weight) {
|
||||||
this.SetWeight((Nucleus)input, weight);
|
this.SetWeight((Nucleus)input, weight);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetInput(Neuroid input) {
|
public void SetInput(Neuroid input) {
|
||||||
if (this.SynapseExists(input) == false)
|
if (this.SynapseExists(input) == false)
|
||||||
this.SetWeight(input, 1.0f);
|
this.SetWeight(input, 1.0f);
|
||||||
UpdateState();
|
UpdateState();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetInput(Neuroid input, float weight) {
|
public void SetInput(Neuroid input, float weight) {
|
||||||
this.SetWeight(input, weight);
|
this.SetWeight(input, weight);
|
||||||
UpdateState();
|
UpdateState();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void UpdateState() {
|
public override void UpdateState() {
|
||||||
Vector3 sum = Vector3.zero;
|
Vector3 sum = Vector3.zero;
|
||||||
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 += synapse.weight * synapse.nucleus.outputValue;
|
||||||
if (synapse.nucleus.outputValue.sqrMagnitude != 0)
|
if (synapse.nucleus.outputValue.sqrMagnitude != 0)
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
if (average)
|
if (average)
|
||||||
sum /= n;
|
sum /= n;
|
||||||
|
|
||||||
// Activation function
|
// Activation function
|
||||||
Vector3 result;
|
Vector3 result;
|
||||||
switch (this.curvePreset) {
|
switch (this.curvePreset) {
|
||||||
case CurvePresets.Linear:
|
case CurvePresets.Linear:
|
||||||
result = sum;
|
result = sum;
|
||||||
break;
|
break;
|
||||||
case CurvePresets.Sqrt:
|
case CurvePresets.Sqrt:
|
||||||
result = sum.normalized * System.MathF.Sqrt(sum.magnitude);
|
result = sum.normalized * System.MathF.Sqrt(sum.magnitude);
|
||||||
break;
|
break;
|
||||||
case CurvePresets.Power:
|
case CurvePresets.Power:
|
||||||
result = sum.normalized * System.MathF.Pow(sum.magnitude, 2);
|
result = sum.normalized * System.MathF.Pow(sum.magnitude, 2);
|
||||||
break;
|
break;
|
||||||
case CurvePresets.Reciprocal:
|
case CurvePresets.Reciprocal:
|
||||||
result = sum.normalized * (1 / sum.magnitude);
|
result = sum.normalized * (1 / sum.magnitude);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
float activatedValue = this.curve.Evaluate(sum.magnitude);
|
float activatedValue = this.curve.Evaluate(sum.magnitude);
|
||||||
result = sum.normalized * activatedValue;
|
result = sum.normalized * activatedValue;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
UpdateResult(result);
|
UpdateResult(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,304 +1,306 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using LinearAlgebra;
|
|
||||||
|
[Serializable]
|
||||||
[System.Serializable]
|
public class Nucleus : INucleus {
|
||||||
public class Nucleus {
|
|
||||||
|
public int id; // hash code
|
||||||
public int id; // hash code
|
|
||||||
|
[SerializeField]
|
||||||
[SerializeField]
|
protected string _name;
|
||||||
protected string _name;
|
public virtual string name {
|
||||||
public virtual string name {
|
get => _name;
|
||||||
get => _name;
|
set => _name = value;
|
||||||
set => _name = value;
|
}
|
||||||
}
|
|
||||||
|
[SerializeField]
|
||||||
[SerializeField]
|
private List<Synapse> _synapses = new();
|
||||||
public List<Synapse> synapses = new();
|
public List<Synapse> synapses => _synapses;
|
||||||
[SerializeField]
|
|
||||||
public List<Receiver> receivers = new();
|
[SerializeField]
|
||||||
|
private List<Receiver> _receivers = new();
|
||||||
#region Serialization
|
public List<Receiver> receivers => _receivers;
|
||||||
|
|
||||||
[SerializeField]
|
#region Serialization
|
||||||
protected string nucleusType;
|
|
||||||
|
[SerializeField]
|
||||||
public virtual void Rebuild(NanoBrain brain) {
|
protected string nucleusType;
|
||||||
if (this.synapses != null) {
|
|
||||||
foreach (Synapse synapse in synapses)
|
public virtual void Rebuild(NanoBrain brain) {
|
||||||
synapse.Rebuild(brain);
|
if (this.synapses != null) {
|
||||||
}
|
foreach (Synapse synapse in synapses)
|
||||||
if (this.receivers == null)
|
synapse.Rebuild(brain);
|
||||||
this.receivers = new();
|
}
|
||||||
else {
|
foreach (Receiver receiver in receivers.ToArray()) {
|
||||||
foreach (Receiver receiver in receivers.ToArray()) {
|
if (receiver.Rebuild(brain) == false) {
|
||||||
if (receiver.Rebuild(brain) == false) {
|
Debug.Log("Rebuilding failed, removing receiver.");
|
||||||
Debug.Log("Rebuilding failed, removing receiver.");
|
receivers.Remove(receiver);
|
||||||
receivers.Remove(receiver);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
public static Nucleus RebuildType(NanoBrain brain, Nucleus nucleus) {
|
||||||
|
if (string.IsNullOrEmpty(nucleus.nucleusType) == false) {
|
||||||
public static Nucleus RebuildType(NanoBrain brain, Nucleus nucleus) {
|
Type nucleusType = Type.GetType(nucleus.nucleusType);
|
||||||
if (string.IsNullOrEmpty(nucleus.nucleusType) == false) {
|
if (nucleusType != null) {
|
||||||
Type nucleusType = Type.GetType(nucleus.nucleusType);
|
object[] args = new object[] { brain, nucleus.name };
|
||||||
if (nucleusType != null) {
|
Nucleus rebuiltNucleus = (Nucleus)Activator.CreateInstance(nucleusType, args);
|
||||||
object[] args = new object[] { brain, nucleus.name };
|
rebuiltNucleus.Deserialize(nucleus);
|
||||||
Nucleus rebuiltNucleus = (Nucleus)Activator.CreateInstance(nucleusType, args);
|
return rebuiltNucleus;
|
||||||
rebuiltNucleus.Deserialize(nucleus);
|
}
|
||||||
return rebuiltNucleus;
|
}
|
||||||
}
|
return nucleus;
|
||||||
}
|
}
|
||||||
return nucleus;
|
|
||||||
}
|
public virtual void Deserialize(Nucleus nucleus) { }
|
||||||
|
|
||||||
public virtual void Deserialize(Nucleus nucleus) { }
|
#endregion Serialization
|
||||||
|
|
||||||
#endregion Serialization
|
#region Runtime state (not serialized)
|
||||||
|
|
||||||
#region Runtime state (not serialized)
|
public Cluster cluster { get; set; }
|
||||||
|
|
||||||
public NanoBrain brain { get; set; }
|
private Vector3 _outputValue;
|
||||||
|
public Vector3 outputValue
|
||||||
private Vector3 _outputValue;
|
{
|
||||||
public Vector3 outputValue
|
get { return _outputValue; }
|
||||||
{
|
set {
|
||||||
get { return _outputValue; }
|
this.stale = 0;
|
||||||
set {
|
this._isSleeping = false;
|
||||||
this.stale = 0;
|
_outputValue = value;
|
||||||
this.isSleeping = false;
|
}
|
||||||
_outputValue = value;
|
}
|
||||||
}
|
|
||||||
}
|
[System.NonSerialized]
|
||||||
|
private int stale = 1000;
|
||||||
[System.NonSerialized]
|
|
||||||
private int stale = 1000;
|
private bool _isSleeping = false;
|
||||||
|
public bool isSleeping => _isSleeping;
|
||||||
public bool isSleeping = false;
|
|
||||||
public void IncreaseAge() {
|
public void IncreaseAge() {
|
||||||
this.stale++;
|
this.stale++;
|
||||||
this.isSleeping = this.stale > 2;
|
this._isSleeping = this.stale > 2;
|
||||||
if (isSleeping)
|
if (isSleeping)
|
||||||
_outputValue = Vector3.zero;
|
_outputValue = Vector3.zero;
|
||||||
}
|
}
|
||||||
[System.NonSerialized]
|
[System.NonSerialized]
|
||||||
public int layerIx;
|
public int layerIx;
|
||||||
|
|
||||||
#endregion Runtime state
|
#endregion Runtime state
|
||||||
|
|
||||||
public Nucleus(string name) {
|
public Nucleus(string name) {
|
||||||
this._name = name;
|
this._name = name;
|
||||||
this.id = this.GetHashCode();
|
this.id = this.GetHashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void AddReceiver(Nucleus receiver) {
|
public virtual void AddReceiver(INucleus receiver) {
|
||||||
this.receivers.Add(new Receiver(receiver));
|
this.receivers.Add(new Receiver(receiver));
|
||||||
receiver.SetWeight(this, 1.0f);
|
//receiver.SetWeight(this, 1.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveReceiver(Nucleus receiverNucleus) {
|
public void RemoveReceiver(INucleus receiverNucleus) {
|
||||||
this.receivers.RemoveAll(receiver => receiver.nucleus == receiverNucleus);
|
this.receivers.RemoveAll(receiver => receiver.nucleus == receiverNucleus);
|
||||||
receiverNucleus.synapses.RemoveAll(synapse => synapse.nucleus == this);
|
receiverNucleus.synapses.RemoveAll(synapse => synapse.nucleus == this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Delete(Nucleus nucleus) {
|
public static void Delete(INucleus nucleus) {
|
||||||
foreach (Synapse synapse in nucleus.synapses) {
|
foreach (Synapse synapse in nucleus.synapses) {
|
||||||
if (synapse.nucleus.receivers.Count > 1) {
|
if (synapse.nucleus.receivers.Count > 1) {
|
||||||
// there is another nucleus feeding into this input nucleus
|
// there is another nucleus feeding into this input nucleus
|
||||||
synapse.nucleus.receivers.RemoveAll(r => r.nucleus == nucleus);
|
synapse.nucleus.receivers.RemoveAll(r => r.nucleus == nucleus);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// No other links, delete it.
|
// No other links, delete it.
|
||||||
Nucleus.Delete(synapse.nucleus);
|
Nucleus.Delete(synapse.nucleus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach (Receiver receiver in nucleus.receivers) {
|
foreach (Receiver receiver in nucleus.receivers) {
|
||||||
if (receiver.nucleus != null && receiver.nucleus.synapses != null)
|
if (receiver.nucleus != null && receiver.nucleus.synapses != null)
|
||||||
receiver.nucleus.synapses.RemoveAll(s => s.nucleus == nucleus);
|
receiver.nucleus.synapses.RemoveAll(s => s.nucleus == nucleus);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nucleus.brain != null) {
|
if (nucleus.cluster != null) {
|
||||||
nucleus.brain.nuclei.RemoveAll(n => n == nucleus);
|
nucleus.cluster.nuclei.RemoveAll(n => n == nucleus);
|
||||||
nucleus.brain.GarbageCollection();
|
nucleus.cluster.GarbageCollection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void GetInputFrom(Nucleus input, float weight = 1.0f) {
|
public void GetInputFrom(Nucleus input, float weight = 1.0f) {
|
||||||
input.AddReceiver(this);
|
input.AddReceiver(this);
|
||||||
this.SetWeight(input, weight);
|
this.SetWeight(input, weight);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool SynapseExists(Nucleus nucleus) {
|
public bool SynapseExists(Nucleus nucleus) {
|
||||||
foreach (Synapse synapse in synapses) {
|
foreach (Synapse synapse in synapses) {
|
||||||
if (synapse.nucleus == nucleus)
|
if (synapse.nucleus == nucleus)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetWeight(Nucleus nucleus, float weight) {
|
public void SetWeight(Nucleus nucleus, float weight) {
|
||||||
foreach (Synapse synapse in synapses) {
|
foreach (Synapse synapse in synapses) {
|
||||||
if (synapse.nucleus == nucleus) {
|
if (synapse.nucleus == nucleus) {
|
||||||
synapse.weight = weight;
|
synapse.weight = weight;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Synapse newSynapse = new(nucleus, weight);
|
Synapse newSynapse = new(nucleus, weight);
|
||||||
synapses.Add(newSynapse);
|
synapses.Add(newSynapse);
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void UpdateState() { }
|
public virtual void UpdateState() { }
|
||||||
|
|
||||||
public void UpdateResult(Vector3 result) {
|
public void UpdateResult(Vector3 result) {
|
||||||
// float d = Vector3.Distance(result, this.outputValue);
|
// float d = Vector3.Distance(result, this.outputValue);
|
||||||
// if (d < 0.5f) {
|
// if (d < 0.5f) {
|
||||||
// //Debug.Log($"insignificant update: {d}");
|
// //Debug.Log($"insignificant update: {d}");
|
||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
this.outputValue = result;
|
this.outputValue = result;
|
||||||
foreach (Receiver receiver in this.receivers)
|
foreach (Receiver receiver in this.receivers)
|
||||||
receiver.nucleus.UpdateState();
|
receiver.nucleus.UpdateState();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
public class Synapse {
|
public class Synapse {
|
||||||
[System.NonSerialized]
|
[System.NonSerialized]
|
||||||
public Nucleus nucleus;
|
public INucleus nucleus;
|
||||||
public int nucleusId;
|
public NanoBrain cluster;
|
||||||
public float weight;
|
public int nucleusId;
|
||||||
|
public float weight;
|
||||||
public enum CurvePresets {
|
|
||||||
Linear,
|
public enum CurvePresets {
|
||||||
Power,
|
Linear,
|
||||||
Sqrt,
|
Power,
|
||||||
Reciprocal,
|
Sqrt,
|
||||||
Custom
|
Reciprocal,
|
||||||
}
|
Custom
|
||||||
// public CurvePresets curvePreset;
|
}
|
||||||
// public AnimationCurve curve;
|
// public CurvePresets curvePreset;
|
||||||
public float curveMax = 1.0f;
|
// public AnimationCurve curve;
|
||||||
|
public float curveMax = 1.0f;
|
||||||
public Synapse(Nucleus nucleus, float weight) {
|
|
||||||
this.nucleus = nucleus;
|
public Synapse(Nucleus nucleus, float weight) {
|
||||||
this.nucleusId = nucleus.id;
|
this.nucleus = nucleus;
|
||||||
this.weight = weight;
|
this.nucleusId = nucleus.id;
|
||||||
}
|
this.weight = weight;
|
||||||
|
}
|
||||||
public void Rebuild(NanoBrain brain) {
|
|
||||||
if (brain == null) {
|
public void Rebuild(NanoBrain brain) {
|
||||||
return;
|
if (brain == null) {
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
foreach (Nucleus nucleus in brain.nuclei) {
|
|
||||||
if (nucleus.id == this.nucleusId) {
|
foreach (Nucleus nucleus in brain.nuclei) {
|
||||||
this.nucleus = nucleus;
|
if (nucleus.id == this.nucleusId) {
|
||||||
return;
|
this.nucleus = nucleus;
|
||||||
}
|
return;
|
||||||
}
|
}
|
||||||
foreach (Perceptoid perceptoid in brain.perceptei) {
|
}
|
||||||
if (perceptoid.id == this.nucleusId) {
|
foreach (Perceptoid perceptoid in brain.perceptei) {
|
||||||
this.nucleus = perceptoid;
|
if (perceptoid.id == this.nucleusId) {
|
||||||
return;
|
this.nucleus = perceptoid;
|
||||||
}
|
return;
|
||||||
}
|
}
|
||||||
Debug.LogError($"Synapse deserialization error: could not find nucleus with id {this.nucleusId}");
|
}
|
||||||
}
|
Debug.LogError($"Synapse deserialization error: could not find nucleus with id {this.nucleusId}");
|
||||||
|
}
|
||||||
// public AnimationCurve GenerateCurve() {
|
|
||||||
// switch (this.curvePreset) {
|
// public AnimationCurve GenerateCurve() {
|
||||||
// case CurvePresets.Linear:
|
// switch (this.curvePreset) {
|
||||||
// this.curveMax = this.weight;
|
// case CurvePresets.Linear:
|
||||||
// return Presets.Linear(this.weight);
|
// this.curveMax = this.weight;
|
||||||
// case CurvePresets.Power:
|
// return Presets.Linear(this.weight);
|
||||||
// this.curveMax = this.weight;
|
// case CurvePresets.Power:
|
||||||
// return Presets.Power(2.0f, this.weight);
|
// this.curveMax = this.weight;
|
||||||
// case CurvePresets.Sqrt:
|
// return Presets.Power(2.0f, this.weight);
|
||||||
// this.curveMax = this.weight;
|
// case CurvePresets.Sqrt:
|
||||||
// return Presets.Power(0.5f, this.weight);
|
// this.curveMax = this.weight;
|
||||||
// case CurvePresets.Reciprocal:
|
// return Presets.Power(0.5f, this.weight);
|
||||||
// this.curveMax = 1 / 0.01f * this.weight;
|
// case CurvePresets.Reciprocal:
|
||||||
// return Presets.Reciprocal(this.weight);
|
// this.curveMax = 1 / 0.01f * this.weight;
|
||||||
// default:
|
// return Presets.Reciprocal(this.weight);
|
||||||
// this.curveMax = weight;
|
// default:
|
||||||
// return AnimationCurve.Constant(0, 1, weight);
|
// this.curveMax = weight;
|
||||||
// }
|
// return AnimationCurve.Constant(0, 1, weight);
|
||||||
// }
|
// }
|
||||||
|
// }
|
||||||
public static class Presets {
|
|
||||||
private const int samples = 32;
|
public static class Presets {
|
||||||
public static AnimationCurve Linear(float weight) {
|
private const int samples = 32;
|
||||||
return AnimationCurve.Linear(0f, 0f, 1000f, weight * 1000);
|
public static AnimationCurve Linear(float weight) {
|
||||||
}
|
return AnimationCurve.Linear(0f, 0f, 1000f, weight * 1000);
|
||||||
public static AnimationCurve Power(float exponent, float weight) {
|
}
|
||||||
// build keyframes
|
public static AnimationCurve Power(float exponent, float weight) {
|
||||||
Keyframe[] keys = new Keyframe[samples];
|
// build keyframes
|
||||||
for (int i = 0; i < samples; i++) {
|
Keyframe[] keys = new Keyframe[samples];
|
||||||
float t = i / (float)(samples - 1);
|
for (int i = 0; i < samples; i++) {
|
||||||
float v = Mathf.Pow(t, exponent) * weight;
|
float t = i / (float)(samples - 1);
|
||||||
keys[i] = new Keyframe(t, v);
|
float v = Mathf.Pow(t, exponent) * weight;
|
||||||
}
|
keys[i] = new Keyframe(t, v);
|
||||||
|
}
|
||||||
AnimationCurve curve = new(keys);
|
|
||||||
|
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++) {
|
// set tangent modes for each key to Auto (smooth). Use Linear if you prefer straight segments.
|
||||||
AnimationUtility.SetKeyLeftTangentMode(curve, i, AnimationUtility.TangentMode.Auto);
|
for (int i = 0; i < curve.length; i++) {
|
||||||
AnimationUtility.SetKeyRightTangentMode(curve, i, AnimationUtility.TangentMode.Auto);
|
AnimationUtility.SetKeyLeftTangentMode(curve, i, AnimationUtility.TangentMode.Auto);
|
||||||
}
|
AnimationUtility.SetKeyRightTangentMode(curve, i, AnimationUtility.TangentMode.Auto);
|
||||||
|
}
|
||||||
return curve;
|
|
||||||
}
|
return curve;
|
||||||
public static AnimationCurve Reciprocal(float weight) {
|
}
|
||||||
int samples = 128;
|
public static AnimationCurve Reciprocal(float weight) {
|
||||||
float xMin = 0.001f;
|
int samples = 128;
|
||||||
float xMax = 1;
|
float xMin = 0.001f;
|
||||||
var keys = new Keyframe[samples];
|
float xMax = 1;
|
||||||
for (int i = 0; i < samples; i++) {
|
var keys = new Keyframe[samples];
|
||||||
float t = i / (float)(samples - 1);
|
for (int i = 0; i < samples; i++) {
|
||||||
float x = Mathf.Lerp(xMin, xMax, t);
|
float t = i / (float)(samples - 1);
|
||||||
float y = 1f / x * weight;
|
float x = Mathf.Lerp(xMin, xMax, t);
|
||||||
keys[i] = new Keyframe(x, y);
|
float y = 1f / x * weight;
|
||||||
}
|
keys[i] = new Keyframe(x, y);
|
||||||
var curve = new AnimationCurve(keys);
|
}
|
||||||
for (int i = 0; i < curve.length; i++) {
|
var curve = new AnimationCurve(keys);
|
||||||
AnimationUtility.SetKeyLeftTangentMode(curve, i, AnimationUtility.TangentMode.Linear);
|
for (int i = 0; i < curve.length; i++) {
|
||||||
AnimationUtility.SetKeyRightTangentMode(curve, i, AnimationUtility.TangentMode.Linear);
|
AnimationUtility.SetKeyLeftTangentMode(curve, i, AnimationUtility.TangentMode.Linear);
|
||||||
}
|
AnimationUtility.SetKeyRightTangentMode(curve, i, AnimationUtility.TangentMode.Linear);
|
||||||
return curve;
|
}
|
||||||
}
|
return curve;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
[System.Serializable]
|
|
||||||
public class Receiver {
|
[Serializable]
|
||||||
[System.NonSerialized]
|
public class Receiver {
|
||||||
public Nucleus nucleus;
|
[NonSerialized]
|
||||||
public int nucleusId;
|
public INucleus nucleus;
|
||||||
|
//public int nucleusId;
|
||||||
public Receiver(Nucleus nucleus) {
|
|
||||||
this.nucleus = nucleus;
|
public Receiver(INucleus nucleus) {
|
||||||
this.nucleusId = nucleus.id;
|
this.nucleus = nucleus;
|
||||||
}
|
//this.nucleusId = nucleus.id;
|
||||||
|
}
|
||||||
public bool Rebuild(NanoBrain brain) {
|
|
||||||
if (brain == null) {
|
public bool Rebuild(NanoBrain brain) {
|
||||||
return false;
|
if (brain == null) {
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
foreach (Nucleus nucleus in brain.nuclei) {
|
|
||||||
if (nucleus.id == this.nucleusId) {
|
// Use SerializedReference instead?
|
||||||
this.nucleus = nucleus;
|
// foreach (Nucleus nucleus in brain.nuclei) {
|
||||||
return true;
|
// if (nucleus.id == this.nucleusId) {
|
||||||
}
|
// this.nucleus = nucleus;
|
||||||
}
|
// return true;
|
||||||
Debug.LogWarning($"Receiver deserialization error: could not find nucleus with id {this.nucleusId}");
|
// }
|
||||||
return false;
|
// }
|
||||||
}
|
//Debug.LogWarning($"Receiver deserialization error: could not find nucleus with id {this.nucleusId}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,98 +1,103 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
public class Perceptoid : Neuroid {
|
public class Perceptoid : Neuroid {
|
||||||
// A neuroid which has no neurons as input
|
// A neuroid which has no neurons as input
|
||||||
// But receives value from a receptor
|
// But receives value from a receptor
|
||||||
public Receptor receptor;
|
|
||||||
public string baseName;
|
public NanoBrain brain;
|
||||||
|
public Receptor receptor;
|
||||||
public int thingId;
|
public string baseName;
|
||||||
|
|
||||||
//[SerializeField]
|
public int thingId;
|
||||||
// Needs serialization!!!!
|
|
||||||
[SerializeReference]
|
//[SerializeField]
|
||||||
public PercepteiArray array;
|
// Needs serialization!!!!
|
||||||
|
[SerializeReference]
|
||||||
#region Serialization
|
public PercepteiArray array;
|
||||||
|
|
||||||
[SerializeField]
|
#region Serialization
|
||||||
public int thingType;
|
|
||||||
|
[SerializeField]
|
||||||
public override void Rebuild(NanoBrain brain) {
|
public int thingType;
|
||||||
base.Rebuild(brain);
|
|
||||||
this.receptor = Receptor.GetReceptor(brain, thingType);
|
public override void Rebuild(NanoBrain brain) {
|
||||||
this.receptor.perceptei.Add(this);
|
base.Rebuild(brain);
|
||||||
if (string.IsNullOrEmpty(this.baseName))
|
this.receptor = Receptor.GetReceptor(brain, thingType);
|
||||||
this.baseName = this.name;
|
this.receptor.perceptei.Add(this);
|
||||||
}
|
if (string.IsNullOrEmpty(this.baseName))
|
||||||
|
this.baseName = this.name;
|
||||||
public override void Deserialize(Nucleus nucleus) {
|
}
|
||||||
base.Deserialize(nucleus);
|
|
||||||
|
public override void Deserialize(Nucleus nucleus) {
|
||||||
if (nucleus is Perceptoid perceptoid)
|
base.Deserialize(nucleus);
|
||||||
this.receptor.thingType = perceptoid.thingType;
|
|
||||||
|
if (nucleus is Perceptoid perceptoid)
|
||||||
// Point all receivers to this perceptoid instead of the default nucleus
|
this.receptor.thingType = perceptoid.thingType;
|
||||||
foreach (Receiver receiver in nucleus.receivers) {
|
|
||||||
foreach (Synapse synapse in receiver.nucleus.synapses) {
|
// Point all receivers to this perceptoid instead of the default nucleus
|
||||||
if (synapse.nucleus == nucleus)
|
foreach (Receiver receiver in nucleus.receivers) {
|
||||||
synapse.nucleus = this;
|
foreach (Synapse synapse in receiver.nucleus.synapses) {
|
||||||
}
|
if (synapse.nucleus == nucleus)
|
||||||
}
|
synapse.nucleus = this;
|
||||||
// Point all synapses to this perceptoid instead of the default nucleus
|
}
|
||||||
foreach (Synapse synapse in nucleus.synapses) {
|
}
|
||||||
foreach (Receiver receiver in synapse.nucleus.receivers) {
|
// Point all synapses to this perceptoid instead of the default nucleus
|
||||||
if (receiver.nucleus == nucleus)
|
foreach (Synapse synapse in nucleus.synapses) {
|
||||||
receiver.nucleus = this;
|
foreach (Receiver receiver in synapse.nucleus.receivers) {
|
||||||
}
|
if (receiver.nucleus == nucleus)
|
||||||
}
|
receiver.nucleus = this;
|
||||||
// Copy all the synapses
|
}
|
||||||
this.synapses = nucleus.synapses;
|
}
|
||||||
// Copy all receivers
|
// Copying disabled for now
|
||||||
this.receivers = nucleus.receivers;
|
// // Copy all the synapses
|
||||||
}
|
// this.synapses = nucleus.synapses;
|
||||||
|
// // Copy all receivers
|
||||||
#endregion Serialization
|
// this.receivers = nucleus.receivers;
|
||||||
|
}
|
||||||
public Perceptoid(NanoBrain brain, int thingType, string name = "sensor") : base(name) {
|
|
||||||
this.brain = brain;
|
#endregion Serialization
|
||||||
if (this.brain != null) {
|
|
||||||
this.brain.perceptei.Add(this);
|
public Perceptoid(NanoBrain brain, int thingType, string name = "sensor") : base(name) {
|
||||||
}
|
this.brain = brain;
|
||||||
else
|
this.cluster = brain.cluster;
|
||||||
Debug.LogError("No neuroid network");
|
if (this.cluster != null) {
|
||||||
|
brain.perceptei.Add(this);
|
||||||
this.nucleusType = nameof(Perceptoid);
|
}
|
||||||
this.name = name;
|
else
|
||||||
this.baseName = name;
|
Debug.LogError("No neuroid network");
|
||||||
this.thingType = thingType;
|
|
||||||
this.receptor = Receptor.GetReceptor(brain, thingType);
|
this.nucleusType = nameof(Perceptoid);
|
||||||
this.receptor.perceptei.Add(this);
|
this.name = name;
|
||||||
this.array = new PercepteiArray(this);
|
this.baseName = name;
|
||||||
}
|
this.thingType = thingType;
|
||||||
|
this.receptor = Receptor.GetReceptor(brain, thingType);
|
||||||
public Perceptoid(PercepteiArray array) : base(array.name) {
|
this.receptor.perceptei.Add(this);
|
||||||
this.array = array;
|
this.array = new PercepteiArray(this);
|
||||||
Perceptoid source = array.perceptei[0];
|
}
|
||||||
this.brain = source.brain;
|
|
||||||
if (this.brain != null) {
|
public Perceptoid(PercepteiArray array) : base(array.name) {
|
||||||
this.brain.perceptei.Add(this);
|
this.array = array;
|
||||||
}
|
Perceptoid source = array.perceptei[0];
|
||||||
else
|
this.brain = source.brain;
|
||||||
Debug.LogError("No neuroid network");
|
this.cluster = source.cluster;
|
||||||
|
if (this.brain != null) {
|
||||||
this.nucleusType = nameof(Perceptoid);
|
this.brain.perceptei.Add(this);
|
||||||
this.name = source.baseName;
|
}
|
||||||
this.baseName = source.baseName;
|
else
|
||||||
this.thingType = source.thingType;
|
Debug.LogError("No neuroid network");
|
||||||
this.receptor = Receptor.GetReceptor(this.brain, this.thingType);
|
|
||||||
this.receptor.perceptei.Add(this);
|
this.nucleusType = nameof(Perceptoid);
|
||||||
}
|
this.name = source.baseName;
|
||||||
|
this.baseName = source.baseName;
|
||||||
public override void UpdateState() {
|
this.thingType = source.thingType;
|
||||||
Vector3 result = this.receptor.localPosition;
|
this.receptor = Receptor.GetReceptor(this.brain, this.thingType);
|
||||||
UpdateResult(result);
|
this.receptor.perceptei.Add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
public override void UpdateState() {
|
||||||
|
Vector3 result = this.receptor.localPosition;
|
||||||
|
UpdateResult(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@ -1,82 +1,82 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using LinearAlgebra;
|
using LinearAlgebra;
|
||||||
|
|
||||||
public class Receptor {
|
public class Receptor {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The list of perceptoid which can process stimuli from this receptor
|
/// The list of perceptoid which can process stimuli from this receptor
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<Perceptoid> perceptei = new();
|
public List<Perceptoid> perceptei = new();
|
||||||
|
|
||||||
private int _thingType = 0;
|
private int _thingType = 0;
|
||||||
public int thingType {
|
public int thingType {
|
||||||
get { return _thingType; }
|
get { return _thingType; }
|
||||||
set {
|
set {
|
||||||
_thingType = value;
|
_thingType = value;
|
||||||
foreach (Perceptoid perceptoid in perceptei) {
|
foreach (Perceptoid perceptoid in perceptei) {
|
||||||
perceptoid.thingType = _thingType;
|
perceptoid.thingType = _thingType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public Vector3 localPosition;
|
public Vector3 localPosition;
|
||||||
public float distanceResolution = 0.1f;
|
public float distanceResolution = 0.1f;
|
||||||
public float directionResolution = 5;
|
public float directionResolution = 5;
|
||||||
|
|
||||||
public Receptor(NanoBrain brain, int thingType) {
|
public Receptor(NanoBrain brain, int thingType) {
|
||||||
this.thingType = thingType;
|
this.thingType = thingType;
|
||||||
//this.perceptei.Add(perceptoid);
|
//this.perceptei.Add(perceptoid);
|
||||||
brain.receptors.Add(this);
|
brain.receptors.Add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Receptor GetReceptor(NanoBrain brain, int thingType) {
|
public static Receptor GetReceptor(NanoBrain brain, int thingType) {
|
||||||
foreach (Receptor receptor in brain.receptors) {
|
foreach (Receptor receptor in brain.receptors) {
|
||||||
if (thingType == 0 || receptor.thingType == thingType)
|
if (thingType == 0 || receptor.thingType == thingType)
|
||||||
return receptor;
|
return receptor;
|
||||||
}
|
}
|
||||||
Receptor newReceptor = new(brain, thingType);
|
Receptor newReceptor = new(brain, thingType);
|
||||||
return newReceptor;
|
return newReceptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual void ProcessStimulus(int thingId, Vector3 newLocalPositionVector, string thingName = null) {
|
public virtual void ProcessStimulus(int thingId, Vector3 newLocalPositionVector, string thingName = null) {
|
||||||
this.localPosition = newLocalPositionVector;
|
this.localPosition = newLocalPositionVector;
|
||||||
|
|
||||||
Perceptoid selectedPerceptoid = null;
|
Perceptoid selectedPerceptoid = null;
|
||||||
foreach (Perceptoid perceptoid in this.perceptei) {
|
foreach (Perceptoid perceptoid in this.perceptei) {
|
||||||
if (perceptoid.thingId == thingId) {
|
if (perceptoid.thingId == thingId) {
|
||||||
// We found an existing perceptoid for this thing
|
// We found an existing perceptoid for this thing
|
||||||
selectedPerceptoid = perceptoid;
|
selectedPerceptoid = perceptoid;
|
||||||
// Do not look any further
|
// Do not look any further
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (perceptoid.isSleeping) {
|
else if (perceptoid.isSleeping) {
|
||||||
// A sleeping perceptoid is not active and can therefore always be reused
|
// A sleeping perceptoid is not active and can therefore always be reused
|
||||||
selectedPerceptoid = perceptoid;
|
selectedPerceptoid = perceptoid;
|
||||||
// Look further because we could find a existing perceptoid for this thing
|
// Look further because we could find a existing perceptoid for this thing
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (selectedPerceptoid == null) {
|
else if (selectedPerceptoid == null) {
|
||||||
// If we haven't found a perceptoid yet, just start by taking the first
|
// If we haven't found a perceptoid yet, just start by taking the first
|
||||||
selectedPerceptoid = perceptoid;
|
selectedPerceptoid = perceptoid;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (selectedPerceptoid.isSleeping == false) {
|
else if (selectedPerceptoid.isSleeping == false) {
|
||||||
// If no existing or sleeping perceptoid is found, we look for the perceptoid
|
// If no existing or sleeping perceptoid is found, we look for the perceptoid
|
||||||
// we the furthest (least interesting) stimulus
|
// we the furthest (least interesting) stimulus
|
||||||
if (perceptoid.receptor.localPosition.magnitude < selectedPerceptoid.receptor.localPosition.magnitude) {
|
if (perceptoid.receptor.localPosition.magnitude < selectedPerceptoid.receptor.localPosition.magnitude) {
|
||||||
Debug.Log($"{selectedPerceptoid.name} {selectedPerceptoid.receptor.localPosition.magnitude} {perceptoid.receptor.localPosition.magnitude} ");
|
Debug.Log($"{selectedPerceptoid.name} {selectedPerceptoid.receptor.localPosition.magnitude} {perceptoid.receptor.localPosition.magnitude} ");
|
||||||
selectedPerceptoid = perceptoid;
|
selectedPerceptoid = perceptoid;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (selectedPerceptoid == null) {
|
if (selectedPerceptoid == null) {
|
||||||
Debug.Log("No perceptoid selected, stimulus is ignored");
|
Debug.Log("No perceptoid selected, stimulus is ignored");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Debug.Log($"Stimulus {thingType} {thingId} {selectedPerceptoid.name}");
|
// Debug.Log($"Stimulus {thingType} {thingId} {selectedPerceptoid.name}");
|
||||||
selectedPerceptoid.thingId = thingId;
|
selectedPerceptoid.thingId = thingId;
|
||||||
if (thingName != null)
|
if (thingName != null)
|
||||||
selectedPerceptoid.name = selectedPerceptoid.baseName + " " + thingName;
|
selectedPerceptoid.name = selectedPerceptoid.baseName + " " + thingName;
|
||||||
selectedPerceptoid.UpdateState();
|
selectedPerceptoid.UpdateState();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
72
Assets/NanoBrain/VisualEditor/Editor/BrainPickerWindow.cs
Normal file
72
Assets/NanoBrain/VisualEditor/Editor/BrainPickerWindow.cs
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
using UnityEditor;
|
||||||
|
using UnityEngine;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
public class BrainPickerWindow : EditorWindow
|
||||||
|
{
|
||||||
|
private Vector2 scroll;
|
||||||
|
private NanoBrain[] items = new NanoBrain[0];
|
||||||
|
private Action<NanoBrain> onPicked;
|
||||||
|
private string search = "";
|
||||||
|
|
||||||
|
public static void ShowPicker(Action<NanoBrain> onPicked, string title = "Select NanoBrain")
|
||||||
|
{
|
||||||
|
var w = CreateInstance<BrainPickerWindow>();
|
||||||
|
w.titleContent = new GUIContent(title);
|
||||||
|
w.minSize = new Vector2(360, 320);
|
||||||
|
w.onPicked = onPicked;
|
||||||
|
w.RefreshList();
|
||||||
|
w.ShowModalUtility(); // modal dialog
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnEnable() => RefreshList();
|
||||||
|
|
||||||
|
private void RefreshList()
|
||||||
|
{
|
||||||
|
var guids = AssetDatabase.FindAssets("t:NanoBrain");
|
||||||
|
items = guids
|
||||||
|
.Select(g => AssetDatabase.LoadAssetAtPath<NanoBrain>(AssetDatabase.GUIDToAssetPath(g)))
|
||||||
|
.Where(b => b != null)
|
||||||
|
.OrderBy(b => b.name)
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnGUI()
|
||||||
|
{
|
||||||
|
EditorGUILayout.Space();
|
||||||
|
EditorGUILayout.BeginHorizontal();
|
||||||
|
EditorGUILayout.LabelField("Choose NanoBrain:", EditorStyles.boldLabel);
|
||||||
|
if (GUILayout.Button("Refresh", GUILayout.Width(80))) RefreshList();
|
||||||
|
GUILayout.FlexibleSpace();
|
||||||
|
EditorGUILayout.EndHorizontal();
|
||||||
|
|
||||||
|
EditorGUILayout.Space();
|
||||||
|
search = EditorGUILayout.TextField(search);
|
||||||
|
|
||||||
|
EditorGUILayout.Space();
|
||||||
|
scroll = EditorGUILayout.BeginScrollView(scroll);
|
||||||
|
foreach (var it in items)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(search) && it.name.IndexOf(search, StringComparison.OrdinalIgnoreCase) < 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
EditorGUILayout.BeginHorizontal();
|
||||||
|
EditorGUILayout.LabelField(EditorGUIUtility.ObjectContent(it, typeof(NanoBrain)), GUILayout.Height(20));
|
||||||
|
if (GUILayout.Button("Select", GUILayout.Width(70)))
|
||||||
|
{
|
||||||
|
onPicked?.Invoke(it);
|
||||||
|
Close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
EditorGUILayout.EndHorizontal();
|
||||||
|
}
|
||||||
|
EditorGUILayout.EndScrollView();
|
||||||
|
|
||||||
|
EditorGUILayout.Space();
|
||||||
|
EditorGUILayout.BeginHorizontal();
|
||||||
|
if (GUILayout.Button("Cancel")) { onPicked?.Invoke(null); Close(); }
|
||||||
|
GUILayout.FlexibleSpace();
|
||||||
|
EditorGUILayout.EndHorizontal();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 9197e2d322d23b5798ab4aef729815b0
|
||||||
633
Assets/NanoBrain/VisualEditor/Editor/ClusterInspector.cs
Normal file
633
Assets/NanoBrain/VisualEditor/Editor/ClusterInspector.cs
Normal file
@ -0,0 +1,633 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using UnityEditor;
|
||||||
|
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.UIElements;
|
||||||
|
|
||||||
|
[CustomEditor(typeof(Cluster))]
|
||||||
|
public class ClusterInspector : Editor {
|
||||||
|
protected static VisualElement mainContainer;
|
||||||
|
protected static VisualElement inspectorContainer;
|
||||||
|
|
||||||
|
protected bool breakOnWake = false;
|
||||||
|
|
||||||
|
#region Start
|
||||||
|
|
||||||
|
public override VisualElement CreateInspectorGUI() {
|
||||||
|
Cluster cluster = target as Cluster;
|
||||||
|
|
||||||
|
serializedObject.Update();
|
||||||
|
|
||||||
|
VisualElement root = new();
|
||||||
|
//root.style.flexDirection = FlexDirection.Row; // side-by-side layout
|
||||||
|
//root.style.flexGrow = 1;
|
||||||
|
//root.style.minHeight = 600;
|
||||||
|
root.style.paddingLeft = 0;
|
||||||
|
root.style.paddingRight = 0;
|
||||||
|
root.style.paddingTop = 0;
|
||||||
|
root.style.paddingBottom = 0;
|
||||||
|
|
||||||
|
root.styleSheets.Add(Resources.Load<StyleSheet>("GraphStyles"));
|
||||||
|
|
||||||
|
mainContainer = new() {
|
||||||
|
// name = "main",
|
||||||
|
style = {
|
||||||
|
// flexDirection = FlexDirection.Row,
|
||||||
|
// flexGrow = 1,
|
||||||
|
height = 450,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
GraphView graph = new();
|
||||||
|
graph.style.flexGrow = 1;
|
||||||
|
|
||||||
|
inspectorContainer = new VisualElement {
|
||||||
|
// name = "inspector"
|
||||||
|
};
|
||||||
|
|
||||||
|
mainContainer.Add(graph);
|
||||||
|
mainContainer.Add(inspectorContainer);
|
||||||
|
root.Add(mainContainer);
|
||||||
|
|
||||||
|
// Run once for initial state (use resolved style width if available)
|
||||||
|
float initialWidth = root.layout.width > 0 ? root.layout.width : root.contentRect.width;
|
||||||
|
UpdateLayout(initialWidth);
|
||||||
|
|
||||||
|
// React to size changes of root (or parent if appropriate)
|
||||||
|
root.RegisterCallback<GeometryChangedEvent>(evt => {
|
||||||
|
UpdateLayout(evt.newRect.width);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (cluster != null)
|
||||||
|
graph.SetGraph(null, cluster, cluster.output, inspectorContainer);
|
||||||
|
else
|
||||||
|
Debug.LogWarning(" No brain!");
|
||||||
|
|
||||||
|
serializedObject.ApplyModifiedProperties();
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class GraphView : VisualElement {
|
||||||
|
Cluster brain;
|
||||||
|
SerializedObject serializedBrain;
|
||||||
|
INucleus currentNucleus;
|
||||||
|
GameObject gameObject;
|
||||||
|
private List<NeuroidLayer> layers = new();
|
||||||
|
private readonly Dictionary<INucleus, Vector2Int> neuroidPositions = new();
|
||||||
|
|
||||||
|
Vector2 pan = Vector2.zero;
|
||||||
|
//float zoom = 1f;
|
||||||
|
bool draggingCanvas = false;
|
||||||
|
Vector2 lastMouse;
|
||||||
|
ClusterWrapper currentWrapper;
|
||||||
|
|
||||||
|
public GraphView() {
|
||||||
|
name = "content";
|
||||||
|
style.flexGrow = 1;
|
||||||
|
|
||||||
|
IMGUIContainer imguiContainer = new(OnIMGUI);
|
||||||
|
imguiContainer.style.position = Position.Absolute;
|
||||||
|
imguiContainer.style.left = 0; imguiContainer.style.top = 0;
|
||||||
|
imguiContainer.style.right = 0; imguiContainer.style.bottom = 0;
|
||||||
|
imguiContainer.pickingMode = PickingMode.Position;
|
||||||
|
imguiContainer.focusable = true;
|
||||||
|
Add(imguiContainer);
|
||||||
|
|
||||||
|
//RegisterCallback<WheelEvent>(OnWheel);
|
||||||
|
RegisterCallback<MouseDownEvent>(OnMouseDown);
|
||||||
|
RegisterCallback<MouseMoveEvent>(OnMouseMove);
|
||||||
|
RegisterCallback<MouseUpEvent>(OnMouseUp);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetGraph(GameObject gameObject, Cluster brain, Nucleus nucleus, VisualElement inspectorContainer) {
|
||||||
|
this.gameObject = gameObject;
|
||||||
|
this.brain = brain;
|
||||||
|
if (Application.isPlaying == false)
|
||||||
|
this.serializedBrain = new SerializedObject(brain);
|
||||||
|
this.currentNucleus = nucleus;
|
||||||
|
Rebuild(inspectorContainer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Rebuild(VisualElement inspectorContainer) {
|
||||||
|
BuildLayers();
|
||||||
|
|
||||||
|
if (this.currentNucleus == null) {
|
||||||
|
inspectorContainer.Clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentWrapper != null)
|
||||||
|
DestroyImmediate(currentWrapper);
|
||||||
|
currentWrapper = CreateInstance<ClusterWrapper>().Init(this.currentNucleus, brain);
|
||||||
|
DrawInspector(inspectorContainer);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void BuildLayers() {
|
||||||
|
// A temporary list to track what's been added to layers
|
||||||
|
this.layers = new();
|
||||||
|
int layerIx = 0;
|
||||||
|
|
||||||
|
INucleus selectedNucleus = this.currentNucleus;
|
||||||
|
if (selectedNucleus == null)
|
||||||
|
return;
|
||||||
|
NeuroidLayer currentLayer = new() { ix = layerIx };
|
||||||
|
|
||||||
|
if (selectedNucleus.receivers != null) {
|
||||||
|
foreach (Receiver receiver in selectedNucleus.receivers) {
|
||||||
|
INucleus outputNeuroid = receiver.nucleus;
|
||||||
|
if (outputNeuroid != null) {
|
||||||
|
AddToLayer(currentLayer, outputNeuroid);
|
||||||
|
// Debug.Log($"layer {layerIx} nucleus {outputNeuroid.name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (currentLayer.neuroids.Count > 0) {
|
||||||
|
this.layers.Add(currentLayer);
|
||||||
|
layerIx++;
|
||||||
|
currentLayer = new() { ix = layerIx };
|
||||||
|
}
|
||||||
|
|
||||||
|
AddToLayer(currentLayer, selectedNucleus);
|
||||||
|
this.layers.Add(currentLayer);
|
||||||
|
// Debug.Log($"layer {layerIx} nucleus {selectedNucleus.name}");
|
||||||
|
|
||||||
|
layerIx++;
|
||||||
|
currentLayer = new() { ix = layerIx };
|
||||||
|
|
||||||
|
if (selectedNucleus.synapses != null) {
|
||||||
|
foreach (Synapse synapse in selectedNucleus.synapses) {
|
||||||
|
INucleus input = synapse.nucleus;
|
||||||
|
AddToLayer(currentLayer, input);
|
||||||
|
// Debug.Log($"layer {layerIx} nucleus {input.name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (currentLayer.neuroids.Count > 0) {
|
||||||
|
this.layers.Add(currentLayer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void AddToLayer(NeuroidLayer layer, INucleus nucleus) {
|
||||||
|
if (nucleus == null)
|
||||||
|
return;
|
||||||
|
layer.neuroids.Add(nucleus);
|
||||||
|
//nucleus.layerIx = layer.ix;
|
||||||
|
// Store its position
|
||||||
|
Vector2Int neuroidPosition = new(layer.ix, layer.neuroids.Count - 1);
|
||||||
|
neuroidPositions[nucleus] = neuroidPosition;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnMouseDown(MouseDownEvent e) {
|
||||||
|
if (e.button == 2) { draggingCanvas = true; lastMouse = e.mousePosition; e.StopPropagation(); }
|
||||||
|
}
|
||||||
|
void OnMouseMove(MouseMoveEvent e) {
|
||||||
|
if (draggingCanvas) {
|
||||||
|
var delta = e.mousePosition - lastMouse;
|
||||||
|
pan += delta;
|
||||||
|
//content.style.left = pan.x;
|
||||||
|
//content.style.top = pan.y;
|
||||||
|
lastMouse = e.mousePosition;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void OnMouseUp(MouseUpEvent e) { if (e.button == 2) draggingCanvas = false; }
|
||||||
|
|
||||||
|
void OnIMGUI() {
|
||||||
|
if (currentNucleus == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (Application.isPlaying == false)
|
||||||
|
serializedBrain.Update();
|
||||||
|
|
||||||
|
Handles.BeginGUI();
|
||||||
|
DrawGraph();
|
||||||
|
Handles.EndGUI();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawGraph() {
|
||||||
|
float size = 20;
|
||||||
|
Vector3 position = new(150, 210, 0);
|
||||||
|
|
||||||
|
DrawReceivers(this.currentNucleus, position, size);
|
||||||
|
DrawSynapses(this.currentNucleus, position, size);
|
||||||
|
|
||||||
|
// Draw selected Nucleus
|
||||||
|
Handles.color = Color.white;
|
||||||
|
Handles.DrawSolidDisc(position, Vector3.forward, size + 2);
|
||||||
|
DrawNucleus(this.currentNucleus, position, this.currentNucleus.outputValue.magnitude, 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawReceivers(INucleus nucleus, Vector3 parentPos, float size) {
|
||||||
|
int nodeCount = nucleus.receivers.Count;
|
||||||
|
|
||||||
|
// Determine the maximum value in this layer
|
||||||
|
// This is used to 'scale' the output value colors of the nuclei
|
||||||
|
float maxValue = 0;
|
||||||
|
foreach (Receiver receiver in nucleus.receivers) {
|
||||||
|
if (receiver.nucleus is Neuroid neuroid) {
|
||||||
|
float value = neuroid.outputValue.magnitude;
|
||||||
|
if (value > maxValue)
|
||||||
|
maxValue = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine the spacing of the nuclei in the layer
|
||||||
|
float spacing = 400f / nodeCount;
|
||||||
|
float margin = 10 + spacing / 2;
|
||||||
|
|
||||||
|
int row = 0;
|
||||||
|
foreach (Receiver receiver in nucleus.receivers) {
|
||||||
|
INucleus receiverNucleus = receiver.nucleus;
|
||||||
|
if (receiverNucleus == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
Vector3 pos = new(50, margin + row * spacing, 0.0f);
|
||||||
|
Handles.color = Color.white;
|
||||||
|
Handles.DrawLine(parentPos, pos);
|
||||||
|
|
||||||
|
DrawNucleus(receiverNucleus, pos, maxValue, size);
|
||||||
|
row++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawSynapses(INucleus nucleus, Vector3 parentPos, float size) {
|
||||||
|
int nodeCount = nucleus.synapses.Count;
|
||||||
|
|
||||||
|
// Determine the maximum value in this layer
|
||||||
|
// This is used to 'scale' the output value colors of the nuclei
|
||||||
|
float maxValue = 0;
|
||||||
|
foreach (Synapse receiver in nucleus.synapses) {
|
||||||
|
if (receiver.nucleus is Neuroid neuroid) {
|
||||||
|
float value = neuroid.outputValue.magnitude;
|
||||||
|
if (value > maxValue)
|
||||||
|
maxValue = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine the spacing of the nuclei in the layer
|
||||||
|
float spacing = 400f / nodeCount;
|
||||||
|
float margin = 10 + spacing / 2;
|
||||||
|
|
||||||
|
int row = 0;
|
||||||
|
List<PercepteiArray> drawnArrays = new();
|
||||||
|
foreach (Synapse synapse in nucleus.synapses) {
|
||||||
|
Vector3 pos = new(250, margin + row * spacing, 0.0f);
|
||||||
|
Handles.color = Color.white;
|
||||||
|
Handles.DrawLine(parentPos, pos);
|
||||||
|
// if (synapse.nucleus is Perceptoid perceptoid && perceptoid.array != null) {
|
||||||
|
// // if (drawnArrays.Contains(perceptoid.array))
|
||||||
|
// // // We already drawn this array
|
||||||
|
// // continue;
|
||||||
|
|
||||||
|
// drawnArrays.Add(perceptoid.array);
|
||||||
|
// DrawArray(perceptoid.array, pos, size);
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
|
||||||
|
DrawNucleus(synapse.nucleus, pos, maxValue, size);
|
||||||
|
row++;
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawNucleus(INucleus nucleus, Vector3 position, float maxValue, float size) {
|
||||||
|
if (nucleus.isSleeping)
|
||||||
|
Handles.color = Color.darkRed;
|
||||||
|
else {
|
||||||
|
if (Application.isPlaying) {
|
||||||
|
float brightness = nucleus.outputValue.magnitude / maxValue;
|
||||||
|
Handles.color = new Color(brightness, brightness, brightness, 1f);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
Handles.color = Color.black;
|
||||||
|
}
|
||||||
|
Handles.DrawSolidDisc(position, Vector3.forward, size);
|
||||||
|
|
||||||
|
Handles.color = Color.white;
|
||||||
|
// Position the label in front of the disc
|
||||||
|
Vector3 labelPosition = position + (Vector3.forward * 0.1f);
|
||||||
|
|
||||||
|
GUIStyle style = new(EditorStyles.label) {
|
||||||
|
alignment = TextAnchor.MiddleCenter,
|
||||||
|
normal = { textColor = Color.white },
|
||||||
|
fontStyle = FontStyle.Bold,
|
||||||
|
};
|
||||||
|
if (nucleus is Perceptoid perceptoid) {
|
||||||
|
if (perceptoid.array == null || perceptoid.array.perceptei == null || perceptoid.array.perceptei.Length == 0)
|
||||||
|
perceptoid.array = new PercepteiArray(perceptoid);
|
||||||
|
|
||||||
|
if (perceptoid.array.perceptei.Length > 1) {
|
||||||
|
Handles.Label(labelPosition, perceptoid.array.perceptei.Length.ToString(), style);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
style.alignment = TextAnchor.UpperCenter;
|
||||||
|
Vector3 labelPos = position - Vector3.down * (size + 0.2f); // below disc along up axis
|
||||||
|
Handles.Label(labelPos, nucleus.name, style);
|
||||||
|
|
||||||
|
Rect neuronRect = new(position.x - size, position.y - size, size * 2, size * 2);
|
||||||
|
int id = GUIUtility.GetControlID(FocusType.Passive);
|
||||||
|
Event e = Event.current;
|
||||||
|
EventType et = e.GetTypeForControl(id);
|
||||||
|
if (e != null && neuronRect.Contains(e.mousePosition)) {
|
||||||
|
// Process Hover
|
||||||
|
HandleMouseHover(nucleus, neuronRect);
|
||||||
|
// Process click
|
||||||
|
if (e.type == EventType.MouseDown && e.button == 0) {
|
||||||
|
// Consume the event so the scene doesn't also handle it
|
||||||
|
e.Use();
|
||||||
|
HandleClicked(nucleus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawArray(PercepteiArray array, Vector3 position, float size) {
|
||||||
|
Vector3 offset = new(size / 4, size / 4, 0);
|
||||||
|
Handles.color = Color.black;
|
||||||
|
Handles.DrawSolidDisc(position, Vector3.forward, size);
|
||||||
|
|
||||||
|
GUIStyle style = new(EditorStyles.label) {
|
||||||
|
alignment = TextAnchor.UpperCenter,
|
||||||
|
normal = { textColor = Color.white },
|
||||||
|
fontStyle = FontStyle.Bold
|
||||||
|
};
|
||||||
|
Handles.Label(position, array.perceptei.Length.ToString(), style);
|
||||||
|
Vector3 labelPos = position - Vector3.down * (size + 0.2f); // below disc along up axis
|
||||||
|
Handles.Label(labelPos, array.name, style);
|
||||||
|
|
||||||
|
// To do: add HandleClick (see above) to expand the array
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleMouseHover(INucleus nucleus, Rect rect) {
|
||||||
|
GUIContent tooltip;
|
||||||
|
if (nucleus is Perceptoid perceptoid) {
|
||||||
|
if (perceptoid.receptor != null) {
|
||||||
|
tooltip = new(
|
||||||
|
$"{perceptoid.name}" +
|
||||||
|
$"\nType {perceptoid.receptor.thingType}" +
|
||||||
|
$" Thing {perceptoid.thingId}" +
|
||||||
|
$"\nValue: {nucleus.outputValue}");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
tooltip = new(
|
||||||
|
$"{perceptoid.name}" +
|
||||||
|
$"\nThing {perceptoid.thingId}" +
|
||||||
|
$"\nValue: {nucleus.outputValue}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
tooltip = new(
|
||||||
|
$"{nucleus.name}" +
|
||||||
|
$"\nsynapse count {nucleus.synapses.Count}" +
|
||||||
|
$"\nValue: {nucleus.outputValue}");
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector2 mousePosition = Event.current.mousePosition;
|
||||||
|
|
||||||
|
// Display tooltip with some offset
|
||||||
|
Vector2 tooltipSize = GUI.skin.box.CalcSize(tooltip);
|
||||||
|
Rect tooltipRect = new Rect(mousePosition.x + 10, mousePosition.y + 10, tooltipSize.x, tooltipSize.y);
|
||||||
|
|
||||||
|
GUI.Box(tooltipRect, tooltip);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void HandleClicked(INucleus nucleus) {
|
||||||
|
this.currentNucleus = nucleus;
|
||||||
|
BuildLayers();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DrawInspector(VisualElement inspectorContainer) {
|
||||||
|
if (inspectorContainer == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
inspectorContainer.Clear();
|
||||||
|
if (this.currentNucleus == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// create a SerializedObject wrapper so Unity inspector controls work (and Undo)
|
||||||
|
SerializedObject so = new(currentWrapper);
|
||||||
|
IMGUIContainer container = new(() => {
|
||||||
|
if (so.targetObject == null)
|
||||||
|
return;
|
||||||
|
so.Update();
|
||||||
|
|
||||||
|
if (this.currentNucleus == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
this.currentNucleus.name = EditorGUILayout.TextField(this.currentNucleus.name);
|
||||||
|
if (this.currentNucleus is Perceptoid perceptoid) {
|
||||||
|
perceptoid.receptor.thingType = EditorGUILayout.IntField("Thing Type", perceptoid.receptor.thingType);
|
||||||
|
|
||||||
|
if (perceptoid.array == null || perceptoid.array.perceptei == null || perceptoid.array.perceptei.Length == 0)
|
||||||
|
perceptoid.array = new PercepteiArray(perceptoid);
|
||||||
|
EditorGUILayout.BeginHorizontal();
|
||||||
|
EditorGUILayout.IntField("Array size", perceptoid.array.perceptei.Length);
|
||||||
|
if (GUILayout.Button("Add"))
|
||||||
|
perceptoid.array.AddPerceptoid();
|
||||||
|
if (GUILayout.Button("Del"))
|
||||||
|
perceptoid.array.RemovePerceptoid();
|
||||||
|
EditorGUILayout.EndHorizontal();
|
||||||
|
}
|
||||||
|
else if (this.currentNucleus is Neuroid neuroid) {
|
||||||
|
EditorGUILayout.BeginHorizontal();
|
||||||
|
EditorGUILayout.LabelField("Activation Curve", GUILayout.Width(150));
|
||||||
|
if (neuroid.curveMax > 0)
|
||||||
|
EditorGUILayout.CurveField(neuroid.curve, Color.cyan, new Rect(0, 0, 1, neuroid.curveMax));
|
||||||
|
else
|
||||||
|
EditorGUILayout.CurveField(neuroid.curve, Color.cyan, new Rect(0, neuroid.curveMax, 1, -neuroid.curveMax));
|
||||||
|
neuroid.curvePreset = (Neuroid.CurvePresets)EditorGUILayout.EnumPopup(neuroid.curvePreset, GUILayout.Width(100));
|
||||||
|
EditorGUILayout.EndHorizontal();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Application.isPlaying)
|
||||||
|
EditorGUILayout.FloatField("Output", this.currentNucleus.outputValue.magnitude);
|
||||||
|
else
|
||||||
|
EditorGUILayout.LabelField(" ");
|
||||||
|
|
||||||
|
if (this.currentNucleus.synapses.Count > 0) {
|
||||||
|
Synapse[] synapses = this.currentNucleus.synapses.ToArray();
|
||||||
|
foreach (Synapse synapse in synapses) {
|
||||||
|
if (synapse.nucleus != null) {
|
||||||
|
EditorGUILayout.Space();
|
||||||
|
|
||||||
|
EditorGUI.BeginDisabledGroup(synapse.nucleus.isSleeping);
|
||||||
|
if (Application.isPlaying)
|
||||||
|
EditorGUILayout.FloatField(synapse.nucleus.name, synapse.nucleus.outputValue.magnitude * synapse.weight);
|
||||||
|
else {
|
||||||
|
EditorGUILayout.BeginHorizontal();
|
||||||
|
EditorGUILayout.LabelField(synapse.nucleus.name);
|
||||||
|
// if (synapse.nucleus is Perceptoid perceptoid) {
|
||||||
|
// if (perceptoid.array == null || perceptoid.array.perceptei == null || perceptoid.array.perceptei.Length == 0) {
|
||||||
|
// perceptoid.array = new PercepteiArray(perceptoid);
|
||||||
|
// }
|
||||||
|
// EditorGUILayout.IntField(perceptoid.array.perceptei.Length);
|
||||||
|
// if (GUILayout.Button("Add"))
|
||||||
|
// perceptoid.array.AddPerceptoid();
|
||||||
|
// }
|
||||||
|
if (GUILayout.Button("Disconnect"))
|
||||||
|
synapse.nucleus.RemoveReceiver(this.currentNucleus);
|
||||||
|
EditorGUILayout.EndHorizontal();
|
||||||
|
}
|
||||||
|
|
||||||
|
EditorGUI.indentLevel++;
|
||||||
|
synapse.weight = EditorGUILayout.FloatField("Weight", synapse.weight);
|
||||||
|
EditorGUI.indentLevel--;
|
||||||
|
EditorGUI.EndDisabledGroup();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EditorGUILayout.Space();
|
||||||
|
|
||||||
|
ConnectNucleus(this.currentNucleus);
|
||||||
|
if (GUILayout.Button("Add Input Neuron"))
|
||||||
|
AddInputNeuron(this.currentNucleus);
|
||||||
|
// if (GUILayout.Button("Add Input Perceptoid"))
|
||||||
|
// AddPerceptoid(this.currentNucleus);
|
||||||
|
if (GUILayout.Button("Add Input Cluster"))
|
||||||
|
AddCluster(this.currentNucleus);
|
||||||
|
|
||||||
|
EditorGUILayout.Space();
|
||||||
|
|
||||||
|
if (GUILayout.Button("Delete this neuron"))
|
||||||
|
DeleteNeuron(this.currentNucleus);
|
||||||
|
|
||||||
|
//DisconnectNucleus(this.currentNucleus);
|
||||||
|
|
||||||
|
if (this.gameObject != null) {
|
||||||
|
Vector3 worldVector = this.gameObject.transform.TransformVector(this.currentNucleus.outputValue);
|
||||||
|
Debug.DrawRay(this.gameObject.transform.position, worldVector, Color.yellow);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
inspectorContainer.Add(container);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void AddInputNeuron(INucleus nucleus) {
|
||||||
|
Neuroid newNeuroid = new(this.brain.cluster, "New neuron");
|
||||||
|
newNeuroid.AddReceiver(nucleus);
|
||||||
|
this.currentNucleus = newNeuroid;
|
||||||
|
BuildLayers();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void DeleteNeuron(INucleus nucleus) {
|
||||||
|
if (nucleus == null)
|
||||||
|
return;
|
||||||
|
if (nucleus.cluster != null)
|
||||||
|
this.currentNucleus = nucleus.cluster.output;
|
||||||
|
foreach (Receiver receiver in nucleus.receivers) {
|
||||||
|
if (receiver.nucleus != null) {
|
||||||
|
this.currentNucleus = receiver.nucleus;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Nucleus.Delete(nucleus);
|
||||||
|
BuildLayers();
|
||||||
|
}
|
||||||
|
|
||||||
|
// protected virtual void AddPerceptoid(INucleus nucleus) {
|
||||||
|
// Perceptoid newPerceptoid = new(this.brain, 0, "New Perceptoid");
|
||||||
|
// newPerceptoid.AddReceiver(nucleus);
|
||||||
|
// this.currentNucleus = newPerceptoid;
|
||||||
|
// BuildLayers();
|
||||||
|
// }
|
||||||
|
|
||||||
|
protected virtual void AddCluster(INucleus nucleus) {
|
||||||
|
BrainPickerWindow.ShowPicker(brain => OnClusterPicked(nucleus, brain), "Select Cluster");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnClusterPicked(INucleus nucleus, NanoBrain brain) {
|
||||||
|
NanoBrain brainInstance = Instantiate(brain);
|
||||||
|
brainInstance.AddReceiver(nucleus);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void ConnectNucleus(INucleus nucleus) {
|
||||||
|
if (this.currentNucleus.cluster == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
IEnumerable<string> synapseNuclei = this.currentNucleus.synapses.Select(synapse => synapse.nucleus.name);
|
||||||
|
//IEnumerable<string> perceptei = this.currentNucleus.brain.perceptei.Select(i => i.name).Except(synapseNuclei);
|
||||||
|
IEnumerable<string> nuclei = this.currentNucleus.cluster.nuclei.Select(i => i.name).Except(synapseNuclei);
|
||||||
|
//string[] names = perceptei.Concat(nuclei).ToArray();
|
||||||
|
string[] names = nuclei.ToArray();
|
||||||
|
int selectedIndex = -1;
|
||||||
|
selectedIndex = EditorGUILayout.Popup("Connect to", selectedIndex, names);
|
||||||
|
if (selectedIndex >= 0) {
|
||||||
|
// if (selectedIndex < perceptei.Count()) {
|
||||||
|
// Nucleus n = this.currentNucleus.brain.perceptei[selectedIndex];
|
||||||
|
// n.AddReceiver(this.currentNucleus);
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// Nucleus n = this.currentNucleus.brain.nuclei[selectedIndex - perceptei.Count()];
|
||||||
|
// n.AddReceiver(this.currentNucleus);
|
||||||
|
// }
|
||||||
|
Nucleus n = this.currentNucleus.cluster.nuclei[selectedIndex];
|
||||||
|
n.AddReceiver(this.currentNucleus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void DisconnectNucleus(Nucleus nucleus) {
|
||||||
|
if (this.currentNucleus.cluster == null)
|
||||||
|
return;
|
||||||
|
string[] names = this.currentNucleus.synapses.Select(synapse => synapse.nucleus.name).ToArray();
|
||||||
|
int selectedIndex = -1;
|
||||||
|
selectedIndex = EditorGUILayout.Popup("Disconnect from", selectedIndex, names);
|
||||||
|
//if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.brain.perceptei.Count) {
|
||||||
|
if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.cluster.nuclei.Count) {
|
||||||
|
Synapse synapse = this.currentNucleus.synapses[selectedIndex];
|
||||||
|
synapse.nucleus.RemoveReceiver(this.currentNucleus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Start
|
||||||
|
|
||||||
|
#region Update
|
||||||
|
|
||||||
|
private void UpdateLayout(float containerWidth) {
|
||||||
|
if (containerWidth > 600f) {
|
||||||
|
mainContainer.style.flexDirection = FlexDirection.Row;
|
||||||
|
inspectorContainer.style.width = 300; // fixed sidebar width
|
||||||
|
inspectorContainer.style.flexGrow = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mainContainer.style.flexDirection = FlexDirection.Column;
|
||||||
|
inspectorContainer.style.width = Length.Percent(100); // full width below
|
||||||
|
inspectorContainer.style.flexDirection = FlexDirection.Column;
|
||||||
|
inspectorContainer.style.flexGrow = 1; // can set 0 or keep as needed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Update
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NeuroidLayer {
|
||||||
|
public int ix = 0;
|
||||||
|
public List<INucleus> neuroids = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ClusterWrapper : ScriptableObject {
|
||||||
|
// expose fields that map to GraphNode
|
||||||
|
//public string title;
|
||||||
|
public Vector2 position;
|
||||||
|
INucleus node;
|
||||||
|
Cluster graph; // needed to write back and mark dirty
|
||||||
|
|
||||||
|
public ClusterWrapper Init(INucleus node, Cluster graphAsset) {
|
||||||
|
this.node = node;
|
||||||
|
this.graph = graphAsset;
|
||||||
|
//this.title = " A " + node.name;
|
||||||
|
//position = node.position;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
void OnValidate() {
|
||||||
|
if (node != null) {
|
||||||
|
//node.name = title;
|
||||||
|
//node.position = position;
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
if (graph != null)
|
||||||
|
UnityEditor.EditorUtility.SetDirty(graph);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1fc1fb7db9f7ad54a87d31313e7f457d
|
||||||
@ -1,103 +1,103 @@
|
|||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEditor.UIElements;
|
using UnityEditor.UIElements;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
|
|
||||||
[CustomEditor(typeof(NanoBrainComponent))]
|
[CustomEditor(typeof(NanoBrainComponent))]
|
||||||
public class NanoBrainComponent_Editor : Editor {
|
public class NanoBrainComponent_Editor : Editor {
|
||||||
protected static VisualElement mainContainer;
|
protected static VisualElement mainContainer;
|
||||||
protected static VisualElement inspectorContainer;
|
protected static VisualElement inspectorContainer;
|
||||||
|
|
||||||
protected NanoBrainComponent component;
|
protected NanoBrainComponent component;
|
||||||
private SerializedProperty brainProp;
|
private SerializedProperty brainProp;
|
||||||
|
|
||||||
public void OnEnable() {
|
public void OnEnable() {
|
||||||
component = target as NanoBrainComponent;
|
component = target as NanoBrainComponent;
|
||||||
|
|
||||||
if (Application.isPlaying == false)
|
if (Application.isPlaying == false)
|
||||||
brainProp = serializedObject.FindProperty(nameof(NanoBrainComponent.defaultBrain));
|
brainProp = serializedObject.FindProperty(nameof(NanoBrainComponent.defaultBrain));
|
||||||
}
|
}
|
||||||
|
|
||||||
public override VisualElement CreateInspectorGUI() {
|
public override VisualElement CreateInspectorGUI() {
|
||||||
//NanoBrainComponent component = target as NanoBrainComponent;
|
//NanoBrainComponent component = target as NanoBrainComponent;
|
||||||
NanoBrain brain = Application.isPlaying ? component.brain : component.defaultBrain;
|
NanoBrain brain = Application.isPlaying ? component.brain : component.defaultBrain;
|
||||||
|
|
||||||
if (Application.isPlaying == false)
|
if (Application.isPlaying == false)
|
||||||
serializedObject.Update();
|
serializedObject.Update();
|
||||||
|
|
||||||
|
|
||||||
VisualElement root = new();
|
VisualElement root = new();
|
||||||
root.style.flexDirection = FlexDirection.Column; // side-by-side layout
|
root.style.flexDirection = FlexDirection.Column; // side-by-side layout
|
||||||
root.style.flexGrow = 1;
|
root.style.flexGrow = 1;
|
||||||
root.style.minHeight = 600;
|
root.style.minHeight = 600;
|
||||||
root.style.paddingLeft = 0;
|
root.style.paddingLeft = 0;
|
||||||
root.style.paddingRight = 0;
|
root.style.paddingRight = 0;
|
||||||
root.style.paddingTop = 0;
|
root.style.paddingTop = 0;
|
||||||
root.style.paddingBottom = 0;
|
root.style.paddingBottom = 0;
|
||||||
|
|
||||||
root.styleSheets.Add(Resources.Load<StyleSheet>("GraphStyles"));
|
root.styleSheets.Add(Resources.Load<StyleSheet>("GraphStyles"));
|
||||||
|
|
||||||
if (Application.isPlaying == false) {
|
if (Application.isPlaying == false) {
|
||||||
PropertyField brainField = new(brainProp) {
|
PropertyField brainField = new(brainProp) {
|
||||||
label = "Nano Brain"
|
label = "Nano Brain"
|
||||||
};
|
};
|
||||||
root.Add(brainField);
|
root.Add(brainField);
|
||||||
}
|
}
|
||||||
|
|
||||||
mainContainer = new() {
|
mainContainer = new() {
|
||||||
name = "main",
|
name = "main",
|
||||||
style = {
|
style = {
|
||||||
flexDirection = FlexDirection.Row,
|
flexDirection = FlexDirection.Row,
|
||||||
flexGrow = 1,
|
flexGrow = 1,
|
||||||
minHeight = 500,
|
minHeight = 500,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
NanoBrainInspector.GraphView board;
|
NanoBrainInspector.GraphView board;
|
||||||
board = new NanoBrainInspector.GraphView();
|
board = new NanoBrainInspector.GraphView();
|
||||||
board.style.flexGrow = 1;
|
board.style.flexGrow = 1;
|
||||||
|
|
||||||
inspectorContainer = new VisualElement {
|
inspectorContainer = new VisualElement {
|
||||||
name = "inspector",
|
name = "inspector",
|
||||||
style = {
|
style = {
|
||||||
width = 400,
|
width = 400,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
mainContainer.Add(board);
|
mainContainer.Add(board);
|
||||||
mainContainer.Add(inspectorContainer);
|
mainContainer.Add(inspectorContainer);
|
||||||
root.Add(mainContainer);
|
root.Add(mainContainer);
|
||||||
|
|
||||||
// Run once for initial state (use resolved style width if available)
|
// Run once for initial state (use resolved style width if available)
|
||||||
float initialWidth = root.layout.width > 0 ? root.layout.width : root.contentRect.width;
|
float initialWidth = root.layout.width > 0 ? root.layout.width : root.contentRect.width;
|
||||||
UpdateLayout(initialWidth);
|
UpdateLayout(initialWidth);
|
||||||
|
|
||||||
// React to size changes of root (or parent if appropriate)
|
// React to size changes of root (or parent if appropriate)
|
||||||
root.RegisterCallback<GeometryChangedEvent>(evt => {
|
root.RegisterCallback<GeometryChangedEvent>(evt => {
|
||||||
UpdateLayout(evt.newRect.width);
|
UpdateLayout(evt.newRect.width);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (brain != null)
|
if (brain != null)
|
||||||
board.SetGraph(component.gameObject, brain, brain.root, inspectorContainer);
|
board.SetGraph(component.gameObject, brain, brain.output, inspectorContainer);
|
||||||
// else
|
// else
|
||||||
// Debug.LogWarning(" No brain!");
|
// Debug.LogWarning(" No brain!");
|
||||||
|
|
||||||
if (Application.isPlaying == false)
|
if (Application.isPlaying == false)
|
||||||
serializedObject.ApplyModifiedProperties();
|
serializedObject.ApplyModifiedProperties();
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateLayout(float containerWidth) {
|
private void UpdateLayout(float containerWidth) {
|
||||||
if (containerWidth > 800f) {
|
if (containerWidth > 800f) {
|
||||||
mainContainer.style.flexDirection = FlexDirection.Row;
|
mainContainer.style.flexDirection = FlexDirection.Row;
|
||||||
inspectorContainer.style.width = 400; // fixed sidebar width
|
inspectorContainer.style.width = 400; // fixed sidebar width
|
||||||
inspectorContainer.style.flexGrow = 0;
|
inspectorContainer.style.flexGrow = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
mainContainer.style.flexDirection = FlexDirection.Column;
|
mainContainer.style.flexDirection = FlexDirection.Column;
|
||||||
inspectorContainer.style.width = Length.Percent(100); // full width below
|
inspectorContainer.style.width = Length.Percent(100); // full width below
|
||||||
inspectorContainer.style.flexDirection = FlexDirection.Column;
|
inspectorContainer.style.flexDirection = FlexDirection.Column;
|
||||||
inspectorContainer.style.flexGrow = 1; // can set 0 or keep as needed
|
inspectorContainer.style.flexGrow = 1; // can set 0 or keep as needed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -59,7 +59,7 @@ public class NanoBrainInspector : Editor {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (brain != null)
|
if (brain != null)
|
||||||
graph.SetGraph(null, brain, brain.root, inspectorContainer);
|
graph.SetGraph(null, brain, brain.output, inspectorContainer);
|
||||||
else
|
else
|
||||||
Debug.LogWarning(" No brain!");
|
Debug.LogWarning(" No brain!");
|
||||||
|
|
||||||
@ -70,10 +70,10 @@ public class NanoBrainInspector : Editor {
|
|||||||
public class GraphView : VisualElement {
|
public class GraphView : VisualElement {
|
||||||
NanoBrain brain;
|
NanoBrain brain;
|
||||||
SerializedObject serializedBrain;
|
SerializedObject serializedBrain;
|
||||||
Nucleus currentNucleus;
|
INucleus currentNucleus;
|
||||||
GameObject gameObject;
|
GameObject gameObject;
|
||||||
private List<NeuroidLayer> layers = new();
|
private List<NeuroidLayer> layers = new();
|
||||||
private readonly Dictionary<Nucleus, Vector2Int> neuroidPositions = new();
|
private readonly Dictionary<INucleus, Vector2Int> neuroidPositions = new();
|
||||||
|
|
||||||
Vector2 pan = Vector2.zero;
|
Vector2 pan = Vector2.zero;
|
||||||
//float zoom = 1f;
|
//float zoom = 1f;
|
||||||
@ -127,14 +127,14 @@ public class NanoBrainInspector : Editor {
|
|||||||
this.layers = new();
|
this.layers = new();
|
||||||
int layerIx = 0;
|
int layerIx = 0;
|
||||||
|
|
||||||
Nucleus selectedNucleus = this.currentNucleus;
|
INucleus selectedNucleus = this.currentNucleus;
|
||||||
if (selectedNucleus == null)
|
if (selectedNucleus == null)
|
||||||
return;
|
return;
|
||||||
NeuroidLayer currentLayer = new() { ix = layerIx };
|
NeuroidLayer currentLayer = new() { ix = layerIx };
|
||||||
|
|
||||||
if (selectedNucleus.receivers != null) {
|
if (selectedNucleus.receivers != null) {
|
||||||
foreach (Receiver receiver in selectedNucleus.receivers) {
|
foreach (Receiver receiver in selectedNucleus.receivers) {
|
||||||
Nucleus outputNeuroid = receiver.nucleus;
|
INucleus outputNeuroid = receiver.nucleus;
|
||||||
if (outputNeuroid != null) {
|
if (outputNeuroid != null) {
|
||||||
AddToLayer(currentLayer, outputNeuroid);
|
AddToLayer(currentLayer, outputNeuroid);
|
||||||
// Debug.Log($"layer {layerIx} nucleus {outputNeuroid.name}");
|
// Debug.Log($"layer {layerIx} nucleus {outputNeuroid.name}");
|
||||||
@ -156,7 +156,7 @@ public class NanoBrainInspector : Editor {
|
|||||||
|
|
||||||
if (selectedNucleus.synapses != null) {
|
if (selectedNucleus.synapses != null) {
|
||||||
foreach (Synapse synapse in selectedNucleus.synapses) {
|
foreach (Synapse synapse in selectedNucleus.synapses) {
|
||||||
Nucleus input = synapse.nucleus;
|
INucleus input = synapse.nucleus;
|
||||||
AddToLayer(currentLayer, input);
|
AddToLayer(currentLayer, input);
|
||||||
// Debug.Log($"layer {layerIx} nucleus {input.name}");
|
// Debug.Log($"layer {layerIx} nucleus {input.name}");
|
||||||
}
|
}
|
||||||
@ -166,11 +166,11 @@ public class NanoBrainInspector : Editor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddToLayer(NeuroidLayer layer, Nucleus nucleus) {
|
private void AddToLayer(NeuroidLayer layer, INucleus nucleus) {
|
||||||
if (nucleus == null)
|
if (nucleus == null)
|
||||||
return;
|
return;
|
||||||
layer.neuroids.Add(nucleus);
|
layer.neuroids.Add(nucleus);
|
||||||
nucleus.layerIx = layer.ix;
|
//nucleus.layerIx = layer.ix;
|
||||||
// Store its position
|
// Store its position
|
||||||
Vector2Int neuroidPosition = new(layer.ix, layer.neuroids.Count - 1);
|
Vector2Int neuroidPosition = new(layer.ix, layer.neuroids.Count - 1);
|
||||||
neuroidPositions[nucleus] = neuroidPosition;
|
neuroidPositions[nucleus] = neuroidPosition;
|
||||||
@ -217,7 +217,7 @@ public class NanoBrainInspector : Editor {
|
|||||||
DrawNucleus(this.currentNucleus, position, this.currentNucleus.outputValue.magnitude, 20);
|
DrawNucleus(this.currentNucleus, position, this.currentNucleus.outputValue.magnitude, 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawReceivers(Nucleus nucleus, Vector3 parentPos, float size) {
|
private void DrawReceivers(INucleus nucleus, Vector3 parentPos, float size) {
|
||||||
int nodeCount = nucleus.receivers.Count;
|
int nodeCount = nucleus.receivers.Count;
|
||||||
|
|
||||||
// Determine the maximum value in this layer
|
// Determine the maximum value in this layer
|
||||||
@ -237,7 +237,7 @@ public class NanoBrainInspector : Editor {
|
|||||||
|
|
||||||
int row = 0;
|
int row = 0;
|
||||||
foreach (Receiver receiver in nucleus.receivers) {
|
foreach (Receiver receiver in nucleus.receivers) {
|
||||||
Nucleus receiverNucleus = receiver.nucleus;
|
INucleus receiverNucleus = receiver.nucleus;
|
||||||
if (receiverNucleus == null)
|
if (receiverNucleus == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -250,7 +250,7 @@ public class NanoBrainInspector : Editor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawSynapses(Nucleus nucleus, Vector3 parentPos, float size) {
|
private void DrawSynapses(INucleus nucleus, Vector3 parentPos, float size) {
|
||||||
int nodeCount = nucleus.synapses.Count;
|
int nodeCount = nucleus.synapses.Count;
|
||||||
|
|
||||||
// Determine the maximum value in this layer
|
// Determine the maximum value in this layer
|
||||||
@ -290,7 +290,7 @@ public class NanoBrainInspector : Editor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DrawNucleus(Nucleus nucleus, Vector3 position, float maxValue, float size) {
|
private void DrawNucleus(INucleus nucleus, Vector3 position, float maxValue, float size) {
|
||||||
if (nucleus.isSleeping)
|
if (nucleus.isSleeping)
|
||||||
Handles.color = Color.darkRed;
|
Handles.color = Color.darkRed;
|
||||||
else {
|
else {
|
||||||
@ -358,7 +358,7 @@ public class NanoBrainInspector : Editor {
|
|||||||
// To do: add HandleClick (see above) to expand the array
|
// To do: add HandleClick (see above) to expand the array
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleMouseHover(Nucleus nucleus, Rect rect) {
|
private void HandleMouseHover(INucleus nucleus, Rect rect) {
|
||||||
GUIContent tooltip;
|
GUIContent tooltip;
|
||||||
if (nucleus is Perceptoid perceptoid) {
|
if (nucleus is Perceptoid perceptoid) {
|
||||||
if (perceptoid.receptor != null) {
|
if (perceptoid.receptor != null) {
|
||||||
@ -391,7 +391,7 @@ public class NanoBrainInspector : Editor {
|
|||||||
GUI.Box(tooltipRect, tooltip);
|
GUI.Box(tooltipRect, tooltip);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleClicked(Nucleus nucleus) {
|
private void HandleClicked(INucleus nucleus) {
|
||||||
this.currentNucleus = nucleus;
|
this.currentNucleus = nucleus;
|
||||||
BuildLayers();
|
BuildLayers();
|
||||||
}
|
}
|
||||||
@ -484,6 +484,8 @@ public class NanoBrainInspector : Editor {
|
|||||||
AddInputNeuron(this.currentNucleus);
|
AddInputNeuron(this.currentNucleus);
|
||||||
if (GUILayout.Button("Add Input Perceptoid"))
|
if (GUILayout.Button("Add Input Perceptoid"))
|
||||||
AddPerceptoid(this.currentNucleus);
|
AddPerceptoid(this.currentNucleus);
|
||||||
|
if (GUILayout.Button("Add Input Cluster"))
|
||||||
|
AddCluster(this.currentNucleus);
|
||||||
|
|
||||||
EditorGUILayout.Space();
|
EditorGUILayout.Space();
|
||||||
|
|
||||||
@ -501,18 +503,18 @@ public class NanoBrainInspector : Editor {
|
|||||||
inspectorContainer.Add(container);
|
inspectorContainer.Add(container);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void AddInputNeuron(Nucleus nucleus) {
|
protected virtual void AddInputNeuron(INucleus nucleus) {
|
||||||
Neuroid newNeuroid = new(this.brain, "New neuron");
|
Neuroid newNeuroid = new(this.brain.cluster, "New neuron");
|
||||||
newNeuroid.AddReceiver(nucleus);
|
newNeuroid.AddReceiver(nucleus);
|
||||||
this.currentNucleus = newNeuroid;
|
this.currentNucleus = newNeuroid;
|
||||||
BuildLayers();
|
BuildLayers();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void DeleteNeuron(Nucleus nucleus) {
|
protected virtual void DeleteNeuron(INucleus nucleus) {
|
||||||
if (nucleus == null)
|
if (nucleus == null)
|
||||||
return;
|
return;
|
||||||
if (nucleus.brain != null)
|
if (nucleus.cluster != null)
|
||||||
this.currentNucleus = nucleus.brain.root;
|
this.currentNucleus = nucleus.cluster.output;
|
||||||
foreach (Receiver receiver in nucleus.receivers) {
|
foreach (Receiver receiver in nucleus.receivers) {
|
||||||
if (receiver.nucleus != null) {
|
if (receiver.nucleus != null) {
|
||||||
this.currentNucleus = receiver.nucleus;
|
this.currentNucleus = receiver.nucleus;
|
||||||
@ -523,42 +525,55 @@ public class NanoBrainInspector : Editor {
|
|||||||
BuildLayers();
|
BuildLayers();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void AddPerceptoid(Nucleus nucleus) {
|
protected virtual void AddPerceptoid(INucleus nucleus) {
|
||||||
Perceptoid newPerceptoid = new(this.brain, 0, "New Perceptoid");
|
Perceptoid newPerceptoid = new(this.brain, 0, "New Perceptoid");
|
||||||
newPerceptoid.AddReceiver(nucleus);
|
newPerceptoid.AddReceiver(nucleus);
|
||||||
this.currentNucleus = newPerceptoid;
|
this.currentNucleus = newPerceptoid;
|
||||||
BuildLayers();
|
BuildLayers();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void ConnectNucleus(Nucleus nucleus) {
|
protected virtual void AddCluster(INucleus nucleus) {
|
||||||
if (this.currentNucleus.brain == null)
|
BrainPickerWindow.ShowPicker(brain => OnClusterPicked(nucleus, brain), "Select Cluster");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnClusterPicked(INucleus nucleus, NanoBrain brain) {
|
||||||
|
NanoBrain brainInstance = Instantiate(brain);
|
||||||
|
brainInstance.AddReceiver(nucleus);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual void ConnectNucleus(INucleus nucleus) {
|
||||||
|
if (this.currentNucleus.cluster == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
IEnumerable<string> synapseNuclei = this.currentNucleus.synapses.Select(synapse => synapse.nucleus.name);
|
IEnumerable<string> synapseNuclei = this.currentNucleus.synapses.Select(synapse => synapse.nucleus.name);
|
||||||
IEnumerable<string> perceptei = this.currentNucleus.brain.perceptei.Select(i => i.name).Except(synapseNuclei);
|
//IEnumerable<string> perceptei = this.currentNucleus.brain.perceptei.Select(i => i.name).Except(synapseNuclei);
|
||||||
IEnumerable<string> nuclei = this.currentNucleus.brain.nuclei.Select(i => i.name).Except(synapseNuclei);
|
IEnumerable<string> nuclei = this.currentNucleus.cluster.nuclei.Select(i => i.name).Except(synapseNuclei);
|
||||||
string[] names = perceptei.Concat(nuclei).ToArray();
|
//string[] names = perceptei.Concat(nuclei).ToArray();
|
||||||
|
string[] names = nuclei.ToArray();
|
||||||
int selectedIndex = -1;
|
int selectedIndex = -1;
|
||||||
selectedIndex = EditorGUILayout.Popup("Connect to", selectedIndex, names);
|
selectedIndex = EditorGUILayout.Popup("Connect to", selectedIndex, names);
|
||||||
if (selectedIndex >= 0) {
|
if (selectedIndex >= 0) {
|
||||||
if (selectedIndex < perceptei.Count()) {
|
// if (selectedIndex < perceptei.Count()) {
|
||||||
Nucleus n = this.currentNucleus.brain.perceptei[selectedIndex];
|
// Nucleus n = this.currentNucleus.brain.perceptei[selectedIndex];
|
||||||
|
// n.AddReceiver(this.currentNucleus);
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// Nucleus n = this.currentNucleus.brain.nuclei[selectedIndex - perceptei.Count()];
|
||||||
|
// n.AddReceiver(this.currentNucleus);
|
||||||
|
// }
|
||||||
|
Nucleus n = this.currentNucleus.cluster.nuclei[selectedIndex];
|
||||||
n.AddReceiver(this.currentNucleus);
|
n.AddReceiver(this.currentNucleus);
|
||||||
}
|
|
||||||
else {
|
|
||||||
Nucleus n = this.currentNucleus.brain.nuclei[selectedIndex - perceptei.Count()];
|
|
||||||
n.AddReceiver(this.currentNucleus);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void DisconnectNucleus(Nucleus nucleus) {
|
protected virtual void DisconnectNucleus(Nucleus nucleus) {
|
||||||
if (this.currentNucleus.brain == null)
|
if (this.currentNucleus.cluster == null)
|
||||||
return;
|
return;
|
||||||
string[] names = this.currentNucleus.synapses.Select(synapse => synapse.nucleus.name).ToArray();
|
string[] names = this.currentNucleus.synapses.Select(synapse => synapse.nucleus.name).ToArray();
|
||||||
int selectedIndex = -1;
|
int selectedIndex = -1;
|
||||||
selectedIndex = EditorGUILayout.Popup("Disconnect from", selectedIndex, names);
|
selectedIndex = EditorGUILayout.Popup("Disconnect from", selectedIndex, names);
|
||||||
if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.brain.perceptei.Count) {
|
//if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.brain.perceptei.Count) {
|
||||||
|
if (selectedIndex >= 0 && selectedIndex < this.currentNucleus.cluster.nuclei.Count) {
|
||||||
Synapse synapse = this.currentNucleus.synapses[selectedIndex];
|
Synapse synapse = this.currentNucleus.synapses[selectedIndex];
|
||||||
synapse.nucleus.RemoveReceiver(this.currentNucleus);
|
synapse.nucleus.RemoveReceiver(this.currentNucleus);
|
||||||
}
|
}
|
||||||
@ -586,23 +601,30 @@ public class NanoBrainInspector : Editor {
|
|||||||
#endregion Update
|
#endregion Update
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
public class NeuroidLayer {
|
||||||
|
public int ix = 0;
|
||||||
|
public List<INucleus> neuroids = new();
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
public class GraphNodeWrapper : ScriptableObject {
|
public class GraphNodeWrapper : ScriptableObject {
|
||||||
// expose fields that map to GraphNode
|
// expose fields that map to GraphNode
|
||||||
public string title;
|
//public string title;
|
||||||
public Vector2 position;
|
public Vector2 position;
|
||||||
Nucleus node;
|
INucleus node;
|
||||||
NanoBrain graph; // needed to write back and mark dirty
|
NanoBrain graph; // needed to write back and mark dirty
|
||||||
|
|
||||||
public GraphNodeWrapper Init(Nucleus node, NanoBrain graphAsset) {
|
public GraphNodeWrapper Init(INucleus node, NanoBrain graphAsset) {
|
||||||
this.node = node;
|
this.node = node;
|
||||||
this.graph = graphAsset;
|
this.graph = graphAsset;
|
||||||
this.title = " A " + node.name;
|
//this.title = " A " + node.name;
|
||||||
//position = node.position;
|
//position = node.position;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
void OnValidate() {
|
void OnValidate() {
|
||||||
if (node != null) {
|
if (node != null) {
|
||||||
node.name = title;
|
//node.name = title;
|
||||||
//node.position = position;
|
//node.position = position;
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
if (graph != null)
|
if (graph != null)
|
||||||
@ -610,4 +632,4 @@ public class GraphNodeWrapper : ScriptableObject {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,92 +1,95 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
[CreateAssetMenu(menuName = "Passer/NanoBrain")]
|
[CreateAssetMenu(menuName = "Passer/NanoBrain")]
|
||||||
public class NanoBrain : ScriptableObject, ISerializationCallbackReceiver {
|
public class NanoBrain : ScriptableObject, ISerializationCallbackReceiver {
|
||||||
|
public List<Neuroid> nuclei = new();
|
||||||
public string title;
|
public List<Perceptoid> perceptei = new();
|
||||||
public int count;
|
public List<Receptor> receptors = new();
|
||||||
public Color color = Color.white;
|
|
||||||
public Texture2D texture;
|
public Cluster cluster;
|
||||||
|
|
||||||
public List<Neuroid> nuclei = new();
|
// This is probably always the first element in the nuclei list...
|
||||||
public List<Perceptoid> perceptei = new();
|
[System.NonSerialized]
|
||||||
public List<Receptor> receptors = new();
|
public Nucleus output;
|
||||||
|
public int rootId;
|
||||||
// This is probably always the first element in the nuclei list...
|
|
||||||
[System.NonSerialized]
|
public NanoBrain() {
|
||||||
public Nucleus root;
|
this.output = new Neuroid(this.cluster, "Root");
|
||||||
public int rootId;
|
this.cluster = new();
|
||||||
|
}
|
||||||
public NanoBrain() {
|
|
||||||
this.root = new Neuroid(this, "Root");
|
public void AddReceiver(INucleus receiver) {
|
||||||
}
|
output.AddReceiver(receiver);
|
||||||
|
}
|
||||||
public Neuroid AddNeuron(string name) {
|
|
||||||
Neuroid neuroid = new(this, name);
|
public Neuroid AddNeuron(string name) {
|
||||||
return neuroid;
|
Neuroid neuroid = new(this.cluster, name);
|
||||||
}
|
return neuroid;
|
||||||
|
}
|
||||||
public void UpdateNuclei() {
|
|
||||||
foreach (Nucleus nucleus in nuclei)
|
public void UpdateNuclei() {
|
||||||
nucleus.IncreaseAge();
|
foreach (Nucleus nucleus in nuclei)
|
||||||
foreach (Perceptoid perception in perceptei)
|
nucleus.IncreaseAge();
|
||||||
perception.IncreaseAge();
|
foreach (Perceptoid perception in perceptei)
|
||||||
}
|
perception.IncreaseAge();
|
||||||
|
}
|
||||||
public void OnBeforeSerialize() {
|
|
||||||
this.rootId = root.id;
|
public void OnBeforeSerialize() {
|
||||||
}
|
this.rootId = output.id;
|
||||||
public void OnAfterDeserialize() {
|
}
|
||||||
try {
|
public void OnAfterDeserialize() {
|
||||||
foreach (Nucleus nucleus in this.nuclei.ToArray()) {
|
try {
|
||||||
if (this.rootId == nucleus.id)
|
foreach (Nucleus nucleus in this.nuclei.ToArray()) {
|
||||||
this.root = nucleus;
|
if (this.rootId == nucleus.id)
|
||||||
nucleus.Rebuild(this);
|
this.output = nucleus;
|
||||||
}
|
nucleus.Rebuild(this);
|
||||||
|
}
|
||||||
foreach (Perceptoid perceptoid in this.perceptei.ToArray())
|
|
||||||
perceptoid.Rebuild(this);
|
foreach (Perceptoid perceptoid in this.perceptei.ToArray())
|
||||||
}
|
perceptoid.Rebuild(this);
|
||||||
catch (System.Exception) { }
|
}
|
||||||
this.GarbageCollection();
|
catch (System.Exception) { }
|
||||||
}
|
this.cluster.GarbageCollection();
|
||||||
|
}
|
||||||
public void GarbageCollection() {
|
|
||||||
HashSet<Nucleus> visitedNuclei = new();
|
/*
|
||||||
MarkNuclei(visitedNuclei, this.root);
|
public void GarbageCollection() {
|
||||||
//Debug.Log($"Garbage collection found {visitedNuclei.Count} Nuclei");
|
HashSet<INucleus> visitedNuclei = new();
|
||||||
this.nuclei.RemoveAll(nucleus => visitedNuclei.Contains(nucleus) == false);
|
MarkNuclei(visitedNuclei, this.output);
|
||||||
this.perceptei.RemoveAll(perceptoid => visitedNuclei.Contains(perceptoid) == false);
|
//Debug.Log($"Garbage collection found {visitedNuclei.Count} Nuclei");
|
||||||
}
|
this.nuclei.RemoveAll(nucleus => visitedNuclei.Contains(nucleus) == false);
|
||||||
|
this.perceptei.RemoveAll(perceptoid => visitedNuclei.Contains(perceptoid) == false);
|
||||||
public void MarkNuclei(HashSet<Nucleus> visitedNuclei, Nucleus nucleus) {
|
}
|
||||||
if (nucleus is null)
|
|
||||||
return;
|
public void MarkNuclei(HashSet<INucleus> visitedNuclei, INucleus nucleus) {
|
||||||
|
if (nucleus is null)
|
||||||
if (nucleus.brain == null)
|
return;
|
||||||
nucleus.brain = this;
|
|
||||||
|
if (nucleus.brain == null)
|
||||||
visitedNuclei.Add(nucleus);
|
nucleus.brain = this;
|
||||||
if (nucleus.synapses != null) {
|
|
||||||
HashSet<Synapse> visitedSynapses = new();
|
visitedNuclei.Add(nucleus);
|
||||||
foreach (Synapse synapse in nucleus.synapses) {
|
if (nucleus.synapses != null) {
|
||||||
if (synapse != null && synapse.nucleus != null) {
|
HashSet<Synapse> visitedSynapses = new();
|
||||||
visitedSynapses.Add(synapse);
|
foreach (Synapse synapse in nucleus.synapses) {
|
||||||
MarkNuclei(visitedNuclei, synapse.nucleus);
|
if (synapse != null && synapse.nucleus != null) {
|
||||||
}
|
visitedSynapses.Add(synapse);
|
||||||
}
|
MarkNuclei(visitedNuclei, synapse.nucleus);
|
||||||
nucleus.synapses.RemoveAll(synapse => visitedSynapses.Contains(synapse) == false);
|
}
|
||||||
}
|
}
|
||||||
if (nucleus.receivers != null) {
|
nucleus.synapses.RemoveAll(synapse => visitedSynapses.Contains(synapse) == false);
|
||||||
HashSet<Receiver> visitedReceivers = new();
|
}
|
||||||
foreach (Receiver receiver in nucleus.receivers) {
|
if (nucleus.receivers != null) {
|
||||||
if (receiver != null && receiver.nucleus != null) {
|
HashSet<Receiver> visitedReceivers = new();
|
||||||
visitedReceivers.Add(receiver);
|
foreach (Receiver receiver in nucleus.receivers) {
|
||||||
visitedNuclei.Add(receiver.nucleus);
|
if (receiver != null && receiver.nucleus != null) {
|
||||||
}
|
visitedReceivers.Add(receiver);
|
||||||
}
|
visitedNuclei.Add(receiver.nucleus);
|
||||||
nucleus.receivers.RemoveAll(receiver => visitedReceivers.Contains(receiver) == false);
|
}
|
||||||
}
|
}
|
||||||
}
|
nucleus.receivers.RemoveAll(receiver => visitedReceivers.Contains(receiver) == false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
@ -1,33 +1,33 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class NanoBrainComponent : MonoBehaviour {
|
public class NanoBrainComponent : MonoBehaviour {
|
||||||
public NanoBrain defaultBrain;
|
public NanoBrain defaultBrain;
|
||||||
private NanoBrain brainInstance;
|
private NanoBrain brainInstance;
|
||||||
|
|
||||||
public Nucleus root => brainInstance.root;
|
public Nucleus root => brainInstance.output;
|
||||||
public NanoBrain brain {
|
public NanoBrain brain {
|
||||||
get {
|
get {
|
||||||
if (brainInstance == null && defaultBrain != null) {
|
if (brainInstance == null && defaultBrain != null) {
|
||||||
brainInstance = Instantiate(defaultBrain);
|
brainInstance = Instantiate(defaultBrain);
|
||||||
brainInstance.name = defaultBrain.name + " (Instance)";
|
brainInstance.name = defaultBrain.name + " (Instance)";
|
||||||
|
|
||||||
SwarmControl sc = FindFirstObjectByType<SwarmControl>();
|
SwarmControl sc = FindFirstObjectByType<SwarmControl>();
|
||||||
UpdateWeight(brainInstance, "Avoidance", sc.avoidanceForce);
|
UpdateWeight(brainInstance, "Avoidance", sc.avoidanceForce);
|
||||||
UpdateWeight(brainInstance, "Cohesion", sc.cohesionForce);
|
UpdateWeight(brainInstance, "Cohesion", sc.cohesionForce);
|
||||||
UpdateWeight(brainInstance, "Separation", sc.separationForce);
|
UpdateWeight(brainInstance, "Separation", sc.separationForce);
|
||||||
UpdateWeight(brainInstance, "Alignment", sc.alignmentForce);
|
UpdateWeight(brainInstance, "Alignment", sc.alignmentForce);
|
||||||
}
|
}
|
||||||
return brainInstance;
|
return brainInstance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void UpdateWeight(NanoBrain brain, string name, float weight) {
|
public static void UpdateWeight(NanoBrain brain, string name, float weight) {
|
||||||
Nucleus root = brain.root;
|
Nucleus root = brain.output;
|
||||||
foreach (Synapse synapse in root.synapses) {
|
foreach (Synapse synapse in root.synapses) {
|
||||||
if (synapse.nucleus.name == name) {
|
if (synapse.nucleus.name == name) {
|
||||||
synapse.weight = weight;
|
synapse.weight = weight;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
20
Assets/Scenes/Boids/New Cluster 1.asset
Normal file
20
Assets/Scenes/Boids/New Cluster 1.asset
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
%YAML 1.1
|
||||||
|
%TAG !u! tag:unity3d.com,2011:
|
||||||
|
--- !u!114 &11400000
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 0}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: 60a957541c24c57e78018c202ebb1d9b, type: 3}
|
||||||
|
m_Name: New Cluster 1
|
||||||
|
m_EditorClassIdentifier: Assembly-CSharp::Cluster
|
||||||
|
nuclei:
|
||||||
|
- id: 949579472
|
||||||
|
_name: Output
|
||||||
|
_synapses: []
|
||||||
|
_receivers: []
|
||||||
|
nucleusType:
|
||||||
@ -1,5 +1,5 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: 5040f18b0515ba23eb0782d6f6794054
|
guid: ad89de17be687dbc18a57252cadda0f3
|
||||||
NativeFormatImporter:
|
NativeFormatImporter:
|
||||||
externalObjects: {}
|
externalObjects: {}
|
||||||
mainObjectFileID: 11400000
|
mainObjectFileID: 11400000
|
||||||
15
Assets/Scenes/Boids/New Cluster.asset
Normal file
15
Assets/Scenes/Boids/New Cluster.asset
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
%YAML 1.1
|
||||||
|
%TAG !u! tag:unity3d.com,2011:
|
||||||
|
--- !u!114 &11400000
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 0}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: 60a957541c24c57e78018c202ebb1d9b, type: 3}
|
||||||
|
m_Name: New Cluster
|
||||||
|
m_EditorClassIdentifier: Assembly-CSharp::Cluster
|
||||||
|
nuclei: []
|
||||||
8
Assets/Scenes/Boids/New Cluster.asset.meta
Normal file
8
Assets/Scenes/Boids/New Cluster.asset.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: eddc759ede59e66cd936ad6ae2c55c46
|
||||||
|
NativeFormatImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
mainObjectFileID: 11400000
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@ -10,14 +10,14 @@ MonoBehaviour:
|
|||||||
m_Enabled: 1
|
m_Enabled: 1
|
||||||
m_EditorHideFlags: 0
|
m_EditorHideFlags: 0
|
||||||
m_Script: {fileID: 11500000, guid: 36081359186edfec998d891a1feeb17b, type: 3}
|
m_Script: {fileID: 11500000, guid: 36081359186edfec998d891a1feeb17b, type: 3}
|
||||||
m_Name: Identity
|
m_Name: New Nano Brain
|
||||||
m_EditorClassIdentifier: Assembly-CSharp::NanoBrain
|
m_EditorClassIdentifier: Assembly-CSharp::NanoBrain
|
||||||
title:
|
title:
|
||||||
count: 0
|
count: 0
|
||||||
color: {r: 1, g: 1, b: 1, a: 1}
|
color: {r: 1, g: 1, b: 1, a: 1}
|
||||||
texture: {fileID: 0}
|
texture: {fileID: 0}
|
||||||
nuclei:
|
nuclei:
|
||||||
- id: 1707104464
|
- id: 2025140912
|
||||||
_name: Root
|
_name: Root
|
||||||
synapses: []
|
synapses: []
|
||||||
receivers: []
|
receivers: []
|
||||||
@ -53,7 +53,7 @@ MonoBehaviour:
|
|||||||
inverse: 0
|
inverse: 0
|
||||||
exponent: 1
|
exponent: 1
|
||||||
perceptei: []
|
perceptei: []
|
||||||
rootId: 1707104464
|
rootId: 2025140912
|
||||||
references:
|
references:
|
||||||
version: 2
|
version: 2
|
||||||
RefIds: []
|
RefIds: []
|
||||||
8
Assets/Scenes/Boids/New Nano Brain.asset.meta
Normal file
8
Assets/Scenes/Boids/New Nano Brain.asset.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: fab876d6bf7dc9b10a56541a7eeccdd2
|
||||||
|
NativeFormatImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
mainObjectFileID: 11400000
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Loading…
x
Reference in New Issue
Block a user