public Chromosomes.IChromosome[] PerformSelection(Populations.IPopulation population, int size) { //Getting all of the chromosomes that are in population. Chromosomes.IChromosome[] individuals = population.GetChromosomes(); //Calculating the number of the those chromosomes. int sz = individuals.Length; //Calculating the fitness of each chromosome. double[] f = new double[sz]; double[] fitnesses = population.GetFitnesses(); for (int i = 0; i < sz; ++i) f[i] = fitnesses[i]; //Defining the to-be-returned array. Chromosomes.IChromosome[] ret = new Chromosomes.IChromosome[size]; //Selecting the parents from the population and putting them in ret. for (int i = 0; i < size; ++i) { int parentidx=-1; for (int j=0;j<TournamentSize;++j){ int idx=random.Next(size); if (parentidx==-1||f[parentidx]<f[idx]){ parentidx=idx; } } ret[i]=individuals[parentidx].Clone(); } //Returning the result. return ret; }
public Chromosomes.IChromosome[] PerformSelection(Populations.IPopulation population, int size) { //Getting all of the chromosomes that are in population. Chromosomes.IChromosome[] individuals = population.GetChromosomes (); //Calculating the number of the those chromosomes. int sz = individuals.Length; //Calculating the fitness of each chromosome. double[] f = new double[sz]; double[] fitnesses = population.GetFitnesses(); for (int i = 0; i < sz; ++i) f[i] = fitnesses[i]; //Sorting the population by descending fitness values. Sort (individuals, f, 0, sz - 1); //Defining the to-be-returned array. Chromosomes.IChromosome[] ret = new Chromosomes.IChromosome[size]; //Selecting the parents from the population and putting them in ret. for (int i = 0; i < size; ++i) ret[i] = individuals[i].Clone(); //Returning the result. return ret; }
public Chromosomes.IChromosome[] PerformSelection(Populations.IPopulation population, int size) { //Getting all of the chromosomes that are in population. Chromosomes.IChromosome[] individuals = population.GetChromosomes(); //Calculating the number of the those chromosomes. int sz = individuals.Length; //Calculating the fitness of each chromosome. double[] f = new double[sz]; double[] fitnesses = population.GetFitnesses(); for (int i = 0; i < sz; ++i) f[i] = fitnesses[i]; //Calculating the sum of the fitnesses. double sum = 0.0; for (int i = 0; i < sz; ++i) sum += f[i]; //The normalization step. for (int i = 0; i < sz; ++i) f[i] /= sum; //Sorting the population by descending fitness values. RouletteWheelSelectionMethod.Sort(individuals, f, 0, sz - 1); //The accumulation step. for (int i = 1; i < sz; ++i) f[i] += f[i - 1]; f[sz - 1] = 1.0; //To ensure that the sum of all the fitnesses is equal to 1.0. //Defining the to-be-returned array. Chromosomes.IChromosome[] ret = new Chromosomes.IChromosome[size]; //Selecting the parents from the population and putting them in ret. for (int i = 0; i < size; ++i) { int idx = RouletteWheelSelectionMethod.Find(f, RouletteWheelSelectionMethod.random.NextDouble()); ret[i] = individuals[idx].Clone(); } //Returning the result. return ret; }