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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/*****************************************************************************
Major portions of this software are copyrighted by the Medical College
of Wisconsin, 1994-2000, and are released under the Gnu General Public
License, Version 2. See the file README.Copyright for details.
******************************************************************************/
/*---------------------------------------------------------------------------*/
/*
This file contains routines for implementing the Simplex algorithm for
non-linear optimization to estimate the unknown parameters.
The Simplex algorithm is adapted from: A Jump Start Course in C++ Programming
File: Simplex.c
Author: B. Douglas Ward
Date: 28 January 2000
Note: The calling program should include the following statements
prior to "#include "Simplex.c"
#define DIMENSION p where p = number of parameters to be estimated
#include "randgen.c"
float calc_error (float * vertex);
*/
/*---------------------------------------------------------------------------*/
/*
Global variables and constants.
*/
int count = 0; /* count of error evaluations */
int number_restarts = 0; /* count of simplex algorithm restarts */
/*---------------------------------------------------------------------------*/
void allocate_arrays (float *** simplex, float ** centroid,
float ** response, float ** step_size,
float ** test1, float ** test2)
{
int i;
*centroid = (float *) malloc (sizeof(float) * DIMENSION);
*response = (float *) malloc (sizeof(float) * (DIMENSION+1));
*step_size = (float *) malloc (sizeof(float) * DIMENSION);
*test1 = (float *) malloc (sizeof(float) * DIMENSION);
*test2 = (float *) malloc (sizeof(float) * DIMENSION);
*simplex = (float **) malloc (sizeof(float *) * (DIMENSION+1));
for (i = 0; i < DIMENSION+1; i++)
(*simplex)[i] = (float *) malloc (sizeof(float) * DIMENSION);
}
/*---------------------------------------------------------------------------*/
void deallocate_arrays (float *** simplex, float ** centroid,
float ** response, float ** step_size,
float ** test1, float ** test2)
{
int i;
free (*centroid); *centroid = NULL;
free (*response); *response = NULL;
free (*step_size); *step_size = NULL;
free (*test1); *test1 = NULL;
free (*test2); *test2 = NULL;
for (i = 0; i < DIMENSION+1; i++)
{
free ((*simplex)[i]);
(*simplex)[i] = NULL;
}
free (*simplex); *simplex = NULL;
}
/*---------------------------------------------------------------------------*/
void eval_vertices (float * response, int * worst, int * next, int * best)
{
int i;
/* initialize values */
*worst = 0;
*best = 0;
/* find the best and worst */
for (i = 1; i < DIMENSION+1; i++)
{
if (response[i] > response[*worst])
*worst = i;
if (response[i] < response[*best])
*best = i;
}
/* find the next worst index */
if (*worst == 0)
*next = 1;
else
*next = 0;
for (i = 0; i < DIMENSION+1; i++)
if ((i != *worst) && (response[i] > response[*next]))
*next = i;
}
/*---------------------------------------------------------------------------*/
void restart (float ** simplex, float * response,
float * step_size)
{
const float STEP_FACTOR = 0.9;
int i, j;
int worst, next, best;
float minval, maxval;
/* find the current best vertex */
eval_vertices (response, &worst, &next, &best);
/* set the first vertex to the current best */
for (i = 0; i < DIMENSION; i++)
simplex[0][i] = simplex[best][i];
/* decrease step size */
for (i = 0; i < DIMENSION; i++)
step_size[i] *= STEP_FACTOR;
/* set up remaining vertices of simplex using new step size */
for (i = 1; i < DIMENSION+1; i++)
for (j = 0; j < DIMENSION; j++)
{
minval = simplex[0][j] - step_size[j];
maxval = simplex[0][j] + step_size[j];
simplex[i][j] = rand_uniform (minval, maxval);
}
/* initialize response for each vector */
for (i = 0; i < DIMENSION+1; i++)
response[i] = calc_error (simplex[i]);
}
/*---------------------------------------------------------------------------*/
/*
Calculate the centroid of the simplex, ignoring the worst vertex.
*/
void calc_centroid (float ** simplex, int worst, float * centroid)
{
int i, j;
for (i = 0; i < DIMENSION; i++)
{
centroid[i] = 0.0;
/* add each vertex, except the worst */
for (j = 0; j < DIMENSION+1; j++)
if (j != worst)
centroid[i] += simplex[j][i];
}
/* divide by the number of vertices */
for (i = 0; i < DIMENSION; i++)
centroid[i] /= DIMENSION;
}
/*---------------------------------------------------------------------------*/
/*
Calculate the reflection of the worst vertex about the centroid.
*/
void calc_reflection (float ** simplex, float * centroid,
int worst, float coef, float * vertex)
{
int i;
for (i = 0; i < DIMENSION; i++)
vertex[i] = centroid[i] + coef*(centroid[i] - simplex[worst][i]);
}
/*---------------------------------------------------------------------------*/
/*
Replace a vertex of the simplex.
*/
void replace (float ** simplex, float * response,
int index, float * vertex, float resp)
{
int i;
for (i = 0; i < DIMENSION; i++)
simplex[index][i] = vertex[i];
response[index] = resp;
}
/*---------------------------------------------------------------------------*/
/*
Calculate goodness of fit.
*/
float calc_good_fit (float * response)
{
int i;
float avg, sd, tmp;
/* average the response values */
avg = 0.0;
for (i = 0; i < DIMENSION+1; i++)
avg += response[i];
avg /= DIMENSION+1;
/* compute standard deviation of response */
sd = 0.0;
for (i = 0; i < DIMENSION+1; i++)
{
tmp = response[i] - avg;
sd += tmp*tmp;
}
sd /= DIMENSION;
return (sqrt(sd));
}
/*---------------------------------------------------------------------------*/
/*
Perform initialization for the Simplex algorithm.
*/
void simplex_initialize (float * parameters, float ** simplex,
float * response, float * step_size)
{
int i, j;
int worst, next, best;
float resp;
float minval, maxval;
for (j = 0; j < DIMENSION; j++)
{
simplex[0][j] = parameters[j];
step_size[j] = 0.5 * parameters[j];
}
for (i = 1; i < DIMENSION+1; i++)
for (j = 0; j < DIMENSION; j++)
{
minval = simplex[0][j] - step_size[j];
maxval = simplex[0][j] + step_size[j];
simplex[i][j] = rand_uniform (minval, maxval);
}
for (i = 0; i < DIMENSION+1; i++)
response[i] = calc_error (simplex[i]);
for (i = 1; i < 500; i++)
{
for (j = 0; j < DIMENSION; j++)
{
minval = simplex[0][j] - step_size[j];
maxval = simplex[0][j] + step_size[j];
parameters[j] = rand_uniform (minval, maxval);
}
resp = calc_error (parameters);
eval_vertices (response, &worst, &next, &best);
if (resp < response[worst])
replace (simplex, response, worst, parameters, resp);
}
}
/*---------------------------------------------------------------------------*/
/*
Use Simplex algorithm to estimate the voxel intensity distribution.
The Simplex algorithm is adapted from: A Jump Start Course in C++ Programming
*/
void simplex_optimization (float * parameters, float * sse)
{
const int MAX_ITERATIONS = 100;
const int MAX_RESTARTS = 25;
const float EXPANSION_COEF = 2.0;
const float REFLECTION_COEF = 1.0;
const float CONTRACTION_COEF = 0.5;
const float TOLERANCE = 1.0e-10;
float ** simplex = NULL;
float * centroid = NULL;
float * response = NULL;
float * step_size = NULL;
float * test1 = NULL;
float * test2 = NULL;
float resp1, resp2;
int i, worst, best, next;
int num_iter, num_restarts;
int done;
float fit;
allocate_arrays (&simplex, ¢roid, &response, &step_size, &test1, &test2);
simplex_initialize (parameters, simplex, response, step_size);
/* start loop to do simplex optimization */
num_iter = 0;
num_restarts = 0;
done = 0;
while (!done)
{
/* find the worst vertex and compute centroid of remaining simplex,
discarding the worst vertex */
eval_vertices (response, &worst, &next, &best);
calc_centroid (simplex, worst, centroid);
/* reflect the worst point through the centroid */
calc_reflection (simplex, centroid, worst,
REFLECTION_COEF, test1);
resp1 = calc_error (test1);
/* test the reflection against the best vertex and expand it if the
reflection is better. if not, keep the reflection */
if (resp1 < response[best])
{
/* try expanding */
calc_reflection (simplex, centroid, worst, EXPANSION_COEF, test2);
resp2 = calc_error (test2);
if (resp2 <= resp1) /* keep expansion */
replace (simplex, response, worst, test2, resp2);
else /* keep reflection */
replace (simplex, response, worst, test1, resp1);
}
else if (resp1 < response[next])
{
/* new response is between the best and next worst
so keep reflection */
replace (simplex, response, worst, test1, resp1);
}
else
{
/* try contraction */
if (resp1 >= response[worst])
calc_reflection (simplex, centroid, worst,
-CONTRACTION_COEF, test2);
else
calc_reflection (simplex, centroid, worst,
CONTRACTION_COEF, test2);
resp2 = calc_error (test2);
/* test the contracted response against the worst response */
if (resp2 > response[worst])
{
/* new contracted response is worse, so decrease step size
and restart */
num_iter = 0;
num_restarts += 1;
restart (simplex, response, step_size);
}
else /* keep contraction */
replace (simplex, response, worst, test2, resp2);
}
/* test to determine when to stop.
first, check the number of iterations */
num_iter += 1; /* increment iteration counter */
if (num_iter >= MAX_ITERATIONS)
{
/* restart with smaller steps */
num_iter = 0;
num_restarts += 1;
restart (simplex, response, step_size);
}
/* limit the number of restarts */
if (num_restarts == MAX_RESTARTS) done = 1;
/* compare relative standard deviation of vertex responses
against a defined tolerance limit */
fit = calc_good_fit (response);
if (fit <= TOLERANCE) done = 1;
/* if done, copy the best solution to the output array */
if (done)
{
eval_vertices (response, &worst, &next, &best);
for (i = 0; i < DIMENSION; i++)
parameters[i] = simplex[best][i];
*sse = response[best];
}
} /* while (!done) */
number_restarts = num_restarts;
deallocate_arrays (&simplex, ¢roid, &response, &step_size,
&test1, &test2);
}
/*---------------------------------------------------------------------------*/
|