📄 my_sga_haimingcrossover.asv
字号:
function [newpop]=my_sga_haimingcrossover(popsize,chromlength,pop,pc,i); %交叉
%函数说明
%入口参数:pop 当前处理的种群,
% popsize 种群大小,
% chromlength 基因位长度,
% up_range 自变量的相对上限,
% down_range 自变量的相对下限
%出口参数:best_fit 各代最有适应度值的集合
%功能说明:初始种群pop通过遗传交叉变异在给定定义域中寻优,在遗传最大代数dai之前得到最优值就自动退出遗传,
% 判定最优值的标准:前后两代间最优值之间差值连续30次小于 阈值0.00001
% 如果达到遗传最大代数也没有达到退出标准,则强制结束遗传。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%调试用语句
% popsize=16;
% n=1024;
% pop1_init=randperm(n); %产生1到32的整数
% pop2_init=randperm(n);
% pop(1,:)=pop1_init(1:popsize); %将前几个整数作为初始群体
% pop(2,:)=pop2_init(1:popsize);; %将前几个整数作为初始群体
%
%
% pop;
% chromlength=10;
% pc=0.96;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dai_t=i;
pop1=pop(1,:); %将种群矩阵拆分
pop1;
pop2=pop(2,:);
pop2;
pop1=dec2bin(pop1,chromlength); %%各自转为二进制
newpop1=pop1;
pop2=dec2bin(pop2,chromlength);
newpop2=pop2;
defference=0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% pop1
T=1000;
haiming_sum=0; % haiming_sum记录i个体与i以后个体的海明距离总和
haiming_num=0; % haiming_num记录计算海明距离的次数
haiming_ave=0; % 平均汉明距离
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 平均海明距离
for i=1:popsize-1
for j=i+1:popsize
for jj=i:chromlength
if pop1(i,jj)~=pop1(j,jj)
haiming_sum=haiming_sum+1;
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 计算haiming_num
for i=1:popsize-1
haiming_num=haiming_num+(popsize-i);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
haiming_ave=haiming_sum/haiming_num;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 平均海明距离计算 完毕!!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 计算海明交叉阈值
haiming_T=exp(-(dai_t)^2/(2*(T/3)^2))*haiming_ave;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 海明交叉阈值计算 完毕!!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% pop1完毕
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:2:popsize-1; %第1行和第2行,也就是说第1个个体和第2个个体进行交叉,依次类推
if(rand<pc)
for j=i+1:popsize
% defference=xor(pop1(i,:),pop1(j,:));
for jj=i:chromlength
if pop1(i,jj)~=pop1(j,jj)
defference=defference+1;
end
end
% defference
if defference>haiming_T
cpoint=round(rand*chromlength);
% cpoint
if cpoint==0
cpoint=1;
end
newpop1(i,:)=[pop1(i,1:cpoint) pop1(j,cpoint+1:chromlength)];
newpop1(j,:)=[pop1(j,1:cpoint) pop1(i,cpoint+1:chromlength)];
break;
% else
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% pop1
T=1000;
haiming_sum=0; % haiming_sum记录i个体与i以后个体的海明距离总和
haiming_num=0; % haiming_num记录计算海明距离的次数
haiming_ave=0; % 平均汉明距离
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 平均海明距离
for i=1:popsize-1
for j=i+1:popsize
for jj=i:chromlength
if pop2(i,jj)~=pop2(j,jj)
haiming_sum=haiming_sum+1;
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 计算haiming_num
for i=1:popsize-1
haiming_num=haiming_num+(popsize-i);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
haiming_ave=haiming_sum/haiming_num;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 平均海明距离计算 完毕!!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 计算海明交叉阈值
haiming_T=exp(-(dai_t)^2/(2*(T/3)^2))*haiming_ave;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 海明交叉阈值计算 完毕!!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% pop1完毕
if(rand<pc)
for j=i+1:popsize
for jj=i:chromlength
if pop2(i,jj)~=pop2(j,jj)
defference=defference+1;
end
end
if defference>haiming_T
cpoint=round(rand*chromlength);
% cpoint
if cpoint==0
cpoint=1;
end
newpop2(i,:)=[pop2(i,1:cpoint) pop2(j,cpoint+1:chromlength)];
newpop2(j,:)=[pop2(j,1:cpoint) pop2(i,cpoint+1:chromlength)];
break;
% else
end
end
end
newpop1;
newpop2;
end
newpop1=bin2dec(newpop1);
newpop2=bin2dec(newpop2);
newpop(1,:)=newpop1';
newpop(2,:)=newpop2';
newpop;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -