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,3 +1,4 @@
|
|||||||
|
/*
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -298,3 +299,4 @@ public class GraphEditorWindow : EditorWindow {
|
|||||||
GetWindow<GraphEditorWindow>("Neuroid Visualizer");
|
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
|
||||||
@ -46,10 +46,10 @@ public class Neuroid : Nucleus {
|
|||||||
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");
|
||||||
|
|||||||
@ -2,10 +2,9 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using LinearAlgebra;
|
|
||||||
|
|
||||||
[System.Serializable]
|
[Serializable]
|
||||||
public class Nucleus {
|
public class Nucleus : INucleus {
|
||||||
|
|
||||||
public int id; // hash code
|
public int id; // hash code
|
||||||
|
|
||||||
@ -17,9 +16,12 @@ public class Nucleus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
public List<Synapse> synapses = new();
|
private List<Synapse> _synapses = new();
|
||||||
|
public List<Synapse> synapses => _synapses;
|
||||||
|
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
public List<Receiver> receivers = new();
|
private List<Receiver> _receivers = new();
|
||||||
|
public List<Receiver> receivers => _receivers;
|
||||||
|
|
||||||
#region Serialization
|
#region Serialization
|
||||||
|
|
||||||
@ -31,9 +33,6 @@ public class Nucleus {
|
|||||||
foreach (Synapse synapse in synapses)
|
foreach (Synapse synapse in synapses)
|
||||||
synapse.Rebuild(brain);
|
synapse.Rebuild(brain);
|
||||||
}
|
}
|
||||||
if (this.receivers == null)
|
|
||||||
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.");
|
||||||
@ -41,7 +40,6 @@ public class Nucleus {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static Nucleus RebuildType(NanoBrain brain, Nucleus nucleus) {
|
public static Nucleus RebuildType(NanoBrain brain, Nucleus nucleus) {
|
||||||
if (string.IsNullOrEmpty(nucleus.nucleusType) == false) {
|
if (string.IsNullOrEmpty(nucleus.nucleusType) == false) {
|
||||||
@ -62,7 +60,7 @@ public class Nucleus {
|
|||||||
|
|
||||||
#region Runtime state (not serialized)
|
#region Runtime state (not serialized)
|
||||||
|
|
||||||
public NanoBrain brain { get; set; }
|
public Cluster cluster { get; set; }
|
||||||
|
|
||||||
private Vector3 _outputValue;
|
private Vector3 _outputValue;
|
||||||
public Vector3 outputValue
|
public Vector3 outputValue
|
||||||
@ -70,7 +68,7 @@ public class Nucleus {
|
|||||||
get { return _outputValue; }
|
get { return _outputValue; }
|
||||||
set {
|
set {
|
||||||
this.stale = 0;
|
this.stale = 0;
|
||||||
this.isSleeping = false;
|
this._isSleeping = false;
|
||||||
_outputValue = value;
|
_outputValue = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -78,10 +76,12 @@ public class Nucleus {
|
|||||||
[System.NonSerialized]
|
[System.NonSerialized]
|
||||||
private int stale = 1000;
|
private int stale = 1000;
|
||||||
|
|
||||||
public bool isSleeping = false;
|
private bool _isSleeping = false;
|
||||||
|
public bool isSleeping => _isSleeping;
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
@ -95,17 +95,17 @@ public class Nucleus {
|
|||||||
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
|
||||||
@ -121,9 +121,9 @@ public class Nucleus {
|
|||||||
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,7 +170,8 @@ public class Nucleus {
|
|||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
public class Synapse {
|
public class Synapse {
|
||||||
[System.NonSerialized]
|
[System.NonSerialized]
|
||||||
public Nucleus nucleus;
|
public INucleus nucleus;
|
||||||
|
public NanoBrain cluster;
|
||||||
public int nucleusId;
|
public int nucleusId;
|
||||||
public float weight;
|
public float weight;
|
||||||
|
|
||||||
@ -276,15 +277,15 @@ public class Synapse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[System.Serializable]
|
[Serializable]
|
||||||
public class Receiver {
|
public class Receiver {
|
||||||
[System.NonSerialized]
|
[NonSerialized]
|
||||||
public Nucleus nucleus;
|
public INucleus nucleus;
|
||||||
public int nucleusId;
|
//public int nucleusId;
|
||||||
|
|
||||||
public Receiver(Nucleus nucleus) {
|
public Receiver(INucleus nucleus) {
|
||||||
this.nucleus = nucleus;
|
this.nucleus = nucleus;
|
||||||
this.nucleusId = nucleus.id;
|
//this.nucleusId = nucleus.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Rebuild(NanoBrain brain) {
|
public bool Rebuild(NanoBrain brain) {
|
||||||
@ -292,13 +293,14 @@ public class Receiver {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (Nucleus nucleus in brain.nuclei) {
|
// Use SerializedReference instead?
|
||||||
if (nucleus.id == this.nucleusId) {
|
// foreach (Nucleus nucleus in brain.nuclei) {
|
||||||
this.nucleus = nucleus;
|
// if (nucleus.id == this.nucleusId) {
|
||||||
return true;
|
// this.nucleus = nucleus;
|
||||||
}
|
// return true;
|
||||||
}
|
// }
|
||||||
Debug.LogWarning($"Receiver deserialization error: could not find nucleus with id {this.nucleusId}");
|
// }
|
||||||
|
//Debug.LogWarning($"Receiver deserialization error: could not find nucleus with id {this.nucleusId}");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -4,6 +4,8 @@ using UnityEngine;
|
|||||||
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 NanoBrain brain;
|
||||||
public Receptor receptor;
|
public Receptor receptor;
|
||||||
public string baseName;
|
public string baseName;
|
||||||
|
|
||||||
@ -47,18 +49,20 @@ public class Perceptoid : Neuroid {
|
|||||||
receiver.nucleus = this;
|
receiver.nucleus = this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Copy all the synapses
|
// Copying disabled for now
|
||||||
this.synapses = nucleus.synapses;
|
// // Copy all the synapses
|
||||||
// Copy all receivers
|
// this.synapses = nucleus.synapses;
|
||||||
this.receivers = nucleus.receivers;
|
// // Copy all receivers
|
||||||
|
// this.receivers = nucleus.receivers;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion Serialization
|
#endregion Serialization
|
||||||
|
|
||||||
public Perceptoid(NanoBrain brain, int thingType, string name = "sensor") : base(name) {
|
public Perceptoid(NanoBrain brain, int thingType, string name = "sensor") : base(name) {
|
||||||
this.brain = brain;
|
this.brain = brain;
|
||||||
if (this.brain != null) {
|
this.cluster = brain.cluster;
|
||||||
this.brain.perceptei.Add(this);
|
if (this.cluster != null) {
|
||||||
|
brain.perceptei.Add(this);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
Debug.LogError("No neuroid network");
|
Debug.LogError("No neuroid network");
|
||||||
@ -76,6 +80,7 @@ public class Perceptoid : Neuroid {
|
|||||||
this.array = array;
|
this.array = array;
|
||||||
Perceptoid source = array.perceptei[0];
|
Perceptoid source = array.perceptei[0];
|
||||||
this.brain = source.brain;
|
this.brain = source.brain;
|
||||||
|
this.cluster = source.cluster;
|
||||||
if (this.brain != null) {
|
if (this.brain != null) {
|
||||||
this.brain.perceptei.Add(this);
|
this.brain.perceptei.Add(this);
|
||||||
}
|
}
|
||||||
|
|||||||
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
|
||||||
@ -77,7 +77,7 @@ public class NanoBrainComponent_Editor : Editor {
|
|||||||
});
|
});
|
||||||
|
|
||||||
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!");
|
||||||
|
|
||||||
|
|||||||
@ -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)
|
||||||
|
|||||||
@ -3,27 +3,28 @@ using UnityEngine;
|
|||||||
|
|
||||||
[CreateAssetMenu(menuName = "Passer/NanoBrain")]
|
[CreateAssetMenu(menuName = "Passer/NanoBrain")]
|
||||||
public class NanoBrain : ScriptableObject, ISerializationCallbackReceiver {
|
public class NanoBrain : ScriptableObject, ISerializationCallbackReceiver {
|
||||||
|
|
||||||
public string title;
|
|
||||||
public int count;
|
|
||||||
public Color color = Color.white;
|
|
||||||
public Texture2D texture;
|
|
||||||
|
|
||||||
public List<Neuroid> nuclei = new();
|
public List<Neuroid> nuclei = new();
|
||||||
public List<Perceptoid> perceptei = new();
|
public List<Perceptoid> perceptei = new();
|
||||||
public List<Receptor> receptors = new();
|
public List<Receptor> receptors = new();
|
||||||
|
|
||||||
|
public Cluster cluster;
|
||||||
|
|
||||||
// This is probably always the first element in the nuclei list...
|
// This is probably always the first element in the nuclei list...
|
||||||
[System.NonSerialized]
|
[System.NonSerialized]
|
||||||
public Nucleus root;
|
public Nucleus output;
|
||||||
public int rootId;
|
public int rootId;
|
||||||
|
|
||||||
public NanoBrain() {
|
public NanoBrain() {
|
||||||
this.root = new Neuroid(this, "Root");
|
this.output = new Neuroid(this.cluster, "Root");
|
||||||
|
this.cluster = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddReceiver(INucleus receiver) {
|
||||||
|
output.AddReceiver(receiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Neuroid AddNeuron(string name) {
|
public Neuroid AddNeuron(string name) {
|
||||||
Neuroid neuroid = new(this, name);
|
Neuroid neuroid = new(this.cluster, name);
|
||||||
return neuroid;
|
return neuroid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,13 +36,13 @@ public class NanoBrain : ScriptableObject, ISerializationCallbackReceiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void OnBeforeSerialize() {
|
public void OnBeforeSerialize() {
|
||||||
this.rootId = root.id;
|
this.rootId = output.id;
|
||||||
}
|
}
|
||||||
public void OnAfterDeserialize() {
|
public void OnAfterDeserialize() {
|
||||||
try {
|
try {
|
||||||
foreach (Nucleus nucleus in this.nuclei.ToArray()) {
|
foreach (Nucleus nucleus in this.nuclei.ToArray()) {
|
||||||
if (this.rootId == nucleus.id)
|
if (this.rootId == nucleus.id)
|
||||||
this.root = nucleus;
|
this.output = nucleus;
|
||||||
nucleus.Rebuild(this);
|
nucleus.Rebuild(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,18 +50,19 @@ public class NanoBrain : ScriptableObject, ISerializationCallbackReceiver {
|
|||||||
perceptoid.Rebuild(this);
|
perceptoid.Rebuild(this);
|
||||||
}
|
}
|
||||||
catch (System.Exception) { }
|
catch (System.Exception) { }
|
||||||
this.GarbageCollection();
|
this.cluster.GarbageCollection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
public void GarbageCollection() {
|
public void GarbageCollection() {
|
||||||
HashSet<Nucleus> visitedNuclei = new();
|
HashSet<INucleus> visitedNuclei = new();
|
||||||
MarkNuclei(visitedNuclei, this.root);
|
MarkNuclei(visitedNuclei, this.output);
|
||||||
//Debug.Log($"Garbage collection found {visitedNuclei.Count} Nuclei");
|
//Debug.Log($"Garbage collection found {visitedNuclei.Count} Nuclei");
|
||||||
this.nuclei.RemoveAll(nucleus => visitedNuclei.Contains(nucleus) == false);
|
this.nuclei.RemoveAll(nucleus => visitedNuclei.Contains(nucleus) == false);
|
||||||
this.perceptei.RemoveAll(perceptoid => visitedNuclei.Contains(perceptoid) == false);
|
this.perceptei.RemoveAll(perceptoid => visitedNuclei.Contains(perceptoid) == false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MarkNuclei(HashSet<Nucleus> visitedNuclei, Nucleus nucleus) {
|
public void MarkNuclei(HashSet<INucleus> visitedNuclei, INucleus nucleus) {
|
||||||
if (nucleus is null)
|
if (nucleus is null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -89,4 +91,5 @@ public class NanoBrain : ScriptableObject, ISerializationCallbackReceiver {
|
|||||||
nucleus.receivers.RemoveAll(receiver => visitedReceivers.Contains(receiver) == false);
|
nucleus.receivers.RemoveAll(receiver => visitedReceivers.Contains(receiver) == false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
@ -4,7 +4,7 @@ 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) {
|
||||||
@ -22,7 +22,7 @@ public class NanoBrainComponent : MonoBehaviour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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