so I'm making a child from 2 of the best parents in the current generation. the idea is to take some random amount of the first half of the route from parent 1, and random amount of the second half from parent 2.
I think the problem arises when I try to prevent duplicates. So my output seems to be the same for the rest of the population. always the same number, even though the first generation of the population is completely random(I've tested). the first generations distances seems different randomly, second generation all have the same distance for every route, but it changes every time I run it. the third generation has identical parents and children. so it basically slowly converges to the same number. 15977.582173243769 . I'm pretty sure that number is some weird thing, but we only get runtime errors, no compiler stuff. if the other code is needed I can post that too.
int [] arr = new int [80]; // creating the child array
int spoint = (int)(Math.random()*20); // start point of the crossover from parent 1
int epoint = (int)(Math.random()*20+20); // endpoint of crossover of parent 1
//array for taking the subtour from parent 1 and adding it to the child
for(int i =spoint;i<epoint;i++){
arr[i]=p1.route[i];
}
int spoint1 = (int)(Math.random()*20 +40);
int epoint1 = (int)(Math.random()*20+60);
//parent 2 does the same thing as parent 1 except at the second half of the route.
for(int i =spoint;i<epoint;i++){
arr[i]=p2.route[i];
}
int [] isTown = new int[80];//array of the towns
for(int i=0;i<arr.length;i++){
isTown[arr[i]] = isTown[arr[i]] + 1;//arr is child route
}
//find duplicates and replace with missing towns
for(int i=0;i<isTown.length;i++)
{
if(isTown[i]>1)
{
for(int j =0;j<arr.length;j++)
{
if(arr[j]== i)
{
for(int k =0;k<arr.length;k++)
{
if(isTown[k]==0)
{
arr[j] = arr[k];//swap a repeating town with one that did not occur
}
}
}
}
isTown[i]--;
}
}
double len = 0;
for(int i=1;i<arr.length;i++)
{
len = len + rdMatrix[i-1][i];
}
genetic child = new genetic(arr,len);
return child;