Fill a list

Asked

Viewed 181 times

-1

I’m in need of help, I’m developing a genetic algorithm, well my problem is that I need my loop to not stop as long as there’s room on the list. As for example I have a population of 10 already created, as soon as it enters the selected function random numbers are drawn according to the apitidão (fitness) and then another population is built with these 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

1 answer

0


This way I was able to solve, so he fills the entire list with the drawn numbers.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.