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?
– Pedro von Hertwig Batista
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
– Patrick Amaral