-
Notifications
You must be signed in to change notification settings - Fork 5
/
TabuSearchAlg.pas
393 lines (351 loc) · 12.6 KB
/
TabuSearchAlg.pas
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
{
Copyright (c) Peter Karpov 2010 - 2018.
Usage of the works is permitted provided that this instrument is retained with
the works, so that any entity that uses the works is notified of this instrument.
DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
}
{$IFDEF FPC} {$MODE DELPHI} {$ENDIF}
unit TabuSearchAlg; /////////////////////////////////////////////////////////////////
{
>> Version: 1.5
>> Description
Tabu search escapes local optima by maintaining a tabu list of forbidden moves. At
each iteration, all non-tabu moves are evaluated and the best is performed and
added to the tabu list for Tenure iterations.
>> Author
Peter Karpov
Email : PeterKarpov@inversed.ru
Homepage : inversed.ru
GitHub : inversed-ru
Twitter : @inversed_ru
>> Usage
By means of a problem definition unit the user must supply the functions that
initialize a tabu list, increase its age, add an item to it, check whether a given
move is tabu, return the tabu tenure and make a move list.
>> ToDo
- Inform the user if a solution is stuck (all moves are tabu) via messages and
the status file. This could indicate that the tenure is too high.
- Experiment with cooperative multi-solution schemes.
- Add Fan and Filter algorithm
- Tabu search with same structure as in balanced hill climbing
>> Changelog
1.5 : 2019.10.01 + Multirun statistics collection
1.4 : 2019.08.25 + Cooperative tabu search
1.3 : 2018.09.18 - Experimental tabu search variants
~ FreePascal compatibility
1.2 : 2011.09.25 + Beam tabu search
1.1 : 2011.09.11 + Cooperative tabu search
1.0 : 2011.03.28 + Unit header
~ Slightly reorganized
0.0 : 2010.11.22 + Initial version
Notation: + added, - removed, * fixed, ~ changed
}
{$MINENUMSIZE 4}
interface ///////////////////////////////////////////////////////////////////////////
uses
Common,
Problem;
type
TTSParameters =
record
MaxIters : Integer;
PopSize : Integer;
ScoreToReach : TScore;
end;
TTSStatus = TBasicStatus;
// Run a tabu search with given Params, return the Best solution found
// and the search Stats, update MultirunStats. If RandomInit = True,
// a random initial solution is created, otherwise the search starts from
// the provided one.
procedure TabuSearch(
var Best : TSolution;
var Stats : TRunStats;
var MultirunStats : TMultirunStats;
const Params : TTSParameters;
const Status : TTSStatus;
RandomInit : Boolean);
// Run a cooperative tabu search with given Params, return the Best
// solution found, update MultirunStats. If RandomInit = True, a random
// initial solution is created, otherwise the search starts from the provided one.
procedure CoopTabuSearch(
var Best : TSolution;
var MultirunStats : TMultirunStats;
const Params : TTSParameters;
const Status : TTSStatus;
RandomInit : Boolean);
implementation //////////////////////////////////////////////////////////////////////
uses
InvSys,
ExtraMath,
Arrays,
SolutionLists;
const
PathStatus = 'TS_Status.txt';
NameBest = 'TS_Best';
// Return a random best Move from MoveList that is either not in TabuList
// or improves the BestSoFar score
procedure GetTabuMove(
var Move : TMove;
var Solution : TSolution;
var TabuList : TTabuList;
const MoveList : TMoveList;
BestSoFar : TScore);
var
i : Integer;
BestMoves : TMoveList;
BestScore : TScore;
TrialMove : TMove;
Comparison : TScoreComparison;
IsTabu,
IsBestest : Boolean;
Undo : TMoveUndo;
begin
// Search the whole move list for the best moves
with Solution do
repeat
InitMoveList(BestMoves);
BestScore := 0;
for i := 0 to MoveList.N - 1 do
begin
// Perform a trial move
TrialMove := MoveList.Moves[i];
IsTabu := IsMoveTabu(TrialMove, Solution, TabuList);
PerformMove(Solution, Undo, TrialMove);
IsBestest := CompareScores(Score, BestSoFar) = scoreBetter;
// Update the list of best moves
if not IsTabu or IsBestest then
begin
if BestMoves.N = 0 then
BestScore := Score;
Comparison := CompareScores(Score, BestScore);
if Comparison = scoreBetter then
begin
BestScore := Score;
BestMoves.N := 0;
AddMove(BestMoves, TrialMove);
end
else if Comparison = scoreEqual then
AddMove(BestMoves, TrialMove);
end;
// Restore the initial state
UndoMove(Solution, Undo);
end;
if BestMoves.N = 0 then
AgeTabuList(TabuList);
until BestMoves.N <> 0;
// Pick a random best move
with BestMoves do
Move := Moves[ Random(N) ];
end;
// Perform a single tabu search step with Work, update Best. If Best has
// been improved, return True and display a message via Status.ShowMessage.
function TabuStep(
var Work, Best : TSolution;
var TabuList : TTabuList;
Tenure : Integer;
const Status : TTSStatus
) : Boolean;
var
Move : TMove;
MoveList : TMoveList;
begin
// Find a move
MakeTSMoveList(MoveList, Work);
GetTabuMove(Move, Work, TabuList, MoveList, Best.Score);
// Update the tabu list
AgeTabuList(TabuList);
AddToTabuList(TabuList, Move, Tenure, Work);
// Make the move
PerformMove(Work, Move);
Result := TryUpdateBest(Best, Work, Status.ShowMessage);
end;
// Run a tabu search with given Params, return the Best solution found
// and the search Stats, update MultirunStats. If RandomInit = True,
// a random initial solution is created, otherwise the search starts from
// the provided one.
procedure TabuSearch(
var Best : TSolution;
var Stats : TRunStats;
var MultirunStats : TMultirunStats;
const Params : TTSParameters;
const Status : TTSStatus;
RandomInit : Boolean);
var
Work : TSolution;
TabuList : TTabuList;
Tenure : Integer;
Iters : Integer;
const
NStatsFields = 4;
// Create the header of MultirunStats
procedure WriteHeader(
var MultirunStats : TMultirunStats);
begin
with MultirunStats do
if NVars = 0 then
begin
InitMultirunStats(MultirunStats, NStatsFields);
SetLength(Header, NStatsFields);
Header[0] := 'Iter';
Header[1] := 'BestScore';
Header[2] := 'WorkScore';
Header[3] := 'Tenure';
end;
end;
// Add the current search statistics to MultirunStats
procedure WriteStatus(
var MultirunStats : TMultirunStats;
const Work, Best : TSolution;
Iters, Tenure : Integer);
var
Data : TRealArray;
begin
SetLength(Data, NStatsFields);
Data[0] := Iters;
Data[1] := Best.Score;
Data[2] := Work.Score;
Data[3] := Tenure;
AddSample(MultirunStats, Data);
end;
begin
// Initialization
if RandomInit then
NewSolution(Best);
AssignSolution(Work, Best);
InitTabuList(TabuList);
Iters := 0;
// Write the status header
if Status.IterStatus > 0 then
begin
WriteHeader(MultirunStats);
PrepareNextRun(MultirunStats);
end;
repeat
// Perform a single tabu search step
Inc(Iters);
Tenure := TabuTenure(Iters / Params.MaxIters);
TabuStep(Work, Best, TabuList, Tenure, Status);
// Write the status
if Divisible(Iters, Status.IterStatus) then
WriteStatus(MultirunStats, Work, Best, Iters, Tenure);
until Iters = Params.MaxIters;
// Run complete
Stats.NFEPartial := Iters;
TrySaveSolution(NameBest, Best, Status.ShowMessage);
end;
// Run a cooperative tabu search with given Params, return the Best
// solution found, update MultirunStats. If RandomInit = True, a random
// initial solution is created, otherwise the search starts from the provided one.
procedure CoopTabuSearch(
var Best : TSolution;
var MultirunStats : TMultirunStats;
const Params : TTSParameters;
const Status : TTSStatus;
RandomInit : Boolean);
var
Work,
LocalBest : TSolutionList;
TabuLists : array of TTabuList;
i, j, k,
IdUpdGlobalBest: Integer;
Iters : Integer;
LSStats : TRunStats;
UpdGlobalBest : Boolean;
UpdLocalBest : TBoolArray;
const
NStatsFields = 3;
//LSParams : TLSParameters = lsmFast;
// Create the header of MultirunStats
procedure WriteHeader(
var MultirunStats : TMultirunStats);
begin
with MultirunStats do
if NVars = 0 then
begin
InitMultirunStats(MultirunStats, NStatsFields);
SetLength(Header, NStatsFields);
Header[0] := 'Iter';
Header[1] := 'BestScore';
Header[2] := 'WorkScore';
end;
end;
// Add the current search statistics to MultirunStats
procedure WriteStatus(
var MultirunStats : TMultirunStats;
const Work : TSolutionList;
const Best : TSolution;
Iters,
IdSelected : Integer);
var
Data : TRealArray;
begin
SetLength(Data, NStatsFields);
Data[0] := Iters;
Data[1] := Best.Score;
Data[2] := Work._[IdSelected].Score;
AddSample(MultirunStats, Data);
end;
begin
// Initialization
if RandomInit then
begin
NewSolList(Work, Params.PopSize);
AssignSolution(Work.Best, Best);
end
else
begin
//NewSolution(Best);
//LocalSearch(Best, LSStats, LSParams, NoStatus, {RandomInit:} False);
FillSolList(Work, Best, Params.PopSize);
end;
AssignSolList(LocalBest, Work);
SetLength(TabuLists, Params.PopSize);
InitArray(UpdLocalBest, Params.PopSize, False);
for i := 0 to Params.PopSize - 1 do
InitTabuList(TabuLists[i]);
Iters := 0;
IdUpdGlobalBest := 0;
UpdGlobalBest := False;
// Write status header
if Status.IterStatus > 0 then
begin
WriteHeader(MultirunStats);
PrepareNextRun(MultirunStats);
end;
repeat
Inc(Iters);
i := Modulo(-Iters, Work.N);
// Share the local best
if (i < (Work.N - 1)) and UpdLocalBest[i + 1] then
//if False then
//if CompareScores(LocalBest._[i + 1], LocalBest._[i]) = scoreBetter then
//if CompareScores(LocalBest._[i + 1], Work._[i]) = scoreBetter then
if True then
begin
AssignSolution(Work._[i], LocalBest._[i + 1]);
AssignTabuList(TabuLists[i], TabuLists[i + 1]);
if CompareScores(Work._[i], LocalBest._[i]) = scoreBetter then
begin
AssignSolution(LocalBest._[i], Work._[i]);
UpdLocalBest[i] := True;
end;
UpdLocalBest[i + 1] := False;
end;
// Perform a tabu step
UpdGlobalBest := TabuStep(Work._[i], Best,
TabuLists[i], TabuTenure(1 - i / (Work.N - 1)), Status);
if UpdGlobalBest then
IdUpdGlobalBest := i;
if CompareScores(Work._[i], LocalBest._[i]) = scoreBetter then
begin
AssignSolution(LocalBest._[i], Work._[i]);
UpdLocalBest[i] := True;
end;
// Write the status
if Divisible(Iters, Status.IterStatus) then
WriteStatus(MultirunStats, Work, Best, Iters, i);
until Iters = Params.MaxIters;
// Run complete
TrySaveSolution(NameBest, Best, Status.ShowMessage);
end;
end.