/**
* Single point crossover method in Java.
*/
public Chromosome[] cross(Chromosome mother, Chromosome father) {
Chromosome[] children = new Chromosome[2];
children[0] = new Chromosome();
children[1] = new Chromosome();
int size = mother.getSize();
int locus = (int) Math.round(GeneticAlgorithm.random() * (size -1));
if(World.IS_TEST) System.out.println("Single Point Crossover, locus is: "+locus);
for (int index = 0; index < size; index++) {
if (index < locus) {
try {
children[0].addGene((Gene)mother.getGene(index).clone());
children[1].addGene((Gene)father.getGene(index).clone());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
else {
try {
children[0].addGene((Gene)father.getGene(index).clone());
children[1].addGene((Gene)mother.getGene(index).clone());
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
if(World.IS_TEST)System.out.println("Child 0: "+children[0]);
if(World.IS_TEST)System.out.println("Child 1: "+children[1]);
return children;
}