Java中的两点交叉

发布于 2025-01-06 09:32:44 字数 1287 浏览 1 评论 0 原文

我已经实现了一点交叉,如下所示;

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 = 染色体,亲本 2 = indi。

我正在把父母原地变成孩子。

我现在还需要进行两点交叉,但遇到了一些麻烦,这是我到目前为止所遇到的,但我相信代码的下半部分正在做与单点交叉相同的事情,而不是交换中间部分。

       public void twoPointCrossover(Individual indi) {
        if (SGA.rand.nextDouble() < pc) {

            int xoverpoint = SGA.rand.nextInt(length);
            int xoverpoint2 = SGA.rand.nextInt(length);



            int tmp;

            if (xoverpoint > xoverpoint2){
                tmp = xoverpoint;
                xoverpoint = xoverpoint2;
                xoverpoint2 = tmp;
            }

            for (int i=xoverpoint; i<xoverpoint2; i++){
                tmp = chromosome[i];
                chromosome[i] = indi.chromosome[i];
                indi.chromosome[i] = tmp;
            }   
        }   
    }
}

这似乎不对,非常感谢任何帮助!谢谢!

I have implemented a one point crossover as follows;

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

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.

I now need to also do a two point crossover but having some trouble, this is what I have so far but I believe the bottom half of the code is doing the same thing as a one point crossover rather than swapping the middle sections.

       public void twoPointCrossover(Individual indi) {
        if (SGA.rand.nextDouble() < pc) {

            int xoverpoint = SGA.rand.nextInt(length);
            int xoverpoint2 = SGA.rand.nextInt(length);



            int tmp;

            if (xoverpoint > xoverpoint2){
                tmp = xoverpoint;
                xoverpoint = xoverpoint2;
                xoverpoint2 = tmp;
            }

            for (int i=xoverpoint; i<xoverpoint2; i++){
                tmp = chromosome[i];
                chromosome[i] = indi.chromosome[i];
                indi.chromosome[i] = tmp;
            }   
        }   
    }
}

This does not seem right and any help will be appreciated so much! Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪我终i 2025-01-13 09:32:44

您应该检查 i < (或 <=) xoverpoint2 而不是循环中的 i

You should check for i < (or <=) xoverpoint2 rather than i<length in the loop.

擦肩而过的背影 2025-01-13 09:32:44

我现在正在解决同样的问题。这是我的解决方案:

// Two-Point Crossover function
public Genome twoPtCrossover(Genome partner) {
    Genome child = new Genome(partner.genome.length);

    int crosspoint1 = xd.nextInt(genome.length);
    int crosspoint2 = xd.nextInt(genome.length);

    // Ensure crosspoints are different...
    if (crosspoint1 == crosspoint2){
        if(crosspoint1 == 0){
            crosspoint2++;
        } else {
            crosspoint1--;
        }
    }
    // .. and crosspoint1 is lower than crosspoint2
    if (crosspoint2 < crosspoint1) {
        int temp = crosspoint1;
        crosspoint1 = crosspoint2;
        crosspoint2 = temp;
    }

    for (int i = 0; i < genome.length; i++) {
        if (i < crosspoint1 || i > crosspoint2)
            child.genome[i] = genome[i];
        else
            child.genome[i] = partner.genome[i];
    }
    return child;
}

I'm working on the same problem now. Here is my solution:

// Two-Point Crossover function
public Genome twoPtCrossover(Genome partner) {
    Genome child = new Genome(partner.genome.length);

    int crosspoint1 = xd.nextInt(genome.length);
    int crosspoint2 = xd.nextInt(genome.length);

    // Ensure crosspoints are different...
    if (crosspoint1 == crosspoint2){
        if(crosspoint1 == 0){
            crosspoint2++;
        } else {
            crosspoint1--;
        }
    }
    // .. and crosspoint1 is lower than crosspoint2
    if (crosspoint2 < crosspoint1) {
        int temp = crosspoint1;
        crosspoint1 = crosspoint2;
        crosspoint2 = temp;
    }

    for (int i = 0; i < genome.length; i++) {
        if (i < crosspoint1 || i > crosspoint2)
            child.genome[i] = genome[i];
        else
            child.genome[i] = partner.genome[i];
    }
    return child;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文