-
Notifications
You must be signed in to change notification settings - Fork 0
/
anneal.h
40 lines (36 loc) · 961 Bytes
/
anneal.h
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
#ifndef __ANNEAL_H
#define __ANNEAL_H
#include <limits>
#include <math.h>
#include <stdlib.h>
template <typename Solution, int RateI = 958>
class Anneal
{
public:
Solution operator () (Solution &s) __attribute__((__flatten__))
{
double Rate = RateI / 1000.0;
int per_temp = s.iterations();
double temp = 2000.0;
Solution best(s);
double ebest = best.e();
double e = s.e();
while (temp >= 0.01) {
for (int i = 0; i < per_temp; ++i) {
s.neighbor();
double enew = s.e();
if (enew < ebest) {
best = s;
ebest = enew;
}
if (enew > e && exp((e - enew) / temp) * RAND_MAX < random())
s.undo();
else
e = enew;
}
temp *= Rate;
}
return best;
}
};
#endif /* __ANNEAL_H */