Question

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;
        }   
    }   
}
Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top