0

I have been able to code a 1-point crossover as follows:

n_parents = length(parents);
randparents = randperm(n_parents);

parent1 = population.ind{parents(randparents(1))};
parent2 = population.ind{parents(randparents(2))};

cross_point = ceil(rand()*(size(parent1,2)-1));
child = [parent1(1:cross_point),parent2(cross_point+1:size(parent2,2))];

Can anyone help in converting this to a uniform crossover please?

Uttara
  • 125
  • 1
  • 1
  • 16
  • What is a "uniform crossover"? – rayryeng Oct 12 '16 at 14:25
  • @raryeng check this out:https://en.wikipedia.org/wiki/Crossover_(genetic_algorithm)#Uniform_crossover_and_half_uniform_crossover – Uttara Oct 12 '16 at 14:31
  • Sorry, but no thanks. I won't answer a question if it requires me to go off-site and read up on something. Please don't take any offense. It has nothing to do with you personally and this is how I've always operated. Good luck. – rayryeng Oct 12 '16 at 14:45

1 Answers1

0

I have no Matlab to check the code, but it should be something like this:

parent1 = population.ind{parents(randparents(1))};
parent2 = population.ind{parents(randparents(2))};

rand_vec = rand(size(parent1));
child1 = parent1;
child1(rand_vec>=0.5) = parent2(rand_vec>=0.5);
child2 = parent2;
child2(rand_vec>=0.5) = parent1(rand_vec>=0.5);
Steffen
  • 2,381
  • 4
  • 20
  • 33