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)
print("NUEVA ITERACION:\n")
print("Puntuados:\n%s"%(puntuados))
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)
print("Punto:%s"%(punto))
listaVistos.append(punto)
print("Punto:%s"%(punto))
print("Padre:%s"%(padre))
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.