-
Notifications
You must be signed in to change notification settings - Fork 5
/
SimAnnealAlg.pas
386 lines (350 loc) · 12 KB
/
SimAnnealAlg.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
{
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 SimAnnealAlg; //////////////////////////////////////////////////////////////////
{
>> Version: 3.1
>> Description
Implementation of a simulated annealing algorithm featuring various acceptance
criteria and temperature schedules.
>> Author
Peter Karpov
Email : PeterKarpov@inversed.ru
Homepage : inversed.ru
GitHub : inversed-ru
Twitter : @inversed_ru
>> How to use
The user must supply MakeNeighbour and UndoSAMove routines via the
problem definition module.
>> ToDo
! Fix the crash when Auto T0 < Tfin
- Incorporate advanced schedules from version 2.3. Need to check schedules that
involve Gaussian integrals after changes in SpecFuncs.
? We can estimate the probability of improving the best solution using standard
deviation of the score. Adaptive scheme: dLnT ~ 1 / ProbImp.
- Modified Lam schedule
- Simulated inertia
- Measure and log correlation length of the score, maybe use it in adaptation
- Barker acceptance is atypical and complicates the code. Maybe it should be
removed, as it proved to be less efficient.
- Temperature schedule constants are calculated inside a loop. They should be
precalculated instead if this impacts the performance.
- Neighborhood dependent on normalized temperature may be useful for some
problems (Heilbronn, Lennard-Jones)
? Multi-particle modification: there are multiple solutions, but only the best
one is selected at each iteration. Has no effect at low temperatures.
>> Changelog
3.1 : 2019.10.01 + Multirun statistics collection
3.0 : 2018.09.20 - Experimental cooling schedules
- Experimental algorithms
~ Automatic final temperature selection formula
~ Freepascal compatibility
2.3 : 2013.02.07 ~ Calibration updated with new filtering methods
2.2 : 2012.04.29 ~ Cooling schedules reorganized
2.1 : 2012.04.23 - Thermal hopping and exploration algorithms split into
basin hopping unit
2.0 : 2011.11.27 + Critical temperature calibration
- Annealing rewritten: new schedules, acceptance criteria
+ Automatic initial temperature option
+ Basin hopping algorithm
1.2 : 2011.07.22 + New experimental algorithms
1.0 : 2011.03.28 ~ Added unit header
~ Slightly reorganized
0.0 : 2010.11.24 + Initial version
Notation: + added, - removed, * fixed, ~ changed
}
{$MINENUMSIZE 4}
interface ///////////////////////////////////////////////////////////////////////////
uses
Math,
Common,
Messages,
Acceptance,
Problem;
type
TScheduleType = (stZero, stLog, stPower, stExp);
TSchedule =
record
Type_ : TScheduleType;
P : Real;
end;
TSAParameters =
record
T0, TFin, Tc,
dEmax, dEmin : Real;
T0Mode : TT0Mode;
TfinEBased : Boolean;
Smoothing : Real;
Acceptance : TAcceptance;
Schedule : TSchedule;
MaxIters : Int64;
NReheat,
PopSize : Integer;
FastReheat : Boolean;
ScoreToReach : TScore;
Calibrate : Boolean;
end;
TSAStatus = TBasicStatus;
// #HACK Huge, needs simplification
// Run the simulated annealing with specified Params, return the Best solution found
// and the search Stats, update MultirunStats
procedure SimulatedAnnealing(
var Best : TSolution;
var Stats : TRunStats;
var MultirunStats : TMultirunStats;
const Params : TSAParameters;
const Status : TSAStatus);
implementation //////////////////////////////////////////////////////////////////////
uses
InvSys,
Arrays,
ExtraMath,
RandVars,
Statistics,
SpecFuncs,
SolutionLists;
const
PathStatus = 'SA_Status.txt';
NameBest = 'SA_Best';
// Return the temperature at relative time t given the initial
// and final temperatures T0 and T1 and the Schedule
function GetTemperature(
t : Real; // [0 .. 1]
T0, T1 : Real;
const Schedule : TSchedule
) : Real;
var
Tau, P, A : Real;
begin
P := Schedule.P;
case Schedule.Type_ of
stZero:
Result := 0;
stLog:
begin
Tau := 1 / 2;
A := Ln(1 + 1 / Tau) / (T0 / T1 - 1);
Result := T0 * A / (A + Ln(1 + t / Tau));
end;
stPower:
begin
Tau := 1 / (Power(T0 / T1, 1 / P) - 1);
Result := T0 * Power(1 + t / Tau, -P);
end;
stExp:
begin
Tau := Power(Ln(T0 / T1), -1 / P);
Result := T0 * Exp(-Power(t / Tau, P));
end;
else
Result := 0;
Assert(False, 'Unknown schedule');
end;
end;
// Return characteristic absolute value of the logarithmic derivative
function LogSlope(
T0, Tfin, Tc : Real;
const Schedule : TSchedule
) : Real;
var
P : Real;
begin
P := Schedule.P;
case Schedule.Type_ of
stZero:
Result := 1;
stLog:
Result := Sqrt(T0 / Tfin) / Ln(3);
stPower:
Result := P * Sqrt(
(Power(T0 / Tfin, 1 / P) - 1) *
(1 - Power(T0 / Tfin, -1 / P)) );
stExp:
Result := Power(Ln(T0 / Tfin), 1 / P);
else
Result := 0;
Assert(False, 'Unknown schedule');
end;
end;
// Return the initial temperature calculated based on Params
function GetT0(
const Params : TSAParameters
) : Real;
var
HotIters : Integer;
begin
with Params do
begin
case T0Mode of
t0Manual:
Result := T0;
t0EBased:
Result := dEToT0(dEmax, Acceptance, t0AutoLow);
t0AutoLow, t0AutoHigh:
begin
HotIters := Round(Power(MaxIters / 6, 2 / 3));
Result := GetAutoT0(t0AutoLow, Acceptance, HotIters);
end;
else
Result := 0;
Assert(False);
end;
end;
end;
// Return the final temperature calculated based on Params
function GetTfin(
const Params : TSAParameters
) : Real;
begin
with Params do
begin
Result := Tfin;
if TfinEBased then
Result := dEmin / InvProbAccept(Ln(MaxIters + 0.0) / MaxIters, Acceptance);
end;
end;
// #HACK Huge, needs simplification
// Run the simulated annealing with specified Params, return the Best solution found
// and the search Stats, update MultirunStats
procedure SimulatedAnnealing(
var Best : TSolution;
var Stats : TRunStats;
var MultirunStats : TMultirunStats;
const Params : TSAParameters;
const Status : TSAStatus);
const
NStatsFields = 7;
// 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] := 'T';
Header[4] := 'PAccept';
Header[5] := 'PImprove';
Header[6] := 'AvgScore';
end;
end;
// Add the current search statistics to MultirunStats
procedure WriteStatus(
var MultirunStats : TMultirunStats;
const Work, Best : TSolution;
Iters : Int64;
T, PAccept,
PImprove,
AvgScore : Real);
var
Data : TRealArray;
begin
SetLength(Data, NStatsFields);
Data[0] := Iters;
Data[1] := Best.Score;
Data[2] := Work.Score;
Data[3] := T;
Data[4] := PAccept;
Data[5] := PImprove;
Data[6] := AvgScore;
AddSample(MultirunStats, Data);
end;
var
Work : TSolution;
Iters, SubIters,
MaxSubIters : Int64;
Stage : Integer;
T, T0Stage,
UseT0, UseTfin,
RelTime,
PAccept, PImprove,
Accepted, Improved,
Tau, Alpha, AvgScore : Real;
Undo : TSAUndo;
OldScore, PrevBest : TScore;
Done : Boolean;
begin
with Params do
begin
// Initialization
NewSolution(Best);
PrevBest := Best.Score;
Iters := 0;
UseT0 := GetT0 (Params);
UseTfin := GetTfin(Params);
if Status.IterStatus <> 0 then
begin
WriteHeader(MultirunStats);
PrepareNextRun(MultirunStats);
end;
// Perform all reheat stages
for Stage := 0 to NReheat - 1 do
begin
// Initialize the solution, temperature and statistics
AssignSolution(Work, Best);
T0Stage := UseT0 * Power(UseTfin / UseT0, Stage / NReheat);
if FastReheat then
MaxSubIters := Round( (NReheat - Stage) *
2 * MaxIters / (NReheat * (NReheat + 1)) )
else
MaxSubIters := Round(MaxIters / NReheat);
SubIters := 0;
PAccept := 1;
PImprove := 1 / 2;
AvgScore := Work.Score;
//Tau := MaxSubIters / LogSlope(T0Stage, UseTfin, Tc, Schedule);
Tau := Power(MaxSubIters + 0.0, 2 / 3);
Alpha := Min(1 / (Smoothing * Tau), 1);
// Run a single annealing stage
repeat
// Set the temperature
Inc(Iters);
Inc(SubIters);
RelTime := SubIters / MaxSubIters;
T := GetTemperature(RelTime, T0Stage, UseTfin, Schedule);
// Make a candidate solution
OldScore := Work.Score;
MakeNeighbour(Work, Undo, T);
Improved := Ord(CompareScores(Work.Score, OldScore) = scoreBetter);
// Accept or reject
if Random < ProbAccept(Work.Score, OldScore, T, Acceptance) then
begin
ReplaceIfBetter(Best, Work);
Accepted := 1;
end
else
begin
UndoSAMove(Work, Undo);
Accepted := 0;
end;
// Update statistics
PAccept := Blend(PAccept, Accepted, Alpha);
PImprove := Blend(PImprove, Improved, Alpha);
AvgScore := Blend(AvgScore, Work.Score, Alpha);
// Write status
if Divisible(Iters, Status.IterStatus) then
begin
WriteStatus(MultirunStats, Work, Best, Iters, T, PAccept, PImprove, AvgScore);
if CompareScores(Best.Score, PrevBest) = scoreBetter then
begin
ShowNewBestScore(Best, Status.ShowMessage);
PrevBest := Best.Score;
end;
end;
Done := SubIters = MaxSubIters;
until Done;
end;
// Run complete
Stats.NFEPartial := Iters;
TrySaveSolution(NameBest, Best, Status.ShowMessage);
end;
end;
end.