Implement roulette method

This commit is contained in:
George Lacey 2017-09-24 17:38:04 +01:00
parent 8ea4b82c0d
commit 8baa410fa4

View File

@ -1,5 +1,7 @@
from individual import Individual from individual import Individual
from random import Random
rand = Random()
class Population(object): class Population(object):
@ -18,10 +20,10 @@ class Population(object):
total += member.fitness total += member.fitness
return total return total
def roulette(self): def roulette(self, divisor=1):
total = self.total_fitness() total = self.total_fitness() / divisor
position = rand.uniform(0, total)
for member in self.members: for member in self.members:
total -= member.fitness position -= member.fitness / divisor
if total <= 0: if position <= 0:
pass return member
# todo: add to crossover list