0% found this document useful (0 votes)
61 views

Matlab Code

This document contains MATLAB code for solving the Traveling Salesman Problem using a genetic algorithm. The code initializes a population of solutions randomly, then iterates through generations of selection, crossover, and mutation to evolve better solutions. It tracks the minimum ticket price found over iterations and displays the number of iterations needed to find that price when complete. The code is part of an optimization methods course assignment.

Uploaded by

Roni Al Maududi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Matlab Code

This document contains MATLAB code for solving the Traveling Salesman Problem using a genetic algorithm. The code initializes a population of solutions randomly, then iterates through generations of selection, crossover, and mutation to evolve better solutions. It tracks the minimum ticket price found over iterations and displays the number of iterations needed to find that price when complete. The code is part of an optimization methods course assignment.

Uploaded by

Roni Al Maududi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MATLAB CODE

%=============================== %
% TUGAS MA5171 METODE OPTIMASI
%
% LANJUT
%
% DOSEN : DR. JANSON NAIBORHU
%
% NAMA : RONI AL MAUDUDI
%
% NIM : 20113026
%
% TRAVEL SALESMAN PROBLEM GENETIC%
% ALGORITHM
%
%=============================== %
format short
clear all;
clc;
cost=[0 806 2275 1100 1340 473.8
1019 1472 1461 523 1563 1508 999.2
1335 950 755 1312 881 1780 2756;
806 0 1474 778 461 1353 1279
2131 571 1169 2340 1819 1394 1210
1374 1609 1388 502 1600 2783;
2275 1474 0 725 1391 918 1271
781 2085 577 650 463 463 1257 1082
871 1587 1566 871 1388;
1100 778 725 0 601 664 521 1078
1130 514 1396 1060 613 363 445 1014
618 522 821 2078;
1340 461 1391 601 0 1227 1123
1774 2409 1033 2557 1712 1227 996
964 1480 786 426 1794 2615;
473.8 1353 918 664 1227 0 1066
1365 2037 341 1381 1306 849 1102
548 378 1433 1517 1366 2585;
1019 1279 1271 521 1123 1066 0
1665 1740 939 2034 1577 1129 912
891 1343 1156 1049 2060 2752;
1472 2131 781 1078 1774 1365
1665 0 2361 825 1115 1080 680 1534
2006 1710 2432 2349 1425 2298;
1461 571 2085 1130 2409 2037
1740 2361 0 1642 2562 2241 1887
1646 2593 3052 2985 3017 2351 4059;
523 1169 577 514 1033 341 939
825 1642 0 986 870 507 930 427 411
1049 1191 826 1964;
1563 2340 650 1396 2557 1381
2034 1115 2562 986 0 1114 1193 1561
1529 2897 1712 2987 1042 4007;
1508 1819 463 1060 1712 1306
1577 1080 2241 870 1114 0 399 1452
1076 2853 1577 1903 862 1910;
999.2 1394 463 613 1227 849
1129 680 1887 507 1193 399 0 1030
683 915 1186 1567 930 1798;

1335 1210 1257 363 996 1102 912


1534 1646 930 1561 1452 1030 0 947
1226 948 852 1795 2209;
950 1374 1082 445 964 548 891
2006 2593 427 1529 1076 683 947 0
774 1051 1038 1436 2313;
755 1609 871 1014 1480 378 1343
1710 3052 411 2897 2853 915 1226
774 0 1435 1332 1506 2775;
1312 1388 1587 618 786 1433
1156 2432 2985 1049 1712 1577 1186
948 1051 1435 0 1211 1432 3770;
881 502 1566 522 426 1517 1049
2349 3017 1191 2987 1903 1567 852
1038 1332 1211 0 1594 3426;
1780 1600 871 821 1794 1366
2060 1425 2351 826 1042 862 930
1795 1436 1506 1432 1594 0 1797;
2756 2783 1388 2078 2615 2585
2752 2298 4059 1964 4007 1910 1798
2209 2313 2775 3770 3426 1797 0];
%Biaya tiket pesawat 20 kota dalam
ribu Rupiah
%Inisialisai%
count=0;
maxiter=50;

for i=1:6
offs(:,i)=randsample(20,19);
end
for i=1:6
for j=1:20
if offs(:,i)~=j
offs(20,i)=j;
end
end
end
%Inisialisasi selesai%
%Mulai men-generate populasi%
%Menghitung harga tiket pesawat%
iter=1;
while iter <=maxiter
harga=[0;0;0;0;0;0];
harga1=[0;0;0;0;0;0];
pop=offs;
for i=1:6
for j=1:19
harga(i)= harga(i)+
cost(pop(j,i), pop(j+1,i));
end
harga(i)=
harga(i)+cost(pop(20,i),pop(1,i));

end

%Roulete wheel selesai%

%Menghitung
energi/kemampuan/fitness%
for i=1:6
fitness(i)=1/harga(i);
end

%populasi yang terseleksi%


for i=1:6
popselek(:,i)=popbar(:,seleksi(i));
end
%populasi yang terseleksi selesai%

totalfitness=sum(fitness);
%Menghitung peluang%
for i=1:6
prob(i)=fitness(i)/totalfitness;
end
cek=min(harga);
prob2=sort(prob,'descend');

for i=1:6
for j=1:6
if prob(j)==prob2(i)
popbar(:,i)=pop(:,j);
end
end
end
%Men-generate populasi selesai%
%Memulai seleksi%
cum(1)=prob2(1);
for i=2:6
cum(i)=prob2(i)+cum(i-1);
end
%roulete wheel%
for i=1:6
R(i)=rand(1);
end
for i=1:6
j=2;
if R(i)<=cum(1)
seleksi(i)=1;
else
while j<=6
if R(i)<=cum(j)
seleksi(i)=j;
j=7;
else
j=j+1;
end
end
end
end

%crossover%
for i=1:6
cross(i)=rand(1);
end
cross2=sort(cross,'ascend');
for i=1:3
for j=1:6
if cross2(i)==cross(j)
cr(i)=j;
end
end
end
for i=1:3
popcross(:,i)= popbar(:,i);
end
simpan=popcross;
%crossover%
%persilangan%
for j=1:3
titik(j)=randi([1,20],1);
end
%cross 1 dan 2%
for l=titik(1)+1:20;
popcross(l,1)=simpan(l,2);
popcross(l,2)=simpan(l,1);
end
status=zeros(20,1);
status2=zeros(20,1);
for i=1:titik(1)
j=titik(1)+1;
while j<=20
if
popcross(i,1)==popcross(j,1)
j=titik(1)+1;
while j<=20
if status(j)==0 &&
j<=20
popcross(i,1)=simpan(j,1);
status(j)=1;

j=21;

while j<=20
if status(j)==0 &&

else
j=j+1;
end
end
j=21;
else
j=j+1;
end
end
end

for i=1:titik(1)
j=titik(1)+1;
while j<=20
if
popcross(i,2)==popcross(j,2)
j=titik(1)+1;
while j<=20
if status2(j)==0 &&
j<=20
popcross(i,2)=simpan(j,2);
status2(j)=1;
j=21;
else
j=j+1;
end
end
j=21;
else
j=j+1;
end
end
end
offs(:,1)=popcross(:,1);
offs(:,2)=popcross(:,2);
%cross 1 dan 2%
%cross 2 dan 3%
pocross=simpan;
for l=titik(2)+1:20;
popcross(l,2)=simpan(l,3);
popcross(l,3)=simpan(l,2);
end
status=zeros(20,1);
status2=zeros(20,1);
for i=1:titik(2)
j=titik(2)+1;
while j<=20
if
popcross(i,2)==popcross(j,2)
j=titik(2)+1;

j<=20
popcross(i,2)=simpan(j,2);
status(j)=1;
j=21;
else
j=j+1;
end
end
j=21;
else
j=j+1;
end
end
end

for i=1:titik(2)
j=titik(2)+1;
while j<=20
if
popcross(i,3)==popcross(j,3)
j=titik(2)+1;
while j<=20
if status2(j)==0 &&
j<=20
popcross(i,3)=simpan(j,3);
status2(j)=1;
j=21;
else
j=j+1;
end
end
j=21;
else
j=j+1;
end
end
end
offs(:,3)=popcross(:,2);
offs(:,4)=popcross(:,3);
%cross 2 dan 3%
%cross 1 dan 3%
popcross=simpan;
for l=titik(3)+1:20;
popcross(l,1)=simpan(l,3);
popcross(l,3)=simpan(l,1);
end
status=zeros(20,1);
status2=zeros(20,1);

for i=1:titik(3)
j=titik(3)+1;
while j<=20
if
popcross(i,1)==popcross(j,1)
j=titik(3)+1;
while j<=20
if status(j)==0 &&
j<=20

for i=1:6
mut(i)=randi([1,20],1);
probm(i)=rand(1);
end
save=offs;
for i=1:6
if probm<=pm
if mut(i)~=20
offs(mut(i),i)=save(mut(i)+1,i);

popcross(i,1)=simpan(j,1);
status(j)=1;
j=21;
else
j=j+1;
end
end
j=21;
else
j=j+1;
end
end
end

for i=1:titik(3)
j=titik(3)+1;
while j<=20
if
popcross(i,3)==popcross(j,3)
j=titik(3)+1;
while j<=20
if status2(j)==0 &&
j<=20
popcross(i,3)=simpan(j,3);
status2(j)=1;
j=21;
else
j=j+1;
end
end
j=21;
else
j=j+1;
end
end
end
offs(:,5)=popcross(:,1);
offs(:,6)=popcross(:,3);
%cross 1 dan 3%
%Persilangan selesai%
%Mutasi%
pm=0.25;

offs(mut(i)+1,i)=save(mut(i),i);
else
offs(mut(i),1)=save(mut(1),i);
ofss(mut(1),i)=save(mut(i),i);
end
else
offs(mut(i),i)=offs(mut(i),i);
end
end
save=offs;
for i=1:6
for j=1:19
harga1(i)= harga1(i)+
cost(save(j,i), save(j+1,i));
end
harga1(i)=
harga1(i)+cost(save(20,i),save(1,i)
);
end
harga2=[harga;harga1];
harga3=sort(harga2, 'ascend');
popu=[pop save];
for i=1:12
j=1;
while j<=12
if harga2(j)==harga3(i)
sel(i)=j;
j=13;
else
j=j+1;
end
end
end
for i=1:6
offs(:,i)=popu(:,sel(i));
end
harga1=[0;0;0;0;0;0];
for i=1:6
for j=1:19

harga1(i)= harga1(i)+
cost(offs(j,i), offs(j+1,i));
end
harga1(i)=
harga1(i)+cost(offs(20,i),offs(1,i)
);
end
cek2=min(harga1);
if cek==cek2 && count<=5
hitung=iter;
iter=iter+1;
count=count+1;
else if cek==cek2 && count>5
hitung=iter;
iter=maxiter+1;
else
hitung=iter;
iter=iter+1;
end
end
end
disp(['Banyak Iterasi' '
' 'Harga
Minimum Tiket']);
disp(['
' num2str(hitung) '
' num2str(cek2)]);

You might also like