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

📄 crossover_2genetic.m

📁 改进的基于遗传算法的被动声目标定位
💻 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 + -