我能够按如下方式对1-point crossover
进行编码:
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))];
任何人都可以帮忙将其转换为uniform crossover
吗?
答案 0 :(得分:0)
我没有Matlab来检查代码,但它应该是这样的:
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);