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;
}
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) {
source.nuclei[i] is Neuron sourceNeuron) {
thisNeuron.CopyWeightsTo(destinationNeuron);
if (thisNeuron.CopyWeightsFrom(sourceNeuron) == false)
return false;
}
}
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
/// <summary>

View File

@ -313,12 +313,36 @@ namespace NanoBrain {
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<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>