    ![求各位牛人給看一下程序(見附件)。matlab遺傳算法實現(xiàn)函數(shù)優(yōu)化,好像陷入了局部最優(yōu)]()
原始數(shù)據(jù).png
程序如下:
%function result=sga(n,a,b,pc,pm,e)
clear;
%n—群體規(guī)模;a—搜索上限;b—搜索下限; %pc—交叉概率;pm—變異概率;e—計算精度;
%clear
E=[12.216,12.678,13.979,14.085,13.428,7.317,9.092,6.64;
20.36,21.13,19.97,15.65,14.92,8.13,7.88,6.64;
6.108,6.339,7.988,12.52,7.46,4.878,4.728,6.64];
IP1=[3355;4500;2385];
IP2=[10,4,3,14,34,120,481,691];
ID=[4,2,2,4,5,7,9,10,0];
ID1=[1,1,1, 1,1,1 ,1,1,1 ,2,2,2 ,2,2,2 ,3,3,3 ,3,3,3 ,3,3,3 ,0];
jieguo=[];
n=10000; %n—群體規(guī)模;
a=111010100100100111101010010010011110101001001001; %a—搜索上限;
b=911010100100100111101010010010011110101001001001; %b—搜索下限;
pc=0.7; %pc—交叉概率;
pm=0.02; %pm—變異概率;
e=1; %e—計算精度;
l=1;
while (b-a)/e>2^(l)
l=l+1;
end
newpopu=[];
popusize=n;
chromlength=l;
j=1;
popu1=[];
Sc=1;
%隨機產生n行m列的初始群體
while j<=100 %設置程序中止條件
j=j+1;
j
if Sc==1
popu=round(rand(popusize,chromlength));
end
py=chromlength;
for i=1:py %進行二進制轉換成十進制的解碼操作
popu1(:,i)=2.^(py-1).*popu(:,i);
py=py-1;
end
popu2=sum(popu1,2);
x=a+popu2*(b-a)/(2^l-1);
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
yvalue=2*x.^2.*cos(3*x)+x.*sin(5*x)+8; %計算群體中每個個體的適應度
Ils=[];
ls=0;
for i=1:size(ID1,2)-1
x=x-ls*10^sum(ID1(i:end));
pf=sum(ID1(i+1:end));
Ils(:,i)=floor(x/(10^pf));
ls=Ils(:,i);
end
Ils1=[];
for i=1:size(Ils,1)
Ils1(:,1)=Ils(i,1:3);
Ils1(:,2)=Ils(i,4:6);
Ils1(:,3)=Ils(i,7:9);
Ils1(:,4)=Ils(i,10:12);
Ils1(:,5)=Ils(i,13:15);
Ils1(:,6)=Ils(i,16:18);
Ils1(:,7)=Ils(i,19:21);
Ils1(:,8)=Ils(i,22:24);
Ism=sum(Ils1,1);
Ils2=round((Ils1./[Ism;Ism;Ism]).*[IP2;IP2;IP2]);
%
if max(sum(Ils2,1)>IP2)==1
yvalue(i,1)=0;
continue
end
if min(sum(Ils2.*E,2)>=IP1)==0
yvalue(i,1)=0;
continue
end
yvalue(i,1)=sum(sum(Ils2.*E));
% if yvalue(i,1)>0
% 1
% end
end
yvalue=yvalue;
if max(yvalue)==0
Sc=1;
continue
else
Sc=0;
end
% yvalue(i,1)=js1*js2/(js13+js23);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% II=II+flipud(II);%依標準差衡量
% yvalue(i,1)=1/sum(std(II,0,2));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% II=II+flipud(II);
% yvalue(i,1)=1/sum(abs(max(II)-min(II)));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
max(yvalue)
for i=1:popusize %執(zhí)行復制操作
if yvalue(i)<0
yvalue(i)=0;
end
end
fitscore=yvalue/sum(yvalue); %個體被選中的概率
fitscore=cumsum(fitscore); % 群體中個體的累積概率
wh=sort(rand(popusize,1)); % 從小到大排列
wheel=1;
fitone=1;
while wheel<=popusize %執(zhí)行轉盤式選擇操作
if wh(wheel)<fitscore(fitone)
newpopu(wheel, =popu(fitone, ;
wheel=wheel+1;
% fitone
else
fitone=fitone+1;
end
end
popu=newpopu;
for i=1:2:popusize-1 %執(zhí)行交叉操作
if rand<pc
cpoint=round(rand*chromlength);
newpopu(i, =[popu(i,1:cpoint) popu(i+1,cpoint+1:chromlength)];
newpopu(i+1, =[popu(i+1,1:cpoint) popu(i,cpoint+1:chromlength)];
else
newpopu(i, =popu(i, ;
newpopu(i+1, =popu(i+1, ;
end
end
popu=newpopu;
for i=1:popusize %執(zhí)行變異操作
if rand<pm
mpoint=round(rand*chromlength);
if mpoint<=0;
mpoint=1;
end
newpopu(i, =popu(i, ;
if newpopu(i,mpoint)==0
newpopu(i,mpoint)=1;
else
newpopu(i,mpoint)=0;
end
else
newpopu(i, =popu(i, ;
end
end
[y(j) index]=max(yvalue); %求出群體中適應值最大的個體及其適應值
bestindividual=newpopu(index, ;
py=chromlength;
for i=1:py %進行二進制轉換成十進制的解碼操作
bestindividual(1,i)=2.^(py-1).*bestindividual(:,i);
py=py-1;
end
r(j)=a+sum(bestindividual,2)*(b-a)/(2^l-1);
popu=newpopu;
% 重新賦值并返回
Ij=x(index, ;
Ij(:,end+1)=max(yvalue);
jieguo=[jieguo;Ij];
save jieguo jieguo
j
end
%計算最大值及其位置 返回優(yōu)化結果
plot(1:length(yvalue),yvalue,'b-',1:length(yvalue),Labels,'r.');
ylabel('yvalue');
xlabel('Pattern Index');
legend('True Labels','Resultant Labels',0); |