Crossing lists in python

Asked

Viewed 244 times

1

Good evening, I’m breaking my head to find the error, is the following I’m crossing this list of binary numbers, the first crossing is working but the second is getting the end of the first, to illustrate this is my entry list:

SELECTED POPULATION

['1', '0', '1', '0', '1', '0']
['1', '0', '1', '1', '1', '1']
['1', '1', '0', '0', '0', '1']
['0', '0', '0', '0', '1', '1']
['1', '0', '1', '0', '1', '0']
['1', '0', '1', '0', '1', '0']
['1', '0', '1', '0', '1', '0']
['0', '0', '1', '0', '0', '1']
['1', '0', '1', '0', '1', '0']
['1', '0', '1', '0', '1', '0']

These are the ones that will cross in pairs (father and mother):

CROSSOVER

 INDICES: [0, 1, 2, 3, 4, 6, 7, 8]
 Corte em: 6
 Pai: ['1', '0', '1', '0', '1', '0']
 Mae: ['1', '0', '1', '1', '1', '1']
 Corte em: 2
 Pai: ['1', '1', '0', '0', '0', '1']
 Mae: ['0', '0', '0', '0', '1', '1']
 Corte em: 1
 Pai: ['1', '0', '1', '0', '1', '0']
 Mae: ['1', '0', '1', '0', '1', '0']
 Corte em: 2
 Pai: ['0', '0', '1', '0', '0', '1']
 Mae: ['1', '0', '1', '0', '1', '0']

And this is my way out that’s wrong:

CROSSOVER POPULATION

['1', '0', '1', '0', '1', '0']
['1', '0', '1', '1', '1', '1']
['1', '1', '0', '0', '1', '1']
['0', '0', '0', '0', '1', '1']
['1', '0', '1', '0', '1', '0']
['1', '0', '1', '0', '1', '0']
['1', '0', '1', '0', '1', '0']
['0', '0', '1', '0', '1', '0']
['1', '0', '1', '0', '1', '0']
['1', '0', '1', '0', '1', '0']

CODE

def crossover(probCrossover):
    indices = []
    randoms = []

    print('\nCROSSOVER')
    print('-----'*6)
    for i in range(len(populacao)):
        randoms.append(random())

    for i , r in enumerate(randoms):
        if r <= probCrossover:
            indices.append(i)

    if len(indices)%2!=0:
        indices.pop()
    print('\nINDICES: {}'.format(indices))
    for ind in range(0, len(indices), 2):

        pontoCorte = randint(1, precind)
        print('Corte em: {}'.format(pontoCorte))
        print('Pai: {}\nMae: {}'.format(populacao[indices[ind]], populacao[indices[ind+1]]))

        populacao.__setitem__(indices[ind], populacao[indices[ind]][0:pontoCorte]+populacao[indices[ind+1]][pontoCorte:])
        populacao.__setitem__(indices[ind+1], populacao[indices[ind+1]][0:pontoCorte]+populacao[indices[ind]][pontoCorte:])
  • What does "cross" the list mean? You can give an example of the desired input and output, and post all the code needed to make the problem playable?

  • Well the input is ah 'selected population' that I showed just above. Cruzara is so have this line for example line[1,2,3,4,5] and Linha2[5,6,7,8,9] and cut point for example cut=2, it results in 2 new lines[1,2,7,8,9] and Linha2[5,6,3,4,5] @Pedrovonhertwig

1 answer

0

I managed to solve my problem, in fact it was very simple, I was using the setitem in the list, then as the code to modify were together and when changing the first the second would use it already modified and this caused the error in the output. As a solution I created two variables filho1 and filho2 which receive the changed item and then was changed in the list.

SOLUTION:

def crossover(probCrossover):
     indices = []
     randoms = []

     print('\nCROSSOVER')
     print('-----'*6)
     for i in range(len(populacao)):
        randoms.append(random())

     for i , r in enumerate(randoms):
         if r <= probCrossover:
             indices.append(i)

     if len(indices)%2!=0:
         indices.pop()
     print('\nINDICES: {}'.format(indices))
     for ind in range(0, len(indices), 2):

         pontoCorte = randint(1, precind)
         print('Corte em: {}'.format(pontoCorte))
         print('Pai: {}\nMae: {}'.format(populacao[indices[ind]], populacao[indices[ind+1]]))
         filho1 = populacao[indices[ind]][0:pontoCorte]+populacao[indices[ind+1]][pontoCorte:]
         filho2 = populacao[indices[ind+1]][0:pontoCorte]+populacao[indices[ind]][pontoCorte:]

         populacao.__setitem__(indices[ind], filho1)
         populacao.__setitem__(indices[ind+1], filho2)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.