private Gene GetGeneNotInMappingSection(Gene candidateGene, Gene[] mappingSection, Gene[] otherParentMappingSection) { var indexOnMappingSection = mappingSection .Select ((item, index) => new { Gene = item, Index = index }) .FirstOrDefault (g => g.Gene.Equals (candidateGene)); if (indexOnMappingSection != null) { return GetGeneNotInMappingSection(otherParentMappingSection [indexOnMappingSection.Index], mappingSection, otherParentMappingSection); } return candidateGene; }
/// <summary> /// Creates the cycle recursively. /// </summary> /// <param name="parent1Genes">The parent one's genes.</param> /// <param name="parent2Genes">The parent two's genes.</param> /// <param name="geneIndex">The current gene index.</param> /// <param name="cycle">The cycle.</param> private void CreateCycle(Gene[] parent1Genes, Gene[] parent2Genes, int geneIndex, List<int> cycle) { if(!cycle.Contains(geneIndex)) { var parent2Gene = parent2Genes[geneIndex]; cycle.Add(geneIndex); var newGeneIndex = parent1Genes.Select((g, i) => new { Value = g.Value, Index = i }).First(g => g.Value.Equals(parent2Gene.Value)); if (geneIndex != newGeneIndex.Index) { CreateCycle(parent1Genes, parent2Genes, newGeneIndex.Index, cycle); } } }