-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenetic algorithms_TSP.m
215 lines (174 loc) · 6.69 KB
/
genetic algorithms_TSP.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
function genetic algorithms_TSP
disp('=============================================================================')
disp('Problem: In the Travelling Salesman Problem (TSP), a salesman has to visit ')
disp(' every city in a given territory once and then return to his starting')
disp(' point. The goal is to find the best route such that the travelling ')
disp(' distance is minimised. ')
disp('=============================================================================')
disp('Hit any key to define the number of cities to be visited by a salesman.')
pause
num=20; % Number of cities to be visited by a salesman
disp(' ')
fprintf(1,'num=%.0f; Number of cities to be visited by a salesman\n',num);
disp(' ')
rand('seed',1.4929e+009);
city_location=(rand(num,2));
disp('Hit any key to plot locations of the cities on the map.')
pause
figure
plot(city_location(:,1),city_location(:,2),'.r','markersize',25)
hold on
for i=1:num;
text(city_location(i,1)+0.02,city_location(i,2),sprintf('%g',i));
end
disp('Hit any key to plot all available roads between the cities.')
pause
for n=1:num
for i=1:num
plot([city_location(n,1) city_location(i,1)],[city_location(n,2) city_location(i,2)])
end
end
disp('Hit any key to find distances between the cities.')
pause
city_distance = dist(city_location')
disp('Hit any key to define the size of a chromosome population, the crossover ')
disp('probability, the mutation probability, and the number of generations.')
pause
nind=100; % Size of a chromosome population
ngenes=num; % Number of genes in a chromosome
Pc=0.7; % Crossover probability
Pm=0.01; % Mutation probability
ngener=60; % Number of generations
n_show=10; % Number of generations between showing the progress
disp(' ')
fprintf(1,' nind=%.0f; Size of the chromosome population\n',nind);
fprintf(1,' Pc=%.1f; Crossover probability\n',Pc);
fprintf(1,' Pm=%.3f; Mutation probability\n',Pm);
fprintf(1,' ngener=%.0f; Number of generations\n',ngener);
fprintf(1,' n_show=%.0f; Number of generations between showing the progress\n',n_show);
disp(' ')
fprintf(1,'Hit any key to generate a population of %.0f chromosomes.\n',nind);
pause
chrom=[];
for k=1:nind
num=ngenes; city_array=1:num; xxx=[];
for n=1:num
a=rand(1);
for i=1:num
if a<i/num
xxx=[xxx city_array(i)];
break
end
end
city_array(i)=[];
num=num-1;
end
chrom(k,:)=xxx;
end
chrom
rout=[chrom chrom(:,1)];
% Calculate the chromosome fitness
ObjV=evalObjFun(rout,city_distance,nind,ngenes);
best=min(ObjV);
ave=mean(ObjV);
disp('Hit any key to display the best rout found in the initial chromosome population.')
pause
[a b]=min(ObjV);
figure('name','The best rout found in the initial population');
plot(city_location(:,1),city_location(:,2),'.r','markersize',25)
title(['The total distance: ',num2str(a)]);
hold on
for i=1:ngenes;
text(city_location(i,1)+0.02,city_location(i,2),sprintf('%g',i));
plot([city_location(rout(b,i),1) city_location(rout(b,(i+1)),1)],[city_location(rout(b,i),2) city_location(rout(b,(i+1)),2)])
end
hold
disp(' ')
disp('Hit any key to run the genetic algorithm.')
pause
for m=1:(ngener/n_show)
for i=1:n_show
% Fitness evaluation
fitness=(1./ObjV)';
% Roulette wheel selection
numsel=round(nind*0.9); % The number of chromosomes to be selected for reproduction
cumfit=repmat(cumsum(fitness),1,numsel);
chance=repmat(rand(1,numsel),nind,1)*cumfit(nind,1);
[selind,j]=find(chance < cumfit & chance >= [zeros(1,numsel);cumfit(1:nind-1,:)]);
newchrom=chrom(selind,:);
% Crossover
points=round(rand(floor(numsel/2),1).*(ngenes-1))+1;
points=[points round(rand(floor(numsel/2),1).*(ngenes-1))+1];
points=sort((points*(rand(1)<Pc)),2);
for j=1:length(points(:,1))
swap_sect=newchrom(2*j-1:2*j,points(j,1)+1:points(j,2));
remain_sect=newchrom(2*j-1:2*j,:);
for k=1:ngenes
for n=1:length(swap_sect(1,:))
if newchrom(2*j-1,k)==swap_sect(2,n);
remain_sect(1,k)=0;
end
if newchrom(2*j,k)==swap_sect(1,n);
remain_sect(2,k)=0;
end
end
end
[a b c1]=find(remain_sect(1,:));
[a b c2]=find(remain_sect(2,:));
remain_sect=[c1; c2];
newchrom(2*j-1:2*j,:)=[remain_sect(1:2,1:points(j,1)),...
flipud(newchrom(2*j-1:2*j,points(j,1)+1:points(j,2))),...
remain_sect(1:2,points(j,1)+1:length(remain_sect(1,:)))];
end
% Mutation
for i=1:numsel
if rand(1)<Pm
points=sort((round(rand(floor(numsel/2),1).*(ngenes-1))+1)');
newchrom(i,:)=[newchrom(i,1:points(1)),...
fliplr(newchrom(i,points(1)+1:points(2))),...
newchrom(i,points(2)+1:ngenes)];
end
end
% Creating a new population of chromosomes
if nind-numsel, % Preserving a part of the parent chromosome population
[ans,Index]=sort(fitness);
chrom=[chrom(Index(numsel+1:nind),:);newchrom];
else % Replacing the entire parent chromosome population with a new one
chrom=newchrom;
end
% Fitness calculation
rout=[chrom chrom(:,1)];
ObjV=evalObjFun(rout,city_distance,nind,ngenes);
best=[best min(ObjV)];
ave=[ave mean(ObjV)];
end
[a b]=min(ObjV);
% Plotting the best rout found in the current population
figure('name','The best rout found in the current population');
plot(city_location(:,1),city_location(:,2),'.r','markersize',25)
title(['Generation # ',num2str(m*n_show),' The total distance: ',num2str(a)]);
hold on
for i=1:ngenes;
text(city_location(i,1)+0.02,city_location(i,2),sprintf('%g',i));
plot([city_location(rout(b,i),1) city_location(rout(b,(i+1)),1)],[city_location(rout(b,i),2) city_location(rout(b,(i+1)),2)])
end
pause(0.2);
hold
end
disp(' ')
disp('Hit any key to display the performance graph.')
pause
figure('name','Performance graph');
plot(0:ngener,best,0:ngener,ave);
legend('Best','Average',0);
title(['Pc = ',num2str(Pc),', Pm = ',num2str(Pm)]);
xlabel('Generations');
ylabel('Distance')
function ObjV=evalObjFun(rout,city_distance,nind,ngenes)
path=0; ObjV=[];
for k=1:nind
for i=1:ngenes
path=path+city_distance(rout(k,i),rout(k,(i+1)));
end
ObjV(k)=path; path=0;
end