-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfminsearch.c
360 lines (315 loc) · 7.78 KB
/
fminsearch.c
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
#ifndef ARDUINO
// sp_fminsearch.c - parameter fitting routine using simplex method
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "sigpro.h"
#define MXNV 21 /* maximum number of parameters + 1 */
#define ALFA 1.0 /* reflection coefficient */
#define BETA 0.5 /* contraction coefficient */
#define GAMA 2.0 /* expansion coefficient */
static double (*variance) (float *pv, void *pp);
static int (*early_exit) ();
static void (*report) (float *pv);
static void *parm;
static int sfconvergence();
static void sfinflate();
static void sfhilo();
static void sfcentroid();
static void sfreflect();
static void sfaccept();
static void sfsavelo();
static void sfexpansion();
static void sfcontraction();
static void sfshrink();
static double lores, hires, lstres;
static double icons = 0.01; /* initial constant */
static double ifrac = 0.01; /* initial fraction */
static double ffrac = 0.0001; /* final fraction */
static int npar, nval, hiidx, loidx, lastii;
static float simp[MXNV][MXNV];
/****************************************************************************
*
*
* usage:
* void simpfit(iniv, npv, mxiter, mniter, pvar, prep, peex)
* float iniv[npv];
* int npv, mxiter, mniter;
*
* where:
* iniv - array of initial parameter values
* (final values will be returned here also)
* npv - number of values in the parameter array
* mxiter - maximum number of iterations before convergence
* mniter - minimum number of iterations between reports
* pvar - pointer to function returning variance
* prep - pointer to function producing reports
* peex - pointer to function checking for "early exit"
* ppar - pointer to user-defined paramter data
*
* The user must also supply a function called "variance" that
* will compute the residual variance for a given set of parameters
* and a function called "report" to handle reports of intermediate
* parameter values prior to convergence:
*
* float pv[npv];
* void *pp;
* double variance(pv, pp)
* void report(pv)
*
****************************************************************************
*/
static void
simpfit(
float *iniv,
int npv,
int mxiter,
int mniter,
double (*pvar) (float *, void *),
void (*prep) (float *),
int (*peex) (),
void *ppar
)
{
int i;
float cent[MXNV], nxtv[MXNV], nxres;
if (npv >= MXNV)
return;
variance = pvar;
report = prep;
early_exit = peex;
parm = ppar;
lores = hires = lstres = 0;
lastii = 0;
npar = npv;
nval = npv + 1;
sfinflate(iniv);
sfhilo(0, 0);
for (i = 0; i < mxiter; i++) {
if (early_exit && early_exit())
break;
sfhilo(i, mniter);
sfcentroid(cent);
sfreflect(cent, nxtv);
if (nxtv[npar] < hires) {
sfaccept(nxtv);
if (nxtv[npar] < lores) {
nxres = nxtv[npar];
sfexpansion(cent, nxtv);
if (nxtv[npar] < nxres)
sfaccept(nxtv);
}
} else {
sfcontraction(cent, nxtv);
if (nxtv[npar] < hires)
sfaccept(nxtv);
else
sfshrink();
if (sfconvergence()!= 0)
break;
}
}
sfhilo(i, 0);
sfsavelo(iniv);
return;
}
static void
sfinflate(iniv)
float iniv[MXNV];
{
int i, j;
float d;
for (i = 0; i < npar; i++)
simp[0][i] = iniv[i];
simp[0][npar] = (float) variance(simp[0], parm);
if (report) {
report(simp[0]);
}
for (j = 1; j < nval; j++) {
for (i = 0; i < npar; i++)
simp[j][i] = simp[0][i];
d = simp[0][j - 1];
simp[j][j - 1] = (float) ((d == 0) ? icons : d * (1 + ifrac));
simp[j][npar] = (float) variance(simp[j], parm);
}
return;
}
static void
sfhilo(ii, ir)
int ii, ir;
{
int i;
hiidx = 0;
hires = simp[hiidx][npar];
loidx = 0;
lores = simp[loidx][npar];
for (i = 0; i < nval; i++) {
if (hires < simp[i][npar]) {
hires = simp[i][npar];
hiidx = i;
}
if (lores > simp[i][npar]) {
lores = simp[i][npar];
loidx = i;
}
}
if (ir > 0) {
if (ii < lastii + ir)
return;
if (lores >= lstres)
return;
}
lastii = ii;
lstres = lores;
if (report) {
report(simp[loidx]);
}
return;
}
static void
sfcentroid(cent)
float cent[MXNV];
{
int i, j;
for (i = 0; i < npar; i++) {
cent[i] = 0;
for (j = 0; j < nval; j++)
if (j != hiidx)
cent[i] = cent[i] + simp[j][i];
cent[i] = cent[i] / npar;
}
return;
}
static void
sfreflect(cent, nxtv)
float cent[MXNV], nxtv[MXNV];
{
int i;
for (i = 0; i < npar; i++)
nxtv[i] = (float) ((1 + ALFA) * cent[i] - ALFA * simp[hiidx][i]);
nxtv[npar] = (float) variance(nxtv, parm);
return;
}
static void
sfaccept(nxtv)
float nxtv[MXNV];
{
int i;
for (i = 0; i < nval; i++)
simp[hiidx][i] = nxtv[i];
return;
}
static void
sfsavelo(iniv)
float iniv[MXNV];
{
int i;
for (i = 0; i < npar; i++)
iniv[i] = simp[loidx][i];
return;
}
static void
sfexpansion(cent, nxtv)
float cent[MXNV], nxtv[MXNV];
{
int i;
for (i = 0; i < npar; i++)
nxtv[i] = (float) ((1 - GAMA) * cent[i] + GAMA * simp[hiidx][i]);
nxtv[npar] = (float) variance(nxtv, parm);
return;
}
static void
sfcontraction(cent, nxtv)
float cent[MXNV], nxtv[MXNV];
{
int i;
for (i = 0; i < npar; i++)
nxtv[i] = (float) ((1 - BETA) * cent[i] + BETA * simp[hiidx][i]);
nxtv[npar] = (float) variance(nxtv, parm);
return;
}
static void
sfshrink()
{
int i, j;
for (j = 0; j < nval; j++) {
for (i = 0; i < npar; i++)
simp[j][i] = (float) ((1 - BETA) * simp[loidx][i] + BETA * simp[j][i]);
simp[j][npar] = (float) variance(simp[j], parm);
}
return;
}
static int
sfconvergence()
{
int i, j;
double error, hi[MXNV], lo[MXNV];
for (i = 0; i < nval; i++) {
hi[i] = fabs(simp[0][i]);
lo[i] = fabs(simp[0][i]);
}
for (j = 1; j < nval; j++) {
for (i = 0; i < nval; i++) {
if (hi[i] < fabs(simp[j][i]))
hi[i] = fabs(simp[j][i]);
if (lo[i] > fabs(simp[j][i]))
lo[i] = fabs(simp[j][i]);
}
}
for (i = 0; i < npar; i++) {
error = (hi[i] > lo[i]) ? (1 - lo[i] / hi[i]) : 0;
if (error > ffrac)
return (0);
}
return (1);
}
/*************************************************************************/
// sp_fminsearch - search for parameters with minimum error
FUNC(int)
sp_fminsearch(
float *iniv,
int npv,
double (*pvar) (float *, void *),
OPT *o,
void *ppar
)
{
int mxiter, mniter;
void (*prep) (float *) = NULL;
int (*peex) () = NULL;
if (npv >= MXNV) { // number of parameters must be
return (1); // less than simplex array size
}
icons = 0.00025;
ifrac = 0.05;
mxiter = 1000;
mniter = npv;
prep = NULL;
peex = NULL;
if (o) {
if (o->icons > 0) {
icons = o->icons;
}
if (o->ifrac > 0) {
ifrac = o->ifrac;
}
if (o->ffrac > 0) {
ffrac = o->ffrac;
}
if (o->maxiter > 0) {
mxiter = o->maxiter;
}
if (o->miniter > 0) {
mniter = o->miniter;
}
if (o->report) {
prep = o->report;
}
if (o->escape) {
peex = o->escape;
}
}
simpfit(iniv, npv, mxiter, mniter, pvar, prep, peex, ppar);
return (0);
}
#endif