Explore Courses Blog Tutorials Interview Questions
0 votes
in AI and Deep Learning by (50.2k points)

I need code for the ranking selection method on a genetic algorithm. I have created a roulette and tournament selections method but now I need ranking and I am stuck.

My roulette code is here (I am using atom struct for genetic atoms) :

const int roulette (const atom *f)


  int i;

  double sum, sumrnd;

  sum = 0;

  for (i = 0; i < N; i++)

    sum += f[i].fitness + OFFSET;

  sumrnd = rnd () * sum;

  sum = 0;

  for (i = 0; i < N; i++) {

    sum += f[i].fitness + OFFSET;

    if (sum > sumrnd)



  return i;


Where atom :

typedef struct atom


  int geno[VARS];

  double pheno[VARS];

  double fitness;

} atom;

1 Answer

0 votes
by (108k points)

There are different types of selection, we can implement in a genetic algorithm. We seldom become confused with two types of selection. One type is Roulette wheel selection and another is Rank based selection.

In Rank Selection:

  • The rank selection first ranks the population and then every chromosome receives fitness from this ranking.

  • The worst-case will have fitness 1, second-worst 2, etc. and the best-case will have fitness N (number of chromosomes in population).

  • After this, all the chromosomes have a chance to be selected.

  • Rank-based selection schemes can avoid premature convergence.

  • But it can be computationally expensive because it manages the populations based on fitness value.

  • But this method can point to slower convergence because the best chromosomes do not differ so much from other ones.

So the process will be:

  1. First, sort the Fitness value of the Population.

  2. Then if the Population number is 10 then give the probability of selection to the Population like 0.1,0.2,0.3,…, 1.0.

  3. Then calculate cumulative Fitness and make the roulette wheel.

  4. And the next steps are the same as the roulette wheel.

If you wish to learn about Genetic Algorithm or Genetic Programming then visit this Artificial Intelligence Course.

Browse Categories