-
Notifications
You must be signed in to change notification settings - Fork 18
/
SA.m
53 lines (50 loc) · 1.5 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
% Simulated Annealing Algorithm for Hybrid Job Shop Scheduling
% Problem.
%
% costs: history of cost for each iteration's schedule.
% bestSol: best solution for the given sT and fT.
%
% iterations: number of iteration for each T.
% sT: start T.
% fT: final T.
% alpha: alpha in geometric cooling schedule.
function [costs, bestSol] = SA(schedule, jobs, m, n, iterations, sT, ...
minTotalIteration, alpha, costFunc, ...
genNeighborFunc)
costsEnd = 0;
costs = [];
bestSol = schedule;
bestSolCost = costFunc(schedule, jobs, m, n);
acceptedSol = schedule;
acceptedSolCost = costFunc(schedule, jobs, m, n);
T = sT;
counter = 0;
while counter < minTotalIteration
for i=1:iterations
counter = counter + 1;
newSol = genNeighborFunc(acceptedSol, m, n);
newSolCost = costFunc(newSol, jobs, m, n);
deltaCost = newSolCost - acceptedSolCost;
if deltaCost < 0
acceptedSol = newSol;
acceptedSolCost = newSolCost;
else
randVal = rand(1);
p = exp(-1*deltaCost / T);
if p > randVal
acceptedSol = newSol;
acceptedSolCost = newSolCost;
end
end
% record the cost value in to history
costsEnd = costsEnd + 1;
costs(costsEnd) = acceptedSolCost;
% Update current best value
if acceptedSolCost < bestSolCost
bestSol = acceptedSol;
bestSolCost = acceptedSolCost;
end
end
T = T * alpha; % cooling
end
end