public DNA <T> SpliceCrossover(DNA <T> otherParent) { DNA <T> child = new DNA <T>(Genes.Count, random, fitnessFunction, IndividualMutationRate, ChromosomeMutationRate, individualFitnessFunction: IndividualFitnessFunction); int crossOverPoint = random.Next(0, Genes.Count); int crossOverPoint2 = random.Next(0, Genes.Count); int end = Math.Max(crossOverPoint, crossOverPoint2); int start = Math.Min(crossOverPoint, crossOverPoint2); child.Genes.AddRange(Genes.GetRange(start, end - start)); for (int i = 0; i < otherParent.Genes.Count; i++) { if (child.Genes.IndexOf(otherParent.Genes[i]) == -1) { child.Genes.Add(otherParent.Genes[i]); } } child.Mutate(); return(child); }
private void TwoOptCross() { int N = Genes.Count; var currentRouteFitness = IndividualFitnessFunction(Genes); for (int i = 1; i < N - 2; i++) { for (int j = i + 1; j < N + 1; j++) { if (j - i == 1) { continue; } var newRoute = Genes.GetRange(0, i); var toBeReversed = Genes.GetRange(i, j - i); toBeReversed.Reverse(); newRoute.AddRange(toBeReversed); newRoute.AddRange(Genes.GetRange(j, N - j)); var newRouteFitness = IndividualFitnessFunction(newRoute); if (newRouteFitness < currentRouteFitness) { Genes = newRoute; Fitness = newRouteFitness; return; } } } }
public IEnumerable <IWeightedEndpoint <TVertex, TEdge> > CrossoverGenes(IChromosome <TVertex, TEdge> other) { return(Genes.GetRange(1, Genes.Count - 2).Intersect(other.Genes)); }