-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabu1.m
104 lines (96 loc) · 3.78 KB
/
tabu1.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
function [globalSol] = tabu1(initSol, tabuSize, maxIters, noImprovLimit, neighborSearch, neighborSize, costLimit, objfunc, neighborSearchLimit)
% initSol = initial Solution
% tabuSize = maximum size of tabu list
% maxIters = maximum number of iterations
% noImprovLimit = maximum number of iterations for which if there is no
% improvement in the solution, then the search should terminate
% neighborSearch = minimum difference from selected candidate
% neighborSize = size of neighbor list
% costLimit = minimum cost for which search can terminate
% objfunc = objective function in string
% neighborSearchLimit = maximum difference from selected candidate
% Initialization
%neighborSearchLimit = 3
D = size(initSol, 2);
k = 0;
close all;
count = 0;
objFunc = str2func(objfunc);
globalSol = [initSol objFunc(initSol)];
tabuList = [];
figure(2);
fit_fig= plot([0],[0],'c');
hold on;
costVec= zeros(maxIters,1);
costVec(1) = globalSol(end);
set(fit_fig,'xDataSource','1:count');
set(fit_fig,'yDataSource','costVec(1:count)');
candidateList = [globalSol];
solSet = candidateList;
%global fitness_xend fitness_y fitness_avg fitness_yavg ptss
%fitness_y = []; fitness_xend = []; fitness_avg = []; fitness_yavg = []; ptss = [];
while ((k <=noImprovLimit) && (count <=maxIters)) && (max(candidateList(:,end)) > costLimit)
%define neighbourhood
candidateList = [];
neighborCandidate = globalSol(1:end-1);
neighbor = [];
for i = 1:D
if (neighborCandidate(1,i)-neighborSearch >= 0)
neighbor(:,i) = abs(randi([neighborCandidate(1,i)-neighborSearch, neighborCandidate(1,i)+neighborSearch],neighborSize,1));
else
neighbor(:,i) = abs(randi([0, neighborCandidate(1,i)+neighborSearch],neighborSize,1));
end
end
% make candidate list and add their fitness value in third row
for i = 1:size(neighbor,1)
sCandidate = neighbor(i,:);
if size(tabuList) == 0
check = 0;
else
scandidatelist = [];
for j = 1:D
scandidatelist = [scandidatelist, sCandidate(j)*ones(size(tabuList, 1),1)];
end
check = sum(tabuList==scandidatelist, 2);
end
check(check~=D) = 0;
if (sum(check) < D)
sCandidate = [sCandidate, objFunc(sCandidate)];
candidateList = [candidateList;sCandidate];
solSet = [solSet;candidateList];
end
end
[minCostVal, index] = min(candidateList(:,end));
% check if best fitness of candidate list > fitness of current best
% solution and update best solution and tabulist
if(minCostVal < globalSol(:,end))&&(sum(candidateList(index,1:end-1)< 0)==0)
tabuList = [tabuList;globalSol(1:end-1)];
globalSol = candidateList(index,:);
if (index == size(candidateList))
addList = candidateList(1:end-1,1:end-1);
else addList = [candidateList(1:index-1,1:end-1);candidateList(index+1:end,1:end-1)];
end
tabuList = [tabuList;addList];
if neighborSearch > neighborSearchLimit
neighborSearch = neighborSearch - 1;
end
count = count+1;
k = 0;
else
tabuList = candidateList(:,1:end-1);
count = count+1;
k = k+1;
neighborSearch = neighborSearch + 1;
end
% manage size of tabu list
if size(tabuList,1) > tabuSize
diff = size(tabuList,1) - tabuSize;
tabuList = tabuList(diff+1:end,:);
end
costVec(count)= globalSol(end);
refreshdata(fit_fig,'caller');
drawnow;
display([count, k])
end
%figure(4);
% obj_fun = patch(solSet(:,1),solSet(:,2),solSet(:,3),solSet(:,3),'FaceColor','interp');