'Crossover two individuals of different lengths
I have two individuals I need to perform crossover on that are of different lengths.
The individuals may be like this, but could be much longer:
0 1 2 2 1 2 0 [0] 1 2 1 2 0 1 2 [0] 1 2 1 2 0 2 1 [1]
1 2 1 1 0 2 0 [0] 1 2 1 2 0 0 1 [1]
However, I need to keep their original length after crossover. I also need to ensure that every 8th bit (in square brackets) cannot be a 2. The length of each individual will always be multiples of 8.
How can I perform crossover on these individuals without changing the length and structure of either individual?
I haven't been able to find a solution to this so any help would be greatly appreciated.
Solution 1:[1]
I've tried this type of solution: it works (but different cross-over location:
the code is in python
from random import choices, randint, randrange, random
a = [1,2,4,5,8,11,3,4,7,2,4,6]
b = [3,4,5,6,9,1,3,6,7]
length_a =len(a)
length_b = len(b)
crossover_prob = 0.8
p = randint(1, length_a - 1)
q = randint(1, length_b -1)
while p < crossover_prob:
c = a[0:p] + b[q:]
while q < crossover_prob:
d = b[0:q] + a[p:]
print (c)
print (d)
result:
[1, 3, 4, 5, 6, 9, 1, 3, 6, 7] [2, 4, 5, 8, 11, 3, 4, 7, 2, 4, 6]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | maryam safiyah |
