mutate ant from 2 parents

This commit is contained in:
Pascal Serrarens 2026-08-13 15:11:18 +02:00
parent 527abb5178
commit cde3a0f4d3
2 changed files with 57 additions and 14 deletions

View File

@ -808,24 +808,43 @@ namespace NanoBrain {
} }
return true; return true;
} }
public bool CopyWeightsFrom(Cluster source) {
public bool CopyWeightsTo(Cluster destination) { int thisNucleiCount = this.nuclei.Count;
if (EqualStructure(this, destination) == false) { int sourceNucleiCount = source.nuclei.Count;
Debug.LogWarning("Tried to copy weight to a cluster with different structure. Copy is not executed."); if (thisNucleiCount != sourceNucleiCount)
return false; return false;
}
for (int i = 0; i < this.nuclei.Count; i++) { for (int i = 0; i < this.nuclei.Count; i++) {
if (this.nuclei[i] is Neuron thisNeuron && if (this.nuclei[i] is Neuron thisNeuron &&
destination.nuclei[i] is Neuron destinationNeuron) { source.nuclei[i] is Neuron sourceNeuron) {
thisNeuron.CopyWeightsTo(destinationNeuron); if (thisNeuron.CopyWeightsFrom(sourceNeuron) == false)
return false;
} }
} }
return true; return true;
} }
public void ProcessWeightsFrom(Cluster source, Func<float, float, float> 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 #region Receivers
/// <summary> /// <summary>

View File

@ -309,16 +309,40 @@ namespace NanoBrain {
for (int i = 0; i < synapseCount1; i++) { for (int i = 0; i < synapseCount1; i++) {
if (Synapse.EqualStructure(neuron1.synapses[i], neuron2.synapses[i]) == false) if (Synapse.EqualStructure(neuron1.synapses[i], neuron2.synapses[i]) == false)
return false; return false;
} }
return true; return true;
} }
public void CopyWeightsTo(Neuron destination) { public bool CopyWeightsFrom(Neuron source) {
for (int i = 0; i < this.synapses.Count; i++) { 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 thisSynapse = this.synapses[i];
Synapse destinationSynapse = destination.synapses[i]; if (thisSynapse.trainable) {
destinationSynapse.weight = thisSynapse.weight; Synapse sourceSynapse = source.synapses[i];
thisSynapse.weight = sourceSynapse.weight;
}
} }
return true;
}
public void ProcessWeightsFrom(Neuron source, Func<float, float, float> 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);
}
}
} }
/// <summary> /// <summary>