javaartificial-intelligencegenetic-algorithmevolutionary-algorithm

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

  • I assume you're talking about a single-point crossover. You could do something like this:

    1. Select a random number between 1 and the length of the shorter individual. This will be your crossover point.
    2. Cut both individuals at this point.
    3. Swap them, as per regular crossover.

    Example:

    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]
    

    The shorter individual has length 16, so we generate a random number between 1 and 16 --> e.g., 9

    Crossover point:

                       |
    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]
                       |
    

    Swap sub-sections after the point:

                       |
    0 1 2 2 1 2 0 [0] 1|2 1 2 0 0 1 [1]
    1 2 1 1 0 2 0 [0] 1|2 1 2 0 1 2 [0] 1 2 1 2 0 2 1 [1]
                       |
    

    This preserves the length of both individuals, and preserves the no-2s-in-brackets rule.