% Algoritma Genetika
Kontinyu
clear all;clc;
% Set semua parameter pada
bagian I, II, and III
%_______________________________________________________
% I Setup the GA
ff='testfunction'; % fungsi
objektif
npar=2; % jumlah variable
optimasi
varhi=10; varlo=0; % batas
variabel
%_______________________________________________________
% II Stopping criteria
maksiterasi=100; % jumlah
iterasi maksimum
mincost=-9999999; % cost
minimum
%_______________________________________________________
% III GA parameters
jumlahpop=10; % set
population size
mutationrate=.2; % set
mutation rate
selectionrate=0.5; % populasi
yang dipakai (kept)
Nt=npar; %
continuous parameter GA Nt=#variables
survive=floor(selectionrate*jumlahpop);
%
#population
% anggota populasi yang
survive
nmut=ceil((jumlahpop-1)*Nt*mutationrate);
%
total jumlah mutasi
M=ceil((jumlahpop-survive)/2); % jumlah
mating
%_______________________________________________________
% Membuat populasi awal
iga=0; %
generation counter initialized
par=(varhi-varlo)*rand(jumlahpop,npar)+varlo;
%
random
cost=feval(ff,par); % hitung
cost setiap populasi
[cost,ind]=sort(cost); % min cost
di element 1
par=par(ind,:); % sort
continuous
minc(1)=min(cost); % minc
terdiri dari min dari
meanc(1)=mean(cost); % meanc
terdiri dari rata-rata populasi
%_______________________________________________________
% iterasi terus-menerus
setiap generasi
while
iga<maksiterasi
iga=iga+1;
%_______________________________________________________
% Pair dan mate
M=ceil((jumlahpop-survive)/2); % jumlah
mating
prob=flipud([1:survive]'/sum([1:survive]));
%
weights
% chromosomes
odds=[0
cumsum(prob(1:survive))']; % probabilitas
mate1=rand(1,M); % mate #1
mate2=rand(1,M); % mate #2
% ma dan pa terdiri dari
kromosom yang akan disilangkan
ic=1;
while ic<=M
for id=2:survive+1
if mate1(ic)<=odds(id) &
mate1(ic)>odds(id-1)
ma(ic)=id-1;
end
if mate2(ic)<=odds(id) &
mate2(ic)>odds(id-1)
pa(ic)=id-1;
end
end
ic=ic+1;
end
%_______________________________________________________
ix=1:2:survive;
xp=ceil(rand(1,M)*Nt); % titik
crossover
r=rand(1,M); %
parameter pencampuran/mixing
for ic=1:M
xy=par(ma(ic),xp(ic))-par(pa(ic),xp(ic));
%
ma and pa
% mate
par(survive+ix(ic),:)=par(ma(ic),:);
%
offspring 1
par(survive+ix(ic)+1,:)=par(pa(ic),:);
%
offspring 2
par(survive+ix(ic),xp(ic))=par(ma(ic),xp(ic))-r(ic).*xy;
% 1st
par(survive+ix(ic)+1,xp(ic))=par(pa(ic),xp(ic))+r(ic).*xy;
% 2nd
if xp(ic)<npar %
crossover ketika variabel terakhir tidak terseleksi
par(survive+ix(ic),:)=[par(survive+ix(ic),1:xp(ic))
par(survive+ix(ic)+1,xp(ic)+1:npar)];
par(survive+ix(ic)+1,:)=[par(survive+ix(ic)+1,1:xp(ic))
par(survive+ix(ic),xp(ic)+1:npar)];
end % if
end
%_______________________________________________________
% Mutate the population
mrow=sort(ceil(rand(1,nmut)*(jumlahpop-1))+1);
mcol=ceil(rand(1,nmut)*Nt);
for ii=1:nmut
par(mrow(ii),mcol(ii))=(varhi-varlo)*rand+varlo;
% mutasi
end % ii
%_______________________________________________________
% offspring baru dan kromosom
termutasi dievaluasi
cost=feval(ff,par);
%_______________________________________________________
[cost,ind]=sort(cost);
par=par(ind,:);
%_______________________________________________________
minc(iga+1)=min(cost);
meanc(iga+1)=mean(cost);
%_______________________________________________________
% Stopping criteria
if iga>maksiterasi |
cost(1)<mincost
break
end
[iga cost(1)]
end
%_______________________________________________________
% Tampilan output
day=clock;
disp(datestr(datenum(day(1),day(2),day(3),day(4),day(5),day(6)),0))
disp(['fungsi
optimumnya adalah ' ff])
format short g
disp(['jumlah
populasinya adalah= ' num2str(jumlahpop)...
' mutation rate nya adalah = '
num2str(mutationrate)...
' # par = ' num2str(npar)])
disp(['#generasi
ke='
num2str(iga) ' best cost=' num2str(cost(1))])
disp(['solusi
terbaik'])
disp([num2str(par(1,:))])
disp('algoritma
genetika kontinyu')
figure(1)
iters=0:length(minc)-1;
plot(iters,minc,iters,meanc,'--k');
xlabel('generasi');ylabel('cost');
text(0,minc(1),'best');text(1,minc(2),'populasi
rata-rata')
grid on