?? mainais.m
字號:
close all
clear all
clc
% set parameter
M=80;
M2=30;
Mc=M/20;
M3=M-M2-Mc;
T=1000;
Pc=0.6;
Pm=0.6;
b=1.5;
N=23;
Len=7;
Lenth=N*Len;
X=zeros(M,N);
Ab1=round(rand(M,Lenth));
X=DecodeChro(Ab1,M,Lenth,N,Len);
X=round(X.*100/127);
MinFitness=zeros(1,T);
AveFitness=zeros(1,T);
for Gen=1:T
% Calculate Object fitness
X=DecodeChro(Ab1,M,Lenth,N,Len);
X=round(X.*100/127);
Fitness=CalculateObjectValue(X,M,N);
Sum=sum(Fitness);
AveFitness(Gen)=Sum/M;
% select operation
[Cost,ind]=sort(Fitness);
MinFitness(Gen)=Fitness(ind(1));
Ab2=Ab1(ind(1:M2),:);
Ab4=Ab1(ind(1:M3),:);
% clone operation
Ms=0;
s=0;
for j=1:M2
Ms1=round(b*M2/j);
Ms=Ms+Ms1;
for k=1:Ms1
s=s+1;
Ab3(s,:)=Ab2(j,:);
end
end
% mutation operation
for j=1:Ms
for k=1:Lenth
Randp=rand(1);
if Randp<Pm
if Ab3(j,k)==1
Ab3(j,k)=0;
else
Ab3(j,k)=1;
end
end
end
end
X3=DecodeChro(Ab3,Ms,Lenth,N,Len);
X3=round(X3.*100/127);
Fitness3=CalculateObjectValue(X3,Ms,N);
% select from ab3
[Cost3,ind3]=sort(Fitness3);
Ab5=Ab3(ind3(1:M2),:);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Ab6=round(rand(M,Lenth));
X6=DecodeChro(Ab6,M,Lenth,N,Len);
X6=round(X6.*100/127);
Fitness6=CalculateObjectValue(X6,M,N);
[Cost6,ind6]=sort(Fitness6);
Ab7=Ab6(ind6(1:Mc),:);
% generate new colony
Ab1=[Ab4;Ab5;Ab7];
end
X=DecodeChro(Ab1,M,Lenth,N,Len);
X=round(X.*100/127);
figure(1)
hold on
plot( MinFitness,'r'),
plot(AveFitness,'b');
hold off
Fitness1=CalculateObjectValue(X,M,N);
[Cost1,ind1]=sort(Fitness1);
BestX=zeros(1,N);
BestX=X(ind1(1),:);
BestValue=zeros(1,101);
s=0;
for u=0:0.01:1
s=s+1;
BestValue(1,s)=1+exp(i*100*pi*u);
for n=1:N
BestValue(1,s)=BestValue(1,s)+exp(i*(pi*BestX(1,n)*u));
end
BestValue(1,s)=abs(BestValue(1,s));
end
figure(2)
f=0:0.01:1;
BestValue=BestValue./25;
BestValue=20*log10(BestValue);
plot(f,BestValue),grid;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -