?? main.m
字號:
close all
clear all
clc
% set parameter
M=80;
M2=M/10;
M3=M-M2;
T=20;
Pc=0.6;
Pm=0.004;
N=23;
Len=7;
Lenth=N*Len;
X=zeros(M,N);
Chro=round(rand(M,Lenth));
Chro1=zeros(1,Lenth);
X=DecodeChro(Chro,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(Chro,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));
Chro1=Chro(ind(1),:);
Sel=ceil(rand(M3,1)*M);
for j=1:M3
if Fitness(ind(M2+j)) > Fitness(Sel(j))
ind(M2+j)=Sel(j);
end
end
Chro=Chro(ind(1:M),:);
% crossover operation
IndexPair=zeros(M,1);
for j=1:M
IndexPair(j)=j;
end
for j=1:M
Point=floor((M-j-1)*rand(1));
Temp = IndexPair(j) ;
IndexPair(j) = IndexPair( j + Point );
IndexPair(j+Point) = Temp;
end
for j=1:2:(M-1)
Randp=rand(1);
if Randp<Pc
P=ceil(rand(1)*(Lenth-Len));
for k=P:(P+Len-1)
Temp = Chro(j,k) ;
Chro(j,k) = Chro(j+1,k) ;
Chro(j+1,k) =Temp;
end
end
end
% mutation
for j=1:M
for k=1:Lenth
Randp=rand(1);
if Randp<Pm
if Chro(j,k)==1
Chro(j,k)=0;
else
Chro(j,k)=1;
end
end
end
end
Chro(M,:)=Chro1(1,:);
end
X=DecodeChro(Chro,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 + -