Passing order from one list to another


Good, we have a list that is ordered by a fitness already calculated called 'scored'. We are trying to 'poblacionNueva' keep the order of 'punctuated' but not show the fitness values of each element.

def selection_and_reproduction(poblacionNueva):

listaVistos = []
puntuados = [ [calcularFitness(i), i] for i in poblacionNueva] 
def takeSecond(puntuados):
    return puntuados[0]
puntuados = sorted(puntuados, key=takeSecond) 

selected =  puntuados[(len(puntuados)-indAReproducir):] 

for i in range(len(poblacionNueva)-indAReproducir):
    punto = random.randint(1,largo-1) 
    padre = random.sample(selected, 2) 

    while punto in listaVistos:
        punto = random.randint(1,largo-1)

    poblacionNueva[i][:punto] = (padre[0])[1][:punto] #Se mezcla el material genetico de los padres en cada nuevo individuo
    print("Primera parte:%s"%(poblacionNueva[i][:punto]))

    poblacionNueva[i][punto:] = (padre[1])[1][punto:]
    print("Segunda parte:%s"%(poblacionNueva[i][punto:]))

    i = poblacionNueva[i][:punto] + poblacionNueva[i][punto:]

    print("Individuo Generado CON FITNESS:%s"%[calcularFitness(i),i])

    print("Poblacion Nueva FINAL:\n%s"%(poblacionNueva))

return poblacionNueva 

EDIT 1: We generate a random initial population. puntuados is a list of lists where each list is formed by the fitness value and a 5-digit number and is also ordered by the fitness value. 4 individuals are generated from the parents (numbers with greater fitness value, in this case 2). PoblacionNueva should be a list of lists where the first 4 were the 4 individuals generated and the last two parents of the previous population.

As it is observed, already in the first individual generated, the order of puntuados is lost in the first New End Population.

asked by Carlos Lozano 09.08.2017 в 13:43

1 answer


The order of the list is not "automatic" . When altering an individual of the population, the order that was held by FITNESS will be altered, being necessary to reorder the list. The appropriate thing would be to reorder the list at the end of the mutations before returning. If you do not need fitness at all, it's best to use the calculaFitness function directly to sort the population.

poblacion_nueva = sorted(poblacion, key=calculaFitness)

I do not know some variables and functions that you use. I've left them as "global" so your code might look something like this:

def selection_and_reproduction(poblacion):

    global largo
    global indAReproducir
    global calcularFitness

    num_mutantes = len(poblacion) - indAReproducir
    poblacion_nueva = sorted(poblacion, key=calcularFitness)

    # usamos como padres los que tienen más fitness
    padres =  poblacion_nueva[num_mutantes:] 

    listaVistos = []

    for i in range(num_mutantes ):

        (padre, madre) = random.sample(padres, 2) 

        # obtener un punto aleatorio que no se haya mirado ya
        punto = random.randint(1, largo-1) 
        while punto in listaVistos:
            punto = random.randint(1, largo-1)

        print("Punto:", punto)
        print("Padres:", padre, madre)

        #Se mezcla el material genetico de los padres en cada nuevo individuo
        poblacion_nueva[i] = padre[0][1][:punto] + madre[1][1][punto:]

        individuo = poblacion_nueva[i]
        fitness = calcularFitness(individuo)
        print(f"Individuo Generado con FITNESS {fitness}: {individuo}")

    print("Poblacion Nueva FINAL:\n%s"%(poblacion_nueva))

    return poblacion_nueva
answered by 10.08.2017 / 14:19