Java 中的统一交叉
我在 java 中实现统一交叉时遇到问题。这就是算法;
// Uniform Crossover
public void UniformCrossover(Individual indi) {
if (RVGA.rand.nextDouble() < pc) {
// Put your implementation of uniform crossover here
// For each gene create a random number in [0, 1].
// If the number is less than 0.5, swap the gene values in
// the parents for this gene; other wise, no swapping .
}
我知道我可以 int tmp 并存储随机数,然后 if tmp 0.5
继续循环
我无法开始任何帮助,不胜感激!
这是我的单点交叉示例,以便您了解我的格式。
一点交叉 - 选择交叉点,从染色体开始到交叉点的二进制字符串从一个亲本复制,其余部分从第二个亲本复制。
亲本 1 = 染色体,亲本 2 = indi。
我正在把父母原地变成孩子
public void onePointCrossover(Individual indi) {
if (SGA.rand.nextDouble() < pc) {
int xoverpoint = SGA.rand.nextInt(length);
int tmp;
for (int i=xoverpoint; i<length; i++){
tmp = chromosome[i];
chromosome[i] = indi.chromosome[i];
indi.chromosome[i] = tmp;
}
}
}
I am having trouble implementing a uniform crossover in java. This is the algorithm;
// Uniform Crossover
public void UniformCrossover(Individual indi) {
if (RVGA.rand.nextDouble() < pc) {
// Put your implementation of uniform crossover here
// For each gene create a random number in [0, 1].
// If the number is less than 0.5, swap the gene values in
// the parents for this gene; other wise, no swapping .
}
I know I can int tmp
and store random number, then if tmp < 0.5
continue with loop
I couldn't manage to make a start any help is appreciated!
This is an example of my one point Crossover just so you know my format.
One point crossover - crossover point is selected, binary string from beginning of chromosome to the crossover point is copied from one parent, the rest is copied from the second parent.
Parent 1 = chromosome and Parent 2 = indi.
I am turning the parents into children inplace
public void onePointCrossover(Individual indi) {
if (SGA.rand.nextDouble() < pc) {
int xoverpoint = SGA.rand.nextInt(length);
int tmp;
for (int i=xoverpoint; i<length; i++){
tmp = chromosome[i];
chromosome[i] = indi.chromosome[i];
indi.chromosome[i] = tmp;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于统一交叉,您通常要做的是:
从您的单点示例来看,您似乎同时就地修改了父母双方。在这种情况下:
With uniform crossover, what you want to do in general is:
You seem, from your one-point example, to be modifying both parents in-place at the same time. In which case: