/** * Checks if the maximum fitness value has been reached, by checking if the population has an individual with the maximum amount of fitness. */ bool CanEnd(ConnectedBlocksGraph connectedBlocks, Population population, GeneticOptions geneticOptions) { return(GetMaxFitness(connectedBlocks, geneticOptions) == population.GetFitness().Max()); }
/** * Creates a new population by recombining the parents to create children. Selects the parents to stay in the new population with roulette wheel selection. * @see ParentRecombination() */ Population RefreshPopulation(Population population, ConnectedBlocksGraph connectedBlocks, GeneticOptions geneticOptions, Random random) { RouletteWheelSelection rouletteWheelSelection = new RouletteWheelSelection(); int childrenAmount = (int)(geneticOptions.PopulationSize * geneticOptions.PopulationRefreshing); Individual[] children = new Individual[geneticOptions.PopulationSize]; for (int i = 0; i < childrenAmount; i++) { int[,] parents = ParentSelection(population, random); children[i] = ParentRecombination(new Individual(Util.GetRow(parents, 0)), new Individual(Util.GetRow(parents, 1)), random); children[i].CalculateFitness(connectedBlocks, geneticOptions); } for (int i = 0; i < geneticOptions.PopulationSize - childrenAmount; i++) { children[i + childrenAmount] = population.Individuals[rouletteWheelSelection.Selection(population.GetFitness(), random)]; } return(new Population(children)); }