1

我无法在 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;
        }   
    }   
}
4

1 回答 1

3

使用统一交叉,您通常想要做的是:

For each gene
  if rand()<0.5
    take from parent a
  else
    take from parent b

从您的单点示例来看,您似乎要同时就地修改两个父母。在这种情况下:

For each gene
  if rand()<0.5
    leave both parents alone
  else
    swap chromosome[i] with indi.chromosome[i] as before
于 2012-02-17T01:58:29.983 回答