jeudi 23 novembre 2017

Remove items from list sequentially based on variable index

def FitnessSelection(population, relative_fitness):
    selected_chromosomes = []
    selected1 = random.choices(population, weights = relative_fitness, k = 1)
    ind1 = selected1[0][1][0]
    del relative_fitness[ind1]
    del population[ind1] 
    selected2 = random.choices(population, weights = relative_fitness, k = 1)
    ind2 = selected2[0][1][0]
    del relative_fitness[ind2]
    del population[ind1] 
    selected = selected1, selected2
    selected_chromosomes = [i[0] for i in selected]
    return (selected_chromosomes)

I am trying to perform a random weighted selection from a list, however, I cant have the same item selected twice, therefore I am trying to exclude the selected items immediately after selection so it won't be in the population in the list for the next run of the function.

The problem is that the program is running but I don't think the items are being excluded

I have the items in the following structure

population=[[[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [4], [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], [1], [3]]]

Where the last brackets of each string is an index counter. I am trying to isolate the counter and exclude the item in the same position, but when I check the population afterward, they were not excluded.

Does anyone know what could be wrong with this approach?

p.s. - I'm sorry for not posting a standalone program, I couldn't make it work in a small sample like this.

Thanks!




Aucun commentaire:

Enregistrer un commentaire