Good, our first for runs 16 positions in this case and we want that both padre
as punto
have the same value for each 2 positions (That is, for i=0
and i=1
the same value, for i=2
and i=3
other ....).
Next, if you enter the 2nd FOR we want that if the variable i
is even do what is inside the IF and if not ELSE keeping for i
and i+1
the same values of padres
and punto
When leaving the second FOR, the i
of the first for should advance two positions and choose new values for padre
and punto
for i in range(len(poblacionNueva)-indAReproducir):
padre = random.sample(selected, 2) #Se eligen dos padres
punto = random.randint(1,largo-1) #Se elige un punto para hacer el intercambio
while punto in listaVistos:
punto = random.randint(1,largo-1)
listaVistos.append(punto)
if random.random() <= probabilidadReproduccion:
print("i:%s"%(i))
print("Padre:%s"%(padre))
for i in range(i, i+1):
if i%2==0:
poblacionNuevaOrdenada[i][:punto] = (padre[0])[1][:punto]
print("Primera parteIF:%s"%(poblacionNuevaOrdenada[i][:punto]))
poblacionNuevaOrdenada[i][punto:] = (padre[1])[1][punto:]
print("Segunda parteIF:%s"%(poblacionNuevaOrdenada[i][punto:]))
else:
poblacionNuevaOrdenada[i][:punto] = (padre[1])[1][:punto]
print("Primera parteELSE:%s"%(poblacionNuevaOrdenada[i][:punto]))
poblacionNuevaOrdenada[i][punto:] = (padre[0])[1][punto:]
print("Segunda parteELSE:%s"%(poblacionNuevaOrdenada[i][punto:]))
j = poblacionNuevaOrdenada[i][:punto] + poblacionNuevaOrdenada[i][punto:]
print("Individuo Generado:%s"%(j))
print("Poblacion Nueva FINAL:\n%s"%(poblacionNuevaOrdenada))
else:
poblacionNuevaOrdenada[i] = poblacionNuevaOrdenada[i]
poblacionNuevaOrdenada[i+1] = poblacionNuevaOrdenada[i]
return poblacionNuevaOrdenada
SOLUTION
As you can see, the value of padre
varies just like the variable punto
so that the divisions of the number do it incorrectly. The only correct thing is the introduction of the individual generated in the list.