Roulette Selection in Genetic Algorithms

后端 未结 14 1009
庸人自扰
庸人自扰 2020-11-27 12:19

Can anyone provide some pseudo code for a roulette selection function? How would I implement this:

14条回答
  •  盖世英雄少女心
    2020-11-27 12:57

    Here is the code in python. This code can also handle the negative value of fitness.

    from numpy import min, sum, ptp, array 
    from numpy.random import uniform 
    
    list_fitness1 = array([-12, -45, 0, 72.1, -32.3])
    list_fitness2 = array([0.5, 6.32, 988.2, 1.23])
    
    def get_index_roulette_wheel_selection(list_fitness=None):
        """ It can handle negative also. Make sure your list fitness is 1D-numpy array"""
        scaled_fitness = (list_fitness - min(list_fitness)) / ptp(list_fitness)
        minimized_fitness = 1.0 - scaled_fitness
        total_sum = sum(minimized_fitness)
        r = uniform(low=0, high=total_sum)
        for idx, f in enumerate(minimized_fitness):
            r = r + f
            if r > total_sum:
                return idx
    
    get_index_roulette_wheel_selection(list_fitness1)
    get_index_roulette_wheel_selection(list_fitness2)
    
    1. Make sure your fitness list is 1D-numpy array
    2. Scaled the fitness list to the range [0, 1]
    3. Transform maximum problem to minimum problem by 1.0 - scaled_fitness_list
    4. Random a number between 0 and sum(minimizzed_fitness_list)
    5. Keep adding element in minimized fitness list until we get the value greater than the total sum
    6. You can see if the fitness is small --> it has bigger value in minimized_fitness --> It has a bigger chance to add and make the value greater than the total sum.

提交回复
热议问题