⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multgas_graycoded.m

📁 GA的源程序,运行过,绝对可以,对于学习GA的人可以参考试用
💻 M
字号:
function MULTGAS()

%problem2;
vlb = [-5,-5];
vub = [5,5];
precision =[5,5];
lbits = decimallbits(precision,vlb,vub);


%----example in notes;
%vlb = [-3.0,4.1];
%vub = [12.1,5.8];
%lbits=[18,15];
pc=0.5;
pm=0.01;
numgens=50;
popnsize=50;
[xpopn,fitness,meanf,maxf,xopt]=MulvariableGA(popnsize,lbits,pc,pm,numgens,vlb,vub);
%plot the mesh plot
step=0.02;
[X,Y] =meshgrid(vlb(1):step:vub(1),vlb(2):step:vub(2));

Z = 20*exp(-0.2 * sqrt(0.5 *(X.^2 +Y.^2)))+...
    exp(0.5*(cos(2*pi*X)+cos(2*pi*Y))) -21*exp(-1);%problem2;
%Z =21.5 +X .*sin(2*pi*X) + Y .*sin(10*pi*Y);        %Example in notes
mesh(X,Y,Z);
surf(X,Y,Z);
xlabel('x1');
ylabel('x2');
zlabel('FValue');
Title('Mesh plot of the function f(x1,x2)');


%Mesh plot End..................................................


function [xpopn,fitness,meanf,maxf,xopt]=MulvariableGA(popnsize,lbits,pc,pm,numgens,vlb,vub);
%onevar SimpleGA minimises a simple function of one variable coded as an unsigned binary integer;
meanfhistory=[];
maxfhistory=[];
xopthistory=[];
%Generate the initial population;
gen=0;
popn=Initialise(popnsize,lbits);

%Binary code convert to Gray code;
   popn1=popn;
   popn(:,1)=popn1(:,1);
   for i=2:size(popn,2)
      popn(:,i)=xor(popn1(:,i-1),popn1(:,i));
   end;

%Obtain fitness statistics for the population and save the history parameter for use in report;
[xpopn,fitness,xopt,maxf,meanf]=Evaluate(popn,lbits,vlb,vub);
xopthistory=[xopthistory;xopt];
maxfhistory=[maxfhistory;maxf];
meanfhistory=[meanfhistory;meanf];

%call Report for the printout of the initial population and the initial population statistics;
Report(gen,popn,xpopn,fitness,meanfhistory,maxfhistory,xopthistory);

%Main generation loop;
for gen=1:numgens
   % Reproduce;
   matingpairs=Reproduce(popn,fitness);
   %Crossover;
   offspring=Crossover(matingpairs,pc);
   %Mutate
   popn=Mutation(offspring,pm);
   %Obtain fitness siatistics for the current population and save the history parameters for use in report;
  [xpopn,fitness,xopt,maxf,meanf]=Evaluate(popn,lbits,vlb,vub);
   xopthistory=[xopthistory;xopt];
   maxfhistory=[maxfhistory;maxf];
   meanfhistory=[meanfhistory;meanf];
end; 
%call Report for the printout of the final population and a summary of the population statistics;
%over all generation;
gen=numgens;
Report(gen,popn,xpopn,fitness,meanfhistory,maxfhistory,xopthistory);

xopt=xopthistory;
maxf=maxfhistory;
meanf=meanfhistory;
%End of Multivariablega;

%..........................................................

function popn=Initialise(popnsize,lbits);
%Initialse generate the initial population;
slbits=sum(lbits);
popn=rand(popnsize,slbits)<0.5;
%End of Initialize; 

%.................................................

function [xpopn,fitness,xopt,maxf,meanf]=Evaluate(popn,lbits,vlb,vub);
%Evaluate returns the fitness of the current population after first ;
%decodings, together with other population statistics;

xpopn=Decodemult(popn,lbits,vlb,vub);
fitness=Objfun(xpopn,lbits);
meanf=sum(fitness)/size(popn,1);
[maxf,imax]=max(fitness);
xopt=xpopn(imax,:);
%End of Evaluate;

%..................................................

function xpopn=Decodemult(popn,lbits,vlb,vub);
% Decode converts a population(popn)of string to real;
% Each string in popn is of length sum(lbits) and consists of m=length(lbits);
% substrings which decode to the variance x1,x2,....,xm;

% First decode each substring which decode to an unsigned decimal injterger:xint

index1=1;
index2=0;
for i=1:length(lbits)
   index2=index2+lbits(i);
   twopowers=2.^(lbits(i)-1:-1:0);
   xint(:,i)=popn(:,index1:index2)*twopowers';
   index1=index1+lbits(i);
end;
% Now calculate the x values;
factor=(vub-vlb)./(2.^lbits-1);
xpopn=ones(size(popn,1),1)*vlb+xint*diag(factor);
% End of Decode;

%......................................................

function fitness=Objfun(xpopn,lbits);
%Objfun evaluates the objective function(fitness)values of a decoded populaion;
%note;
x1=xpopn(:,1);
x2=xpopn(:,2);
%fitness=21.5*ones(size(xpopn,1),1)+x1.*sin(2*pi*x1)+x2.*sin(10*pi*x2);
%problem2;
fitness = 20*exp(-0.2 * sqrt(0.5 *(x1.^2 +x2.^2)))+...
    exp(0.5*(cos(2*pi*x1)+cos(2*pi*x2))) -21*exp(-1)*ones(size(xpopn,1),1);
%End of Objfun

%.........................................................


function Report(gen,popn,xpopn,fitness,meanfhistory,maxfhistory,xopthistory);
%Report the ouputs population(string and reals) and the fitness values for the current generation;
%together with the history of mean and maxium fitness values;

disp('  ');disp('  ');disp('  ');
disp([sprintf('      Generation %5.0f',gen)]);
disp(' ');
disp([BLANKS(10) 'string'  BLANKS(35)  'x'  BLANKS(22)  'fitness']);
%Compress the matrix popn into the a vector of character strings;
popnstring=char(48+popn);
%Oupt the current population details;
for i=1:size(popn,1)
   disp([popnstring(i,:),sprintf('%16.8g %16.8g',xpopn(i,:),fitness(i))]);
end;

disp('  ');
disp('         Fitness  History Statistics');
disp('Generation       Mean Fitness     Max  Fitness      Optimal x');
history=[[0:gen]',meanfhistory,maxfhistory,xopthistory];
disp([sprintf('  %5.0f %16.6g %16.6g %16.6g %16.6g\n',history')]);
mean(history)
%End of Report;

%..............................................................

function [matingpairs,select]=Reproduce(popn,fitness);
%Reproduce uses roulette wheel selecton to produce the mating pairs;
%for crossover;
%select contains the numbers of each string in popn which has been added to the mating pool.
normfit=fitness/sum(fitness);
partsum=0;
randnums=rand(size(fitness));
count(1)=0;
matepool=[];
for i=1:length(fitness)
   partsum=partsum+normfit(i);
   count(i+1)=length(find(randnums<partsum));
   select(i,1)=count(i+1)-count(i);
   matepool=[matepool;ones(select(i,1),1)*popn(i,:)];
end;
%Now are --order the strings for the mating so that the string in row one is mated with the string two,etc;
[junk,mating]=sort(rand(size(matepool,1),1));
matingpairs=matepool(mating,:);
%End of Reproduce;

%..................................................................

function offspring=Crossover(popn,pc);
%Crossover creates offspring from a population(ordered mating pool);
%Use crossover with probability pc;
%Crossover sites are chosen at random,so there are half as many sites;
%as there are string in the population;
lbits=size(popn,2);
sites=ceil(rand(size(popn,1)/2,1)*(lbits-1));
sites=sites.*(rand(size(sites))<pc);
for j=1:length(sites)
   offspring(2*j-1,:)=[popn(2*j-1,1:sites(j)),popn(2*j,sites(j)+1:lbits)];
   offspring(2*j,:)=[popn(2*j,1:sites(j)),popn(2*j-1,sites(j)+1:lbits)];
end;
%end of Crossover;

%..................................................................

function newpopn=Mutation(offspring,pm);
%Mutation changes a gene of the offspring with probability pm;
mutate=find(rand(size(offspring))<pm);
%Mutate contains the poditions of the genes to be mutated as a column vector;going down the colums the 
%matric of spring;
newpopn=offspring;
newpopn(mutate)=1-offspring(mutate);
%End of Mutation;

%....................................................................
function lbits = decimallbits(precision,vlb,vub)
%lbits: [lbits1,lbits2,...]
%precision is row vector,

lbits = ceil(log((vub-vlb) .* (10.^precision)+ones(1,size(vlb,2)))/log(2));
% End of decimal2lbits

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -