You are on page 1of 2

4/13/2017 GENETIC_ALGORITHM_BLI_WILSON_KWESI

%**************************************************
%STUDENTNAMEBLIWILSONKWESI
%JIANGSUUNIVERSITYOFSCIENCEANDTECHNOLOGY
%MAJORMSC.INFORMATIONCOMMUNICATIONENGINEERING
%YEARONE
%****************************************************
%Contents
%*********
%OPTIMIZATIONOFRANDOMSIGNALSUSINGGENETICALGORITHM
%INITIALIZATIONSTAGE
%FITNESSFUNCTION
%SELECTIONSTAGE
%CROSSOVERSTAGE
%MUTATIONSTAGE
clearall
closeall
clc
%OPTIMIZATIONOFRANDOMSIGNALSUSINGGENETICALGORITHM

%INITIALIZATIONSTAGE

D=5;%Dimension
N=10;%Populationsize
tau=2;%delayof2seconds
maxG=5;%maxgenerationThiscanbechangedtogivebetterresults
Best=zeros(maxG,D);%initializingthebestpopulationtozeros
%FITNESSFUNCTION

x=rand(N,D);
random_signal=exp(tau)*sin(pi*x);%randomsinewavewith2secdelay
P=random_signal;%Population
%SELECTIONSTAGE

[ord,ind]=sort(P(:,D));
Best(1,:)=P(ind(1),:);
foriter=2:maxG
iter;

S=sum(1./P(:,D));%Inversefunctionvalue
R=1./P(:,D)/S;%Possibilityofchosen
R=cumsum(R);%Accumlatedpossibility
Pn=zeros(N,D);%Anewemptypopulation
fori=1:N
r=rand;%createarandomnumber
k=1;%Index
whileR(k)<r
k=k+1;
end
Pn(i,:)=P(k,:);
end
P=Pn;%Replacetheoldpopulation
%CROSSOVERSTAGE

Nn=N;
P=[];%createemptypopulation
fori=1:floor(N/2)%fromfirsttransmitedsignalof
%populationtothehalfwaysignal
n=ceil(rand*Nn);%pickarandomsignaltocrossover
Nn=Nn1;

file:///E:/Software/Matlab%20Lectures/html/GENETIC_ALGORITHM_BLI_WILSON_KWESI.html 1/2
4/13/2017 GENETIC_ALGORITHM_BLI_WILSON_KWESI
C1=Pn(n,:);
Pn(n,:)=[];
m=ceil(rand*Nn);
Nn=Nn1;
C2=Pn(m,:);
Pn(m,:)=[];
r=ceil(rand*(D2));%Chosewhichpositiontocrossover
temp=C1(1:r);
C1(1:r)=C2(1:r);
C2(1:r)=temp;
P=[P;C1;C2];%Storetheresultsofcrossover
end
%MUTATIONSTAGE

fori=1:N
r=ceil(rand*(D1));
P(i,r)=P(i,r)+randn;%Addnormaldistribution
end

fori=1:N%Sincewewanttocreate
%arandomsignal,wechoose
%signalsbetweenzerosandone
forj=1:D1
ifP(i,j)>1
P(i,j)=1;
elseifP(i,j)<0
P(i,j)=0;
end
end
end

[ord,ind]=sort(P(:,D));%sortoutthenewsignalpopulution
ifBest(iter1,D)>P(ind(1),D)%Ifbetterresultsfound
Best(iter,:)=P(ind(1),:);%thenreplacebestsofar
else%otherwiseusetheoldbestsofarvalue
%asthebestvalueofthisgeneration
Best(iter,:)=Best(iter1,:)*randn+max(P);%applynormal
%distributiontothe
%bestpopulation
end
end

PublishedwithMATLABR2014a

file:///E:/Software/Matlab%20Lectures/html/GENETIC_ALGORITHM_BLI_WILSON_KWESI.html 2/2

You might also like