اگر امکانش هست این کد را خط به خط توضیح دهید .ممنون
%
clear
%_________________________________________________ ____________
% I. Setup the GA
ff='testfunction'; % objective function
npar=2; % number of optimization variables
%_________________________________________________ ____________
% II. Stopping criteria
maxit=100; % max number of iterations
mincost=-9999999; % minimum cost
%_________________________________________________ ____________
% III. GA parameters
popsize=16; % set population size
mutrate=.15; % set mutation rate
selection=0.5; % fraction of population kept
nbits=8; % number of bits in each parameter
Nt=nbits*npar; % total number of bits in a chormosome
keep=floor(selection*popsize); % #population members that survive
%_________________________________________________ ____________
% Create the initial population
iga=0; % generation counter initialized
pop=round(rand(popsize,Nt)); % random population of 1s and 0s
par=gadecode(pop,0,10,nbits); % convert binary to continuous values
cost=feval(ff,par); % calculates population cost using ff
[cost,ind]=sort(cost); % min cost in element 1
par=par(ind,
; pop=pop(ind,
; % sorts population with lowest cost first
minc(1)=min(cost); % minc contains min of population
meanc(1)=mean(cost); % meanc contains mean of population
%_________________________________________________ ____________
% Iterate through generations
while iga<maxit
iga=iga+1; % increments generation counter
%_________________________________________________ ____________
% Pair and mate
M=ceil((popsize-keep)/2); % number of matings
prob=flipud([1:keep]'/sum([1:keep])); % weights chromosomes based upon position in list
odds=[0 cumsum(prob(1:keep))']; % probability distribution function
pick1=rand(1,M); % mate #1
pick2=rand(1,M); % mate #2
% ma and pa contain the indicies of the chromosomes that will mate
ic=1;
while ic<=M
for id=2:keep+1
if pick1(ic)<=odds(id) & pick1(ic)>odds(id-1)
ma(ic)=id-1;
end % if
if pick2(ic)<=odds(id) & pick2(ic)>odds(id-1)
pa(ic)=id-1;
end % if
end % id
ic=ic+1;
end % while
%_________________________________________________ ____________
% Performs mating using single point crossover
ix=1
keep; % index of mate #1
xp=ceil(rand(1,M)*(Nt-1)); % crossover point
pop(keep+ix,
=[pop(ma,1:xp) pop(pa,xp+1:Nt)]; % first offspring
pop(keep+ix+1,
=[pop(pa,1:xp) pop(ma,xp+1:Nt)]; % second offspring
%_________________________________________________ ____________
% Mutate the population
nmut=ceil((popsize-1)*Nt*mutrate); % total number of mutations
mrow=ceil(rand(1,nmut)*(popsize-1))+1; % row to mutate
mcol=ceil(rand(1,nmut)*Nt); % column to mutate
for ii=1:nmut
pop(mrow(ii),mcol(ii))=abs(pop(mrow(ii),mcol(ii))-1); % toggles bits
end % ii
%_________________________________________________ ____________
% The population is re-evaluated for cost
par(2
opsize,
=gadecode(pop(2
opsize,
,0,10,nb its); % decode
cost(2
opsize)=feval(ff,par(2
opsize,
);
%_________________________________________________ ____________
% Sort the costs and associated parameters
[cost,ind]=sort(cost);
par=par(ind,
; pop=pop(ind,
;
%_________________________________________________ ____________
% Do statistics for a single nonaveraging run
minc(iga+1)=min(cost);
meanc(iga+1)=mean(cost);
%_________________________________________________ ____________
% Stopping criteria
if iga>maxit | cost(1)<mincost
break
end
[iga cost(1)]
end %iga
%_________________________________________________ ____________
% Displays the output
day=clock;
disp(datestr(datenum(day(1),day(2),day(3),day(4),d ay(5),day(6)),0))
disp(['optimized function is ' ff])
format short g
disp(['popsize = ' num2str(popsize) ' mutrate = ' num2str(mutrate) ' # par = ' num2str(npar)])
disp(['#generations=' num2str(iga) ' best cost=' num2str(cost(1))])
disp(['best solution'])
disp([num2str(par(1,
)])
disp('binary genetic algorithm')
disp(['each parameter represented by ' num2str(nbits) ' bits'])
figure(24)
iters=0:length(minc)-1;
plot(iters,minc,iters,meanc,'--');
xlabel('generation');ylabel('cost');
text(0,minc(1),'best');text(1,minc(2),'population average')