Java 中的统一交叉

发布于 2025-01-06 08:24:55 字数 1069 浏览 1 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(1

捶死心动 2025-01-13 08:24:55

对于统一交叉,您通常要做的是:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文