diff --git a/Runtime/Scripts/Core/Cluster.cs b/Runtime/Scripts/Core/Cluster.cs index beb2430..7dbbc2a 100644 --- a/Runtime/Scripts/Core/Cluster.cs +++ b/Runtime/Scripts/Core/Cluster.cs @@ -372,11 +372,11 @@ namespace NanoBrain { } } - public Cluster Clone() { + public Cluster Copy() { Cluster clone = new() { name = this.name, prefab = this.prefab, - version = this.version, + version = this.version, parent = this.parent }; if (clone.parent != null) { @@ -386,7 +386,7 @@ namespace NanoBrain { // first clone the nuclei without their connections foreach (Nucleus nucleus in this.nuclei) nucleus.ShallowCloneTo(clone); - + Nucleus[] clonedNuclei = clone.nuclei.ToArray(); // Now clone the connections for (int nucleusIx = 0; nucleusIx < this.nuclei.Count; nucleusIx++) { @@ -403,8 +403,9 @@ namespace NanoBrain { if (synapseNeuron.parent.prefab != null && synapseNeuron.parent.prefab != clone.prefab) { // Neuron is in another cluster, find the cloned cluster first Cluster sourceCluster = synapseNeuron.parent; - Cluster clonedCluster = clone.nuclei.Find(n => n.name == sourceCluster.name) as Cluster; - if (clonedCluster == null) + //Cluster clonedCluster = clone.nuclei.Find(n => n.name == sourceCluster.name) as Cluster; + Nucleus clonedClusterNucleus = clone.nuclei.Find(n => n.name == sourceCluster.name); + if (clonedClusterNucleus is not Cluster clonedCluster) continue; // Now find the neuron in that cloned cluster @@ -433,26 +434,6 @@ namespace NanoBrain { } } - foreach (Nucleus clonedNucleus in clonedNuclei) { - if (clonedNucleus is not Cluster clonedCluster) - continue; - - 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}", - parent = clone.parent, - instanceCount = clone.instanceCount, - }; - siblings.Add(sibling); - CopyAllExternalReceivers(clonedCluster, sibling, this); - } - Cluster[] siblingClusters = siblings.ToArray(); - foreach (Cluster sibling in siblings) - sibling.instances = siblingClusters; - } - return clone; } @@ -512,8 +493,8 @@ namespace NanoBrain { public static int GetNucleusIndex(List nuclei, Nucleus nucleus) { int i = 0; foreach (Nucleus nucleiElement in nuclei) { - //for (int i = 0; i < nuclei.Length; i++) { - if (nucleiElement == nucleus) + // if (nucleiElement == nucleus) + if (nucleiElement.name == nucleus.name) return i; i++; } @@ -1124,20 +1105,20 @@ namespace NanoBrain { public ClusterData(Cluster cluster) { this.name = cluster.name; foreach (Nucleus nucleus in cluster.nuclei) { - if (nucleus is Neuron neuron) { - NeuronData neuronData = new(neuron); - this.neurons.Add(neuronData); - foreach (Synapse synapse in neuron.synapses) { - if (synapse.neuron.parent.name != cluster.name) { - ExternalClusterData clusterData = new(synapse.neuron.parent); - if (GetCluster(clusterData.name) == null) { - //if (this.clusters.Find(data => data.name == clusterData.name) == null) { - this.clusters.Add(clusterData); - //Debug.Log("Add cluster"); - } - } - } + if (nucleus is not Neuron neuron) + continue; + + NeuronData neuronData = new(neuron); + this.neurons.Add(neuronData); + foreach (Synapse synapse in neuron.synapses) { + if (synapse.neuron.parent.baseName == cluster.name) + continue; + + ExternalClusterData clusterData = new(synapse.neuron.parent); + if (GetCluster(clusterData.name) == null) + this.clusters.Add(clusterData); } + } } @@ -1157,7 +1138,7 @@ namespace NanoBrain { public uint instanceCount; public ExternalClusterData(Cluster cluster) { - this.name = cluster.name; + this.name = cluster.baseName; this.prefabName = cluster.prefab.name; this.instanceCount = (uint)cluster.instanceCount; } diff --git a/Runtime/Scripts/Core/Neuron.cs b/Runtime/Scripts/Core/Neuron.cs index 3b60353..1221121 100644 --- a/Runtime/Scripts/Core/Neuron.cs +++ b/Runtime/Scripts/Core/Neuron.cs @@ -256,15 +256,15 @@ namespace NanoBrain { /// Check if the neuron is sleeping. /// /// This will reset the output value if it is sleeping -// public void SleepCheck() { -// if (this.isSleeping && this.outputSqrMagnitude > 0) { -// #if UNITY_MATHEMATICS -// this._outputValue = new float3(0, 0, 0); -// #else -// this._outputValue = new Vector3(0,0,0); -// #endif -// } -// } + // public void SleepCheck() { + // if (this.isSleeping && this.outputSqrMagnitude > 0) { + // #if UNITY_MATHEMATICS + // this._outputValue = new float3(0, 0, 0); + // #else + // this._outputValue = new Vector3(0,0,0); + // #endif + // } + // } /// /// The time at which the last update has been done @@ -791,7 +791,7 @@ namespace NanoBrain { #endregion Back propagation private CancellationTokenSource _cts; - private Action resetStimulus; + private Action resetStimulus; /// /// Process an external stimulus @@ -806,7 +806,7 @@ namespace NanoBrain { if (autoResetDelay > 0) { long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); long resetTime = now + (long)(autoResetDelay * 1000.0f); - Cluster.TimedAction.AddTo(this.parent.actions, this.parent.name + "." +this.name, this.resetStimulus, resetTime); + Cluster.TimedAction.AddTo(this.parent.actions, this.parent.name + "." + this.name, this.resetStimulus, resetTime); } } @@ -839,6 +839,11 @@ namespace NanoBrain { this.activationType = neuron.activator; foreach (Synapse synapse in neuron.synapses) { + // Check whether synapse exists already first + if (this.synapses.Find(s => s.neuronName == synapse.neuron.name) != null) + // But then: what to do with the weights???? :-) + continue; + SynapseData synapseData = new(synapse); this.synapses.Add(synapseData); } diff --git a/Runtime/Scripts/Core/Population.cs b/Runtime/Scripts/Core/Population.cs index a02a2ff..254dc73 100644 --- a/Runtime/Scripts/Core/Population.cs +++ b/Runtime/Scripts/Core/Population.cs @@ -55,11 +55,11 @@ namespace NanoBrain { if (members.Count <= 0) return null; - Cluster result = members[0].brain.Clone(); - for (int memberIx = 1; memberIx < members.Count; memberIx++) { - Member member = members[memberIx]; - result.ProcessWeightsFrom(member.brain, Average); - } + Cluster result = members[0].brain.Copy(); + // for (int memberIx = 1; memberIx < members.Count; memberIx++) { + // Member member = members[memberIx]; + // result.ProcessWeightsFrom(member.brain, Average); + // } return result; } diff --git a/Runtime/Scripts/Core/Synapse.cs b/Runtime/Scripts/Core/Synapse.cs index 1317720..b8af76a 100644 --- a/Runtime/Scripts/Core/Synapse.cs +++ b/Runtime/Scripts/Core/Synapse.cs @@ -152,7 +152,7 @@ namespace NanoBrain { // this.clusterName = synapse.neuron.parent.prefab.name; // else // this.clusterName = synapse.neuron.parent.name; - this.clusterName = synapse.neuron.parent.name; + this.clusterName = synapse.neuron.parent.baseName; this.neuronName = synapse.neuron.name; this.weight = synapse.weight; this.trainable = synapse.trainable;