?? wx_crossover_function.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 + -