1

My question is how to perform a cross over and avoid duplication?

Consider p1 = {1, 2, 5, 6, 3, 4} and p2 = {2, 6, 5, 4, 1, 3}

One of crossovers is res = {1, 2, 5 , 6, 1 ,3}

As you see there is a duplication...

Can we avoid it?



Here is My Code

private static Chromosome crossover(Chromosome chromosome1, Chromosome chromosome2) {
    Chromosome newChromosome = new Chromosome();
    for (int i = 0; i < chromosome1.size(); i++) {
        if (Math.random() < uniformRate) {
            newChromosome.addGene(chromosome1.getGene(i));
        } else {
            newChromosome.addGene(chromosome2.getGene(i));
        }
    }
    return newChromosome;
}
  • Hmm - no. In two arrays of unique values you can only exchange elements and not have duplicates in the results if the exchanged elements are identical, rendering the exchange pointless. If this is an exercise can we see the problem formulation, please? – 500 - Internal Server Error May 17 '16 at 20:46
  • @500-InternalServerError nope this is not a homework, it's just for leaning... the problem is sorting an array using genetic algorithm, that's it, and besides, using cross over is necessary in genetic algorithm... – Shahrooz Andrea May 18 '16 at 03:41
  • What makes you think sorting is a good application for GAs? – 500 - Internal Server Error May 18 '16 at 09:22
  • @500-InternalServerError i'm not thinking of it, but it's maybe a solution for sorting and cause I want to learn more and more, I want to do it but facing with problems that I want to solve them... – Shahrooz Andrea May 19 '16 at 08:51

1 Answers1

0

There are different techniques to do crossover depending on the problem.

The problem being whether there can be duplicate value or not.

The method that you are using allows duplicate values. To avoid that I would suggest the follow various algorithm:

p1={1,2,3,4,5,6} and p2={a,b,c,d,e,f}

  1. First method:

    • select an random index limited to the number of length of chromosome p(mid)

    • copy p1 from 0 index to mid index to p3

    • then loop over p2 and copy value that are not already in p3

  2. Second method

    • select 2 random index (start, end)

    • copy p1 value to p3 from the start to end index

    • loop through p2 and copy value that are not already in p3

So the basic idea to avoid duplicate values would simply be looping over and check if the value already exist in the array or not.

Second method is more difficult since you have to copy p1 from start index to end index and paste it in p3 at the same indexes, then looping over p3 to for empty/null value and copy value from p2 that are not already in p3.

If you are really interested in Artificial Intelligence I guess following Artificial Intelligence StackExchange though its still in staging.

j4rey
  • 2,582
  • 20
  • 34