Professional Documents
Culture Documents
ALGORITMA GENETIKA
Oleh:
JURUSAN MATEMATIKA
UNIVERSITAS BRAWIJAYA
2018
Soal
Pada kelas mata kuliah optimasi numerik terdiri dari 40 orang mahasiswa. Pada akhir
perkuliahan ada tugas proyek sebagai tugas akhir mata kuliah, dosen ingin membagi mahasiswa
dalam kelas tersebut menjadi 10 kelompok. Agar pembagian nomor kelompok adil dan proyek
dapat dikerjakan maka dosen menetapkan aturan pembagian kelompok sebagai berikut :
a. Dalam setiap kelompok minimum ada satu mahasiswa yang nilai kuisnya diatas 75.
b. Dalam setiap kelompok minimum ada satu mahasiswa yang nilai utsnya diatas 75.
c. Dalam satu kelompok minimum ada satu orang mahasiswa yang memiliki satu orang
yang berjenis kelamin beda.
d. Dalam setiap kelompok minimum ada seorang mahasiswa yang rata-rata tugasnya diatas
80.
Data diambil dari file bertipe xls dan output pembagian kelompok juga berupa file bertipe xls.
Gunakan algoritma genetika untuk menyelesaikan masalah diatas.
1. Tentukan model kromosom untuk masalah diatas.
2. Tentukan fungsi objektif untuk masalah diatas.
3. Tentukan operasi crossover untuk masalah diatas.
4. Tentukan operasi mutasi untuk masalah diatas.
5. Buatlah program untuk masalah diatas.
6. Analisislah hasil program diatas.
Pembahasan
1.
Keterangan
Kotak 1-4 : Kelompok 1
Kotak 5-8 : Kelompok 2
Kotak 9-12 : Kelompok 3
Kotak 13-16 : Kelompok 4
Kotak 17-20 : kelompok 5
Kotak 21-24 : Kelompok 6
Kotak 25-28 : Kelompok 7
Kotak 29-32 : Kelompok 8
Kotak 33-36 : Kelompok 9
Kotak 37-40 : Kelompok 10
2. Fungsi objektif
f=x1+x2+x3+x4+x5+x6+x7+x8+x9+x10
function xx=crossover(MA,PA)
L=length(MA);
k=round(rand*(L-2))+1;
xx(1,1:k)=MA(1:k);
n=k+1;
for i=1:L
q=0;
for j=1:k
if PA(i)==MA(j)
q=q+1;
end
end
if q==0
xx(1,n)=PA(i);
n=n+1;
end
end
xx(2,1:k)=PA(1:k);
n=k+1;
for i=1:L
q=0;
for j=1:k
if MA(i)==PA(j)
q=q+1;
end
end
if q==0
xx(2,n)=MA(i);
n=n+1;
end
end
end
4. Mutasi
function xx=mutasi(xx)
p=round(rand*19+1);
y=round(rand*19+1);
temp1=xx(1,p);
xx(1,p)=xx(1,y);
xx(1,y)=temp1;
temp2=xx(2,p);
xx(2,p)=xx(2,y);
xx(2,y)=temp2;
end
5. Program
clear all;
clc;
n=40;
kuis=xlsread('Book1','B2:B41');
uts=xlsread('Book1','C2:C41');
kelamin=xlsread('Book1','D2:D41');
tugas=xlsread('Book1','E2:E41');
Ngen=n;
Npop=6;
generasi=600;
%costfunction
f='x1+x2+x3+x4+x5+x6+x7+x8+x9+x10';
costfunction=inline(f);
%Generate Initial Random Population
for i=1:Npop
kromosom(i,:)=randperm(Ngen);
end
for s=1:generasi
%mencari penalty
x=penalty(kromosom,kelamin,tugas,kuis,uts);
%Evaluasi Fungsi Objektif
for i=1:Npop
costkromosom(i)=costfunction(x(i,1),x(i,2),x(i,3),x(i,4),x(i,5),x(i,6),x(i,7)
,x(i,8),x(i,9),x(i,10));
fitness(i)=1/(costkromosom(i)+10^-5);
end
oldkromosom=kromosom;
oldfitness=fitness;
%%Rank Selection Method
%Pengurutan Kromosom berdasarkan Fitness Terbesar
for i=1:Npop
BestCost=costkromosom(i);
BestIndex=i;
BestKromosom=kromosom(i,:);
for j=i:Npop
if BestCost >= costkromosom(j)
BestIndex=j;
BestCost=costkromosom(j);
BestKromosom=kromosom(j,:);
end
end
temp=kromosom(i,:);
kromosom(i,:)=BestKromosom;
kromosom(BestIndex,:)=temp;
temp=costkromosom(i);
costkromosom(i)=costkromosom(BestIndex);
costkromosom(BestIndex)=temp;
temp=fitness(i);
fitness(i)=fitness(BestIndex);
fitness(BestIndex)=temp;
costkromosom(i)=costfunction(x(i,1),x(i,2),x(i,3),x(i,4),x(i,5),x(i,6),x(i,7)
,x(i,8),x(i,9),x(i,10));
fitness(i)=1/(costkromosom(i)+10^-5);
end
6. Hasil Analisis