📄 paretoga.m
字号:
%PROGRAM 4: Pareto Genetic Algorithm帕累托遗传算法
%
% minimizes the objective function designated in ff
% All optimization variables are normalized between 0
% and 1.
% ff must map variables to actual range
% Haupt & Haupt
% 2003
%_______________________________________________________
% Setup the GA
ff='testfunction'; % objective function
npar=4; % number of optimization variables
%_______________________________________________________
% Stopping criteria
maxit=50; % max number of iterations
mincost=.001; % minimum cost
%_______________________________________________________
% GA parameters
selection=0.5; % fraction of population kept
popsize=100;
keep=selection*popsize;
M=ceil((popsize-keep)/2); % number of matings
odds=1;
for ii=2:keep
odds=[odds ii*ones(1,ii)];
end
odds=keep+1-odds;
Nodds=length(odds);
mutrate=0.1; % mutation rate
%_______________________________________________________
% Create the initial population
iga=0; % generation counter initialized
pop=rand(popsize,npar); % random population of continuous values
fout=feval(ff,pop); % calculates population cost using ff
%_______________________________________________________
% Iterate through generations
while iga<maxit
iga=iga+1; % increments generation counter cost(1:4,:)
[g,ind]=sort(fout(:,1));
pop=pop(ind,:); % sorts chromosomes
h=fout(ind,2);
ct=0; rank=1;
q=0;
while ct<popsize
for ig=1:popsize
if h(ig)<=min(h(1:ig))
ct=ct+1;
q(ct)=ig;
cost(ct,1)=rank;
end
if rank==1
px=g(q);py=h(q);
elite=length(q);
end
if ct==popsize; break; end
end
rank=rank+1;
end
pop=pop(q,:);
figure(1); clf;plot(g,h,'.',px,py); axis([0 1 0 1]);
axis square pause
[cost,ind]=sort(cost);
pop=pop(ind,:);
% tournament selection
Ntourn=2;
picks=ceil(keep*rand(Ntourn,M));
[c,pt]=min(cost(picks));
for ib=1:M
ma(ib)=picks(pt(ib),ib);
end
picks=ceil(keep*rand(Ntourn,M));
[c,pt]=min(cost(picks));
for ib=1:M
pa(ib)=picks(pt(ib),ib);
end
%_______________________________________________________
% Performs mating
ix=1:2:keep; % index of mate #1
xp=floor(rand(1,M)*npar); % crossover point
r=rand(1,M); % mixing parameter
xy=pop(ma+popsize*xp)-pop(pa+popsize*xp);
% mix from ma and pa
pop(keep+ix+popsize*xp)=pop(ma+popsize*xp)-r.*xy;
% 1st offspring
pop(keep+ix+1+popsize*xp)=pop(pa+popsize*xp)+r.*xy;
% 2nd offspring
for ic=1:M/2
if xp(ic)<npar % perform crossover when last
% variable not selected
pop(keep+ix(ic),:)=[pop(ma(ic),1:xp(ic))
pop(pa(ic),xp(ic)+1:npar)];
pop(keep+ix(ic)+1,:)=[pop(pa(ic),1:xp(ic))
pop(ma(ic),xp(ic)+1:npar)];
end % if
end % end ic
pop(1:4,:)
%_______________________________________________________
% Mutate the population
nmut=ceil((popsize-elite)*npar*mutrate);% total number of mutations
mrow=ceil(rand(1,nmut)*(popsize-elite))+elite;
mcol=ceil(rand(1,nmut)*npar);
mutindx=mrow+(mcol-1)*popsize;
pop(mutindx)=rand(1,nmut);
%_______________________________________________________
% The new offspring and mutated chromosomes are evaluated for cost
row=sort(rem(mutindx,popsize));
iq=1; rowmut(iq)=row(1);
for ic=2:nmut
if row(ic)>keep;break;end
if row(ic)>rowmut(iq)
iq=iq+1; rowmut(iq)=row(ic);
end
end
if rowmut(1)==0;rowmut=rowmut(2:length(rowmut));end
fout(rowmut,:)=feval(ff,pop(rowmut,:));
fout(keep+1:popsize,:)=feval(ff,pop(keep+1:popsize,:));
fout(keep+1:popsize,:)=feval(ff,pop(keep+1:popsize,:));
%_______________________________________________________
% Stopping criteria
if iga>maxit;break;end
[iga cost(1) fout(1,:)]
end %iga
%_______________________________________________________
% Displays the output
day=clock;
disp(datestr(datenum(day(1),day(2),day(3),day(4),day(5),day(6)),0))
disp(['optimized function is ' ff])
format short g
disp(['popsize = ' num2str(popsize) ' mutrate = ' num2str(mutrate) ' # par = ' num2str(npar)])
disp(['Pareto front'])
disp([num2str(pop)])
disp('continuous parameter genetic algorithm')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -