gpt4 book ai didi

java - 交叉算法实现

转载 作者:搜寻专家 更新时间:2023-11-01 03:14:00 25 4
gpt4 key购买 nike

我开始在这里深入研究 GA 进行研究,但我似乎无法找到交叉生成断点的答案。例如,如果我从 parent 开始:
Father = [A,B,B,A,C]<br/>
Mother = [D,D,B,A,A]

我什么时候可以合法地停止生育 child 以证明所有可能的组合都已用尽?代码如下:

void reproduce(String[] father, String[] mother) {<br/>
double choice = Math.random() * 100;<br/>
if((int) choice % 10 < 2){<br/>
//start at father[1] and swap.<br/>
//Continue for other choices

这是关于我正在使用的逻辑的一小部分。所以我的问题又回到了,我如何合法地确定何时停止生育 child ?或者这只是一个数学问题,我应该只看一个直接的排列生成器而暂时忽略 GA?

最佳答案

首先,这应该是从 parent 那里生出 child 的一个不错的方法。这是单点交叉。

public String[] reproduce(String[] father, String[] mother) {
int[] child=new String[father.length];
int crossPoint = Math.random()*father.length;//make a crossover point
for (int i=0;i<father.length;++i)
{
if (i<crossPoint)
child[i]=father[i];
else
child[i]=mother[i];
}
return child;
}

没有咖啡,所以不能保证。您可能需要检查是否存在差一错误。

关于java - 交叉算法实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3852074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com