% GA proba clear clc % minimum and maximum values for the indipendent variable x xMin=0; xMax=255; x=xMin:xMax; % number of individuals in the population N=8; % find the maximum of the following function y=sin((pi*x)/256); % plot(x,y) % axis([xMin-5 xMax+5 min(y)-0.05 max(y)+0.05]) % grid on % hold on y1=inline('sin((pi*x)/256)'); % individuals in decimal form % indsDec=[189 216 99 236 174 74 35 53]; indsDec=floor(511*rand(1,N)); elitismRate=0.25; % currentPopulation=indsDec; avgFitnessX=[]; iter=100; while iter>0 % individuals in binary form % indsBin=dec2bin(indsDec); [selectedInds fitness avgFitness]=selection(indsDec,y1,N); plotIt(indsDec,x,y,y1,fitness); recombinatedInds=recombination(indsDec,selectedInds,N); mutatedInds=mutation(recombinatedInds,N); reproductedInds=reproduction(indsDec, mutatedInds, elitismRate,y1,N); indsDec=reproductedInds; iter=iter-1; if iter==0 break end avgFitnessX(end+1)=avgFitness; clf end figure,plot(avgFitnessX)