MATLAB中的统一交叉

时间:2016-10-12 13:11:13

标签: matlab genetic-algorithm uniform crossover

我能够按如下方式对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吗?

1 个答案:

答案 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);