# Passing order from one list to another

1

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]
print("NUEVA ITERACION:\n")

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

while punto in listaVistos:
punto = random.randint(1,largo-1)
print("Punto:%s"%(punto))
listaVistos.append(punto)

print("Punto:%s"%(punto))
print("Primera parte:%s"%(poblacionNueva[i][:punto]))

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

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

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
source

2

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

listaVistos = []

for i in range(num_mutantes ):

# 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)
listaVistos.append(punto)

print("Punto:", punto)

#Se mezcla el material genetico de los padres en cada nuevo individuo