-
Notifications
You must be signed in to change notification settings - Fork 2
/
simann.cxx
251 lines (197 loc) · 4.96 KB
/
simann.cxx
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
// simanneal.c++ Implementation of a General Purpose Simulated Annealing Class
// (c) Copyright 1994, Everett F. Carter Jr.
// Permission is granted by the author to use
// this software for any application provided this
// copyright notice is preserved.
// Updated to 2003 C++ standard by Shawn Waldon in 2014
/* Uses Cauchy training */
static const char rcsid[] = "@(#)simann.c++ 1.4 15:02:21 7/25/94 EFC";
#include <cstdlib>
#include <cmath>
#include "simann.hpp"
#ifndef PI2
#define PI2 (PI / 2.0)
#endif
SimAnneal::SimAnneal(const CostFunction& f, const int d)
: func(f),
dimension(d),
maxit(400),
ddwell(20),
dt(0.1),
c_jump(100.0),
rrange(PI2),
K(1.0),
rho(0.5),
t0(0.0),
tscale(0.1)
{
x = new double[dimension];
xnew = new double[dimension];
xbest = new double[dimension];
y = ybest = HUGE;
if (x == NULL || xnew == NULL || xbest == NULL)
err = -1;
else
err = 0;
}
int SimAnneal::set_up(CostFunction f, const int d)
{
dimension = d;
func = f;
x = new double[dimension];
xnew = new double[dimension];
xbest = new double[dimension];
y = ybest = HUGE;
if (x == NULL || xnew == NULL || xbest == NULL)
err = -1;
else
err = 0;
return err;
}
/* increase the temperature until the system "melts" */
double SimAnneal::melt(const int iters)
{
int i, j, ok = 0;
double xc, ynew, t, cold, c = 0.0;
int n = iters;
if (n < 1) n = maxit;
t = t0;
for (i = 0; i < n; i++) {
if (i > 0 && c > 0.0) {
cold = c;
ok = 1;
}
t += dt;
for (j = 0; j < dimension; j++) {
xc = rho * t * tan(uniform.number(-rrange, rrange));
x[j] += xc;
}
equilibrate(t, ddwell);
/* "energy" */
ynew = func(x);
c = ynew - y;
if (c < 0.0 && ynew < ybest) {
for (j = 0; j < dimension; j++) xbest[j] = x[j];
ybest = ynew;
}
y = ynew;
if (ok && c > (c_jump * cold)) /* phase transition */
break;
}
return t0 = t;
}
/* iterate a few times at the present temperature */
/* to get to thermal equilibrium */
int SimAnneal::equilibrate(const double t, const int n)
{
int i, j, equil = 0;
double xc, ynew, c, delta;
double* xtmp;
// double p;
delta = 1.0;
for (i = 0; i < n; i++) {
for (j = 0; j < dimension; j++) {
xc = rho * t * tan(uniform.number(-rrange, rrange));
xnew[j] = x[j] + xc;
}
/* "energy" */
ynew = func(xnew);
c = ynew - y;
if (c < 0.0) /* keep xnew if energy is reduced */
{
xtmp = x;
x = xnew;
xnew = xtmp;
y = ynew;
if (y < ybest) {
for (j = 0; j < dimension; j++) xbest[j] = x[j];
ybest = y;
}
delta = fabs(c);
delta = (y != 0.0) ? delta / y : (ynew != 0.0) ? delta / ynew : delta;
/* equilibrium is defined as a 10% or smaller change
in 10 iterations */
if (delta < 0.10)
equil++;
else
equil = 0;
} else {
/* keep xnew with probability, p, if ynew is increased */
/*
p = exp( - (ynew - y) / (K * t) );
if ( p > uniform.number(0.0, 1.0) )
{
xtmp = x;
x = xnew;
xnew = xtmp;
y = ynew;
equil = 0;
}
else
*/
equil++;
}
if (equil > 9) break;
}
return i + 1;
}
/* cool the system with annealing */
double SimAnneal::anneal(const int iters)
{
int i, j;
double xc, p, ynew, t, c, dt, told;
double* xtmp;
int n = iters;
if (n < 1) n = maxit;
equilibrate(t0, 10 * ddwell);
told = t0;
for (i = 0; i < n; i++) {
t = t0 / (1.0 + i * tscale);
dt = t - told;
told = t;
equilibrate(t, ddwell);
for (j = 0; j < dimension; j++) {
xc = rho * t * tan(uniform.number(-rrange, rrange));
xnew[j] = x[j] + xc;
}
/* "energy" */
ynew = func(xnew);
c = ynew - y;
if (ynew <= y) /* keep xnew if energy is reduced */
{
xtmp = x;
x = xnew;
xnew = xtmp;
y = ynew;
if (y < ybest) {
for (j = 0; j < dimension; j++) xbest[j] = x[j];
ybest = y;
}
continue;
} else {
/* keep xnew with probability, p, if ynew is increased */
p = exp(-(ynew - y) / (K * t));
if (p > uniform.number(0.0, 1.0)) {
xtmp = x;
x = xnew;
xnew = xtmp;
y = ynew;
}
}
}
equilibrate(t, 10 * ddwell);
t0 = t;
return t;
}
void SimAnneal::initial(const double* xi)
{
for (int k = 0; k < dimension; k++) x[k] = xi[k];
}
void SimAnneal::current(double* xc)
{
for (int k = 0; k < dimension; k++) xc[k] = x[k];
}
void SimAnneal::optimum(double* xb)
{
for (int k = 0; k < dimension; k++) xb[k] = xbest[k];
}