2

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;
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
desy
  • 43
  • 1
  • 7
  • Can you please explain what are your results, and what is expected ? Or any errors you have will running your code ? – JFPicard Apr 24 '15 at 15:41
  • *"I'm making a child from 2 of the best parents in the current generation"*--are you generating just 1 child for next generation? Or many of them? What happens to parents after that? – Alex Salauyou Apr 24 '15 at 15:48
  • What is expected is an object(named genetic) The object has an array of size 80, containing the towns visited, and the total distance between all the 80 towns. @JFPicard We only get bugs after the code has run, we know it's wrong because the numbers should all be different, but are all identical. The results are that every time we run 2 completely different parents through the function the same result comes out. every single time. even if the parents are 100% random(we've verified that the parents are random before they get passed in)@Sasha Salauyou We are trying to generate 1 child object. – desy Apr 27 '15 at 11:31
  • The parents seem to always have a total distance of 15977.582173243769. No matter how random the previous generation of routes are. – desy Apr 27 '15 at 11:31

1 Answers1

0

I noticed that you are using spoint and epoint in the second for loop, so you are using the first part of the route again.

NickD
  • 506
  • 5
  • 15