I am trying to implement Uniform order based crossover, which should be using shuffle logic, like:
Initial:
a =[5, 1, 4, 6, 7, 8, 2, 3]
b =[6, 7, 5, 2, 8, 3, 4, 1]
Randomly select positions that will no longer change. The remaining positions are filled with the missing elements copied in the order they appear in the other parent
a' =[ , 7, , 2, 8, , , 1]
b' =[ , 1, , 6, 7, , , 3]
Starting from the second crossover site the values that are not already present in the offspring from the alternative parent (the parent other than the one whose values are copied by the offspring in the initial phase) in the order they appear output:
a = [5, 7, 4, 2, 8, 6, 3, 1]
b = [5, 1, 2, 6, 7, 8, 4, 3]
I have tried to implement this but my code is not able to hold fixed index.
cross.py
import random
def uniformCrossover(indA, indB):
"""
Your Uniform Crossover Implementation
"""
crossprob = 0.1
size = min(len(indA), len(indB))
for i in range(size):
if random.random() < crossprob:
indA[i], indB[i] = indB[i], indA[i]
return indA, indB
a,b = uniformCrossover([5, 1, 4, 6, 7, 8, 2, 3], [6, 7, 5, 2, 8, 3, 4, 1])