Java中的统一交叉

时间:2012-02-16 20:53:47

标签: java genetic-algorithm crossover

我在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继续循环

我无法开始任何帮助表示赞赏!

这是我的一点Crossover的一个例子,只是让你知道我的格式。

选择一点交叉 - 交叉点,从一个父项复制从染色体开始到交叉点的二进制字符串,其余字符串从第二个父项复制。

父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;
        }   
    }   
}

1 个答案:

答案 0 :(得分:1)

通过统一的交叉,你想要做的一般是:

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
相关问题