0

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()

2 Answers2

1

I presume you have an array of Chromosomes 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();
René Vogt
  • 43,056
  • 14
  • 77
  • 99
0

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:

Community
  • 1
  • 1