I'm trying to learn about genetic algorithms and am currently working on "crossing over" two "genes". A gene is an integer array, consisting of ones and zeros. To exemplify my problem let's say we have two genes.
int[] geneA = {1,0,0,0,0};
int[] geneB = {0,1,1,1,0};
The expected result from a cross-over, for example at position 3 would be:
geneA = [1,0,0,1,0]
geneB = [0,1,1,0,0]
Meaning that every element at the index of 3 or above would be swapped with the equivalent element of the other gene. To achieve this I wrote the following method:
private void crossOver(int[] geneA, int[] geneB, int pos) {
int copyA[];
int copyB[];
copyA = geneA;
copyB = geneB;
for(int i = pos; i < geneA.length; i++) {
geneA[i] = copyB[i];
geneB[i] = copyA[i];
}
System.out.println(Arrays.toString(geneA);
System.out.println(Arrays.toString(geneB);
}
However, it seems that the elements of geneB simply get copied into geneA at the index of 3 or higher. The console output is as following:
[1, 0, 0, 1, 0]
[0, 1, 1, 1, 0]
Any explanations or help are highly appreciated. Thanks in advance!