【发布时间】:2015-07-03 07:28:42
【问题描述】:
所以我要从当前这一代最好的 2 位父母中培养一个孩子。这个想法是从父 1 中随机抽取前半部分的路径,从父 2 中抽取后半部分的随机数量。
我认为当我尝试防止重复时会出现问题。所以我的输出对于其他人来说似乎是一样的。总是相同的数字,即使第一代人口是完全随机的(我已经测试过)。第一代的距离似乎随机不同,第二代每条路线的距离都相同,但每次我运行它都会改变。第三代有相同的父母和孩子。所以它基本上会慢慢收敛到相同的数字。 15977.582173243769。我很确定这个数字很奇怪,但我们只会得到运行时错误,没有编译器的东西。如果需要其他代码,我也可以发布。
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;
【问题讨论】:
-
您能否解释一下您的结果是什么,以及预期的结果是什么?或者你有任何错误会运行你的代码?
-
“我正在从当前这一代最优秀的 2 位父母中生一个孩子”——您是否只为下一代生了 1 个孩子?还是很多?之后父母会怎样?
-
预期的是一个对象(命名为遗传) 该对象有一个大小为 80 的数组,包含访问过的城镇,以及所有 80 个城镇之间的总距离。 @JFPicard我们只有在代码运行后才会出现错误,我们知道这是错误的,因为数字应该都不同,但都是相同的。结果是,每次我们通过该函数运行 2 个完全不同的父母时,都会得出相同的结果。每一次。即使父母是 100% 随机的(我们已经验证父母在传入之前是随机的)@Sasha Salauyou 我们正在尝试生成 1 个子对象。
-
父母的总距离似乎总是15977.582173243769。不管上一代路由多么随机。
标签: java genetic-algorithm crossover