-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSA.m
102 lines (76 loc) · 2.48 KB
/
SA.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
%% cleaning
clf;
clc;
clear;
close all;
%% parameters (different settings for test)
Temp_init = 50;
Temp_min = 1;
alpha = 0.999;
n_iter = 100;
%% import cities
% There are three different test cases we are running
% 'European_Cities_Big.xlsx' = 41 cities
% 'European_Cities_Medium.xlsx' = 20 cities
% 'European_Cities_Short.xlsx' = 10 cities
filename = 'European_Cities_Medium.xlsx';
sol = readtable(filename);
cities = table2struct(readtable(filename));
sol = [sol; sol(1,:)];
[num_cities, ~] = size(sol);
% Generate Figures
map = figure('Name','Best Route in current Generation', 'Numbertitle','off');
hist = figure('Name', 'Distance Evolution', 'Numbertitle','off');
% Create World Map
figure(map)
createmap(cities)
%% loops
Temp = Temp_init;
count = 0;
convergence = 0;
while Temp > Temp_min
if Temp == Temp_init
[prev_cost, dist] = cost(sol, num_cities);
end
iter = 1;
while iter < n_iter
new_sol = neigh(sol, num_cities);
[new_cost, new_dist] = cost(new_sol, num_cities);
ap = acc_prob(Temp, prev_cost, new_cost);
if ap > rand
sol = new_sol;
prev_cost = new_cost;
dist = new_dist;
convergence = 0;
end
iter = iter + 1;
convergence = convergence + 1;
lat = zeros(size(sol ,1) - 1, 2); % All lat in the best population
lon = zeros(size(sol ,1) - 1, 2); % All lon in best population
figure(map)
for i = 1:(size(sol,1) - 1)
lat(i,:) = [sol.Lat(i), sol.Lat(i+1)];
lon(i,:) = [sol.Lon(i), sol.Lon(i+1)];
end
% To close the loop, and adding the first and last cities lat lon
pt = plotm(lat, lon, 'r-', 'linewidth',1);
title(sprintf('Temperature: %g | Best Distance: %g', Temp, dist));
figure(hist)
hold on
plot(count, dist/1000, 'ro');
ylabel('Distance')
xlabel('Iterations')
axis([0 inf 0 inf]);
title('Cost Evolution');
drawnow
pause(0.01)
if (Temp > Temp_min)
delete(pt); % To delete the last plot lines drawn on the map
end
end
count = count + 1;
%-------------------------------- Plotting -------------------------------%
% First plotting the lines on the map
%plan evolution
Temp = alpha*Temp;
end