public void NewGeneration() { if (CurrentGeneration.Count <= 0) { return; } newGeneration = new List <Specimen>(); oldGeneration = new List <Specimen>(); List <Specimen> bestOfTheBest = new List <Specimen>(); allGenerations.Add(CurrentGeneration); for (int i = 0; i < CurrentGeneration.Count; i++) { for (int j = 0; j < CurrentGeneration.Count; j++) { if (i == j) { j++; } if (j == CurrentGeneration.Count) { break; } var firstParent = CurrentGeneration[i]; var secondParent = CurrentGeneration[j]; var child = firstParent.CrossoverWith(secondParent); if (RandomForGA.Generator.NextDouble() < Parameters.MutationRate) { child.Mutate(Parameters.MutationAmplitude); } newGeneration.Add(child); } } for (int i = 0; i < CurrentGeneration.Count; i++) { oldGeneration.Add(CurrentGeneration[i]); } CurrentGeneration.Clear(); for (int i = 0; i < oldGeneration.Count; i++) { CurrentGeneration.Add(oldGeneration[i]); } for (int i = 0; i < newGeneration.Count; i++) { CurrentGeneration.Add(newGeneration[i]); } CalculateFitness(); CurrentGeneration.Sort((a, b) => population.FittingFunction(a).CompareTo(population.FittingFunction(b))); for (int i = 0; i < Parameters.PopulationSize; i++) { bestOfTheBest.Add(CurrentGeneration[i]); } CurrentGeneration = bestOfTheBest; GenerationNum++; }