optimization-test-functions 1.0.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mark Colley
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,172 @@
1
+ # optimization-test-functions
2
+
3
+ Standard global-optimization benchmarks and classical psychophysical laws, in
4
+ JavaScript. No dependencies, no build step, ESM.
5
+
6
+ Every function ships with the metadata you need to use it correctly: the domain
7
+ it is actually defined on, its known optimum, and which direction is the
8
+ interesting one.
9
+
10
+ ```bash
11
+ npm install optimization-test-functions
12
+ ```
13
+
14
+ ```js
15
+ import { ackley, branin, domain, negate } from 'optimization-test-functions';
16
+
17
+ ackley([0, 0, 0]); // 0 — the global minimum
18
+ branin([-Math.PI, 12.275]); // 0.397887 — one of three equal minima
19
+
20
+ domain('branin'); // [[-5, 10], [0, 15]] — note the asymmetry
21
+ domain('ackley', 4); // four copies of [-32.768, 32.768]
22
+
23
+ const maximise = negate(ackley);
24
+ maximise.meta.sense; // 'max'
25
+ ```
26
+
27
+ ## Why this exists
28
+
29
+ There was no JavaScript implementation of these. Python has BoTorch, DEAP and
30
+ several standalone packages; the npm registry had nothing, so anyone writing an
31
+ optimizer for the browser was retyping Hartmann's constant tables by hand.
32
+
33
+ Two things this gets right that hand-ported versions usually do not:
34
+
35
+ **Domains are per-axis.** Branin is defined on `x₁ ∈ [-5, 10], x₂ ∈ [0, 15]`.
36
+ Applying one range to both axes is a common and completely silent mistake — the
37
+ function still evaluates, it just is not Branin any more.
38
+
39
+ **Nothing is silently negated.** The optimization benchmarks are minimized, as
40
+ their sources define them. The psychophysical laws are given in the sense the
41
+ law states — Hick–Hyman returns a reaction time, so lower is better, and
42
+ `meta.sense` says so. If your optimizer maximizes, wrap with `negate`.
43
+
44
+ ## What's included
45
+
46
+ ### Classical optimization benchmarks — minimized
47
+
48
+ | function | dims | domain | minimum |
49
+ |---|---|---|---|
50
+ | `ackley` | any | `[-32.768, 32.768]ᵈ` | `0` at the origin |
51
+ | `griewank` | any | `[-600, 600]ᵈ` | `0` at the origin |
52
+ | `schwefel` | any | `[-500, 500]ᵈ` | `0` at `(420.9687, …)` |
53
+ | `eggholder` | 2 | `[-512, 512]²` | `-959.6407` |
54
+ | `powell` | multiple of 4 | `[-4, 5]ᵈ` | `0` at the origin |
55
+ | `shekel` | 4 | `[0, 10]⁴` | `-10.5364` |
56
+ | `hartmann3` | 3 | `[0, 1]³` | `-3.86278` |
57
+ | `hartmann6` | 6 | `[0, 1]⁶` | `-3.32237` |
58
+ | `branin` | 2 | `[-5, 10] × [0, 15]` | `0.397887`, three times |
59
+ | `rosenbrock` | ≥ 2 | `[-5, 10]ᵈ` | `0` at all-ones |
60
+ | `rastrigin` | any | `[-5.12, 5.12]ᵈ` | `0` at the origin |
61
+ | `michalewicz` | any | `[0, π]ᵈ` | d-dependent |
62
+
63
+ ### Classical psychophysical laws
64
+
65
+ Human response functions rather than optimization benchmarks. Three of the four
66
+ are monotone, so on any box their extremum sits on the boundary — weak as search
67
+ problems, but a landscape class the benchmarks above do not contain, and one
68
+ some acquisition functions handle badly.
69
+
70
+ | function | law | sense |
71
+ |---|---|---|
72
+ | `yerkesDodson` | inverted-U of arousal against performance (1908) | max, interior |
73
+ | `stevens` | ψ = I^0.67, Stevens' power law (1957) | max, boundary |
74
+ | `hickHyman` | RT = a + b·log₂(n+1), Hick (1952) / Hyman (1953) | min, boundary |
75
+ | `weberFechner` | ψ = ln(1 + I/I₀), Fechner (1860) | max, boundary |
76
+
77
+ ## Metadata
78
+
79
+ ```js
80
+ import { shekel } from 'optimization-test-functions';
81
+
82
+ shekel.meta;
83
+ // {
84
+ // name: 'Shekel', dims: 4, sense: 'min',
85
+ // domain: d => [[0, 10], [0, 10], [0, 10], [0, 10]],
86
+ // optimum: {
87
+ // value: -10.53644315348353,
88
+ // at: () => [4.0007468671, 3.9995094806, 4.00074687, 3.9995094776],
89
+ // approx: [4, 4, 4, 4],
90
+ // note: 'for m = 10',
91
+ // },
92
+ // reference: 'Shekel, J. (1971). …',
93
+ // }
94
+ ```
95
+
96
+ Shekel is a good example of why the metadata is worth having. Its minimum is
97
+ almost always quoted at `(4, 4, 4, 4)`, but that is only approximate — the
98
+ neighbouring wells pull it slightly off, and `f(4,4,4,4) = -10.536283`, not
99
+ `-10.536443`. Both the quoted and the refined location are recorded.
100
+
101
+ ## Optional parameters
102
+
103
+ ```js
104
+ shekel(x, { m: 5 }); // fewer wells
105
+ michalewicz(x, { m: 1 }); // gentler ridges (default 10)
106
+ ackley(x, { a: 20, b: 0.2, c: 2 * Math.PI });
107
+ branin(x, { a: 1, b: 5.1 / (4 * Math.PI ** 2), /* … */ });
108
+ yerkesDodson(x, { peak: 0.5, width: 0.15 });
109
+ hickHyman(x, { a: 0.2, b: 0.15 });
110
+ ```
111
+
112
+ ## Helpers
113
+
114
+ ```js
115
+ import { functions, optimizationBenchmarks, psychophysicalLaws,
116
+ domain, fromUnitCube, negate } from 'optimization-test-functions';
117
+
118
+ functions.ackley([1, 2]); // registry, keyed by name
119
+ optimizationBenchmarks; // the 12 classical names
120
+ psychophysicalLaws; // the 4 human response names
121
+ fromUnitCube('branin', [0.5, 0.5]); // [2.5, 7.5]
122
+ ```
123
+
124
+ `fromUnitCube` matters more than it looks: most optimizers work on the unit
125
+ cube, and mapping back with a single shared range is exactly how Branin gets
126
+ evaluated on the wrong box.
127
+
128
+ ## Correctness
129
+
130
+ Two independent checks, both run in CI:
131
+
132
+ - `npm test` — 11 suites. Verifies that every stated optimum is actually
133
+ attained at its stated location, and that 200 000 random samples inside each
134
+ domain fail to beat it. Also covers dimension validation, `negate`, and the
135
+ per-axis domain mapping.
136
+ - `python test/crosscheck_botorch.py` — compares **308 values across 11
137
+ functions** against [BoTorch](https://botorch.org)'s `test_functions.synthetic`,
138
+ the reference implementation most of the Bayesian-optimization field uses.
139
+
140
+ | function | max relative difference |
141
+ |---|---|
142
+ | `ackley`, `eggholder`, `powell`, `rosenbrock`, `rastrigin` | `0` exactly |
143
+ | `griewank` | `3.5e-16` |
144
+ | `branin` | `5.5e-16` |
145
+ | `michalewicz` | `2.2e-15` |
146
+ | `shekel` | `5.8e-09` |
147
+ | `hartmann6` | `1.2e-08` |
148
+ | `hartmann3` | `2.9e-08` |
149
+
150
+ Nothing exceeds `3e-08`, and everything above machine epsilon is BoTorch's
151
+ own doing: it stores `ALPHA`, `A` and `C` as **float32** and upcasts, so its
152
+ constants carry about `1e-8` of rounding while the values here are exact
153
+ float64. That is why the tolerance is `1e-6` and not tighter — tightening it
154
+ fails on BoTorch's precision, not on ours.
155
+
156
+ Schwefel is not in BoTorch, and the psychophysical laws are not optimization
157
+ benchmarks, so those five are checked by the JS suite only.
158
+
159
+ ## Notes
160
+
161
+ ESM only. Node 18+. For CommonJS, use a dynamic import:
162
+
163
+ ```js
164
+ const { ackley } = await import('optimization-test-functions');
165
+ ```
166
+
167
+ TypeScript types are hand-written and shipped alongside — there is no build
168
+ step, so what you install is what is in `src/`.
169
+
170
+ ## License
171
+
172
+ MIT
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "optimization-test-functions",
3
+ "version": "1.0.0",
4
+ "description": "Canonical global-optimization benchmarks and classical psychophysical laws, with per-axis domains and known optima. Zero dependencies.",
5
+ "keywords": [
6
+ "optimization", "benchmark", "test-functions", "global-optimization",
7
+ "ackley", "rastrigin", "rosenbrock", "griewank", "hartmann", "branin",
8
+ "schwefel", "shekel", "michalewicz", "eggholder", "powell",
9
+ "bayesian-optimization", "psychophysics", "hci", "fitts", "hick", "stevens"
10
+ ],
11
+ "type": "module",
12
+ "main": "./src/index.js",
13
+ "exports": {
14
+ ".": { "types": "./src/index.d.ts", "default": "./src/index.js" }
15
+ },
16
+ "types": "./src/index.d.ts",
17
+ "files": ["src/"],
18
+ "sideEffects": false,
19
+ "engines": { "node": ">=18" },
20
+ "scripts": {
21
+ "test": "node --test"
22
+ },
23
+ "license": "MIT",
24
+ "author": "Mark Colley",
25
+ "repository": { "type": "git", "url": "git+https://github.com/M-Colley/optimization-test-functions.git" },
26
+ "bugs": { "url": "https://github.com/M-Colley/optimization-test-functions/issues" },
27
+ "homepage": "https://github.com/M-Colley/optimization-test-functions#readme"
28
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,81 @@
1
+ export type Vec = readonly number[] | Float64Array | Float32Array;
2
+
3
+ /** One [lo, hi] pair per axis. */
4
+ export type Box = [number, number][];
5
+
6
+ export interface Optimum {
7
+ /** The optimal value, or null when it depends on the dimension. */
8
+ value: number | null;
9
+ /** The location, given the dimension. Absent when `value` is null. */
10
+ at?: (d: number) => number[];
11
+ /** A commonly quoted but only approximate location, where one exists. */
12
+ approx?: number[];
13
+ /** Every global optimum, for functions that have more than one. */
14
+ all?: number[][];
15
+ /** Published values keyed by dimension, when the optimum is d-dependent. */
16
+ known?: Record<number, number>;
17
+ note?: string;
18
+ }
19
+
20
+ export interface Meta {
21
+ name: string;
22
+ /** 'any', a fixed number, or a description such as 'multiple of 4'. */
23
+ dims: number | string;
24
+ /** Which direction is interesting. Nothing here is silently negated. */
25
+ sense: 'min' | 'max';
26
+ /** The function's own domain, per axis, at dimension d. */
27
+ domain: (d: number) => Box;
28
+ optimum: Optimum;
29
+ /** Present on the psychophysical laws; false or a direction. */
30
+ monotone?: false | 'increasing' | 'decreasing';
31
+ note?: string;
32
+ reference: string;
33
+ }
34
+
35
+ export interface TestFunction<O = Record<string, never>> {
36
+ (x: Vec, options?: O): number;
37
+ meta: Meta;
38
+ }
39
+
40
+ /* classical optimization benchmarks -- minimized */
41
+ export const ackley: TestFunction<{ a?: number; b?: number; c?: number }>;
42
+ export const griewank: TestFunction;
43
+ export const schwefel: TestFunction;
44
+ export const eggholder: TestFunction;
45
+ export const powell: TestFunction;
46
+ export const shekel: TestFunction<{ m?: number }>;
47
+ export const hartmann3: TestFunction;
48
+ export const hartmann6: TestFunction;
49
+ export const branin: TestFunction<{
50
+ a?: number; b?: number; c?: number; r?: number; s?: number; t?: number;
51
+ }>;
52
+ export const rosenbrock: TestFunction;
53
+ export const rastrigin: TestFunction;
54
+ export const michalewicz: TestFunction<{ m?: number }>;
55
+
56
+ /* classical psychophysical laws */
57
+ export const yerkesDodson: TestFunction<{ peak?: number; width?: number }>;
58
+ export const stevens: TestFunction<{ a?: number }>;
59
+ export const hickHyman: TestFunction<{ a?: number; b?: number }>;
60
+ export const weberFechner: TestFunction<{ i0?: number }>;
61
+
62
+ export type FunctionName =
63
+ | 'ackley' | 'griewank' | 'schwefel' | 'eggholder' | 'powell' | 'shekel'
64
+ | 'hartmann3' | 'hartmann6' | 'branin' | 'rosenbrock' | 'rastrigin'
65
+ | 'michalewicz' | 'yerkesDodson' | 'stevens' | 'hickHyman' | 'weberFechner';
66
+
67
+ export const functions: Record<FunctionName, TestFunction<any>>;
68
+ export const optimizationBenchmarks: FunctionName[];
69
+ export const psychophysicalLaws: FunctionName[];
70
+
71
+ /** Wrap a function so a minimizer can be used as a maximizer, or vice versa. */
72
+ export function negate<O>(f: TestFunction<O>): TestFunction<O>;
73
+
74
+ /** The per-axis domain of `name` at dimension `d`. */
75
+ export function domain(name: FunctionName, d?: number): Box;
76
+
77
+ /** Map a point from the unit cube onto `name`'s own domain. */
78
+ export function fromUnitCube(name: FunctionName, u: Vec): number[];
79
+
80
+ declare const _default: Record<FunctionName, TestFunction<any>>;
81
+ export default _default;
package/src/index.js ADDED
@@ -0,0 +1,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 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
+ /** Wrap a function so a minimizer can be used as a maximizer, or vice versa. */
40
+ export function negate(f) {
41
+ const g = (...args) => -f(...args);
42
+ if (f.meta) g.meta = { ...f.meta, sense: f.meta.sense === 'min' ? 'max' : 'min' };
43
+ return g;
44
+ }
45
+
46
+ /** Expand a per-axis domain to `d` dimensions, for functions defined on a cube. */
47
+ function cube(lo, hi) {
48
+ return (d) => Array.from({ length: d }, () => [lo, hi]);
49
+ }
50
+
51
+ /* ------------------------------------------------------------------ *
52
+ * classical optimization benchmarks (minimized) *
53
+ * ------------------------------------------------------------------ */
54
+
55
+ /** Ackley (1987). Flat outer plain, narrow basin at the origin. */
56
+ export function ackley(x, { a = 20, b = 0.2, c = TAU } = {}) {
57
+ assertLength(x, 'ackley', (n) => n >= 1);
58
+ const d = x.length;
59
+ let s1 = 0, s2 = 0;
60
+ for (let i = 0; i < d; i++) { s1 += x[i] * x[i]; s2 += Math.cos(c * x[i]); }
61
+ return -a * Math.exp(-b * Math.sqrt(s1 / d)) - Math.exp(s2 / d) + a + Math.E;
62
+ }
63
+ ackley.meta = {
64
+ name: 'Ackley', dims: 'any', sense: 'min', domain: cube(-32.768, 32.768),
65
+ optimum: { value: 0, at: (d) => new Array(d).fill(0) },
66
+ reference: 'Ackley, D. H. (1987). A Connectionist Machine for Genetic Hillclimbing.',
67
+ };
68
+
69
+ /** Griewank (1981). Wide bowl with fine cosine ripples. */
70
+ export function griewank(x) {
71
+ assertLength(x, 'griewank', (n) => n >= 1);
72
+ let s = 0, p = 1;
73
+ for (let i = 0; i < x.length; i++) {
74
+ s += x[i] * x[i];
75
+ p *= Math.cos(x[i] / Math.sqrt(i + 1));
76
+ }
77
+ return s / 4000 - p + 1;
78
+ }
79
+ griewank.meta = {
80
+ name: 'Griewank', dims: 'any', sense: 'min', domain: cube(-600, 600),
81
+ optimum: { value: 0, at: (d) => new Array(d).fill(0) },
82
+ reference: 'Griewank, A. O. (1981). Journal of Optimization Theory and Applications 34(1).',
83
+ };
84
+
85
+ /** Schwefel (1981). Global optimum far from the deepest local one. */
86
+ export function schwefel(x) {
87
+ assertLength(x, 'schwefel', (n) => n >= 1);
88
+ let s = 0;
89
+ for (let i = 0; i < x.length; i++) s += x[i] * Math.sin(Math.sqrt(Math.abs(x[i])));
90
+ return 418.9828872724339 * x.length - s;
91
+ }
92
+ schwefel.meta = {
93
+ name: 'Schwefel', dims: 'any', sense: 'min', domain: cube(-500, 500),
94
+ optimum: { value: 0, at: (d) => new Array(d).fill(420.9687463) },
95
+ reference: 'Schwefel, H.-P. (1981). Numerical Optimization of Computer Models.',
96
+ };
97
+
98
+ /** Eggholder. Two dimensions, wildly multimodal, no usable global structure. */
99
+ export function eggholder(x) {
100
+ assertLength(x, 'eggholder', (n) => n === 2);
101
+ const [a, b] = x;
102
+ return -(b + 47) * Math.sin(Math.sqrt(Math.abs(a / 2 + (b + 47))))
103
+ - a * Math.sin(Math.sqrt(Math.abs(a - (b + 47))));
104
+ }
105
+ eggholder.meta = {
106
+ name: 'Eggholder', dims: 2, sense: 'min', domain: () => [[-512, 512], [-512, 512]],
107
+ optimum: { value: -959.6406627208506, at: () => [512, 404.2318058008512] },
108
+ reference: 'Vanaret et al. (2014); after Whitley et al. (1996).',
109
+ };
110
+
111
+ /** Powell (1962). Smooth quartic valley. Dimension must be a multiple of 4. */
112
+ export function powell(x) {
113
+ assertLength(x, 'powell', (n) => n > 0 && n % 4 === 0);
114
+ let s = 0;
115
+ for (let i = 0; i < x.length; i += 4) {
116
+ s += (x[i] + 10 * x[i + 1]) ** 2
117
+ + 5 * (x[i + 2] - x[i + 3]) ** 2
118
+ + (x[i + 1] - 2 * x[i + 2]) ** 4
119
+ + 10 * (x[i] - x[i + 3]) ** 4;
120
+ }
121
+ return s;
122
+ }
123
+ powell.meta = {
124
+ name: 'Powell', dims: 'multiple of 4', sense: 'min', domain: cube(-4, 5),
125
+ optimum: { value: 0, at: (d) => new Array(d).fill(0) },
126
+ reference: 'Powell, M. J. D. (1962). The Computer Journal 5(2).',
127
+ };
128
+
129
+ const SHEKEL_A = [
130
+ [4, 4, 4, 4], [1, 1, 1, 1], [8, 8, 8, 8], [6, 6, 6, 6], [3, 7, 3, 7],
131
+ [2, 9, 2, 9], [5, 3, 5, 3], [8, 1, 8, 1], [6, 2, 6, 2], [7, 3.6, 7, 3.6],
132
+ ];
133
+ const SHEKEL_C = [0.1, 0.2, 0.2, 0.4, 0.4, 0.6, 0.3, 0.7, 0.5, 0.5];
134
+
135
+ /** Shekel. Four dimensions, `m` narrow wells on a flat plain. */
136
+ export function shekel(x, { m = 10 } = {}) {
137
+ assertLength(x, 'shekel', (n) => n === 4);
138
+ if (!(m >= 1 && m <= 10)) throw new RangeError('shekel: m must be 1..10');
139
+ let s = 0;
140
+ for (let i = 0; i < m; i++) {
141
+ let q = 0;
142
+ for (let j = 0; j < 4; j++) { const d = x[j] - SHEKEL_A[i][j]; q += d * d; }
143
+ s += 1 / (q + SHEKEL_C[i]);
144
+ }
145
+ return -s;
146
+ }
147
+ shekel.meta = {
148
+ name: 'Shekel', dims: 4, sense: 'min', domain: cube(0, 10),
149
+ optimum: {
150
+ value: -10.53644315348353,
151
+ // Almost always quoted as (4, 4, 4, 4), which is only approximate: the
152
+ // neighbouring wells pull the minimum slightly off that corner, and
153
+ // f(4,4,4,4) = -10.536283, not -10.536443. Refined location below.
154
+ at: () => [4.0007468671, 3.9995094806, 4.0007468700, 3.9995094776],
155
+ approx: [4, 4, 4, 4],
156
+ note: 'for m = 10',
157
+ },
158
+ reference: 'Shekel, J. (1971). Proc. 1971 IEEE Conf. on Decision and Control.',
159
+ };
160
+
161
+ const H_ALPHA = [1.0, 1.2, 3.0, 3.2];
162
+ const H3_A = [[3.0, 10, 30], [0.1, 10, 35], [3.0, 10, 30], [0.1, 10, 35]];
163
+ const H3_P = [[3689, 1170, 2673], [4699, 4387, 7470],
164
+ [1091, 8732, 5547], [381, 5743, 8828]].map((r) => r.map((v) => v * 1e-4));
165
+
166
+ /** Hartmann 3-D. Four Gaussian basins of unequal depth. */
167
+ export function hartmann3(x) {
168
+ assertLength(x, 'hartmann3', (n) => n === 3);
169
+ let s = 0;
170
+ for (let i = 0; i < 4; i++) {
171
+ let inner = 0;
172
+ for (let j = 0; j < 3; j++) { const d = x[j] - H3_P[i][j]; inner += H3_A[i][j] * d * d; }
173
+ s += H_ALPHA[i] * Math.exp(-inner);
174
+ }
175
+ return -s;
176
+ }
177
+ hartmann3.meta = {
178
+ name: 'Hartmann-3', dims: 3, sense: 'min', domain: cube(0, 1),
179
+ optimum: { value: -3.86278214782076, at: () => [0.114614, 0.555649, 0.852547] },
180
+ reference: 'Hartman, J. K. (1973). Naval Research Logistics Quarterly 20(3).',
181
+ };
182
+
183
+ const H6_A = [[10, 3, 17, 3.5, 1.7, 8], [0.05, 10, 17, 0.1, 8, 14],
184
+ [3, 3.5, 1.7, 10, 17, 8], [17, 8, 0.05, 10, 0.1, 14]];
185
+ const H6_P = [[1312, 1696, 5569, 124, 8283, 5886], [2329, 4135, 8307, 3736, 1004, 9991],
186
+ [2348, 1451, 3522, 2883, 3047, 6650], [4047, 8828, 8732, 5743, 1091, 381]]
187
+ .map((r) => r.map((v) => v * 1e-4));
188
+
189
+ /** Hartmann 6-D. The usual stress test for a GP with one lengthscale per axis. */
190
+ export function hartmann6(x) {
191
+ assertLength(x, 'hartmann6', (n) => n === 6);
192
+ let s = 0;
193
+ for (let i = 0; i < 4; i++) {
194
+ let inner = 0;
195
+ for (let j = 0; j < 6; j++) { const d = x[j] - H6_P[i][j]; inner += H6_A[i][j] * d * d; }
196
+ s += H_ALPHA[i] * Math.exp(-inner);
197
+ }
198
+ return -s;
199
+ }
200
+ hartmann6.meta = {
201
+ name: 'Hartmann-6', dims: 6, sense: 'min', domain: cube(0, 1),
202
+ optimum: {
203
+ value: -3.322368011391339,
204
+ at: () => [0.20169, 0.150011, 0.476874, 0.275332, 0.311652, 0.6573],
205
+ },
206
+ reference: 'Hartman, J. K. (1973). Naval Research Logistics Quarterly 20(3).',
207
+ };
208
+
209
+ /**
210
+ * Branin-Hoo. Three equal global minima in a shallow curved trough.
211
+ * Note the ASYMMETRIC domain: x1 in [-5, 10], x2 in [0, 15]. Applying one
212
+ * range to both axes is a common and silent mistake.
213
+ */
214
+ export function branin(x, { a = 1, b = 5.1 / (4 * Math.PI ** 2), c = 5 / Math.PI,
215
+ r = 6, s = 10, t = 1 / (8 * Math.PI) } = {}) {
216
+ assertLength(x, 'branin', (n) => n === 2);
217
+ const [x1, x2] = x;
218
+ return a * (x2 - b * x1 * x1 + c * x1 - r) ** 2 + s * (1 - t) * Math.cos(x1) + s;
219
+ }
220
+ branin.meta = {
221
+ name: 'Branin', dims: 2, sense: 'min', domain: () => [[-5, 10], [0, 15]],
222
+ optimum: {
223
+ value: 0.39788735772973816,
224
+ at: () => [-Math.PI, 12.275],
225
+ all: [[-Math.PI, 12.275], [Math.PI, 2.275], [9.42478, 2.475]],
226
+ },
227
+ reference: 'Branin, F. H. (1972). IBM Journal of Research and Development 16(5).',
228
+ };
229
+
230
+ /** Rosenbrock (1960). The banana valley: easy to enter, slow to follow. */
231
+ export function rosenbrock(x) {
232
+ assertLength(x, 'rosenbrock', (n) => n >= 2);
233
+ let s = 0;
234
+ for (let i = 0; i < x.length - 1; i++) {
235
+ s += 100 * (x[i + 1] - x[i] * x[i]) ** 2 + (x[i] - 1) ** 2;
236
+ }
237
+ return s;
238
+ }
239
+ rosenbrock.meta = {
240
+ name: 'Rosenbrock', dims: '>= 2', sense: 'min', domain: cube(-5, 10),
241
+ optimum: { value: 0, at: (d) => new Array(d).fill(1) },
242
+ reference: 'Rosenbrock, H. H. (1960). The Computer Journal 3(3).',
243
+ };
244
+
245
+ /** Rastrigin (1974). A regular lattice of local minima on a quadratic bowl. */
246
+ export function rastrigin(x) {
247
+ assertLength(x, 'rastrigin', (n) => n >= 1);
248
+ let s = 0;
249
+ for (let i = 0; i < x.length; i++) s += x[i] * x[i] - 10 * Math.cos(TAU * x[i]);
250
+ return 10 * x.length + s;
251
+ }
252
+ rastrigin.meta = {
253
+ name: 'Rastrigin', dims: 'any', sense: 'min', domain: cube(-5.12, 5.12),
254
+ optimum: { value: 0, at: (d) => new Array(d).fill(0) },
255
+ reference: 'Rastrigin, L. A. (1974). Systems of Extremal Control.',
256
+ };
257
+
258
+ /** Michalewicz (1996). Steep ridges separated by flat plateaus. */
259
+ export function michalewicz(x, { m = 10 } = {}) {
260
+ assertLength(x, 'michalewicz', (n) => n >= 1);
261
+ let s = 0;
262
+ for (let i = 0; i < x.length; i++) {
263
+ s += Math.sin(x[i]) * Math.sin((i + 1) * x[i] * x[i] / Math.PI) ** (2 * m);
264
+ }
265
+ return -s;
266
+ }
267
+ michalewicz.meta = {
268
+ name: 'Michalewicz', dims: 'any', sense: 'min', domain: cube(0, Math.PI),
269
+ optimum: {
270
+ value: null,
271
+ note: 'depends on d; published values are -1.8013 (d=2), -4.687658 (d=5), -9.66015 (d=10) at m=10',
272
+ known: { 2: -1.8013, 5: -4.687658, 10: -9.66015 },
273
+ },
274
+ reference: 'Michalewicz, Z. (1996). Genetic Algorithms + Data Structures = Evolution Programs.',
275
+ };
276
+
277
+ /* ------------------------------------------------------------------ *
278
+ * classical psychophysical laws *
279
+ * *
280
+ * These are human response functions, not optimization benchmarks. *
281
+ * They are given in the sense the law states. Three of the four are *
282
+ * MONOTONE, so on any box their extremum is a corner — which makes *
283
+ * them weak as search problems but useful as a landscape class that *
284
+ * the classical benchmarks above do not contain. *
285
+ * ------------------------------------------------------------------ */
286
+
287
+ /** Yerkes-Dodson (1908). Inverted-U of arousal against performance. */
288
+ export function yerkesDodson(x, { peak = 0.5, width = 0.15 } = {}) {
289
+ assertLength(x, 'yerkesDodson', (n) => n >= 1);
290
+ let s = 0;
291
+ for (let i = 0; i < x.length; i++) {
292
+ s += Math.exp(-((x[i] - peak) ** 2) / (2 * width * width));
293
+ }
294
+ return s / x.length;
295
+ }
296
+ yerkesDodson.meta = {
297
+ name: 'Yerkes-Dodson', dims: 'any', sense: 'max', domain: cube(0, 1), monotone: false,
298
+ optimum: { value: 1, at: (d) => new Array(d).fill(0.5) },
299
+ reference: 'Yerkes, R. M., & Dodson, J. D. (1908). J. Comparative Neurology and Psychology 18.',
300
+ };
301
+
302
+ /** Stevens' power law (1957): perceived magnitude psi = I^a. Monotone. */
303
+ export function stevens(x, { a = 0.67 } = {}) {
304
+ assertLength(x, 'stevens', (n) => n >= 1);
305
+ let s = 0;
306
+ for (let i = 0; i < x.length; i++) s += Math.max(x[i], 0) ** a;
307
+ return s / x.length;
308
+ }
309
+ stevens.meta = {
310
+ name: "Stevens' power law", dims: 'any', sense: 'max', domain: cube(0, 1),
311
+ monotone: 'increasing',
312
+ optimum: { value: 1, at: (d) => new Array(d).fill(1) },
313
+ note: 'a = 0.67 is the exponent Stevens reported for loudness.',
314
+ reference: 'Stevens, S. S. (1957). Psychological Review 64(3).',
315
+ };
316
+
317
+ /** Hick-Hyman law (1952/1953): choice reaction time a + b*log2(n + 1). Monotone. */
318
+ export function hickHyman(x, { a = 0.2, b = 0.15 } = {}) {
319
+ assertLength(x, 'hickHyman', (n) => n >= 1);
320
+ let s = 0;
321
+ for (let i = 0; i < x.length; i++) {
322
+ s += a + b * Math.log2(Math.max(x[i], 0) + 1);
323
+ }
324
+ return s / x.length;
325
+ }
326
+ hickHyman.meta = {
327
+ name: "Hick-Hyman law", dims: 'any', sense: 'min', domain: cube(0, 10),
328
+ monotone: 'increasing',
329
+ optimum: { value: 0.2, at: (d) => new Array(d).fill(0) },
330
+ note: 'Returns reaction time in seconds, so lower is better. a and b are in the usual reported range.',
331
+ reference: 'Hick, W. E. (1952). QJEP 4(1); Hyman, R. (1953). J. Experimental Psychology 45(3).',
332
+ };
333
+
334
+ /** Weber-Fechner (1860): sensation psi = ln(1 + I / I0). Monotone. */
335
+ export function weberFechner(x, { i0 = 0.01 } = {}) {
336
+ assertLength(x, 'weberFechner', (n) => n >= 1);
337
+ let s = 0;
338
+ for (let i = 0; i < x.length; i++) s += Math.log1p(Math.max(x[i], 0) / i0);
339
+ return s / x.length;
340
+ }
341
+ weberFechner.meta = {
342
+ name: 'Weber-Fechner', dims: 'any', sense: 'max', domain: cube(0, 1),
343
+ monotone: 'increasing',
344
+ optimum: { value: Math.log1p(1 / 0.01), at: (d) => new Array(d).fill(1) },
345
+ note: 'ln(1 + I/I0) rather than ln(I/I0), so the value stays finite at zero intensity.',
346
+ reference: 'Fechner, G. T. (1860). Elemente der Psychophysik.',
347
+ };
348
+
349
+ /* ------------------------------------------------------------------ *
350
+ * registry *
351
+ * ------------------------------------------------------------------ */
352
+
353
+ export const functions = {
354
+ ackley, griewank, schwefel, eggholder, powell, shekel,
355
+ hartmann3, hartmann6, branin, rosenbrock, rastrigin, michalewicz,
356
+ yerkesDodson, stevens, hickHyman, weberFechner,
357
+ };
358
+
359
+ /** Every benchmark that is a classical optimization test function. */
360
+ export const optimizationBenchmarks = [
361
+ 'ackley', 'griewank', 'schwefel', 'eggholder', 'powell', 'shekel',
362
+ 'hartmann3', 'hartmann6', 'branin', 'rosenbrock', 'rastrigin', 'michalewicz',
363
+ ];
364
+
365
+ /** Every benchmark that is a classical human response function. */
366
+ export const psychophysicalLaws = [
367
+ 'yerkesDodson', 'stevens', 'hickHyman', 'weberFechner',
368
+ ];
369
+
370
+ /** The per-axis domain of `name` at dimension `d`, as [[lo, hi], ...]. */
371
+ export function domain(name, d) {
372
+ const f = functions[name];
373
+ if (!f) throw new RangeError(`unknown function: ${name}`);
374
+ const dims = f.meta.dims;
375
+ if (typeof dims === 'number') d = dims;
376
+ if (d === undefined) throw new RangeError(`${name}: dimension is required`);
377
+ return f.meta.domain(d);
378
+ }
379
+
380
+ /** Uniformly map a point from the unit cube onto `name`'s own domain. */
381
+ export function fromUnitCube(name, u) {
382
+ const box = domain(name, u.length);
383
+ return u.map((v, i) => box[i][0] + v * (box[i][1] - box[i][0]));
384
+ }
385
+
386
+ export default functions;