def selRoulette(individuals, k):
 s_inds = sorted(individuals, key=attrgetter('fitness'), reverse=True)
 sum_fits = sum((ind.fitness.values[0] for ind in individuals))
 chosen = []
 for i in xrange(k):
  u = (random.random() * sum_fits)
  sum_ = 0
  for ind in s_inds:
   sum_ += ind.fitness.values[0]
   if (sum_ > u):
    chosen.append(ind)
    break
 return chosen

def selRoulette ( individuals , k ) : s_inds = sorted ( individuals , key = attrgetter ( 'fitness' ) , reverse = True ) sum_fits = sum ( ( ind . fitness . values [ 0 ] for ind in individuals ) ) chosen = [ ] for i in xrange ( k ) : u = ( random . random ( ) * sum_fits ) sum_ = 0 for ind in s_inds : sum_ += ind . fitness . values [ 0 ] if ( sum_ > u ) : chosen . append ( ind ) break return chosen
select *k* individuals from the input *individuals* using *k* spins of a roulette .