private void ApplyCrossover( IOrderedEnumerable <Chromosome <T> > sortedPopulation, List <Chromosome <T> > newPopulation) { while (newPopulation.Count() != geneticOptions.PopulationSize) { var parentA = geneticAlgorithm.ChooseParent(sortedPopulation); var parentB = geneticAlgorithm.ChooseParent(sortedPopulation); if (random.NextDouble() < geneticOptions.CrossoverProbability) { newPopulation.Add(geneticAlgorithm.Crossover(parentA, parentB)); } } }
public void NextGeneration() { FitnessStats.SaveCurrentToHistory(); Generation++; var newGenomes = new List <Genome>(); if (_genetics.EliteCount > 0) { var elites = Genomes.OrderByDescending(x => x.Fitness).Take(_genetics.EliteCount); newGenomes.AddRange(elites); } while (newGenomes.Count < Genomes.Count()) { var mother = getGenomeByRoulette(); var father = getGenomeByRoulette(); var children = _genetics.Crossover(mother, father); foreach (var child in children) { _genetics.Mutate(child); newGenomes.Add(child); } } newGenomes.ForEach(x => x.ResetFitness()); Genomes = newGenomes.Take(Genomes.Count()).ToList(); }