optimization-test-functions 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +45 -10
- package/package.json +28 -28
- package/src/index.d.ts +93 -81
- package/src/index.js +660 -386
package/src/index.js
CHANGED
|
@@ -1,386 +1,660 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* optimization-test-functions
|
|
3
|
-
*
|
|
4
|
-
* Canonical implementations of standard global-optimization benchmarks and
|
|
5
|
-
* classical psychophysical laws, with the metadata you need to use them
|
|
6
|
-
* honestly: the domain each is defined on, its known optimum, and whether the
|
|
7
|
-
* interesting direction is a minimum or a maximum.
|
|
8
|
-
*
|
|
9
|
-
* Every function is in its PUBLISHED sense. The optimization benchmarks are
|
|
10
|
-
* minimized, as their sources define them; the psychophysical laws are given
|
|
11
|
-
* as the law states them. Nothing here is silently negated — if your optimizer
|
|
12
|
-
* maximizes, wrap with `negate`.
|
|
13
|
-
*
|
|
14
|
-
* Domains are per-axis, not one range applied to every dimension. Branin in
|
|
15
|
-
* particular is defined on an asymmetric box and is often gotten wrong.
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
const TAU = 2 * Math.PI;
|
|
19
|
-
|
|
20
|
-
/* ------------------------------------------------------------------ *
|
|
21
|
-
* helpers *
|
|
22
|
-
* ------------------------------------------------------------------ */
|
|
23
|
-
|
|
24
|
-
function
|
|
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
|
-
const
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
},
|
|
206
|
-
reference: '
|
|
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
|
-
export function
|
|
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
|
-
|
|
1
|
+
/**
|
|
2
|
+
* optimization-test-functions
|
|
3
|
+
*
|
|
4
|
+
* Canonical implementations of standard global-optimization benchmarks and
|
|
5
|
+
* classical psychophysical laws, with the metadata you need to use them
|
|
6
|
+
* honestly: the domain each is defined on, its known optimum, and whether the
|
|
7
|
+
* interesting direction is a minimum or a maximum.
|
|
8
|
+
*
|
|
9
|
+
* Every function is in its PUBLISHED sense. The optimization benchmarks are
|
|
10
|
+
* minimized, as their sources define them; the psychophysical laws are given
|
|
11
|
+
* as the law states them. Nothing here is silently negated — if your optimizer
|
|
12
|
+
* maximizes, wrap with `negate`.
|
|
13
|
+
*
|
|
14
|
+
* Domains are per-axis, not one range applied to every dimension. Branin in
|
|
15
|
+
* particular is defined on an asymmetric box and is often gotten wrong.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const TAU = 2 * Math.PI;
|
|
19
|
+
|
|
20
|
+
/* ------------------------------------------------------------------ *
|
|
21
|
+
* helpers *
|
|
22
|
+
* ------------------------------------------------------------------ */
|
|
23
|
+
|
|
24
|
+
function isVec(x) {
|
|
25
|
+
return Array.isArray(x) || (
|
|
26
|
+
ArrayBuffer.isView(x) &&
|
|
27
|
+
!(x instanceof DataView) &&
|
|
28
|
+
!(x instanceof BigInt64Array) &&
|
|
29
|
+
!(x instanceof BigUint64Array)
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function assertVec(x, name) {
|
|
34
|
+
if (!isVec(x)) throw new TypeError(`${name}: expected an array of numbers`);
|
|
35
|
+
if (x.length < 1) throw new RangeError(`${name}: unsupported dimension ${x.length}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function assertDim(x, name, d) {
|
|
39
|
+
if (!isVec(x)) throw new TypeError(`${name}: expected an array of numbers`);
|
|
40
|
+
if (x.length !== d) throw new RangeError(`${name}: unsupported dimension ${x.length}`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function assertOpts(options, name) {
|
|
44
|
+
if (options === null || typeof options !== 'object') {
|
|
45
|
+
throw new TypeError(`${name}: options must be an object`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function checkDimFor(name, dims, d) {
|
|
50
|
+
if (typeof dims === 'number') {
|
|
51
|
+
if (d !== undefined && d !== dims) throw new RangeError(`${name} is ${dims}-D, not ${d}-D`);
|
|
52
|
+
return dims;
|
|
53
|
+
}
|
|
54
|
+
if (d === undefined) throw new RangeError(`${name}: dimension is required`);
|
|
55
|
+
if (!Number.isInteger(d) || d < 1) {
|
|
56
|
+
throw new RangeError(`${name}: dimension must be a positive integer, got ${d}`);
|
|
57
|
+
}
|
|
58
|
+
if (dims === '>= 2' && d < 2) {
|
|
59
|
+
throw new RangeError(`${name}: dimension must be >= 2, got ${d}`);
|
|
60
|
+
}
|
|
61
|
+
if (dims === 'multiple of 4' && (d & 3) !== 0) {
|
|
62
|
+
throw new RangeError(`${name}: dimension must be a multiple of 4, got ${d}`);
|
|
63
|
+
}
|
|
64
|
+
return d;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function fillAt(name, dims, val) {
|
|
68
|
+
return (d) => new Array(checkDimFor(name, dims, d)).fill(val);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function fixedAt(name, pt) {
|
|
72
|
+
const n = pt.length;
|
|
73
|
+
return (d = n) => {
|
|
74
|
+
if (d !== n) throw new RangeError(`${name} is ${n}-D, not ${d}-D`);
|
|
75
|
+
return pt.slice();
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Wrap a function so a minimizer can be used as a maximizer, or vice versa.
|
|
81
|
+
* The metadata follows: the sense, the optimum value (and any per-dimension
|
|
82
|
+
* table of them) and the direction of monotonicity all flip with the sign.
|
|
83
|
+
*/
|
|
84
|
+
export function negate(f) {
|
|
85
|
+
if (typeof f !== 'function') throw new TypeError('negate: expected a function');
|
|
86
|
+
const g = (...args) => 0 - f(...args);
|
|
87
|
+
if (f.meta) {
|
|
88
|
+
const { sense, monotone, optimum } = f.meta;
|
|
89
|
+
g.meta = {
|
|
90
|
+
...f.meta,
|
|
91
|
+
sense: sense === 'min' ? 'max' : 'min',
|
|
92
|
+
optimum: { ...optimum, value: optimum.value === null ? null : 0 - optimum.value },
|
|
93
|
+
};
|
|
94
|
+
if (optimum.known) {
|
|
95
|
+
g.meta.optimum.known = Object.fromEntries(
|
|
96
|
+
Object.entries(optimum.known).map(([d, v]) => [d, 0 - v]));
|
|
97
|
+
}
|
|
98
|
+
if (monotone) g.meta.monotone = monotone === 'increasing' ? 'decreasing' : 'increasing';
|
|
99
|
+
}
|
|
100
|
+
return g;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Expand a per-axis domain to `d` dimensions, for functions defined on a cube. */
|
|
104
|
+
function cube(lo, hi, dims = 'any', name = 'domain') {
|
|
105
|
+
if (typeof dims === 'number') {
|
|
106
|
+
return (d = dims) => {
|
|
107
|
+
if (d !== dims) throw new RangeError(`${name} is ${dims}-D, not ${d}-D`);
|
|
108
|
+
const out = new Array(dims);
|
|
109
|
+
for (let i = 0; i < dims; i++) out[i] = [lo, hi];
|
|
110
|
+
return out;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
return (d) => {
|
|
114
|
+
const n = checkDimFor(name, dims, d);
|
|
115
|
+
const out = new Array(n);
|
|
116
|
+
for (let i = 0; i < n; i++) out[i] = [lo, hi];
|
|
117
|
+
return out;
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/* ------------------------------------------------------------------ *
|
|
122
|
+
* classical optimization benchmarks (minimized) *
|
|
123
|
+
* ------------------------------------------------------------------ */
|
|
124
|
+
|
|
125
|
+
/** Ackley (1987). Flat outer plain, narrow basin at the origin. */
|
|
126
|
+
export function ackley(x, options) {
|
|
127
|
+
assertVec(x, 'ackley');
|
|
128
|
+
let a = 20, b = 0.2, c = TAU;
|
|
129
|
+
if (options !== undefined) {
|
|
130
|
+
assertOpts(options, 'ackley');
|
|
131
|
+
if (options.a !== undefined) a = options.a;
|
|
132
|
+
if (options.b !== undefined) b = options.b;
|
|
133
|
+
if (options.c !== undefined) c = options.c;
|
|
134
|
+
}
|
|
135
|
+
const d = x.length;
|
|
136
|
+
let s1 = 0, s2 = 0;
|
|
137
|
+
for (let i = 0; i < d; i++) {
|
|
138
|
+
const xi = x[i];
|
|
139
|
+
s1 += xi * xi;
|
|
140
|
+
s2 += Math.cos(c * xi);
|
|
141
|
+
}
|
|
142
|
+
return (a - a * Math.exp(-b * Math.sqrt(s1 / d))) + (Math.E - Math.exp(s2 / d));
|
|
143
|
+
}
|
|
144
|
+
ackley.meta = {
|
|
145
|
+
name: 'Ackley', dims: 'any', sense: 'min', domain: cube(-32.768, 32.768, 'any', 'ackley'),
|
|
146
|
+
optimum: { value: 0, at: fillAt('ackley', 'any', 0) },
|
|
147
|
+
reference: 'Ackley, D. H. (1987). A Connectionist Machine for Genetic Hillclimbing.',
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const GRIEWANK_SQRT = Float64Array.from({ length: 64 }, (_, i) => Math.sqrt(i + 1));
|
|
151
|
+
|
|
152
|
+
/** Griewank (1981). Wide bowl with fine cosine ripples. */
|
|
153
|
+
export function griewank(x) {
|
|
154
|
+
assertVec(x, 'griewank');
|
|
155
|
+
const d = x.length;
|
|
156
|
+
let s = 0, p = 1;
|
|
157
|
+
const fast = d <= 64 ? d : 64;
|
|
158
|
+
let i = 0;
|
|
159
|
+
for (; i < fast; i++) {
|
|
160
|
+
const xi = x[i];
|
|
161
|
+
s += xi * xi;
|
|
162
|
+
p *= Math.cos(xi / GRIEWANK_SQRT[i]);
|
|
163
|
+
}
|
|
164
|
+
for (; i < d; i++) {
|
|
165
|
+
const xi = x[i];
|
|
166
|
+
s += xi * xi;
|
|
167
|
+
p *= Math.cos(xi / Math.sqrt(i + 1));
|
|
168
|
+
}
|
|
169
|
+
return s / 4000 + (1 - p);
|
|
170
|
+
}
|
|
171
|
+
griewank.meta = {
|
|
172
|
+
name: 'Griewank', dims: 'any', sense: 'min', domain: cube(-600, 600, 'any', 'griewank'),
|
|
173
|
+
optimum: { value: 0, at: fillAt('griewank', 'any', 0) },
|
|
174
|
+
reference: 'Griewank, A. O. (1981). Journal of Optimization Theory and Applications 34(1).',
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/** Schwefel (1981). Global optimum far from the deepest local one. */
|
|
178
|
+
export function schwefel(x) {
|
|
179
|
+
assertVec(x, 'schwefel');
|
|
180
|
+
const d = x.length;
|
|
181
|
+
let s = 0;
|
|
182
|
+
// max of t sin(sqrt t): t = u^2 where tan(u) = -u/2, solved at 50 digits.
|
|
183
|
+
// Subtract per coordinate so large-d summation does not drift off 0.
|
|
184
|
+
for (let i = 0; i < d; i++) {
|
|
185
|
+
const xi = x[i];
|
|
186
|
+
s += 418.9828872724337 - xi * Math.sin(Math.sqrt(Math.abs(xi)));
|
|
187
|
+
}
|
|
188
|
+
return s;
|
|
189
|
+
}
|
|
190
|
+
schwefel.meta = {
|
|
191
|
+
name: 'Schwefel', dims: 'any', sense: 'min', domain: cube(-500, 500, 'any', 'schwefel'),
|
|
192
|
+
optimum: { value: 0, at: fillAt('schwefel', 'any', 420.96874635998205) },
|
|
193
|
+
reference: 'Schwefel, H.-P. (1981). Numerical Optimization of Computer Models.',
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/** Eggholder. Two dimensions, wildly multimodal, no usable global structure. */
|
|
197
|
+
export function eggholder(x) {
|
|
198
|
+
assertDim(x, 'eggholder', 2);
|
|
199
|
+
const a = x[0], b = x[1], b47 = b + 47;
|
|
200
|
+
return -b47 * Math.sin(Math.sqrt(Math.abs(a / 2 + b47)))
|
|
201
|
+
- a * Math.sin(Math.sqrt(Math.abs(a - b47)));
|
|
202
|
+
}
|
|
203
|
+
eggholder.meta = {
|
|
204
|
+
name: 'Eggholder', dims: 2, sense: 'min', domain: cube(-512, 512, 2, 'eggholder'),
|
|
205
|
+
optimum: { value: -959.6406627208509, at: fixedAt('eggholder', [512, 404.2318051137578]) },
|
|
206
|
+
reference: 'Vanaret et al. (2014); after Whitley et al. (1996).',
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/** Powell (1962). Smooth quartic valley. Dimension must be a multiple of 4. */
|
|
210
|
+
export function powell(x) {
|
|
211
|
+
if (!isVec(x)) throw new TypeError('powell: expected an array of numbers');
|
|
212
|
+
const d = x.length;
|
|
213
|
+
if (d === 0 || (d & 3) !== 0) throw new RangeError(`powell: unsupported dimension ${d}`);
|
|
214
|
+
let s = 0;
|
|
215
|
+
for (let i = 0; i < d; i += 4) {
|
|
216
|
+
const x0 = x[i], x1 = x[i + 1], x2 = x[i + 2], x3 = x[i + 3];
|
|
217
|
+
const t1 = x0 + 10 * x1;
|
|
218
|
+
const t2 = x2 - x3;
|
|
219
|
+
const t3 = x1 - 2 * x2, t3_2 = t3 * t3;
|
|
220
|
+
const t4 = x0 - x3, t4_2 = t4 * t4;
|
|
221
|
+
s += t1 * t1 + 5 * (t2 * t2) + t3_2 * t3_2 + 10 * (t4_2 * t4_2);
|
|
222
|
+
}
|
|
223
|
+
return s;
|
|
224
|
+
}
|
|
225
|
+
powell.meta = {
|
|
226
|
+
name: 'Powell', dims: 'multiple of 4', sense: 'min', domain: cube(-4, 5, 'multiple of 4', 'powell'),
|
|
227
|
+
optimum: { value: 0, at: fillAt('powell', 'multiple of 4', 0) },
|
|
228
|
+
reference: 'Powell, M. J. D. (1962). The Computer Journal 5(2).',
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const SHEKEL_A = new Float64Array([
|
|
232
|
+
4, 4, 4, 4, 1, 1, 1, 1, 8, 8, 8, 8, 6, 6, 6, 6, 3, 7, 3, 7,
|
|
233
|
+
2, 9, 2, 9, 5, 3, 5, 3, 8, 1, 8, 1, 6, 2, 6, 2, 7, 3.6, 7, 3.6,
|
|
234
|
+
]);
|
|
235
|
+
const SHEKEL_C = new Float64Array([0.1, 0.2, 0.2, 0.4, 0.4, 0.6, 0.3, 0.7, 0.5, 0.5]);
|
|
236
|
+
|
|
237
|
+
/** Shekel. Four dimensions, `m` narrow wells on a flat plain. */
|
|
238
|
+
export function shekel(x, options) {
|
|
239
|
+
assertDim(x, 'shekel', 4);
|
|
240
|
+
let m = 10;
|
|
241
|
+
if (options !== undefined) {
|
|
242
|
+
assertOpts(options, 'shekel');
|
|
243
|
+
if (options.m !== undefined) m = options.m;
|
|
244
|
+
if (!(Number.isInteger(m) && m >= 1 && m <= 10)) {
|
|
245
|
+
throw new RangeError('shekel: m must be an integer in 1..10');
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
const x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3];
|
|
249
|
+
let s = 0;
|
|
250
|
+
for (let i = 0, k = 0; i < m; i++, k += 4) {
|
|
251
|
+
const d0 = x0 - SHEKEL_A[k];
|
|
252
|
+
const d1 = x1 - SHEKEL_A[k + 1];
|
|
253
|
+
const d2 = x2 - SHEKEL_A[k + 2];
|
|
254
|
+
const d3 = x3 - SHEKEL_A[k + 3];
|
|
255
|
+
s += 1 / (d0 * d0 + d1 * d1 + d2 * d2 + d3 * d3 + SHEKEL_C[i]);
|
|
256
|
+
}
|
|
257
|
+
return -s;
|
|
258
|
+
}
|
|
259
|
+
shekel.meta = {
|
|
260
|
+
name: 'Shekel', dims: 4, sense: 'min', domain: cube(0, 10, 4, 'shekel'),
|
|
261
|
+
optimum: {
|
|
262
|
+
value: -10.536443153483528,
|
|
263
|
+
// Almost always quoted as (4, 4, 4, 4), which is only approximate: the
|
|
264
|
+
// neighbouring wells pull the minimum slightly off that corner, and
|
|
265
|
+
// f(4,4,4,4) = -10.536284, not -10.536443. Refined location below; it
|
|
266
|
+
// has x1 = x3 and x2 = x4, as the symmetry of the wells requires.
|
|
267
|
+
at: fixedAt('shekel', [4.000746868270634, 3.9995094800857736, 4.000746868270634, 3.9995094800857736]),
|
|
268
|
+
approx: [4, 4, 4, 4],
|
|
269
|
+
note: 'for m = 10',
|
|
270
|
+
},
|
|
271
|
+
reference: 'Shekel, J. (1971). Test functions for multimodal search techniques. '
|
|
272
|
+
+ 'Fifth Annual Princeton Conf. on Information Science and Systems.',
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
const H_ALPHA = new Float64Array([1.0, 1.2, 3.0, 3.2]);
|
|
276
|
+
const H3_A = new Float64Array([3.0, 10, 30, 0.1, 10, 35, 3.0, 10, 30, 0.1, 10, 35]);
|
|
277
|
+
// v / 1e4 rather than v * 1e-4: 1e-4 is inexact, and the product is 1 ulp off
|
|
278
|
+
// the decimal constant for a third of the entries; the quotient never is.
|
|
279
|
+
const H3_P = Float64Array.from(
|
|
280
|
+
[3689, 1170, 2673, 4699, 4387, 7470, 1091, 8732, 5547, 381, 5743, 8828],
|
|
281
|
+
(v) => v / 1e4,
|
|
282
|
+
);
|
|
283
|
+
|
|
284
|
+
/** Hartmann 3-D. Four Gaussian basins of unequal depth. */
|
|
285
|
+
export function hartmann3(x) {
|
|
286
|
+
assertDim(x, 'hartmann3', 3);
|
|
287
|
+
const x0 = x[0], x1 = x[1], x2 = x[2];
|
|
288
|
+
let s = 0;
|
|
289
|
+
for (let i = 0, k = 0; i < 4; i++, k += 3) {
|
|
290
|
+
const d0 = x0 - H3_P[k], d1 = x1 - H3_P[k + 1], d2 = x2 - H3_P[k + 2];
|
|
291
|
+
s += H_ALPHA[i] * Math.exp(-(H3_A[k] * d0 * d0 + H3_A[k + 1] * d1 * d1 + H3_A[k + 2] * d2 * d2));
|
|
292
|
+
}
|
|
293
|
+
return -s;
|
|
294
|
+
}
|
|
295
|
+
hartmann3.meta = {
|
|
296
|
+
name: 'Hartmann-3', dims: 3, sense: 'min', domain: cube(0, 1, 3, 'hartmann3'),
|
|
297
|
+
optimum: {
|
|
298
|
+
value: -3.8627797873326624,
|
|
299
|
+
// The widely copied -3.86278214782076 is 2.4e-6 BELOW the true minimum,
|
|
300
|
+
// so no point attains it, and the usual location (kept as `approx`) is
|
|
301
|
+
// 2.5e-5 off in x1. Both re-derived at 50 digits.
|
|
302
|
+
at: fixedAt('hartmann3', [0.11458887665506896, 0.55564889461693, 0.8525469846866774]),
|
|
303
|
+
approx: [0.114614, 0.555649, 0.852547],
|
|
304
|
+
},
|
|
305
|
+
reference: 'Hartman, J. K. (1973). Naval Research Logistics Quarterly 20(3).',
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
const H6_A = new Float64Array([
|
|
309
|
+
10, 3, 17, 3.5, 1.7, 8, 0.05, 10, 17, 0.1, 8, 14,
|
|
310
|
+
3, 3.5, 1.7, 10, 17, 8, 17, 8, 0.05, 10, 0.1, 14,
|
|
311
|
+
]);
|
|
312
|
+
const H6_P = Float64Array.from([
|
|
313
|
+
1312, 1696, 5569, 124, 8283, 5886, 2329, 4135, 8307, 3736, 1004, 9991,
|
|
314
|
+
2348, 1451, 3522, 2883, 3047, 6650, 4047, 8828, 8732, 5743, 1091, 381,
|
|
315
|
+
], (v) => v / 1e4);
|
|
316
|
+
|
|
317
|
+
/** Hartmann 6-D. The usual stress test for a GP with one lengthscale per axis. */
|
|
318
|
+
export function hartmann6(x) {
|
|
319
|
+
assertDim(x, 'hartmann6', 6);
|
|
320
|
+
const x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3], x4 = x[4], x5 = x[5];
|
|
321
|
+
let s = 0;
|
|
322
|
+
for (let i = 0, k = 0; i < 4; i++, k += 6) {
|
|
323
|
+
const d0 = x0 - H6_P[k], d1 = x1 - H6_P[k + 1], d2 = x2 - H6_P[k + 2];
|
|
324
|
+
const d3 = x3 - H6_P[k + 3], d4 = x4 - H6_P[k + 4], d5 = x5 - H6_P[k + 5];
|
|
325
|
+
const inner = H6_A[k] * d0 * d0 + H6_A[k + 1] * d1 * d1 + H6_A[k + 2] * d2 * d2
|
|
326
|
+
+ H6_A[k + 3] * d3 * d3 + H6_A[k + 4] * d4 * d4 + H6_A[k + 5] * d5 * d5;
|
|
327
|
+
s += H_ALPHA[i] * Math.exp(-inner);
|
|
328
|
+
}
|
|
329
|
+
return -s;
|
|
330
|
+
}
|
|
331
|
+
hartmann6.meta = {
|
|
332
|
+
name: 'Hartmann-6', dims: 6, sense: 'min', domain: cube(0, 1, 6, 'hartmann6'),
|
|
333
|
+
optimum: {
|
|
334
|
+
// The usual value -3.322368011391339 is f at the usual six-digit
|
|
335
|
+
// location (kept as `approx`), not the minimum, which is 2.4e-11 lower.
|
|
336
|
+
value: -3.3223680114155147,
|
|
337
|
+
at: fixedAt('hartmann6', [
|
|
338
|
+
0.20168951100670543, 0.15001069182345797, 0.476873974221897,
|
|
339
|
+
0.2753324304940561, 0.31165161660011326, 0.6573005340656203,
|
|
340
|
+
]),
|
|
341
|
+
approx: [0.20169, 0.150011, 0.476874, 0.275332, 0.311652, 0.6573],
|
|
342
|
+
},
|
|
343
|
+
reference: 'Hartman, J. K. (1973). Naval Research Logistics Quarterly 20(3).',
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
const BRANIN_B = 5.1 / (4 * Math.PI ** 2);
|
|
347
|
+
const BRANIN_C = 5 / Math.PI;
|
|
348
|
+
const BRANIN_T = 1 / (8 * Math.PI);
|
|
349
|
+
const BRANIN_S1MT = 10 * (1 - BRANIN_T);
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Branin-Hoo. Three equal global minima in a shallow curved trough.
|
|
353
|
+
* Note the ASYMMETRIC domain: x1 in [-5, 10], x2 in [0, 15]. Applying one
|
|
354
|
+
* range to both axes is a common and silent mistake.
|
|
355
|
+
*/
|
|
356
|
+
export function branin(x, options) {
|
|
357
|
+
assertDim(x, 'branin', 2);
|
|
358
|
+
const x1 = x[0], x2 = x[1];
|
|
359
|
+
if (options === undefined) {
|
|
360
|
+
const u = x2 - BRANIN_B * x1 * x1 + BRANIN_C * x1 - 6;
|
|
361
|
+
return u * u + BRANIN_S1MT * Math.cos(x1) + 10;
|
|
362
|
+
}
|
|
363
|
+
assertOpts(options, 'branin');
|
|
364
|
+
const a = options.a !== undefined ? options.a : 1;
|
|
365
|
+
const b = options.b !== undefined ? options.b : BRANIN_B;
|
|
366
|
+
const c = options.c !== undefined ? options.c : BRANIN_C;
|
|
367
|
+
const r = options.r !== undefined ? options.r : 6;
|
|
368
|
+
const s = options.s !== undefined ? options.s : 10;
|
|
369
|
+
const t = options.t !== undefined ? options.t : BRANIN_T;
|
|
370
|
+
const u = x2 - b * x1 * x1 + c * x1 - r;
|
|
371
|
+
return a * (u * u) + s * (1 - t) * Math.cos(x1) + s;
|
|
372
|
+
}
|
|
373
|
+
branin.meta = {
|
|
374
|
+
name: 'Branin', dims: 2, sense: 'min',
|
|
375
|
+
domain: (d = 2) => {
|
|
376
|
+
if (d !== 2) throw new RangeError(`branin is 2-D, not ${d}-D`);
|
|
377
|
+
return [[-5, 10], [0, 15]];
|
|
378
|
+
},
|
|
379
|
+
optimum: {
|
|
380
|
+
value: 5 / (4 * Math.PI),
|
|
381
|
+
at: fixedAt('branin', [-Math.PI, 12.275]),
|
|
382
|
+
all: [[-Math.PI, 12.275], [Math.PI, 2.275], [3 * Math.PI, 2.475]],
|
|
383
|
+
},
|
|
384
|
+
reference: 'Branin, F. H. (1972). IBM Journal of Research and Development 16(5).',
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
/** Rosenbrock (1960). The banana valley: easy to enter, slow to follow. */
|
|
388
|
+
export function rosenbrock(x) {
|
|
389
|
+
if (!isVec(x)) throw new TypeError('rosenbrock: expected an array of numbers');
|
|
390
|
+
const n = x.length;
|
|
391
|
+
if (n < 2) throw new RangeError(`rosenbrock: unsupported dimension ${n}`);
|
|
392
|
+
let s = 0, xi = x[0];
|
|
393
|
+
for (let i = 0; i < n - 1; i++) {
|
|
394
|
+
const next = x[i + 1];
|
|
395
|
+
const d1 = next - xi * xi;
|
|
396
|
+
const d2 = xi - 1;
|
|
397
|
+
s += 100 * (d1 * d1) + d2 * d2;
|
|
398
|
+
xi = next;
|
|
399
|
+
}
|
|
400
|
+
return s;
|
|
401
|
+
}
|
|
402
|
+
rosenbrock.meta = {
|
|
403
|
+
name: 'Rosenbrock', dims: '>= 2', sense: 'min', domain: cube(-5, 10, '>= 2', 'rosenbrock'),
|
|
404
|
+
optimum: { value: 0, at: fillAt('rosenbrock', '>= 2', 1) },
|
|
405
|
+
reference: 'Rosenbrock, H. H. (1960). The Computer Journal 3(3).',
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
/** Rastrigin (1974). A regular lattice of local minima on a quadratic bowl. */
|
|
409
|
+
export function rastrigin(x) {
|
|
410
|
+
assertVec(x, 'rastrigin');
|
|
411
|
+
const d = x.length;
|
|
412
|
+
let s1 = 0, s2 = 0;
|
|
413
|
+
for (let i = 0; i < d; i++) {
|
|
414
|
+
const xi = x[i];
|
|
415
|
+
s1 += xi * xi;
|
|
416
|
+
s2 += Math.cos(TAU * xi);
|
|
417
|
+
}
|
|
418
|
+
return s1 + 10 * (d - s2);
|
|
419
|
+
}
|
|
420
|
+
rastrigin.meta = {
|
|
421
|
+
name: 'Rastrigin', dims: 'any', sense: 'min', domain: cube(-5.12, 5.12, 'any', 'rastrigin'),
|
|
422
|
+
optimum: { value: 0, at: fillAt('rastrigin', 'any', 0) },
|
|
423
|
+
reference: 'Rastrigin, L. A. (1974). Systems of Extremal Control.',
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
function powEven(u, m) {
|
|
427
|
+
// u^(2m) by squaring, for positive integer m.
|
|
428
|
+
let p = u * u;
|
|
429
|
+
if (m === 10) {
|
|
430
|
+
const u4 = p * p, u8 = u4 * u4, u16 = u8 * u8;
|
|
431
|
+
return u16 * u4;
|
|
432
|
+
}
|
|
433
|
+
let r = 1, e = m;
|
|
434
|
+
while (e > 0) {
|
|
435
|
+
if (e & 1) r *= p;
|
|
436
|
+
e >>>= 1;
|
|
437
|
+
if (e > 0) p *= p;
|
|
438
|
+
}
|
|
439
|
+
return r;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** Michalewicz (1996). Steep ridges separated by flat plateaus. */
|
|
443
|
+
export function michalewicz(x, options) {
|
|
444
|
+
assertVec(x, 'michalewicz');
|
|
445
|
+
let m = 10;
|
|
446
|
+
if (options !== undefined) {
|
|
447
|
+
assertOpts(options, 'michalewicz');
|
|
448
|
+
if (options.m !== undefined) m = options.m;
|
|
449
|
+
if (!(Number.isInteger(m) && m >= 1)) {
|
|
450
|
+
throw new RangeError('michalewicz: m must be a positive integer');
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
const d = x.length;
|
|
454
|
+
let s = 0;
|
|
455
|
+
if (m === 10) {
|
|
456
|
+
for (let i = 0; i < d; i++) {
|
|
457
|
+
const xi = x[i];
|
|
458
|
+
const u = Math.sin((i + 1) * xi * xi / Math.PI);
|
|
459
|
+
const u2 = u * u, u4 = u2 * u2, u8 = u4 * u4, u16 = u8 * u8;
|
|
460
|
+
s += Math.sin(xi) * (u16 * u4);
|
|
461
|
+
}
|
|
462
|
+
} else {
|
|
463
|
+
for (let i = 0; i < d; i++) {
|
|
464
|
+
const xi = x[i];
|
|
465
|
+
s += Math.sin(xi) * powEven(Math.sin((i + 1) * xi * xi / Math.PI), m);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return 0 - s;
|
|
469
|
+
}
|
|
470
|
+
michalewicz.meta = {
|
|
471
|
+
name: 'Michalewicz', dims: 'any', sense: 'min', domain: cube(0, Math.PI, 'any', 'michalewicz'),
|
|
472
|
+
optimum: {
|
|
473
|
+
value: null,
|
|
474
|
+
// f is a sum of one-dimensional terms, so its d-D minimum is exactly the
|
|
475
|
+
// sum of the 1-D minima. Each was solved at 50 digits; see
|
|
476
|
+
// test/crosscheck_optima.py.
|
|
477
|
+
note: 'depends on d; exact for d = 1..10 at m = 10. Agrees with the published '
|
|
478
|
+
+ '-1.8013 (d=2), -4.687658 (d=5) and -9.66015 (d=10).',
|
|
479
|
+
known: {
|
|
480
|
+
1: -0.8013034100985525, 2: -1.8013034100985525, 3: -2.7603946799945587,
|
|
481
|
+
4: -3.698857098466642, 5: -4.687658179088146, 6: -5.687658179088146,
|
|
482
|
+
7: -6.680885314444028, 8: -7.663757350716239, 9: -8.66015171564134,
|
|
483
|
+
10: -9.66015171564134,
|
|
484
|
+
},
|
|
485
|
+
},
|
|
486
|
+
reference: 'Michalewicz, Z. (1996). Genetic Algorithms + Data Structures = Evolution Programs.',
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
/* ------------------------------------------------------------------ *
|
|
490
|
+
* classical psychophysical laws *
|
|
491
|
+
* *
|
|
492
|
+
* These are human response functions, not optimization benchmarks. *
|
|
493
|
+
* They are given in the sense the law states. Three of the four are *
|
|
494
|
+
* MONOTONE, so on any box their extremum is a corner — which makes *
|
|
495
|
+
* them weak as search problems but useful as a landscape class that *
|
|
496
|
+
* the classical benchmarks above do not contain. *
|
|
497
|
+
* ------------------------------------------------------------------ */
|
|
498
|
+
|
|
499
|
+
/** Yerkes-Dodson (1908). Inverted-U of arousal against performance. */
|
|
500
|
+
export function yerkesDodson(x, options) {
|
|
501
|
+
assertVec(x, 'yerkesDodson');
|
|
502
|
+
let peak = 0.5, width = 0.15;
|
|
503
|
+
if (options !== undefined) {
|
|
504
|
+
assertOpts(options, 'yerkesDodson');
|
|
505
|
+
if (options.peak !== undefined) peak = options.peak;
|
|
506
|
+
if (options.width !== undefined) width = options.width;
|
|
507
|
+
if (!Number.isFinite(peak)) throw new RangeError('yerkesDodson: peak must be finite');
|
|
508
|
+
if (!(Number.isFinite(width) && width > 0)) {
|
|
509
|
+
throw new RangeError('yerkesDodson: width must be positive');
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
const d = x.length, scale = -0.5 / (width * width);
|
|
513
|
+
let s = 0;
|
|
514
|
+
for (let i = 0; i < d; i++) {
|
|
515
|
+
const u = x[i] - peak;
|
|
516
|
+
s += Math.exp(u * u * scale);
|
|
517
|
+
}
|
|
518
|
+
return s / d;
|
|
519
|
+
}
|
|
520
|
+
yerkesDodson.meta = {
|
|
521
|
+
name: 'Yerkes-Dodson', dims: 'any', sense: 'max', domain: cube(0, 1, 'any', 'yerkesDodson'),
|
|
522
|
+
monotone: false,
|
|
523
|
+
optimum: { value: 1, at: fillAt('yerkesDodson', 'any', 0.5) },
|
|
524
|
+
note: 'The 1908 paper reports the effect, not a formula; the Gaussian inverted-U is a stylisation.',
|
|
525
|
+
reference: 'Yerkes, R. M., & Dodson, J. D. (1908). J. Comparative Neurology and Psychology 18.',
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
/** Stevens' power law (1957): perceived magnitude psi = I^a. Monotone. */
|
|
529
|
+
export function stevens(x, options) {
|
|
530
|
+
assertVec(x, 'stevens');
|
|
531
|
+
let a = 0.67;
|
|
532
|
+
if (options !== undefined) {
|
|
533
|
+
assertOpts(options, 'stevens');
|
|
534
|
+
if (options.a !== undefined) a = options.a;
|
|
535
|
+
if (!(Number.isFinite(a) && a > 0)) throw new RangeError('stevens: a must be positive');
|
|
536
|
+
}
|
|
537
|
+
const d = x.length;
|
|
538
|
+
let s = 0;
|
|
539
|
+
for (let i = 0; i < d; i++) {
|
|
540
|
+
const xi = x[i];
|
|
541
|
+
if (xi > 0) s += xi ** a;
|
|
542
|
+
else if (Number.isNaN(xi)) return NaN;
|
|
543
|
+
}
|
|
544
|
+
return s / d;
|
|
545
|
+
}
|
|
546
|
+
stevens.meta = {
|
|
547
|
+
name: "Stevens' power law", dims: 'any', sense: 'max', domain: cube(0, 1, 'any', 'stevens'),
|
|
548
|
+
monotone: 'increasing',
|
|
549
|
+
optimum: { value: 1, at: fillAt('stevens', 'any', 1) },
|
|
550
|
+
note: 'a = 0.67 is the exponent Stevens reported for loudness.',
|
|
551
|
+
reference: 'Stevens, S. S. (1957). Psychological Review 64(3).',
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
/** Hick-Hyman law (1952/1953): choice reaction time a + b*log2(n + 1). Monotone. */
|
|
555
|
+
export function hickHyman(x, options) {
|
|
556
|
+
assertVec(x, 'hickHyman');
|
|
557
|
+
let a = 0.2, b = 0.15;
|
|
558
|
+
if (options !== undefined) {
|
|
559
|
+
assertOpts(options, 'hickHyman');
|
|
560
|
+
if (options.a !== undefined) a = options.a;
|
|
561
|
+
if (options.b !== undefined) b = options.b;
|
|
562
|
+
if (!Number.isFinite(a)) throw new RangeError('hickHyman: a must be finite');
|
|
563
|
+
if (!(Number.isFinite(b) && b > 0)) throw new RangeError('hickHyman: b must be positive');
|
|
564
|
+
}
|
|
565
|
+
const d = x.length;
|
|
566
|
+
let s = 0;
|
|
567
|
+
for (let i = 0; i < d; i++) {
|
|
568
|
+
s += Math.log2(Math.max(x[i], 0) + 1);
|
|
569
|
+
}
|
|
570
|
+
return a + b * (s / d);
|
|
571
|
+
}
|
|
572
|
+
hickHyman.meta = {
|
|
573
|
+
name: 'Hick-Hyman law', dims: 'any', sense: 'min', domain: cube(0, 10, 'any', 'hickHyman'),
|
|
574
|
+
monotone: 'increasing',
|
|
575
|
+
optimum: { value: 0.2, at: fillAt('hickHyman', 'any', 0) },
|
|
576
|
+
note: 'Returns reaction time in seconds, so lower is better. a and b are in the usual reported range.',
|
|
577
|
+
reference: 'Hick, W. E. (1952). QJEP 4(1); Hyman, R. (1953). J. Experimental Psychology 45(3).',
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
/** Weber-Fechner (1860): sensation psi = ln(1 + I / I0). Monotone. */
|
|
581
|
+
export function weberFechner(x, options) {
|
|
582
|
+
assertVec(x, 'weberFechner');
|
|
583
|
+
let i0 = 0.01;
|
|
584
|
+
if (options !== undefined) {
|
|
585
|
+
assertOpts(options, 'weberFechner');
|
|
586
|
+
if (options.i0 !== undefined) i0 = options.i0;
|
|
587
|
+
if (!(Number.isFinite(i0) && i0 > 0)) throw new RangeError('weberFechner: i0 must be positive');
|
|
588
|
+
}
|
|
589
|
+
const d = x.length;
|
|
590
|
+
let s = 0;
|
|
591
|
+
for (let i = 0; i < d; i++) s += Math.log1p(Math.max(x[i], 0) / i0);
|
|
592
|
+
return s / d;
|
|
593
|
+
}
|
|
594
|
+
weberFechner.meta = {
|
|
595
|
+
name: 'Weber-Fechner', dims: 'any', sense: 'max', domain: cube(0, 1, 'any', 'weberFechner'),
|
|
596
|
+
monotone: 'increasing',
|
|
597
|
+
optimum: { value: Math.log1p(1 / 0.01), at: fillAt('weberFechner', 'any', 1) },
|
|
598
|
+
note: 'ln(1 + I/I0) rather than ln(I/I0), so the value stays finite at zero intensity.',
|
|
599
|
+
reference: 'Fechner, G. T. (1860). Elemente der Psychophysik.',
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
/* ------------------------------------------------------------------ *
|
|
603
|
+
* registry *
|
|
604
|
+
* ------------------------------------------------------------------ */
|
|
605
|
+
|
|
606
|
+
export const functions = {
|
|
607
|
+
ackley, griewank, schwefel, eggholder, powell, shekel,
|
|
608
|
+
hartmann3, hartmann6, branin, rosenbrock, rastrigin, michalewicz,
|
|
609
|
+
yerkesDodson, stevens, hickHyman, weberFechner,
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
/** Every benchmark that is a classical optimization test function. */
|
|
613
|
+
export const optimizationBenchmarks = [
|
|
614
|
+
'ackley', 'griewank', 'schwefel', 'eggholder', 'powell', 'shekel',
|
|
615
|
+
'hartmann3', 'hartmann6', 'branin', 'rosenbrock', 'rastrigin', 'michalewicz',
|
|
616
|
+
];
|
|
617
|
+
|
|
618
|
+
/** Every benchmark that is a classical human response function. */
|
|
619
|
+
export const psychophysicalLaws = [
|
|
620
|
+
'yerkesDodson', 'stevens', 'hickHyman', 'weberFechner',
|
|
621
|
+
];
|
|
622
|
+
|
|
623
|
+
const CUBE_BOUNDS = {
|
|
624
|
+
ackley: [-32.768, 32.768], griewank: [-600, 600], schwefel: [-500, 500],
|
|
625
|
+
eggholder: [-512, 512], powell: [-4, 5], shekel: [0, 10],
|
|
626
|
+
hartmann3: [0, 1], hartmann6: [0, 1], rosenbrock: [-5, 10],
|
|
627
|
+
rastrigin: [-5.12, 5.12], michalewicz: [0, Math.PI],
|
|
628
|
+
yerkesDodson: [0, 1], stevens: [0, 1], hickHyman: [0, 10], weberFechner: [0, 1],
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
/** The per-axis domain of `name` at dimension `d`, as [[lo, hi], ...]. */
|
|
632
|
+
export function domain(name, d) {
|
|
633
|
+
if (!Object.hasOwn(functions, name)) throw new RangeError(`unknown function: ${name}`);
|
|
634
|
+
const f = functions[name];
|
|
635
|
+
return f.meta.domain(checkDimFor(name, f.meta.dims, d));
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/** Uniformly map a point from the unit cube onto `name`'s own domain. */
|
|
639
|
+
export function fromUnitCube(name, u) {
|
|
640
|
+
if (!Object.hasOwn(functions, name)) throw new RangeError(`unknown function: ${name}`);
|
|
641
|
+
if (!isVec(u)) throw new TypeError('fromUnitCube: expected an array of numbers');
|
|
642
|
+
const f = functions[name];
|
|
643
|
+
const d = checkDimFor(name, f.meta.dims, u.length);
|
|
644
|
+
// Plain Array, not u.map: a Float32Array input would otherwise come back as
|
|
645
|
+
// a Float32Array and round the mapped point to single precision.
|
|
646
|
+
if (name === 'branin') return [-5 + u[0] * 15, u[1] * 15];
|
|
647
|
+
const bounds = Object.hasOwn(CUBE_BOUNDS, name) ? CUBE_BOUNDS[name] : null;
|
|
648
|
+
if (bounds) {
|
|
649
|
+
const lo = bounds[0], span = bounds[1] - lo;
|
|
650
|
+
const out = new Array(d);
|
|
651
|
+
for (let i = 0; i < d; i++) out[i] = lo + u[i] * span;
|
|
652
|
+
return out;
|
|
653
|
+
}
|
|
654
|
+
const box = f.meta.domain(d);
|
|
655
|
+
const out = new Array(d);
|
|
656
|
+
for (let i = 0; i < d; i++) out[i] = box[i][0] + u[i] * (box[i][1] - box[i][0]);
|
|
657
|
+
return out;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
export default functions;
|