Search code examples
pythonpython-3.xgenetic-algorithmfitness

Generate probabilities list for fitness proportionate selection (roulette wheel) in genetic algorithms


firs of all I apologize if my approach is too dumb or simplistic, I am an economist trying very hard to get into programming, therefore I lack some specific skills. Anyways, I have the following code:

population = [[[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [1], [0]],
 [[0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1], [3], [1]],
 [[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [4], [2]],
 [[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], [3], [3]]]

def ProbabilityList(population):
    fitness = chromosome[2] for chromosome in population
    manipulated_fitness = fitness + 1
    total_weight=sum(manipulated_fitness)
    relative_fitness= [chromosome[1]/total_weight for chromosome in population]
    probabilities= [sum(relative_fitness) for i in range(len(relative_fitness))]
    return (probabilities)

The logic of the population is [[[individual1],[fitness][counter]],[individual3],[fitness][counter]], and so on... the counter is just a number so I can order the individuals.

So what I need in this case is to create a selection probability list based on the total fitness. I also need to add 1 to the basic fitness, since in the future the value might be zero and I cant use a deterministic selection method (that is, no individuals can have 0 probabilities)

Would anyone know a correct approach to deal with it like this?


Solution

  • One library you might consider is numpy which has a function that does exactly what you are asking for: A weighted version of random.choice

    Edit: here is one way to do it based on your code.

    from numpy.random import choice    
    def ProbabilityList(population):
        #manipulated fitness in one line
        manipulated_fitness = [chromosome[1]+1 for chromosome in population]
        total_weight=sum(manipulated_fitness)
        #define probabilities - note we should use +1 here too otherwise we won't get a proper distribution
        relative_fitness= [(chromosome[1]+1)/total_weight for chromosome in population]
        #get a list of the ids
        ids = [chromosome[2] for chromosome in population]
        #choose one id based on their relative fitness
        draw = choice(ids, 1, p=relative_fitness)
        #return your choice
        return draw
        #if you want to return the probability distribution you can just return relative_fitness
    

    Let me also make two suggestions for slightly more complicated data structures/methods you could read about that may make your life a bit easier: dictionaries or classes.

    Edit: What I meant by this is to do something like:

    chromosome_dict={id1:{fitness:4,chromosome:[0,1,1,1,0]},
                     id2:{fitness:3,chromosome:[0,0,0,1,1]}}
    

    This is not for any computational reason, but because it would be easier to read and manipulate.