diff --git a/Editor/ClusterView.cs b/Editor/ClusterView.cs index d15cafb..7f55f51 100644 --- a/Editor/ClusterView.cs +++ b/Editor/ClusterView.cs @@ -109,7 +109,7 @@ namespace NanoBrain.Unity { public Nucleus selectedSynapseNeuron = null; public Nucleus selectedOutput; public bool isOpen = true; - public bool initialized; + public bool initialized = false; #region Focus Graph diff --git a/Runtime/Scripts/Brain.cs b/Runtime/Scripts/Brain.cs deleted file mode 100644 index fe12ba5..0000000 --- a/Runtime/Scripts/Brain.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using UnityEngine; -/* -namespace NanoBrain.Unity { - - /// - /// A NanoBrain which can be used to control a gameobject - /// - /// A NanoBrain is a small neural network which can be used to implement functional behaviour. - /// The network consists of neurons which are connected together with synapses. - /// The output values of the neurons are of type Vector3 to support spatial computing. - /// - /// This component is basically a Unity representation of a nanobrain cluster. - /// \sa - /// - \ref NanoBrain::Cluster "Cluster" - /// - \ref NanoBrain::Neuron "Neuron" - [HelpURL("https://passer.life/documentation/nanobrain/Documentation/html/class_nano_brain_1_1_unity_1_1_brain.html")] - public class Brain : MonoBehaviour { - /// - /// The Cluster prefab from which the cluster is created - /// - public ClusterPrefab brainPrefab; - - [NonSerialized] - private Cluster brainInstance; - /// - /// The cluster isntance - /// - public Cluster brain { - get { - if (brainInstance == null && brainPrefab != null) { - brainInstance = new Cluster(brainPrefab) { - name = brainPrefab.name - }; - } else if (brainInstance != null && brainPrefab == null) { - brainInstance = null; - } - return brainInstance; - } - } - - // public Cluster InitializeBrain() { - // brainInstance = new Cluster(brainPrefab) { - // name = brainPrefab.name - // }; - // return brainInstance; - // } - - /// - /// Update the weight for all Synapses coming from the Neuron with the given name - /// - /// The cluster in which the synapses are updated - /// The name of the Neuron for which the weights are updated - /// The new Synapse weight - public static void UpdateWeight(Cluster brain, string name, float weight) { - Neuron root = brain.defaultOutput; - foreach (Synapse synapse in root.synapses) { - if (synapse.neuron.name == name) { - if (synapse.weight != weight) { - synapse.weight = weight; - // Debug.Log($"Updated weight for {name}"); - } - } - } - } - } - -} -*/ \ No newline at end of file diff --git a/Runtime/Scripts/Brain.cs.meta b/Runtime/Scripts/Brain.cs.meta deleted file mode 100644 index fc8b1c9..0000000 --- a/Runtime/Scripts/Brain.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 92f34a5e4027a1dc39efd8ce63cf6aba -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Runtime/Scripts/Core/Cluster.cs b/Runtime/Scripts/Core/Cluster.cs index 96e6ece..69e30f2 100644 --- a/Runtime/Scripts/Core/Cluster.cs +++ b/Runtime/Scripts/Core/Cluster.cs @@ -24,6 +24,9 @@ namespace NanoBrain { /// Cluster should always be created from prefabs public ClusterPrefab prefab; + //[HideInInspector] + public int version; + /// /// The base name of the cluster. I don't think this is actively used at this moment /// @@ -44,6 +47,7 @@ namespace NanoBrain { /// The value instanceCount determines how many instances will be present at runtime. //[NonSerialized] [SerializeReference] + [HideInInspector] public Cluster[] instances; /// @@ -63,6 +67,7 @@ namespace NanoBrain { /// All nuclei in this cluster /// [SerializeReference] + [HideInInspector] public List nuclei = new(); // the nuclei sorted using topological sorting // to ensure that the cluster is computer in the right order @@ -78,6 +83,7 @@ namespace NanoBrain { /// The cluster in which this new cluster will be placed public Cluster(ClusterPrefab prefab, Cluster parent) { this.prefab = prefab; + this.version = prefab.version; this.name = prefab.name; this.parent = parent; @@ -94,6 +100,7 @@ namespace NanoBrain { /// The prefab in which the new copy is placed public Cluster(ClusterPrefab prefab, ClusterPrefab parent = null) { this.prefab = prefab; + this.version = prefab.version; this.name = prefab.name; if (parent != null) this.parent = parent.cluster; @@ -109,11 +116,10 @@ namespace NanoBrain { /// Strange that this does not take any parameters or return values. /// Where which the clone be found??? private void ClonePrefab() { - this.name += " " + Time.time; - Debug.Log($"Clone Prefab {this.prefab.name} -> {this.name}"); + //Debug.Log($"Clone Prefab {this.prefab.name} -> {this.name}"); if (this.prefab == null || this.prefab.cluster == null || this.prefab.cluster.nuclei == null) return; - + Nucleus[] prefabNuclei = this.prefab.cluster.nuclei.ToArray(); // first clone the nuclei without their connections @@ -168,35 +174,34 @@ namespace NanoBrain { } //if (Application.isPlaying) { - // Only create cluster siblings at runtime - foreach (Nucleus clonedNucleus in clonedNuclei) { - if (clonedNucleus is not Cluster clonedCluster) - continue; + // Only create cluster siblings at runtime + foreach (Nucleus clonedNucleus in clonedNuclei) { + if (clonedNucleus is not Cluster clonedCluster) + continue; - List siblings = new() { + List siblings = new() { clonedCluster }; - for (int instanceIx = 1; instanceIx < clonedCluster.instanceCount; instanceIx++) { - // Create another sibling - Cluster sibling = new(clonedCluster.prefab, this) { - name = $"{clonedCluster.baseName}: {instanceIx} [{Time.time}]", - parent = this.parent, - instanceCount = this.instanceCount, - }; - Debug.Log($"create {clonedCluster.prefab.name} sibling: {sibling.name}"); - siblings.Add(sibling); - CopyAllExternalReceivers(clonedCluster, sibling, clonedCluster.prefab, this); - } - Cluster[] siblingClusters = siblings.ToArray(); - foreach (Cluster sibling in siblings) - sibling.instances = siblingClusters; + for (int instanceIx = 1; instanceIx < clonedCluster.instanceCount; instanceIx++) { + // Create another sibling + Cluster sibling = new(clonedCluster.prefab, this) { + name = $"{clonedCluster.baseName}: {instanceIx}", + parent = this.parent, + instanceCount = this.instanceCount, + }; + siblings.Add(sibling); + CopyAllExternalReceivers(clonedCluster, sibling, clonedCluster.prefab, this); } + Cluster[] siblingClusters = siblings.ToArray(); + foreach (Cluster sibling in siblings) + sibling.instances = siblingClusters; + } - // Ensure that all neurons are computed to initialize bias - foreach (Nucleus clonedNucleus in clonedNuclei) { - if (clonedNucleus is not Cluster) - clonedNucleus.UpdateStateIsolated(); - } + // Ensure that all neurons are computed to initialize bias + foreach (Nucleus clonedNucleus in clonedNuclei) { + if (clonedNucleus is not Cluster) + clonedNucleus.UpdateStateIsolated(); + } //} } diff --git a/Runtime/Scripts/Core/Neuron.cs b/Runtime/Scripts/Core/Neuron.cs index 0625801..8490974 100644 --- a/Runtime/Scripts/Core/Neuron.cs +++ b/Runtime/Scripts/Core/Neuron.cs @@ -42,6 +42,7 @@ namespace NanoBrain { /// /// The bias which a value which is always added to the combined value of the neuron /// It does not have a synapse and therefore no weight of source nucleus + [HideInInspector] public Vector3 bias = Vector3.zero; #region Synapses @@ -111,6 +112,7 @@ namespace NanoBrain { /// /// The type of combinator used for this Neuron /// + [HideInInspector] public CombinatorType combinator = CombinatorType.Sum; /// @@ -130,6 +132,7 @@ namespace NanoBrain { /// The activation function /// [SerializeField] + [HideInInspector] public ActivationType _activator; /// /// The activation funtion @@ -144,10 +147,12 @@ namespace NanoBrain { /// /// The curve representing the activation function /// + [HideInInspector] public AnimationCurve curve; /// /// The maximum value of the curve /// + [HideInInspector] public float curveMax = 1.0f; /// @@ -287,6 +292,7 @@ namespace NanoBrain { /// /// The output value of the neuron /// + [HideInInspector] protected float3 _outputValue; /// /// The output value of the neuron @@ -369,6 +375,7 @@ namespace NanoBrain { /// /// The time at which the last update has been done /// + [HideInInspector] public float lastUpdate = 0; /// /// Time in seconds after the last update the neuron can go to sleep @@ -737,6 +744,7 @@ namespace NanoBrain { /// The nuclei which have a synapse to this neuron /// [SerializeReference] + [HideInInspector] private List _receivers = new(); /// /// The nuclei which have a synapse to this neuron diff --git a/Runtime/Scripts/Core/Nucleus.cs b/Runtime/Scripts/Core/Nucleus.cs index 5302c19..a93c153 100644 --- a/Runtime/Scripts/Core/Nucleus.cs +++ b/Runtime/Scripts/Core/Nucleus.cs @@ -14,12 +14,14 @@ namespace NanoBrain { /// /// The name of the Nucleus /// + [HideInInspector] public string name; /// /// The cluster instance in which the nucleus is located /// [SerializeReference] + [HideInInspector] public Cluster parent; /// diff --git a/Runtime/Scripts/ScriptableObjects/ClusterPrefab.cs b/Runtime/Scripts/ScriptableObjects/ClusterPrefab.cs index a586568..187b8b2 100644 --- a/Runtime/Scripts/ScriptableObjects/ClusterPrefab.cs +++ b/Runtime/Scripts/ScriptableObjects/ClusterPrefab.cs @@ -14,6 +14,9 @@ namespace NanoBrain.Unity { /// public Cluster cluster; + //[HideInInspector] + public int version; + /// /// Retrieve a nucleus in this cluster /// @@ -36,6 +39,12 @@ namespace NanoBrain.Unity { new Neuron(this.cluster, "Output"); // Every cluster should have at least 1 neuron this.cluster.instanceCount = 1; } + +#if UNITY_EDITOR + private void OnValidate() { + version++; + } +#endif } }