Fill in a list

-1

I'm in need of help, I'm developing a genetic algorithm, well my problem is that I need my loop not to stop as long as I have space on the list. As for example I have a population of 10 already created, as soon as it enters the selection function random numbers are drawn according to the fitness and then another population is constructed with those numbers that were "drawn".

def selecao(fitness, populacao):
    populacaoIntermediaria=[]
    somatotal = sum(fitness)

    for i in range(tamPopulacao):
        r = uniform(0, somatotal)
        if (fitness[i] > r):
            populacaoIntermediaria.append(populacao[i])

    print('População Intermediaria: {}'.format(populacaoIntermediaria))

    return  populacaoIntermediaria
    
asked by anonymous 18.05.2018 / 02:05

1 answer

0

In this way I managed to solve, so it fills the entire list with the numbers drawn.

def selecao(fitness, populacao):
    populacaoIntermediaria=[]
    somatotal = sum(fitness)

    for j in range(tamPopulacao):
        i = 0
        parcial = 0
        r = uniform(0, somatotal)
        while (r >= parcial):
            parcial += fitness[i]
            i += 1
        populacaoIntermediaria.append(populacao[i])

    print('População Intermediaria: {}'.format(populacaoIntermediaria))

    return  populacaoIntermediaria
    
18.05.2018 / 02:05