Tuesday, April 17, 2012

Efficient Roulette Wheel Selection

I ran into a pretty nice way of doing roulette wheel selection the other day. It requires O(n) set up time, and each selection takes O(log(n)) time, for a total of O(n+nlog(n)) or O(nlog(n)) time for the whole selection process. This is a lot better than the completely naive way (generating the "spin" of the wheel, and then running through the individuals looking for the one that was "hit") which takes O(n^2), and which I've been guilty of in the past.

First off is the setup. Given a population of individuals, paired with their fitnesses. Lets assume that this is in some linear structure like a linked list or vector. Go through each individual, setting their fitness value to the total sum of all previous individuals plus their own fitness. The summing of the fitnesses takes time linear in the population size.

Next is doing the actual selection. This is amazingly easy- generate a value between 0 and the sum of all fitness values. Then do a binary search through the population with the new (summed) fitness values. Each selection takes log(n) time (to do the binary search) and you need to do it n times, so the second part of this selection technique takes nlog(n) time.

So there you have it- an easy and quick O(nlog(n)) time implementation of roulette wheel selection. I will probably end up using this soon enough.

4 comments:

  1. For an O(1) version you might look at
    http://arxiv.org/abs/1109.3627
    regards,
    Adam Lipowski (lipowski@amu.edu.pl)

    ReplyDelete
  2. Do you have to sort by fitness first? The sum fitness monotonically increases even if the fitness list is not sorted, and the inter-fitness distances still mirror the relative fitness.

    ReplyDelete
  3. Thanks for the comments. Your right about not needing to sort them- I don't actually know where I got that idea from!
    Also, thanks for the link! I had heard that there was a O(1) time algorithm, and I am glad to see a paper on it.

    ReplyDelete
  4. For the sake of future generations- I modified the original post to remove mention of sorting the population as part of the setup.

    ReplyDelete