Uniform Crossover in Java

情到浓时终转凉″ 提交于 2019-12-12 03:23:28

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/9318881/uniform-crossover-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!