public float Distance(NEAT partner) { if (partner == null) { Debug.Log("NULL partner"); Debug.Break(); } //var synapseIenumerator = AllSynapses.GetEnumerator(); //var partnerSynapseIenumerator = partner.AllSynapses.GetEnumerator(); //synapseIenumerator.MoveNext(); float result = 0; if (AllSynapses.Any() || partner.AllSynapses.Any()) { int disjointGenes = 0; int excessGenes = 0; float weightDifference = 0; float N = Math.Max(AllSynapses.Count, partner.AllSynapses.Count); disjointGenes = 2 * AllSynapses.Keys.Union(partner.AllSynapses.Keys).Count() - AllSynapses.Count - partner.AllSynapses.Count; foreach (var synapseInnovationNo in AllSynapses.Keys.Intersect(partner.AllSynapses.Keys)) { weightDifference += Math.Abs((float)(AllSynapses[synapseInnovationNo].Weight - partner.AllSynapses[synapseInnovationNo].Weight)); //if (Math.Abs((float)(AllSynapses[synapseInnovationNo].Weight - partner.AllSynapses[synapseInnovationNo].Weight)) > Constants.Con.synapse_difference_threshold) // disjointGenes++; } result += (Constants.Con.c1 * excessGenes + Constants.Con.c2 * disjointGenes + Constants.Con.c3 * weightDifference) / N; } //if(HiddenLayers.Any() || partner.HiddenLayers.Any()) OutputLayer.Count > 0 always { int disjointGenes = 0; int excessGenes = 0; float weightDifference = 0; float N = Math.Max(HiddenLayers.Count, partner.HiddenLayers.Count) + OutputLayer.Count; foreach (var neuronPair in OutputLayer.Zip(partner.OutputLayer)) { weightDifference += Mathf.Abs((float)(neuronPair.Key.Bias - neuronPair.Value.Bias)); //if (Mathf.Abs((float)(neuronPair.Key.Bias - neuronPair.Value.Bias)) > Constants.Con.bias_difference_threshold) // disjointGenes++; } disjointGenes = 2 * HiddenLayers.Keys.Union(partner.HiddenLayers.Keys).Count() - HiddenLayers.Count - partner.HiddenLayers.Count; foreach (var neuronInnovationNo in HiddenLayers.Keys.Intersect(partner.HiddenLayers.Keys)) { weightDifference += Math.Abs((float)(HiddenLayers[neuronInnovationNo].Bias - partner.HiddenLayers[neuronInnovationNo].Bias)); //if (Math.Abs((float)(HiddenLayers[neuronInnovationNo].Bias - partner.HiddenLayers[neuronInnovationNo].Bias)) > Constants.Con.bias_difference_threshold) // disjointGenes++; } result += (Constants.Con.c1 * excessGenes + Constants.Con.c2 * disjointGenes + Constants.Con.c3 * weightDifference) / N; } return(result); //if (result < Constants.Con.delta_t) // return true; //return false; }
public NEAT Copy() { NEAT c = new NEAT(InputLayer.Count, OutputLayer.Count, true); foreach (var synapse in AllSynapses) { c.AddSynapse(synapse.Value); } foreach (var neuronpair in OutputLayer.Zip(c.OutputLayer)) { neuronpair.Value.Bias = neuronpair.Key.Bias; } return(c); }