(population)
return psum
A preparation for Roulette Wheel selection
A preparation for Roulette Wheel selection
A preparation for Roulette Wheel selection
def GRouletteWheel_PrepareWheel(population):
   """ A preparation for Roulette Wheel selection """

   len_pop = len(population)

   psum = [i for i in xrange(len_pop)]

   population.statistics()

   if population.sortType == Consts.sortType["scaled"]:
      pop_fitMax = population.stats["fitMax"]
      pop_fitMin = population.stats["fitMin"]

      if pop_fitMax == pop_fitMin:
         for index in xrange(len_pop):
            psum[index] = (index + 1) / float(len_pop)
      elif (pop_fitMax > 0 and pop_fitMin >= 0) or (pop_fitMax <= 0 and pop_fitMin < 0):
         population.sort()
         if population.minimax == Consts.minimaxType["maximize"]:
            psum[0] = population[0].fitness
            for i in xrange(1, len_pop):
               psum[i] = population[i].fitness + psum[i - 1]
            for i in xrange(len_pop):
               psum[i] /= float(psum[len_pop - 1])
         else:
            psum[0] = -population[0].fitness + pop_fitMax + pop_fitMin
            for i in xrange(1, len_pop):
               psum[i] = -population[i].fitness + pop_fitMax + pop_fitMin + psum[i - 1]
            for i in xrange(len_pop):
               psum[i] /= float(psum[len_pop - 1])
   else:
      pop_rawMax = population.stats["rawMax"]
      pop_rawMin = population.stats["rawMin"]

      if pop_rawMax == pop_rawMin:
         for index in xrange(len_pop):
            psum[index] = (index + 1) / float(len_pop)

      elif (pop_rawMax > 0 and pop_rawMin >= 0) or (pop_rawMax <= 0 and pop_rawMin < 0):
         population.sort()
         if population.minimax == Consts.minimaxType["maximize"]:
            psum[0] = population[0].score
            for i in xrange(1, len_pop):
               psum[i] = population[i].score + psum[i - 1]
            for i in xrange(len_pop):
               psum[i] /= float(psum[len_pop - 1])
         else:
            psum[0] = - population[0].score + pop_rawMax + pop_rawMin
            for i in xrange(1, len_pop):
               psum[i] = - population[i].score + pop_rawMax + pop_rawMin + psum[i - 1]
            for i in xrange(len_pop):
               psum[i] /= float(psum[len_pop - 1])

   return psum
def GRouletteWheel_PrepareWheel ( population ) : len_pop = len ( population ) psum = [ i for i in xrange ( len_pop ) ] population . statistics ( ) if population . sortType == Consts . sortType [ "scaled" ] : pop_fitMax = population . stats [ "fitMax" ] pop_fitMin = population . stats [ "fitMin" ] if pop_fitMax == pop_fitMin : for index in xrange ( len_pop ) : psum [ index ] = ( index + 1 ) / float ( len_pop ) elif ( pop_fitMax > 0 and pop_fitMin >= 0 ) or ( pop_fitMax <= 0 and pop_fitMin < 0 ) : population . sort ( ) if population . minimax == Consts . minimaxType [ "maximize" ] : psum [ 0 ] = population [ 0 ] . fitness for i in xrange ( 1 , len_pop ) : psum [ i ] = population [ i ] . fitness + psum [ i - 1 ] for i in xrange ( len_pop ) : psum [ i ] /= float ( psum [ len_pop - 1 ] ) else : psum [ 0 ] = - population [ 0 ] . fitness + pop_fitMax + pop_fitMin for i in xrange ( 1 , len_pop ) : psum [ i ] = - population [ i ] . fitness + pop_fitMax + pop_fitMin + psum [ i - 1 ] for i in xrange ( len_pop ) : psum [ i ] /= float ( psum [ len_pop - 1 ] ) else : pop_rawMax = population . stats [ "rawMax" ] pop_rawMin = population . stats [ "rawMin" ] if pop_rawMax == pop_rawMin : for index in xrange ( len_pop ) : psum [ index ] = ( index + 1 ) / float ( len_pop ) elif ( pop_rawMax > 0 and pop_rawMin >= 0 ) or ( pop_rawMax <= 0 and pop_rawMin < 0 ) : population . sort ( ) if population . minimax == Consts . minimaxType [ "maximize" ] : psum [ 0 ] = population [ 0 ] . score for i in xrange ( 1 , len_pop ) : psum [ i ] = population [ i ] . score + psum [ i - 1 ] for i in xrange ( len_pop ) : psum [ i ] /= float ( psum [ len_pop - 1 ] ) else : psum [ 0 ] = - population [ 0 ] . score + pop_rawMax + pop_rawMin for i in xrange ( 1 , len_pop ) : psum [ i ] = - population [ i ] . score + pop_rawMax + pop_rawMin + psum [ i - 1 ] for i in xrange ( len_pop ) : psum [ i ] /= float ( psum [ len_pop - 1 ] ) return psum