?? crossover_2genetic.m
字號:
%這里采用的是選擇法,選擇最適應的四個個體,進行交叉,并將后四個淘汰
function crossover_population=crossover_genetic(population,fitness_population,p_crossover)
%首先通過選擇法選擇最適應的四個個體,并進行交叉點選取
[sort_population,index]=sort(fitness_population,'descend');
for n1=1:4
select_population(:,n1)=population(:,index(n1));
end
rand_number1=ceil(rand()*4);
rand_number2=ceil(rand()*4);
while rand_number1==rand_number2
rand_number2=ceil(rand()*4);
end
%進行交叉
select1=select_population(:,rand_number1);
select2=select_population(:,rand_number2);
select1_result=[select1(1);select2(2)];
select2_result=[select2(1);select1(2)];
number=[1 2 3 4];
select_population34=find(number~=rand_number1&number~=rand_number2);
select3=select_population(:,select_population34(1));
select4=select_population(:,select_population34(2));
select3_result=[select3(1);select4(2)];
select4_result=[select4(1);select3(2)];
% 并將后四個淘汰,得到交叉后的新群體
select=[select1 select2 select3 select4];
select_result=[select1_result select2_result select3_result select4_result];
% rand_comp1=[1 2 3 4;0 0 0 0];
% rand_comp2=[1 2 3 4;0 0 0 0];
% for n3=1:4
% rand_select=rand();
% rand_selectemp=ceil(rand()*4);
% if rand_select<=p_crossover
% while rand_comp1(2,rand_selectemp)==1
% rand_selectemp=ceil(rand()*4);
% end
% rand_comp1(2,rand_selectemp)=1;
% select_final(:,n3)=select(:,rand_selectemp);
% else
% while rand_comp2(2,rand_selectemp)==1
% rand_selectemp=ceil(rand()*4);
% end
% rand_comp1(2,rand_selectemp)=1;
% select_final(:,n3)=select_result(:,rand_selectemp);
% end
% end
crossover_population=population;
for n=1:4
crossover_population(:,index(n))=select(:,n);
end
population_size=length(population);
for n=(population_size-3):population_size
crossover_population(:,index(n))=select_result(:,n-population_size+4);
end
%進行交叉
% select1=select_population(rand_number1);
% select1_str=num2str(select1);
% select2=select_population(rand_number2);
% select2_str=num2str(select2);
% length12=min(length(select1_str),length(select2_str));
% rand_position12=ceil(rand()*(length12-1));
% temp12=select1_str;
% select1_str=[select1_str(1:(length(select1_str)-rand_position12)) select2_str((length(select2_str)-rand_position12+1):length(select2_str))];
% select2_str=[select2_str(1:(length(select2_str)-rand_position12)) temp12((length(temp12)-rand_position12+1):length(temp12))];
% number=[1 2 3 4];
% select_population34=find(number~=rand_number1&number~=rand_number2);
% select3=select_population(select_population34(1));
% select3_str=num2str(select3);
% select4=select_population(select_population34(2));
% select4_str=num2str(select4);
% length34=min(length(select3_str),length(select4_str));
% rand_position34=ceil(rand()*(length34-1));
% temp34=select3_str;
% select3_str=[select3_str(1:(length(select3_str)-rand_position34)) select4_str((length(select4_str)-rand_position34+1):length(select4_str))];
% select4_str=[select4_str(1:(length(select4_str)-rand_position34)) temp34((length(temp34)-rand_position34+1):length(temp34))];
%
% %并將后四個淘汰,得到交叉后的新群體
% select1_result=str2num(select1_str);
% select2_result=str2num(select2_str);
% select3_result=str2num(select3_str);
% select4_result=str2num(select4_str);
% select=[select1 select2 select3 select4];
% select_result=[select1_result select2_result select3_result select4_result];
% beyond_limit=find(select_result>360);
% for n2=1:length(beyond_limit)
% select_result(beyond_limit(n2))=360;
% end
%
% rand_comp1=[1 2 3 4;0 0 0 0];
% rand_comp2=[1 2 3 4;0 0 0 0];
% for n3=1:4
% rand_select=rand();
% rand_selectemp=ceil(rand()*4);
% if rand_select<=p_crossover
% while rand_comp1(2,rand_selectemp)==1
% rand_selectemp=ceil(rand()*4);
% end
% rand_comp1(2,rand_selectemp)=1;
% select_final(n3)=select(rand_selectemp);
% else
% while rand_comp2(2,rand_selectemp)==1
% rand_selectemp=ceil(rand()*4);
% end
% rand_comp1(2,rand_selectemp)=1;
% select_final(n3)=select_result(rand_selectemp);
% end
% end
%
% crossover_population=population;
%
% for n=1:4
% crossover_population(index(n))=select_final(n);
% end
%
%
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -