Search code examples
genetic-algorithmroulette-wheel-selection

How does this algorithm corresponds to the roulette wheel selection?


I am trying to implement a roulette wheel selection. I have understood this algorithm:

  1. Calculate the sum S of all chromosome fitnesses in population
  2. Generate a random number, r, from interval (0,S)
  3. Loop through the population and sum fitnesses from 0 till S, this is the partial sum, call it P.
  4. When the P > S: stop and return the corresponding chromosome.

What I don't understand is how this corresponds to doing this instead: Roulette wheel selection algorithm (the answer with 44 votes). This makes sense to me, but not the one above.


Solution

  • The following is done using the sum

    def choose_parent_using_RWS(genes, S, points):
        P = randint(0, int(S))
        for x in genes:
            P += evaluate(x, points)
            if P > S:
                return x
        return genes[-1]
    

    the following is done by normalizing between 0 and 1

    def choose_parent_using_RWS(genes, S, points):
        P = randint(0, int(S))/S
        for x in genes:
            P += evaluate(x, points)/S
            if P > S/S:
                return x
        return genes[-1]