だから私は現世代で最高の両親の2人から子供を作っています. アイデアは、親 1 からルートの前半のランダムな量を取得し、親 2 からルートの後半のランダムな量を取得することです。
重複を防止しようとすると問題が発生すると思います。したがって、私の出力は、残りの人口と同じようです。母集団の最初の世代は完全にランダムですが (私がテストしました)、常に同じ数です。第一世代の距離はランダムに異なるように見えます.第二世代はすべてのルートですべて同じ距離ですが、実行するたびに変化します. 第 3 世代には同一の親と子がいます。したがって、基本的にゆっくりと同じ数に収束します。15977.582173243769 . number は奇妙なものだと確信していますが、実行時エラーのみが発生し、コンパイラに関する問題は発生しません。他のコードが必要な場合は、それも投稿できます。
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;