optimization-test-functions 1.0.1 → 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/README.md CHANGED
@@ -170,8 +170,11 @@ Three independent checks, all run in CI:
170
170
 
171
171
  | function | max relative difference |
172
172
  |---|---|
173
- | `ackley`, `eggholder`, `powell`, `rosenbrock`, `rastrigin` | `0` exactly |
173
+ | `eggholder`, `rosenbrock` | `0` exactly |
174
+ | `rastrigin` | `1.9e-16` |
175
+ | `powell` | `2.3e-16` |
174
176
  | `griewank` | `3.5e-16` |
177
+ | `ackley` | `4.4e-16` |
175
178
  | `branin` | `5.5e-16` |
176
179
  | `michalewicz` | `2.2e-15` |
177
180
  | `shekel` | `5.8e-09` |
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "optimization-test-functions",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Canonical global-optimization benchmarks and classical psychophysical laws, with per-axis domains and known optima. Zero dependencies.",
5
5
  "keywords": [
6
6
  "optimization", "benchmark", "test-functions", "global-optimization",
7
7
  "ackley", "rastrigin", "rosenbrock", "griewank", "hartmann", "branin",
8
8
  "schwefel", "shekel", "michalewicz", "eggholder", "powell",
9
- "bayesian-optimization", "psychophysics", "hci", "fitts", "hick", "stevens"
9
+ "bayesian-optimization", "psychophysics", "hci", "hick", "stevens"
10
10
  ],
11
11
  "type": "module",
12
12
  "main": "./src/index.js",
package/src/index.d.ts CHANGED
@@ -1,4 +1,9 @@
1
- export type Vec = readonly number[] | Float64Array | Float32Array;
1
+ export type TypedNumberArray =
2
+ | Float64Array | Float32Array
3
+ | Int32Array | Int16Array | Int8Array
4
+ | Uint32Array | Uint16Array | Uint8Array | Uint8ClampedArray;
5
+
6
+ export type Vec = readonly number[] | TypedNumberArray;
2
7
 
3
8
  /** One [lo, hi] pair per axis. */
4
9
  export type Box = [number, number][];
@@ -6,8 +11,8 @@ export type Box = [number, number][];
6
11
  export interface Optimum {
7
12
  /** The optimal value, or null when it depends on the dimension. */
8
13
  value: number | null;
9
- /** The location, given the dimension. Absent when `value` is null. */
10
- at?: (d: number) => number[];
14
+ /** The location, given the dimension (optional on fixed-dimension functions). Absent when `value` is null. */
15
+ at?: (d?: number) => number[];
11
16
  /** A commonly quoted but only approximate location, where one exists. */
12
17
  approx?: number[];
13
18
  /** Every global optimum, for functions that have more than one. */
@@ -23,8 +28,8 @@ export interface Meta {
23
28
  dims: number | string;
24
29
  /** Which direction is interesting. Nothing here is silently negated. */
25
30
  sense: 'min' | 'max';
26
- /** The function's own domain, per axis, at dimension d. */
27
- domain: (d: number) => Box;
31
+ /** The function's own domain, per axis, at dimension d (optional on fixed-dimension functions). */
32
+ domain: (d?: number) => Box;
28
33
  optimum: Optimum;
29
34
  /** Present on the psychophysical laws; false or a direction. */
30
35
  monotone?: false | 'increasing' | 'decreasing';
package/src/index.js CHANGED
@@ -1,438 +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 assertLength(x, name, ok) {
25
- if (!Array.isArray(x) && !ArrayBuffer.isView(x)) {
26
- throw new TypeError(`${name}: expected an array of numbers`);
27
- }
28
- if (!ok(x.length)) {
29
- throw new RangeError(`${name}: unsupported dimension ${x.length}`);
30
- }
31
- }
32
-
33
- const mean = (x) => {
34
- let s = 0;
35
- for (let i = 0; i < x.length; i++) s += x[i];
36
- return s / x.length;
37
- };
38
-
39
- /**
40
- * Wrap a function so a minimizer can be used as a maximizer, or vice versa.
41
- * The metadata follows: the sense, the optimum value (and any per-dimension
42
- * table of them) and the direction of monotonicity all flip with the sign.
43
- */
44
- export function negate(f) {
45
- const g = (...args) => -f(...args);
46
- if (f.meta) {
47
- const { sense, monotone, optimum } = f.meta;
48
- g.meta = {
49
- ...f.meta,
50
- sense: sense === 'min' ? 'max' : 'min',
51
- optimum: { ...optimum, value: optimum.value === null ? null : -optimum.value },
52
- };
53
- if (optimum.known) {
54
- g.meta.optimum.known = Object.fromEntries(
55
- Object.entries(optimum.known).map(([d, v]) => [d, -v]));
56
- }
57
- if (monotone) g.meta.monotone = monotone === 'increasing' ? 'decreasing' : 'increasing';
58
- }
59
- return g;
60
- }
61
-
62
- /** Expand a per-axis domain to `d` dimensions, for functions defined on a cube. */
63
- function cube(lo, hi) {
64
- return (d) => Array.from({ length: d }, () => [lo, hi]);
65
- }
66
-
67
- /* ------------------------------------------------------------------ *
68
- * classical optimization benchmarks (minimized) *
69
- * ------------------------------------------------------------------ */
70
-
71
- /** Ackley (1987). Flat outer plain, narrow basin at the origin. */
72
- export function ackley(x, { a = 20, b = 0.2, c = TAU } = {}) {
73
- assertLength(x, 'ackley', (n) => n >= 1);
74
- const d = x.length;
75
- let s1 = 0, s2 = 0;
76
- for (let i = 0; i < d; i++) { s1 += x[i] * x[i]; s2 += Math.cos(c * x[i]); }
77
- return -a * Math.exp(-b * Math.sqrt(s1 / d)) - Math.exp(s2 / d) + a + Math.E;
78
- }
79
- ackley.meta = {
80
- name: 'Ackley', dims: 'any', sense: 'min', domain: cube(-32.768, 32.768),
81
- optimum: { value: 0, at: (d) => new Array(d).fill(0) },
82
- reference: 'Ackley, D. H. (1987). A Connectionist Machine for Genetic Hillclimbing.',
83
- };
84
-
85
- /** Griewank (1981). Wide bowl with fine cosine ripples. */
86
- export function griewank(x) {
87
- assertLength(x, 'griewank', (n) => n >= 1);
88
- let s = 0, p = 1;
89
- for (let i = 0; i < x.length; i++) {
90
- s += x[i] * x[i];
91
- p *= Math.cos(x[i] / Math.sqrt(i + 1));
92
- }
93
- return s / 4000 - p + 1;
94
- }
95
- griewank.meta = {
96
- name: 'Griewank', dims: 'any', sense: 'min', domain: cube(-600, 600),
97
- optimum: { value: 0, at: (d) => new Array(d).fill(0) },
98
- reference: 'Griewank, A. O. (1981). Journal of Optimization Theory and Applications 34(1).',
99
- };
100
-
101
- /** Schwefel (1981). Global optimum far from the deepest local one. */
102
- export function schwefel(x) {
103
- assertLength(x, 'schwefel', (n) => n >= 1);
104
- let s = 0;
105
- for (let i = 0; i < x.length; i++) s += x[i] * Math.sin(Math.sqrt(Math.abs(x[i])));
106
- // max of t sin(sqrt t): t = u^2 where tan(u) = -u/2, solved at 50 digits
107
- return 418.9828872724337 * x.length - s;
108
- }
109
- schwefel.meta = {
110
- name: 'Schwefel', dims: 'any', sense: 'min', domain: cube(-500, 500),
111
- optimum: { value: 0, at: (d) => new Array(d).fill(420.96874635998205) },
112
- reference: 'Schwefel, H.-P. (1981). Numerical Optimization of Computer Models.',
113
- };
114
-
115
- /** Eggholder. Two dimensions, wildly multimodal, no usable global structure. */
116
- export function eggholder(x) {
117
- assertLength(x, 'eggholder', (n) => n === 2);
118
- const [a, b] = x;
119
- return -(b + 47) * Math.sin(Math.sqrt(Math.abs(a / 2 + (b + 47))))
120
- - a * Math.sin(Math.sqrt(Math.abs(a - (b + 47))));
121
- }
122
- eggholder.meta = {
123
- name: 'Eggholder', dims: 2, sense: 'min', domain: () => [[-512, 512], [-512, 512]],
124
- optimum: { value: -959.6406627208509, at: () => [512, 404.2318051137578] },
125
- reference: 'Vanaret et al. (2014); after Whitley et al. (1996).',
126
- };
127
-
128
- /** Powell (1962). Smooth quartic valley. Dimension must be a multiple of 4. */
129
- export function powell(x) {
130
- assertLength(x, 'powell', (n) => n > 0 && n % 4 === 0);
131
- let s = 0;
132
- for (let i = 0; i < x.length; i += 4) {
133
- s += (x[i] + 10 * x[i + 1]) ** 2
134
- + 5 * (x[i + 2] - x[i + 3]) ** 2
135
- + (x[i + 1] - 2 * x[i + 2]) ** 4
136
- + 10 * (x[i] - x[i + 3]) ** 4;
137
- }
138
- return s;
139
- }
140
- powell.meta = {
141
- name: 'Powell', dims: 'multiple of 4', sense: 'min', domain: cube(-4, 5),
142
- optimum: { value: 0, at: (d) => new Array(d).fill(0) },
143
- reference: 'Powell, M. J. D. (1962). The Computer Journal 5(2).',
144
- };
145
-
146
- const SHEKEL_A = [
147
- [4, 4, 4, 4], [1, 1, 1, 1], [8, 8, 8, 8], [6, 6, 6, 6], [3, 7, 3, 7],
148
- [2, 9, 2, 9], [5, 3, 5, 3], [8, 1, 8, 1], [6, 2, 6, 2], [7, 3.6, 7, 3.6],
149
- ];
150
- const SHEKEL_C = [0.1, 0.2, 0.2, 0.4, 0.4, 0.6, 0.3, 0.7, 0.5, 0.5];
151
-
152
- /** Shekel. Four dimensions, `m` narrow wells on a flat plain. */
153
- export function shekel(x, { m = 10 } = {}) {
154
- assertLength(x, 'shekel', (n) => n === 4);
155
- if (!(Number.isInteger(m) && m >= 1 && m <= 10)) {
156
- throw new RangeError('shekel: m must be an integer in 1..10');
157
- }
158
- let s = 0;
159
- for (let i = 0; i < m; i++) {
160
- let q = 0;
161
- for (let j = 0; j < 4; j++) { const d = x[j] - SHEKEL_A[i][j]; q += d * d; }
162
- s += 1 / (q + SHEKEL_C[i]);
163
- }
164
- return -s;
165
- }
166
- shekel.meta = {
167
- name: 'Shekel', dims: 4, sense: 'min', domain: cube(0, 10),
168
- optimum: {
169
- value: -10.536443153483528,
170
- // Almost always quoted as (4, 4, 4, 4), which is only approximate: the
171
- // neighbouring wells pull the minimum slightly off that corner, and
172
- // f(4,4,4,4) = -10.536284, not -10.536443. Refined location below; it
173
- // has x1 = x3 and x2 = x4, as the symmetry of the wells requires.
174
- at: () => [4.000746868270634, 3.9995094800857736, 4.000746868270634, 3.9995094800857736],
175
- approx: [4, 4, 4, 4],
176
- note: 'for m = 10',
177
- },
178
- reference: 'Shekel, J. (1971). Test functions for multimodal search techniques. '
179
- + 'Fifth Annual Princeton Conf. on Information Science and Systems.',
180
- };
181
-
182
- const H_ALPHA = [1.0, 1.2, 3.0, 3.2];
183
- const H3_A = [[3.0, 10, 30], [0.1, 10, 35], [3.0, 10, 30], [0.1, 10, 35]];
184
- // v / 1e4 rather than v * 1e-4: 1e-4 is inexact, and the product is 1 ulp off
185
- // the decimal constant for a third of the entries; the quotient never is.
186
- const H3_P = [[3689, 1170, 2673], [4699, 4387, 7470],
187
- [1091, 8732, 5547], [381, 5743, 8828]].map((r) => r.map((v) => v / 1e4));
188
-
189
- /** Hartmann 3-D. Four Gaussian basins of unequal depth. */
190
- export function hartmann3(x) {
191
- assertLength(x, 'hartmann3', (n) => n === 3);
192
- let s = 0;
193
- for (let i = 0; i < 4; i++) {
194
- let inner = 0;
195
- for (let j = 0; j < 3; j++) { const d = x[j] - H3_P[i][j]; inner += H3_A[i][j] * d * d; }
196
- s += H_ALPHA[i] * Math.exp(-inner);
197
- }
198
- return -s;
199
- }
200
- hartmann3.meta = {
201
- name: 'Hartmann-3', dims: 3, sense: 'min', domain: cube(0, 1),
202
- optimum: {
203
- value: -3.8627797873326624,
204
- // The widely copied -3.86278214782076 is 2.4e-6 BELOW the true minimum,
205
- // so no point attains it, and the usual location (kept as `approx`) is
206
- // 2.5e-5 off in x1. Both re-derived at 50 digits.
207
- at: () => [0.11458887665506896, 0.55564889461693, 0.8525469846866774],
208
- approx: [0.114614, 0.555649, 0.852547],
209
- },
210
- reference: 'Hartman, J. K. (1973). Naval Research Logistics Quarterly 20(3).',
211
- };
212
-
213
- const H6_A = [[10, 3, 17, 3.5, 1.7, 8], [0.05, 10, 17, 0.1, 8, 14],
214
- [3, 3.5, 1.7, 10, 17, 8], [17, 8, 0.05, 10, 0.1, 14]];
215
- const H6_P = [[1312, 1696, 5569, 124, 8283, 5886], [2329, 4135, 8307, 3736, 1004, 9991],
216
- [2348, 1451, 3522, 2883, 3047, 6650], [4047, 8828, 8732, 5743, 1091, 381]]
217
- .map((r) => r.map((v) => v / 1e4));
218
-
219
- /** Hartmann 6-D. The usual stress test for a GP with one lengthscale per axis. */
220
- export function hartmann6(x) {
221
- assertLength(x, 'hartmann6', (n) => n === 6);
222
- let s = 0;
223
- for (let i = 0; i < 4; i++) {
224
- let inner = 0;
225
- for (let j = 0; j < 6; j++) { const d = x[j] - H6_P[i][j]; inner += H6_A[i][j] * d * d; }
226
- s += H_ALPHA[i] * Math.exp(-inner);
227
- }
228
- return -s;
229
- }
230
- hartmann6.meta = {
231
- name: 'Hartmann-6', dims: 6, sense: 'min', domain: cube(0, 1),
232
- optimum: {
233
- // The usual value -3.322368011391339 is f at the usual six-digit
234
- // location (kept as `approx`), not the minimum, which is 2.4e-11 lower.
235
- value: -3.3223680114155147,
236
- at: () => [0.20168951100670543, 0.15001069182345797, 0.476873974221897,
237
- 0.2753324304940561, 0.31165161660011326, 0.6573005340656203],
238
- approx: [0.20169, 0.150011, 0.476874, 0.275332, 0.311652, 0.6573],
239
- },
240
- reference: 'Hartman, J. K. (1973). Naval Research Logistics Quarterly 20(3).',
241
- };
242
-
243
- /**
244
- * Branin-Hoo. Three equal global minima in a shallow curved trough.
245
- * Note the ASYMMETRIC domain: x1 in [-5, 10], x2 in [0, 15]. Applying one
246
- * range to both axes is a common and silent mistake.
247
- */
248
- export function branin(x, { a = 1, b = 5.1 / (4 * Math.PI ** 2), c = 5 / Math.PI,
249
- r = 6, s = 10, t = 1 / (8 * Math.PI) } = {}) {
250
- assertLength(x, 'branin', (n) => n === 2);
251
- const [x1, x2] = x;
252
- return a * (x2 - b * x1 * x1 + c * x1 - r) ** 2 + s * (1 - t) * Math.cos(x1) + s;
253
- }
254
- branin.meta = {
255
- name: 'Branin', dims: 2, sense: 'min', domain: () => [[-5, 10], [0, 15]],
256
- optimum: {
257
- value: 5 / (4 * Math.PI),
258
- at: () => [-Math.PI, 12.275],
259
- all: [[-Math.PI, 12.275], [Math.PI, 2.275], [3 * Math.PI, 2.475]],
260
- },
261
- reference: 'Branin, F. H. (1972). IBM Journal of Research and Development 16(5).',
262
- };
263
-
264
- /** Rosenbrock (1960). The banana valley: easy to enter, slow to follow. */
265
- export function rosenbrock(x) {
266
- assertLength(x, 'rosenbrock', (n) => n >= 2);
267
- let s = 0;
268
- for (let i = 0; i < x.length - 1; i++) {
269
- s += 100 * (x[i + 1] - x[i] * x[i]) ** 2 + (x[i] - 1) ** 2;
270
- }
271
- return s;
272
- }
273
- rosenbrock.meta = {
274
- name: 'Rosenbrock', dims: '>= 2', sense: 'min', domain: cube(-5, 10),
275
- optimum: { value: 0, at: (d) => new Array(d).fill(1) },
276
- reference: 'Rosenbrock, H. H. (1960). The Computer Journal 3(3).',
277
- };
278
-
279
- /** Rastrigin (1974). A regular lattice of local minima on a quadratic bowl. */
280
- export function rastrigin(x) {
281
- assertLength(x, 'rastrigin', (n) => n >= 1);
282
- let s = 0;
283
- for (let i = 0; i < x.length; i++) s += x[i] * x[i] - 10 * Math.cos(TAU * x[i]);
284
- return 10 * x.length + s;
285
- }
286
- rastrigin.meta = {
287
- name: 'Rastrigin', dims: 'any', sense: 'min', domain: cube(-5.12, 5.12),
288
- optimum: { value: 0, at: (d) => new Array(d).fill(0) },
289
- reference: 'Rastrigin, L. A. (1974). Systems of Extremal Control.',
290
- };
291
-
292
- /** Michalewicz (1996). Steep ridges separated by flat plateaus. */
293
- export function michalewicz(x, { m = 10 } = {}) {
294
- assertLength(x, 'michalewicz', (n) => n >= 1);
295
- let s = 0;
296
- for (let i = 0; i < x.length; i++) {
297
- s += Math.sin(x[i]) * Math.sin((i + 1) * x[i] * x[i] / Math.PI) ** (2 * m);
298
- }
299
- return -s;
300
- }
301
- michalewicz.meta = {
302
- name: 'Michalewicz', dims: 'any', sense: 'min', domain: cube(0, Math.PI),
303
- optimum: {
304
- value: null,
305
- // f is a sum of one-dimensional terms, so its d-D minimum is exactly the
306
- // sum of the 1-D minima. Each was solved at 50 digits; see
307
- // test/crosscheck_optima.py.
308
- note: 'depends on d; exact for d = 1..10 at m = 10. Agrees with the published '
309
- + '-1.8013 (d=2), -4.687658 (d=5) and -9.66015 (d=10).',
310
- known: {
311
- 1: -0.8013034100985525, 2: -1.8013034100985525, 3: -2.7603946799945587,
312
- 4: -3.698857098466642, 5: -4.687658179088146, 6: -5.687658179088146,
313
- 7: -6.680885314444028, 8: -7.663757350716239, 9: -8.66015171564134,
314
- 10: -9.66015171564134,
315
- },
316
- },
317
- reference: 'Michalewicz, Z. (1996). Genetic Algorithms + Data Structures = Evolution Programs.',
318
- };
319
-
320
- /* ------------------------------------------------------------------ *
321
- * classical psychophysical laws *
322
- * *
323
- * These are human response functions, not optimization benchmarks. *
324
- * They are given in the sense the law states. Three of the four are *
325
- * MONOTONE, so on any box their extremum is a corner — which makes *
326
- * them weak as search problems but useful as a landscape class that *
327
- * the classical benchmarks above do not contain. *
328
- * ------------------------------------------------------------------ */
329
-
330
- /** Yerkes-Dodson (1908). Inverted-U of arousal against performance. */
331
- export function yerkesDodson(x, { peak = 0.5, width = 0.15 } = {}) {
332
- assertLength(x, 'yerkesDodson', (n) => n >= 1);
333
- let s = 0;
334
- for (let i = 0; i < x.length; i++) {
335
- s += Math.exp(-((x[i] - peak) ** 2) / (2 * width * width));
336
- }
337
- return s / x.length;
338
- }
339
- yerkesDodson.meta = {
340
- name: 'Yerkes-Dodson', dims: 'any', sense: 'max', domain: cube(0, 1), monotone: false,
341
- optimum: { value: 1, at: (d) => new Array(d).fill(0.5) },
342
- note: 'The 1908 paper reports the effect, not a formula; the Gaussian inverted-U is a stylisation.',
343
- reference: 'Yerkes, R. M., & Dodson, J. D. (1908). J. Comparative Neurology and Psychology 18.',
344
- };
345
-
346
- /** Stevens' power law (1957): perceived magnitude psi = I^a. Monotone. */
347
- export function stevens(x, { a = 0.67 } = {}) {
348
- assertLength(x, 'stevens', (n) => n >= 1);
349
- let s = 0;
350
- for (let i = 0; i < x.length; i++) s += Math.max(x[i], 0) ** a;
351
- return s / x.length;
352
- }
353
- stevens.meta = {
354
- name: "Stevens' power law", dims: 'any', sense: 'max', domain: cube(0, 1),
355
- monotone: 'increasing',
356
- optimum: { value: 1, at: (d) => new Array(d).fill(1) },
357
- note: 'a = 0.67 is the exponent Stevens reported for loudness.',
358
- reference: 'Stevens, S. S. (1957). Psychological Review 64(3).',
359
- };
360
-
361
- /** Hick-Hyman law (1952/1953): choice reaction time a + b*log2(n + 1). Monotone. */
362
- export function hickHyman(x, { a = 0.2, b = 0.15 } = {}) {
363
- assertLength(x, 'hickHyman', (n) => n >= 1);
364
- let s = 0;
365
- for (let i = 0; i < x.length; i++) {
366
- s += a + b * Math.log2(Math.max(x[i], 0) + 1);
367
- }
368
- return s / x.length;
369
- }
370
- hickHyman.meta = {
371
- name: "Hick-Hyman law", dims: 'any', sense: 'min', domain: cube(0, 10),
372
- monotone: 'increasing',
373
- optimum: { value: 0.2, at: (d) => new Array(d).fill(0) },
374
- note: 'Returns reaction time in seconds, so lower is better. a and b are in the usual reported range.',
375
- reference: 'Hick, W. E. (1952). QJEP 4(1); Hyman, R. (1953). J. Experimental Psychology 45(3).',
376
- };
377
-
378
- /** Weber-Fechner (1860): sensation psi = ln(1 + I / I0). Monotone. */
379
- export function weberFechner(x, { i0 = 0.01 } = {}) {
380
- assertLength(x, 'weberFechner', (n) => n >= 1);
381
- let s = 0;
382
- for (let i = 0; i < x.length; i++) s += Math.log1p(Math.max(x[i], 0) / i0);
383
- return s / x.length;
384
- }
385
- weberFechner.meta = {
386
- name: 'Weber-Fechner', dims: 'any', sense: 'max', domain: cube(0, 1),
387
- monotone: 'increasing',
388
- optimum: { value: Math.log1p(1 / 0.01), at: (d) => new Array(d).fill(1) },
389
- note: 'ln(1 + I/I0) rather than ln(I/I0), so the value stays finite at zero intensity.',
390
- reference: 'Fechner, G. T. (1860). Elemente der Psychophysik.',
391
- };
392
-
393
- /* ------------------------------------------------------------------ *
394
- * registry *
395
- * ------------------------------------------------------------------ */
396
-
397
- export const functions = {
398
- ackley, griewank, schwefel, eggholder, powell, shekel,
399
- hartmann3, hartmann6, branin, rosenbrock, rastrigin, michalewicz,
400
- yerkesDodson, stevens, hickHyman, weberFechner,
401
- };
402
-
403
- /** Every benchmark that is a classical optimization test function. */
404
- export const optimizationBenchmarks = [
405
- 'ackley', 'griewank', 'schwefel', 'eggholder', 'powell', 'shekel',
406
- 'hartmann3', 'hartmann6', 'branin', 'rosenbrock', 'rastrigin', 'michalewicz',
407
- ];
408
-
409
- /** Every benchmark that is a classical human response function. */
410
- export const psychophysicalLaws = [
411
- 'yerkesDodson', 'stevens', 'hickHyman', 'weberFechner',
412
- ];
413
-
414
- /** The per-axis domain of `name` at dimension `d`, as [[lo, hi], ...]. */
415
- export function domain(name, d) {
416
- const f = functions[name];
417
- if (!f) throw new RangeError(`unknown function: ${name}`);
418
- const dims = f.meta.dims;
419
- if (typeof dims === 'number') {
420
- if (d !== undefined && d !== dims) throw new RangeError(`${name} is ${dims}-D, not ${d}-D`);
421
- d = dims;
422
- }
423
- if (d === undefined) throw new RangeError(`${name}: dimension is required`);
424
- if (!(Number.isInteger(d) && d >= 1)) {
425
- throw new RangeError(`${name}: dimension must be a positive integer, got ${d}`);
426
- }
427
- return f.meta.domain(d);
428
- }
429
-
430
- /** Uniformly map a point from the unit cube onto `name`'s own domain. */
431
- export function fromUnitCube(name, u) {
432
- const box = domain(name, u.length);
433
- // Array.from, not u.map: a Float32Array input would otherwise come back as
434
- // a Float32Array and round the mapped point to single precision.
435
- return Array.from(u, (v, i) => box[i][0] + v * (box[i][1] - box[i][0]));
436
- }
437
-
438
- export default functions;
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;