⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wx_crossover_function.m

📁 这是在matlab中编写的遗传算法交叉程序
💻 M
字号:
function new_neural_weights=wx_crossover_function(probability_crossover,population_size,neural_weights,length_chromosome,crossover_function_choice)
new_chromosome_count=1;
match_num=probability_crossover*population_size;
match_num=match_num-rem(match_num,1);
rand_num=randperm(population_size);
if mod(match_num,2)~=0
    match_num=(match_num-1)/2;
else
    match_num=match_num/2;
end
% match_num
for i=1:match_num
    % 随机选择交叉点的位置;
    chromosome_position=rand();
    chromosome_position=chromosome_position*(length_chromosome-1)-rem(chromosome_position*(length_chromosome-1),1)+1;
    if chromosome_position==length_chromosome
        chromosome_position=length_chromosome-1;
    end
    % 第一种交叉方法,最简单的;
    if crossover_function_choice==1
        new_neural_weights(new_chromosome_count,:)=[neural_weights(rand_num(new_chromosome_count),1:chromosome_position) neural_weights(rand_num(new_chromosome_count+1),chromosome_position+1:length_chromosome)];
        new_neural_weights(new_chromosome_count+1,:)=[neural_weights(rand_num(new_chromosome_count+1),1:chromosome_position) neural_weights(rand_num(new_chromosome_count),chromosome_position+1:length_chromosome)];
    end
    % 第二种交叉方法:算数交叉(arithmetic crossover)
    if crossover_function_choice==2
        alpha_arithmetic=rand(1,2);
        new_neural_weights(new_chromosome_count,:)=alpha_arithmetic(1)*neural_weights(rand_num(new_chromosome_count),:)-(1-alpha_arithmetic(1))*neural_weights(rand_num(new_chromosome_count+1),:);
        new_neural_weights(new_chromosome_count+1,:)=alpha_arithmetic(2)*neural_weights(rand_num(new_chromosome_count+1),:)-(1-alpha_arithmetic(2))*neural_weights(rand_num(new_chromosome_count),:);
    end
    new_chromosome_count=new_chromosome_count+2;
end
for i=1:population_size-match_num*2
    new_neural_weights=[new_neural_weights;neural_weights(rand_num(match_num*2+i),:)];
end




% function new_neural_weights=one_point_crossover(
%     new_neural_weights(new_chromosome_count,:)=[neural_weights(rand_num(new_chromosome_count),1:chromosome_position) neural_weights(rand_num(new_chromosome_count+1),chromosome_position+1:length_chromosome)];
%     new_neural_weights(new_chromosome_count+1,:)=[neural_weights(rand_num(new_chromosome_count+1),1:chromosome_position) neural_weights(rand_num(new_chromosome_count),chromosome_position+1:length_chromosome)];
% end
%
% for i=1:match_num
%     % 随机选择两个父辈染色体进行交叉chose two parents randomly;
%     for j=1:2
%         parent(j)=rand();
%         parent(j)=parent(j)*population_size-rem(parent(j)*population_size,1)+1;
%         if parent(j)==population_size+1
%             parent(j)=population_size;
%         end
%     end
%     % 随机选择交叉点的位置;
%     chromosome_position=rand();
%     chromosome_position=chromosome_position*(length_chromosome-1)-rem(chromosome_position*(length_chromosome-1),1)+1;
%     if chromosome_position==length_chromosome
%         chromosome_position=length_chromosome-1;
%     end
%     % 第一种交叉方法,最简单的;
%     new_neural_weights(new_chromosome_count,:)=[neural_weights(parent(1),1:chromosome_position) neural_weights(parent(2),chromosome_position+1:length_chromosome)];
%     new_neural_weights(new_chromosome_count+1,:)=[neural_weights(parent(2),1:chromosome_position) neural_weights(parent(1),chromosome_position+1:length_chromosome)];
%     new_chromosome_count=new_chromosome_cout+2;
% end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -