public IGenome Breed(IGenome mate) { List <IGene> offspringGenes = new List <IGene>(); // Find disjoint or excess genes IEnumerable <IGene> genes = Genes.Concat(mate.Genes); IGenome mostFitGenome = Fitness > mate.Fitness ? this : mate; foreach (IGene gene in genes) { if (offspringGenes.Any(g => g.Innovation == gene.Innovation)) { continue; } List <IGene> comparedGenes = genes.Where(g => g.Innovation == gene.Innovation).ToList(); if (comparedGenes.Count > 1) // Matching gene { IGene chosenGene = comparedGenes[new Random().Next(2)].Copy(); if (comparedGenes[0].IsExpressed == false || comparedGenes[1].IsExpressed == false) { chosenGene.IsExpressed = new Random().NextDouble() < GeneParameters.InheritedGeneChanceOfReExpression; } offspringGenes.Add(chosenGene); } else if (comparedGenes.Count == 1) // Disjoint or excess gene { bool addGene = (Fitness == mate.Fitness && new Random().Next(2) == 0) || (mostFitGenome.Genes.FirstOrDefault(mfg => mfg == comparedGenes[0]) != null); if (addGene == true && !offspringGenes.Any(og => og.Innovation == comparedGenes[0].Innovation)) { offspringGenes.Add(comparedGenes[0].Copy()); } } } return(new Genome() { Nodes = GenomeHelper.GetNodesFromGenes(offspringGenes).ToList(), Genes = offspringGenes }); }