How to choose n best chromosomes and return them as an array?
Chromosome[] Selection(int n) {}
I know that I need to compare chromosomes by their fitness.
I have method public double calculateFitness()
How to choose n best chromosomes and return them as an array?
Chromosome[] Selection(int n) {}
I know that I need to compare chromosomes by their fitness.
I have method public double calculateFitness()
I presume you have an array of Chromosome
s you want to compare. Then you can simply order them by their fitness and take the best n:
Chromosome[] sourceArray = ..... // wherever you got them from
Chromosome[] bestN = sourceArray.
OrderByDescending(chromosome => chromosome.calculateFitness()).
Take(n).ToArray();
I would suggest you to have a look at other non-elitist selection schemes to avoid premature convergence. In short, getting stuck in a local optima with no progress in terms of solutions' fitness quality. Alternative probabilistic schemes, rather easy to code, are Roulette-Wheel Selection or Tournament Selection.
Check out this other posts that may give you an idea about how to implement them: