Java中的统一交叉 [英] Uniform Crossover in Java
问题描述
我在用Java实现统一转换时遇到麻烦.这是算法;
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 .
}
我知道我可以int tmp
并存储随机数,然后if tmp < 0.5
继续循环
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.
父1 =染色体,父2 =个体.
Parent 1 = chromosome and Parent 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;
}
}
}
推荐答案
使用统一分频,您通常要做的是:
With uniform crossover, what you want to do in general is:
For each gene
if rand()<0.5
take from parent a
else
take from parent b
从您的单点示例来看,您似乎正在同时修改两个父级.在这种情况下:
You seem, from your one-point example, to be modifying both parents in-place at the same time. In which case:
For each gene
if rand()<0.5
leave both parents alone
else
swap chromosome[i] with indi.chromosome[i] as before
这篇关于Java中的统一交叉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!