Go through 2 positions per iteration FOR

3

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.

    
asked by Carlos Lozano 15.08.2017 в 20:52
source

1 answer

1

It is difficult to see exactly what you want to achieve in your code because it is a little complicated, a simpler example that shows its difficulty would be easier to answer directly. However, the question in the first paragraph can be answered, and I think it will help you solve your difficulty.

The for can go for two, so you can make your lists have the same value of i=0 and i=1 as well with each pair of positions. This is done using the third parameter in range()

range() is used as seen next

range(mínimo, máximo, paso)

where step is default 1, but you can specify it.

Example:

for i in range(0, 16, 2):
    print(i)

Result:

0
2
4
6
#... ecétera
14

So, to make every two positions have the same value, you can take advantage of this for like that.

lista_final = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
for i in range(0, 16, 2):
    lista_final[i] = i
    lista_final[i+1] = i
print(lista_final)

Result:

[0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14]

I hope this tool helps you.

    
answered by 17.08.2017 в 16:26