Lecture 4.3 Genetic Algorithms For Optimum Design
Lecture 4.3 Genetic Algorithms For Optimum Design
Optimization
Lecture 7:
Genetic Algorithms
for Optimum
Design
The Binary GA in MATLAB
GA Setup
%_______________________________________________________
% 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 (Cumulative Sum)
pick1=rand(1,keep); % mate #1
pick2=rand(1,keep); % 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
% picks those ma and pa that their randomly generated probabailties (i.e. pick1and2) are
% between probabailties intervals (odds)
%_______________________________________________________
% 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
%_______________________________________________________
Lecture 7: Genetic Algorithms for Optimum Design Optimization 7
The Binary GA in MATLAB
% The population is re-evaluated for cost
par(2:popsize,:)=gadecode(pop(2:popsize,:),0,10,nbits);
% decode
cost(2:popsize)=feval(ff,par(2:popsize,:));
%_______________________________________________________
% 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);
f=x(:,1).^2+x(:,2).^2;