From cde3a0f4d336ff13b656baa98553ac9c9e2b139e Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Thu, 13 Aug 2026 15:11:18 +0200 Subject: [PATCH] mutate ant from 2 parents --- Runtime/Scripts/Core/Cluster.cs | 37 +++++++++++++++++++++++++-------- Runtime/Scripts/Core/Neuron.cs | 34 +++++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/Runtime/Scripts/Core/Cluster.cs b/Runtime/Scripts/Core/Cluster.cs index 25c27ce..1e3a25b 100644 --- a/Runtime/Scripts/Core/Cluster.cs +++ b/Runtime/Scripts/Core/Cluster.cs @@ -808,24 +808,43 @@ namespace NanoBrain { } return true; } - - public bool CopyWeightsTo(Cluster destination) { - if (EqualStructure(this, destination) == false) { - Debug.LogWarning("Tried to copy weight to a cluster with different structure. Copy is not executed."); + public bool CopyWeightsFrom(Cluster source) { + int thisNucleiCount = this.nuclei.Count; + int sourceNucleiCount = source.nuclei.Count; + if (thisNucleiCount != sourceNucleiCount) return false; - } for (int i = 0; i < this.nuclei.Count; i++) { - if (this.nuclei[i] is Neuron thisNeuron && - destination.nuclei[i] is Neuron destinationNeuron) { + if (this.nuclei[i] is Neuron thisNeuron && + source.nuclei[i] is Neuron sourceNeuron) { - thisNeuron.CopyWeightsTo(destinationNeuron); + if (thisNeuron.CopyWeightsFrom(sourceNeuron) == false) + return false; } } - + return true; } + public void ProcessWeightsFrom(Cluster source, Func processor) { + if (processor is null) + throw new ArgumentNullException(nameof(processor)); + if (source is null) + throw new ArgumentNullException(nameof(source)); + int thisNucleiCount = this.nuclei.Count; + int sourceNucleiCount = source.nuclei.Count; + if (thisNucleiCount != sourceNucleiCount) + throw new ArgumentException("Lists must have the same length.", nameof(source)); + + for (int i = 0; i < thisNucleiCount; i++) { + if (this.nuclei[i] is Neuron thisNeuron && + source.nuclei[i] is Neuron sourceNeuron) { + + thisNeuron.ProcessWeightsFrom(sourceNeuron, processor); + } + } + } + #region Receivers /// diff --git a/Runtime/Scripts/Core/Neuron.cs b/Runtime/Scripts/Core/Neuron.cs index 77918d0..7d8005a 100644 --- a/Runtime/Scripts/Core/Neuron.cs +++ b/Runtime/Scripts/Core/Neuron.cs @@ -309,16 +309,40 @@ namespace NanoBrain { for (int i = 0; i < synapseCount1; i++) { if (Synapse.EqualStructure(neuron1.synapses[i], neuron2.synapses[i]) == false) return false; - } + } return true; } - public void CopyWeightsTo(Neuron destination) { - for (int i = 0; i < this.synapses.Count; i++) { + public bool CopyWeightsFrom(Neuron source) { + int thisSynapseCount = this.synapses.Count; + int sourceSynapseCount = source.synapses.Count; + if (thisSynapseCount != sourceSynapseCount) + return false; + + for (int i = 0; i < thisSynapseCount; i++) { Synapse thisSynapse = this.synapses[i]; - Synapse destinationSynapse = destination.synapses[i]; - destinationSynapse.weight = thisSynapse.weight; + if (thisSynapse.trainable) { + Synapse sourceSynapse = source.synapses[i]; + thisSynapse.weight = sourceSynapse.weight; + } } + return true; + } + + public void ProcessWeightsFrom(Neuron source, Func processor) { + int thisSynapseCount = this.synapses.Count; + int sourceSynapseCount = source.synapses.Count; + if (thisSynapseCount != sourceSynapseCount) + return; + + for (int i = 0; i < thisSynapseCount; i++) { + Synapse thisSynapse = this.synapses[i]; + if (thisSynapse.trainable) { + Synapse sourceSynapse = source.synapses[i]; + thisSynapse.weight = processor(thisSynapse.weight, sourceSynapse.weight); + } + } + } ///