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
|
/*
Explore.c
sample region, determine min and max, split if necessary
this file is part of Divonne
last modified 15 Nov 11 th
*/
typedef struct {
real fmin, fmax;
creal *xmin, *xmax;
} Extrema;
/*********************************************************************/
static int Explore(This *t, ccount iregion)
{
TYPEDEFREGION;
Region *region = RegionPtr(iregion);
cBounds *bounds = region->bounds;
Result *result = region->result;
count n, dim, comp, maxcomp;
Extrema extrema[NCOMP];
Result *r;
creal *x;
real *f;
real halfvol, maxerr;
cSamples *samples = &t->samples[region->isamples];
/* needed as of gcc 3.3 to make gcc correctly address region #@$&! */
sizeof(*region);
for( comp = 0; comp < t->ncomp; ++comp ) {
Extrema *e = &extrema[comp];
e->fmin = INFTY;
e->fmax = -INFTY;
e->xmin = e->xmax = NULL;
}
if( region->isamples == 0 ) { /* others already sampled */
real vol = 1;
for( dim = 0; dim < t->ndim; ++dim ) {
cBounds *b = &bounds[dim];
vol *= b->upper - b->lower;
}
region->vol = vol;
for( comp = 0; comp < t->ncomp; ++comp ) {
Result *r = &result[comp];
r->fmin = INFTY;
r->fmax = -INFTY;
}
x = t->xgiven;
f = t->fgiven;
n = t->ngiven;
if( t->nextra ) n += SampleExtra(t, bounds);
for( ; n; --n ) {
for( dim = 0; dim < t->ndim; ++dim ) {
cBounds *b = &bounds[dim];
if( x[dim] < b->lower || x[dim] > b->upper ) goto skip;
}
for( comp = 0; comp < t->ncomp; ++comp ) {
Extrema *e = &extrema[comp];
creal y = f[comp];
if( y < e->fmin ) e->fmin = y, e->xmin = x;
if( y > e->fmax ) e->fmax = y, e->xmax = x;
}
skip:
x += t->ldxgiven;
f += t->ncomp;
}
samples->sampler(t, iregion);
}
x = samples->x;
f = samples->f;
for( n = samples->n; n; --n ) {
for( comp = 0; comp < t->ncomp; ++comp ) {
Extrema *e = &extrema[comp];
creal y = *f++;
if( y < e->fmin ) e->fmin = y, e->xmin = x;
if( y > e->fmax ) e->fmax = y, e->xmax = x;
}
x += t->ndim;
}
t->neval_opt -= t->neval;
halfvol = .5*region->vol;
maxerr = -INFTY;
maxcomp = -1;
for( comp = 0; comp < t->ncomp; ++comp ) {
Extrema *e = &extrema[comp];
Result *r = &result[comp];
real xtmp[NDIM], ftmp, err;
if( e->xmin ) { /* not all NaNs */
t->selectedcomp = comp;
VecCopy(xtmp, e->xmin);
ftmp = FindMinimum(t, bounds, xtmp, e->fmin);
if( ftmp < r->fmin ) {
r->fmin = ftmp;
VecCopy(r->xmin, xtmp);
}
t->selectedcomp = Tag(comp);
VecCopy(xtmp, e->xmax);
ftmp = -FindMinimum(t, bounds, xtmp, -e->fmax);
if( ftmp > r->fmax ) {
r->fmax = ftmp;
VecCopy(r->xmax, xtmp);
}
}
r->spread = halfvol*(r->fmax - r->fmin);
err = r->spread/Max(fabs(r->avg), NOTZERO);
if( err > maxerr ) {
maxerr = err;
maxcomp = comp;
}
}
t->neval_opt += t->neval;
if( maxcomp == -1 ) { /* all NaNs */
region->depth = 0;
return -1;
}
region->cutcomp = maxcomp;
r = ®ion->result[maxcomp];
if( halfvol*(r->fmin + r->fmax) > r->avg ) {
region->fminor = r->fmin;
region->fmajor = r->fmax;
region->xmajor = r->xmax - (real *)region->result;
}
else {
region->fminor = r->fmax;
region->fmajor = r->fmin;
region->xmajor = r->xmin - (real *)region->result;
}
if( region->isamples == 0 ) {
if( r->spread < samples->neff*r->err ||
r->spread < t->totals[maxcomp].secondspread ) region->depth = 0;
if( region->depth == 0 )
for( comp = 0; comp < t->ncomp; ++comp )
t->totals[comp].secondspread =
Max(t->totals[comp].secondspread, result[comp].spread);
}
if( region->depth ) Split(t, iregion);
return iregion;
}
|