This repository has been archived by the owner on Dec 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSA.m
99 lines (85 loc) · 2.91 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
function [numIts, BestSoln BestSolnCost] = SA(sc, temp)
%% SA
% Following the Figure 9.1 from http://www.eecs.harvard.edu/~parkes/pubs/ch9.pdf
%
% disp(' ')
% disp(' ---------------------------------- ')
% disp(' ---------------------------------- ')
% disp(' ---------------------------------- ')
% disp(' ---------------------------------- ')
% disp(' ---------------------------------- ')
% disp(' ---------------------------------- ')
% disp(' ---------------------------------- ')
% disp(' ---------------------------------- ')
% disp(' ---------------------------------- ')
% disp(' ')
% disp('------------------------ Running SA ------------------------')
%clearvars
% sc = Scenario();
% sc = RandomTrains(35,20,4);
rs = sc.getRS();
% Calculate ideal
IdealSolution = rs.genIdealSolution();
rs.reset();
% Create initial solution
[m, nTrains] = size(rs.trains);
[n, nNodes] = size(rs.nodes);
[curSolution, curConflicts, curLateness] = rs.getSolution();
curDelay = zeros(nTrains, nNodes);
curSolution;
curConflicts;
initialLateness = curLateness;
bestSolution = curSolution;
bestConflicts = curConflicts;
bestLateness = curLateness;
bestDelay = curDelay;
rs.reset();
numIts = 0;
% disp('------------------- Set Up Completed --------------------')
T = nTrains*temp;
coolingRate = 0.1;
while(T > 0)
numIts = numIts + 1;
newDelay = curDelay;
cons = find(curConflicts);
if(length(cons > 0))
rCon = cons(randi([1,length(cons)]));
newDelay(rCon) = newDelay(rCon) + 1;
[newSolution, newConflicts, newLateness] = rs.genSolutionWithDelay(newDelay);
if (newLateness < bestLateness)
accProb = 1;
else
accProb = exp((curLateness - newLateness)/T);
end
accRand = randi([0,10000])/10000;
if (accProb > accRand)
curLateness = newLateness;
curConflicts = newConflicts;
curSolution = newSolution;
curDelay = newDelay;
end
if (curLateness < bestLateness)
bestLateness = curLateness;
bestConflicts = curConflicts;
bestSolution = curSolution;
bestDelay = curDelay;
end
else
% disp('Converged')
% T
T = 0;
end
rs.reset();
T = T * (1 - coolingRate);
end
BestSoln = bestDelay;
BestSolnCost = bestLateness;
% disp(' ')
% disp('------------------------ Results ------------------------')
bestSolution;
bestDelay;
bestLateness;
initialLateness;
optimizationPercent = ((initialLateness / bestLateness) - 1) * 100;
% disp('-------------------------- End --------------------------')
clear JUNCTION LEFT RIGHT STATION junction1 junction2 junction3 junction4 station1 station2 station3 train1 train2 train3