@mark1russell7/layout-contract 0.7.0 → 0.9.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/build/index.d.ts +20 -425
- package/build/index.d.ts.map +1 -1
- package/build/index.js +83 -1517
- package/build/index.js.map +1 -1
- package/package.json +4 -2
- package/src/hash.ts +7 -68
- package/src/index.ts +0 -1
- package/src/random.ts +4 -7
- package/src/strategy-catalog.ts +1 -1
- package/src/strategy-space.ts +97 -1207
- package/src/verdict.ts +26 -72
- package/src/space-walk.ts +0 -72
- package/src/strategy-options.ts +0 -375
- package/src/utf8.ts +0 -51
package/build/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { mulberry32 as mulberry32$1 } from "@mark1russell7/rigor-generators";
|
|
1
|
+
import { canonicalJson as canonicalJson$1, deriveSeed, fnv1a32 as fnv1a32$1, mulberry32 as mulberry32$1, sha256Hex as sha256Hex$1 } from "@mark1russell7/rigor-generators";
|
|
2
|
+
import { STRATEGY_SPACE_SCHEMA, canonicalConfig, checkConfig, checkOptions, checkSchema, checkSpace as checkSpace$1, clampOptions, configId, declarationIssues, enumerateSpace as enumerateSpace$1, joinConfigs, listOptions, optionDefaults, optionIssue, paramActive, paramLevels, paramProblem, paramValue, productSpace, spaceShapeKinds as spaceShapeKinds$1, spaceSize as spaceSize$1, splitConfig, sweepGrid, sweepOf, sweepValues } from "@mark1russell7/strategy-space";
|
|
3
|
+
import { bootstrap, bootstrapStrata, signFlipTest, signTest } from "@mark1russell7/rigor-statistics";
|
|
2
4
|
// src/geometry.ts
|
|
3
5
|
/** The sides in clockwise order, from the top. */
|
|
4
6
|
const SIDES = [
|
|
@@ -1929,57 +1931,6 @@ function balanceMeasureSpecs(stage) {
|
|
|
1929
1931
|
}
|
|
1930
1932
|
]);
|
|
1931
1933
|
}
|
|
1932
|
-
// src/utf8.ts
|
|
1933
|
-
/**
|
|
1934
|
-
* This function writes the UTF-8 bytes of a string into `out`, from the index `at`, and gives the index after the last byte.
|
|
1935
|
-
* With no `out`, it writes nothing and gives the count of the bytes. A lone surrogate gives the 3 bytes of its code.
|
|
1936
|
-
* It reads the UTF-16 code units, so it makes no string for each character (L81: `sha256Hex` allocated 2 GB in one pair).
|
|
1937
|
-
*/
|
|
1938
|
-
function utf8Write(text, out, at = 0) {
|
|
1939
|
-
let n = at;
|
|
1940
|
-
for (let i = 0; i < text.length; i++) {
|
|
1941
|
-
let c = text.charCodeAt(i);
|
|
1942
|
-
if (c >= 55296 && c < 56320 && i + 1 < text.length) {
|
|
1943
|
-
const low = text.charCodeAt(i + 1);
|
|
1944
|
-
if (low >= 56320 && low < 57344) {
|
|
1945
|
-
c = 65536 + (c - 55296 << 10) + (low - 56320);
|
|
1946
|
-
i++;
|
|
1947
|
-
}
|
|
1948
|
-
}
|
|
1949
|
-
if (c < 128) {
|
|
1950
|
-
if (out !== void 0) out[n] = c;
|
|
1951
|
-
n += 1;
|
|
1952
|
-
} else if (c < 2048) {
|
|
1953
|
-
if (out !== void 0) {
|
|
1954
|
-
out[n] = 192 | c >> 6;
|
|
1955
|
-
out[n + 1] = 128 | c & 63;
|
|
1956
|
-
}
|
|
1957
|
-
n += 2;
|
|
1958
|
-
} else if (c < 65536) {
|
|
1959
|
-
if (out !== void 0) {
|
|
1960
|
-
out[n] = 224 | c >> 12;
|
|
1961
|
-
out[n + 1] = 128 | c >> 6 & 63;
|
|
1962
|
-
out[n + 2] = 128 | c & 63;
|
|
1963
|
-
}
|
|
1964
|
-
n += 3;
|
|
1965
|
-
} else {
|
|
1966
|
-
if (out !== void 0) {
|
|
1967
|
-
out[n] = 240 | c >> 18;
|
|
1968
|
-
out[n + 1] = 128 | c >> 12 & 63;
|
|
1969
|
-
out[n + 2] = 128 | c >> 6 & 63;
|
|
1970
|
-
out[n + 3] = 128 | c & 63;
|
|
1971
|
-
}
|
|
1972
|
-
n += 4;
|
|
1973
|
-
}
|
|
1974
|
-
}
|
|
1975
|
-
return n;
|
|
1976
|
-
}
|
|
1977
|
-
/** This function gives the UTF-8 bytes of a string. It does not use `TextEncoder`, because `TextEncoder` encodes a lone surrogate as U+FFFD. */
|
|
1978
|
-
function utf8(text) {
|
|
1979
|
-
const bytes = new Uint8Array(utf8Write(text));
|
|
1980
|
-
utf8Write(text, bytes);
|
|
1981
|
-
return bytes;
|
|
1982
|
-
}
|
|
1983
1934
|
// src/random.ts
|
|
1984
1935
|
/**
|
|
1985
1936
|
* This function gives a generator of random numbers in [0, 1). It is `mulberry32`, as in `docs/FORMAT.md` of `packing`.
|
|
@@ -1989,13 +1940,11 @@ function mulberry32(seed) {
|
|
|
1989
1940
|
return mulberry32$1(seed);
|
|
1990
1941
|
}
|
|
1991
1942
|
/**
|
|
1992
|
-
* This function gives the FNV-1a hash of 32 bits of the UTF-8 bytes of a string.
|
|
1993
|
-
*
|
|
1943
|
+
* This function gives the FNV-1a hash of 32 bits of the UTF-8 bytes of a string. A lone surrogate gives the 3 bytes of its code.
|
|
1944
|
+
* Its home is `fnv1a32` of `@mark1russell7/rigor-generators`, with the option `input: "wtf8"`. This function keeps the name and the signature of the API.
|
|
1994
1945
|
*/
|
|
1995
1946
|
function fnv1a32(text) {
|
|
1996
|
-
|
|
1997
|
-
for (const b of utf8(text)) h = Math.imul(h ^ b, 16777619) >>> 0;
|
|
1998
|
-
return h;
|
|
1947
|
+
return fnv1a32$1(text, { input: "wtf8" });
|
|
1999
1948
|
}
|
|
2000
1949
|
/** This function gives the generator of one step of a seeded generator: `mulberry32(fnv1a32("<seed>:<step>"))`. */
|
|
2001
1950
|
function stepRandom(seed, step) {
|
|
@@ -2262,143 +2211,21 @@ function edgeKeyStep(input, params, seed = input.seed ?? 0) {
|
|
|
2262
2211
|
};
|
|
2263
2212
|
}
|
|
2264
2213
|
// src/hash.ts
|
|
2265
|
-
/** The round constants of SHA-256. */
|
|
2266
|
-
const K = [
|
|
2267
|
-
1116352408,
|
|
2268
|
-
1899447441,
|
|
2269
|
-
3049323471,
|
|
2270
|
-
3921009573,
|
|
2271
|
-
961987163,
|
|
2272
|
-
1508970993,
|
|
2273
|
-
2453635748,
|
|
2274
|
-
2870763221,
|
|
2275
|
-
3624381080,
|
|
2276
|
-
310598401,
|
|
2277
|
-
607225278,
|
|
2278
|
-
1426881987,
|
|
2279
|
-
1925078388,
|
|
2280
|
-
2162078206,
|
|
2281
|
-
2614888103,
|
|
2282
|
-
3248222580,
|
|
2283
|
-
3835390401,
|
|
2284
|
-
4022224774,
|
|
2285
|
-
264347078,
|
|
2286
|
-
604807628,
|
|
2287
|
-
770255983,
|
|
2288
|
-
1249150122,
|
|
2289
|
-
1555081692,
|
|
2290
|
-
1996064986,
|
|
2291
|
-
2554220882,
|
|
2292
|
-
2821834349,
|
|
2293
|
-
2952996808,
|
|
2294
|
-
3210313671,
|
|
2295
|
-
3336571891,
|
|
2296
|
-
3584528711,
|
|
2297
|
-
113926993,
|
|
2298
|
-
338241895,
|
|
2299
|
-
666307205,
|
|
2300
|
-
773529912,
|
|
2301
|
-
1294757372,
|
|
2302
|
-
1396182291,
|
|
2303
|
-
1695183700,
|
|
2304
|
-
1986661051,
|
|
2305
|
-
2177026350,
|
|
2306
|
-
2456956037,
|
|
2307
|
-
2730485921,
|
|
2308
|
-
2820302411,
|
|
2309
|
-
3259730800,
|
|
2310
|
-
3345764771,
|
|
2311
|
-
3516065817,
|
|
2312
|
-
3600352804,
|
|
2313
|
-
4094571909,
|
|
2314
|
-
275423344,
|
|
2315
|
-
430227734,
|
|
2316
|
-
506948616,
|
|
2317
|
-
659060556,
|
|
2318
|
-
883997877,
|
|
2319
|
-
958139571,
|
|
2320
|
-
1322822218,
|
|
2321
|
-
1537002063,
|
|
2322
|
-
1747873779,
|
|
2323
|
-
1955562222,
|
|
2324
|
-
2024104815,
|
|
2325
|
-
2227730452,
|
|
2326
|
-
2361852424,
|
|
2327
|
-
2428436474,
|
|
2328
|
-
2756734187,
|
|
2329
|
-
3204031479,
|
|
2330
|
-
3329325298
|
|
2331
|
-
];
|
|
2332
2214
|
/**
|
|
2333
|
-
* This function gives the SHA-256 digest of the
|
|
2334
|
-
*
|
|
2215
|
+
* This function gives the SHA-256 digest of the bytes of a string, as 64 lowercase hex characters. A lone surrogate gives the 3 bytes of its code.
|
|
2216
|
+
* Its home is `sha256Hex` of `@mark1russell7/rigor-generators`, with the option `input: "wtf8"`. This function keeps the name and the signature of the API.
|
|
2335
2217
|
*/
|
|
2336
2218
|
function sha256Hex(text) {
|
|
2337
|
-
|
|
2338
|
-
const bits = length * 8;
|
|
2339
|
-
const total = Math.ceil((length + 9) / 64) * 64;
|
|
2340
|
-
const data = new Uint8Array(total);
|
|
2341
|
-
utf8Write(text, data);
|
|
2342
|
-
data[length] = 128;
|
|
2343
|
-
const view = new DataView(data.buffer);
|
|
2344
|
-
view.setUint32(total - 8, Math.floor(bits / 4294967296));
|
|
2345
|
-
view.setUint32(total - 4, bits >>> 0);
|
|
2346
|
-
const h = [
|
|
2347
|
-
1779033703,
|
|
2348
|
-
3144134277,
|
|
2349
|
-
1013904242,
|
|
2350
|
-
2773480762,
|
|
2351
|
-
1359893119,
|
|
2352
|
-
2600822924,
|
|
2353
|
-
528734635,
|
|
2354
|
-
1541459225
|
|
2355
|
-
];
|
|
2356
|
-
const w = /* @__PURE__ */ new Uint32Array(64);
|
|
2357
|
-
const rotr = (x, n) => x >>> n | x << 32 - n;
|
|
2358
|
-
for (let off = 0; off < total; off += 64) {
|
|
2359
|
-
for (let i = 0; i < 16; i++) w[i] = view.getUint32(off + 4 * i);
|
|
2360
|
-
for (let i = 16; i < 64; i++) {
|
|
2361
|
-
const a = w[i - 15];
|
|
2362
|
-
const b = w[i - 2];
|
|
2363
|
-
const s0 = rotr(a, 7) ^ rotr(a, 18) ^ a >>> 3;
|
|
2364
|
-
const s1 = rotr(b, 17) ^ rotr(b, 19) ^ b >>> 10;
|
|
2365
|
-
w[i] = w[i - 16] + s0 + w[i - 7] + s1 >>> 0;
|
|
2366
|
-
}
|
|
2367
|
-
let [a, b, c, d, e, f, g, k] = h;
|
|
2368
|
-
for (let i = 0; i < 64; i++) {
|
|
2369
|
-
const s1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
2370
|
-
const ch = e & f ^ ~e & g;
|
|
2371
|
-
const t1 = k + s1 + ch + K[i] + w[i] >>> 0;
|
|
2372
|
-
const t2 = (rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + (a & b ^ a & c ^ b & c) >>> 0;
|
|
2373
|
-
k = g;
|
|
2374
|
-
g = f;
|
|
2375
|
-
f = e;
|
|
2376
|
-
e = d + t1 >>> 0;
|
|
2377
|
-
d = c;
|
|
2378
|
-
c = b;
|
|
2379
|
-
b = a;
|
|
2380
|
-
a = t1 + t2 >>> 0;
|
|
2381
|
-
}
|
|
2382
|
-
h[0] = h[0] + a >>> 0;
|
|
2383
|
-
h[1] = h[1] + b >>> 0;
|
|
2384
|
-
h[2] = h[2] + c >>> 0;
|
|
2385
|
-
h[3] = h[3] + d >>> 0;
|
|
2386
|
-
h[4] = h[4] + e >>> 0;
|
|
2387
|
-
h[5] = h[5] + f >>> 0;
|
|
2388
|
-
h[6] = h[6] + g >>> 0;
|
|
2389
|
-
h[7] = h[7] + k >>> 0;
|
|
2390
|
-
}
|
|
2391
|
-
return h.map((x) => x.toString(16).padStart(8, "0")).join("");
|
|
2219
|
+
return sha256Hex$1(text, { input: "wtf8" });
|
|
2392
2220
|
}
|
|
2393
2221
|
/**
|
|
2394
2222
|
* This function gives the canonical form of a value: sorted keys and no whitespace, with the numbers of `JSON.stringify`.
|
|
2395
2223
|
* A key with the value `undefined` is not in the form. An `undefined` in an array becomes `null`.
|
|
2224
|
+
* Its home is `canonicalJson` of `@mark1russell7/rigor-generators`, with the option `form: "json"`.
|
|
2225
|
+
* So a value with a method `toJSON`, for example a `Date`, gives the form of the result of that method. A function, a symbol and a cycle cause a `TypeError`.
|
|
2396
2226
|
*/
|
|
2397
2227
|
function canonicalJson(value) {
|
|
2398
|
-
|
|
2399
|
-
if (Array.isArray(value)) return `[${value.map((v) => v === void 0 ? "null" : canonicalJson(v)).join(",")}]`;
|
|
2400
|
-
const o = value;
|
|
2401
|
-
return `{${Object.keys(o).filter((k) => o[k] !== void 0).sort().map((k) => `${JSON.stringify(k)}:${canonicalJson(o[k])}`).join(",")}}`;
|
|
2228
|
+
return canonicalJson$1(value, { form: "json" });
|
|
2402
2229
|
}
|
|
2403
2230
|
/** This function gives the factor of a grid: `1 / grid`, or `undefined` for the exact numbers. */
|
|
2404
2231
|
function gridFactor(options) {
|
|
@@ -2800,7 +2627,7 @@ const FAMILY_ANY_KIND = "*";
|
|
|
2800
2627
|
/** The separator of the parts of a family key. */
|
|
2801
2628
|
const FAMILY_SEP = "/";
|
|
2802
2629
|
/** This function gives the text of a dimension value. A missing value, or a value that is not a string, a number or a boolean, gives `undefined`. */
|
|
2803
|
-
function partOf
|
|
2630
|
+
function partOf(value) {
|
|
2804
2631
|
if (typeof value === "string") return value;
|
|
2805
2632
|
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
2806
2633
|
}
|
|
@@ -2812,7 +2639,7 @@ function familyParts(cell, familyBy) {
|
|
|
2812
2639
|
if (Array.isArray(familyBy)) dims = familyBy;
|
|
2813
2640
|
else {
|
|
2814
2641
|
const map = familyBy;
|
|
2815
|
-
kind = partOf
|
|
2642
|
+
kind = partOf(cell["kind"]);
|
|
2816
2643
|
if (kind === void 0) return { problem: "a cell without kind, which the map form of familyBy needs" };
|
|
2817
2644
|
const entry = Object.hasOwn(map, kind) ? map[kind] : Object.hasOwn(map, "*") ? map["*"] : void 0;
|
|
2818
2645
|
if (entry === void 0) return { problem: `the kind "${kind}", which has no entry in familyBy and no entry "*"` };
|
|
@@ -2820,7 +2647,7 @@ function familyParts(cell, familyBy) {
|
|
|
2820
2647
|
parts.push(kind);
|
|
2821
2648
|
}
|
|
2822
2649
|
for (const dim of dims) {
|
|
2823
|
-
const part = partOf
|
|
2650
|
+
const part = partOf(cell[dim]);
|
|
2824
2651
|
if (part === void 0) return { problem: `a cell${kind === void 0 ? "" : ` of the kind "${kind}"`} without the dimension "${dim}" of familyBy` };
|
|
2825
2652
|
parts.push(part);
|
|
2826
2653
|
}
|
|
@@ -2839,7 +2666,7 @@ function familyOf(cell, familyBy) {
|
|
|
2839
2666
|
/** This function tells if a value is a `FamilyBy`: a list of dimensions, or a map from a kind to a list of dimensions. */
|
|
2840
2667
|
function isFamilyBy(value) {
|
|
2841
2668
|
const isDims = (v) => Array.isArray(v) && v.every((d) => typeof d === "string");
|
|
2842
|
-
return isDims(value) || isObject
|
|
2669
|
+
return isDims(value) || isObject(value) && Object.values(value).every(isDims);
|
|
2843
2670
|
}
|
|
2844
2671
|
/**
|
|
2845
2672
|
* This function gives the count of the inputs for each property id. Each id of `ids` has a count, also when the count is 0.
|
|
@@ -2851,7 +2678,7 @@ function declaredCounts(inputs, declares, ids = []) {
|
|
|
2851
2678
|
return out;
|
|
2852
2679
|
}
|
|
2853
2680
|
/** This function tells if a value is a plain object. */
|
|
2854
|
-
const isObject
|
|
2681
|
+
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
2855
2682
|
/**
|
|
2856
2683
|
* This function reads the field `claims.declared` of a manifest. A manifest without it gives `undefined`.
|
|
2857
2684
|
* It throws an `Error` for a claim without a whole `minCount`, a list `thin` of ids and a whole count for each id.
|
|
@@ -2859,14 +2686,14 @@ const isObject$1 = (value) => value !== null && typeof value === "object" && !Ar
|
|
|
2859
2686
|
function declaredClaimOf(manifest) {
|
|
2860
2687
|
const claims = manifest["claims"];
|
|
2861
2688
|
if (claims === void 0) return void 0;
|
|
2862
|
-
if (!isObject
|
|
2689
|
+
if (!isObject(claims)) throw new Error("The field claims of a corpus file is an object.");
|
|
2863
2690
|
const declared = claims["declared"];
|
|
2864
2691
|
if (declared === void 0) return void 0;
|
|
2865
|
-
if (!isObject
|
|
2692
|
+
if (!isObject(declared)) throw new Error("The field claims.declared of a corpus file is an object.");
|
|
2866
2693
|
const { minCount, thin, counts } = declared;
|
|
2867
2694
|
if (!Number.isInteger(minCount) || minCount < 0) throw new Error("The field claims.declared.minCount is a whole number of 0 or more.");
|
|
2868
2695
|
if (!Array.isArray(thin) || !thin.every((id) => typeof id === "string")) throw new Error("The field claims.declared.thin is a list of property ids.");
|
|
2869
|
-
if (!isObject
|
|
2696
|
+
if (!isObject(counts) || !Object.values(counts).every((n) => Number.isInteger(n) && n >= 0)) throw new Error("The field claims.declared.counts gives a whole count for each property id.");
|
|
2870
2697
|
const { familyBy, familyMin } = declared;
|
|
2871
2698
|
if (familyBy !== void 0 && !isFamilyBy(familyBy)) throw new Error("The field claims.declared.familyBy is a list of dimensions, or a map from a kind to a list of dimensions.");
|
|
2872
2699
|
if (familyMin !== void 0 && (!Number.isInteger(familyMin) || familyMin < 0)) throw new Error("The field claims.declared.familyMin is a whole number of 0 or more.");
|
|
@@ -2912,12 +2739,12 @@ function checkDeclared(inputs, declares, claim, ids = Object.keys(claim.counts),
|
|
|
2912
2739
|
}
|
|
2913
2740
|
/** This function gives the field `cell` of an input, when it is an object. */
|
|
2914
2741
|
function cellField(input) {
|
|
2915
|
-
const cell = isObject
|
|
2916
|
-
return isObject
|
|
2742
|
+
const cell = isObject(input) ? input["cell"] : void 0;
|
|
2743
|
+
return isObject(cell) ? cell : void 0;
|
|
2917
2744
|
}
|
|
2918
2745
|
/** This function gives the field `id` of an input, or its place in the list. */
|
|
2919
2746
|
function inputId(input, at) {
|
|
2920
|
-
const id = isObject
|
|
2747
|
+
const id = isObject(input) ? input["id"] : void 0;
|
|
2921
2748
|
return typeof id === "string" ? id : `#${at}`;
|
|
2922
2749
|
}
|
|
2923
2750
|
/** This function gives the breaks of the family keys of the inputs: the problems of each key, the collisions and the thin families. */
|
|
@@ -3046,1265 +2873,39 @@ function leaveOneSeedOut(seeds) {
|
|
|
3046
2873
|
heldOut: [s]
|
|
3047
2874
|
}));
|
|
3048
2875
|
}
|
|
3049
|
-
// src/space-walk.ts
|
|
3050
|
-
const refMatches = (ref, choices) => (choices.get(ref.stage) ?? null) === ref.option;
|
|
3051
|
-
/** This function tells if the option choices keep a constraint. */
|
|
3052
|
-
function holds(c, choices) {
|
|
3053
|
-
if (!refMatches(c.if, choices)) return true;
|
|
3054
|
-
return "not" in c.then ? !refMatches(c.then.not, choices) : refMatches(c.then, choices);
|
|
3055
|
-
}
|
|
3056
|
-
const otherStage = (c) => "not" in c.then ? c.then.not.stage : c.then.stage;
|
|
3057
|
-
/** This function tells if an option of a stage keeps each constraint between that stage and the stages that a walk has fixed. */
|
|
3058
|
-
function fitsWalk(constraints, fixed, stage, id) {
|
|
3059
|
-
fixed.set(stage, id);
|
|
3060
|
-
const ok = constraints.every((c) => !fixed.has(c.if.stage === stage ? otherStage(c) : c.if.stage) || holds(c, fixed));
|
|
3061
|
-
fixed.delete(stage);
|
|
3062
|
-
return ok;
|
|
3063
|
-
}
|
|
3064
|
-
/**
|
|
3065
|
-
* This function gives the plan of a walk. Stages of equal size keep the order of the space.
|
|
3066
|
-
* The stages of `first` go first, for example the stages of the sample option `fixed`. Thus the other stages draw only from the options that keep the constraints with them.
|
|
3067
|
-
*/
|
|
3068
|
-
function walkPlan(stages, lists, constraints, n, first = /* @__PURE__ */ new Set()) {
|
|
3069
|
-
return {
|
|
3070
|
-
stages,
|
|
3071
|
-
lists,
|
|
3072
|
-
n,
|
|
3073
|
-
order: stages.map((_, k) => k).sort((a, b) => Number(first.has(stages[b].name)) - Number(first.has(stages[a].name)) || lists[b].length - lists[a].length || a - b),
|
|
3074
|
-
naming: stages.map((s) => constraints.filter((c) => c.if.stage === s.name || otherStage(c) === s.name))
|
|
3075
|
-
};
|
|
3076
|
-
}
|
|
3077
|
-
/**
|
|
3078
|
-
* This function gives the options of one point of a walk. It fixes the largest stage first. Thus that stage keeps its equal strata, and the constraints limit only the smaller stages.
|
|
3079
|
-
* The value `picks[k][i]` is the stratum of the stage `k` for the point `i`. A stage with no open option gets `null`, and the caller rejects that point.
|
|
3080
|
-
* The argument `fits` lets a test give a fault. The default is `fitsWalk`.
|
|
3081
|
-
*/
|
|
3082
|
-
function walkPoint(plan, picks, i, fits = fitsWalk) {
|
|
3083
|
-
const fixed = /* @__PURE__ */ new Map();
|
|
3084
|
-
const out = /* @__PURE__ */ new Map();
|
|
3085
|
-
for (const k of plan.order) {
|
|
3086
|
-
const s = plan.stages[k];
|
|
3087
|
-
const open = plan.lists[k].filter((o) => fits(plan.naming[k], fixed, s.name, o?.id ?? null));
|
|
3088
|
-
const o = open[Math.floor(picks[k][i] * open.length / plan.n)] ?? null;
|
|
3089
|
-
fixed.set(s.name, o?.id ?? null);
|
|
3090
|
-
out.set(s.name, o);
|
|
3091
|
-
}
|
|
3092
|
-
return out;
|
|
3093
|
-
}
|
|
3094
2876
|
// src/strategy-space.ts
|
|
3095
|
-
/**
|
|
3096
|
-
const
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
const zero = (v) => v === 0 ? 0 : v;
|
|
3101
|
-
/** This function gives the stable form of a real number: 12 significant digits, with `-0` as `0`. */
|
|
3102
|
-
const realForm = (v, digits = 12) => zero(Number(v.toPrecision(digits)));
|
|
3103
|
-
/** This function gives the stable form of a value of a param. */
|
|
3104
|
-
function normalize(p, v) {
|
|
3105
|
-
if (typeof v !== "number") return v;
|
|
3106
|
-
if (p.kind !== "real") return zero(v);
|
|
3107
|
-
return Math.min(p.max ?? Infinity, Math.max(p.min ?? -Infinity, realForm(v)));
|
|
3108
|
-
}
|
|
3109
|
-
/** This function tells if a value is on the step of a param. */
|
|
3110
|
-
function onStep(p, v) {
|
|
3111
|
-
if (p.step === void 0 || p.min === void 0) return true;
|
|
3112
|
-
const q = (v - p.min) / p.step;
|
|
3113
|
-
return Math.abs(q - Math.round(q)) <= 1e-9 * Math.max(1, Math.abs(q));
|
|
3114
|
-
}
|
|
3115
|
-
/** This function gives the problem of a value of a param, or `undefined` for a value in the range. */
|
|
3116
|
-
function valueProblem(p, v) {
|
|
3117
|
-
if (p.kind === "bool") return typeof v === "boolean" ? void 0 : `${show(v)} is not true or false`;
|
|
3118
|
-
if (p.kind === "enum") return (p.values ?? []).some((x) => x === v) ? void 0 : `${show(v)} is not one of ${(p.values ?? []).map(show).join(", ")}`;
|
|
3119
|
-
if (typeof v !== "number" || !Number.isFinite(v)) return `${show(v)} is not a finite number`;
|
|
3120
|
-
if (p.kind === "int" && !Number.isInteger(v)) return `${v} is not a whole number`;
|
|
3121
|
-
if (p.min !== void 0 && v < p.min) return `${v} is less than the min ${p.min}`;
|
|
3122
|
-
if (p.max !== void 0 && v > p.max) return `${v} is more than the max ${p.max}`;
|
|
3123
|
-
if (!onStep(p, v)) return `${v} is not on the step ${p.step} from ${p.min}`;
|
|
3124
|
-
}
|
|
3125
|
-
/** This function tells if a param is active for the values of the earlier params. */
|
|
3126
|
-
function isActive(p, values) {
|
|
3127
|
-
if (p.when === void 0) return true;
|
|
3128
|
-
if (!Object.hasOwn(values, p.when.param)) return false;
|
|
3129
|
-
const v = values[p.when.param];
|
|
3130
|
-
return p.when.in.some((x) => x === v);
|
|
3131
|
-
}
|
|
3132
|
-
/** This function gives the problem of a value of a param, or `undefined` for a value in its range. The corpus dimensions of `families.ts` use it. */
|
|
3133
|
-
function paramProblem(param, value) {
|
|
3134
|
-
return valueProblem(param, value);
|
|
3135
|
-
}
|
|
3136
|
-
/** This function tells if a param is active for the values of the earlier params. A param without `when` is always active. */
|
|
3137
|
-
function paramActive(param, values) {
|
|
3138
|
-
return isActive(param, values);
|
|
3139
|
-
}
|
|
3140
|
-
/** This function gives the stable form of a value of a param: a real number gets 12 significant digits, and `-0` becomes `0`. */
|
|
3141
|
-
function paramValue(param, value) {
|
|
3142
|
-
return normalize(param, value);
|
|
3143
|
-
}
|
|
3144
|
-
/** This function gives the part of a space that has a stage, and the prefix of the names of its stages. */
|
|
3145
|
-
function partOf(space, stage) {
|
|
3146
|
-
for (const part of space.parts ?? []) {
|
|
3147
|
-
const head = `${part.engine}.`;
|
|
3148
|
-
if (stage.startsWith(head)) {
|
|
3149
|
-
const inner = partOf(part, stage.slice(head.length));
|
|
3150
|
-
return {
|
|
3151
|
-
space: inner.space,
|
|
3152
|
-
prefix: head + inner.prefix
|
|
3153
|
-
};
|
|
3154
|
-
}
|
|
3155
|
-
}
|
|
3156
|
-
return {
|
|
3157
|
-
space,
|
|
3158
|
-
prefix: ""
|
|
3159
|
-
};
|
|
3160
|
-
}
|
|
3161
|
-
/** This function gives the prefix of the innermost part of each stage. */
|
|
3162
|
-
const prefixes = (space) => new Map(space.stages.map((s) => [s.name, partOf(space, s.name).prefix]));
|
|
3163
|
-
const describe = (c) => {
|
|
3164
|
-
const ref = (r) => `${r.stage}=${r.option ?? "null"}`;
|
|
3165
|
-
return "not" in c.then ? `if ${ref(c.if)} then not ${ref(c.then.not)}` : `if ${ref(c.if)} then ${ref(c.then)}`;
|
|
3166
|
-
};
|
|
3167
|
-
/** This function gives the problems of the range and the default of one param. */
|
|
3168
|
-
function paramProblems(p, at) {
|
|
3169
|
-
const out = [];
|
|
3170
|
-
if (isNumeric(p)) {
|
|
3171
|
-
const { min, max, step } = p;
|
|
3172
|
-
if (typeof min !== "number" || typeof max !== "number" || !Number.isFinite(min) || !Number.isFinite(max)) {
|
|
3173
|
-
out.push(["range", `${at} has no finite min and max`]);
|
|
3174
|
-
return out;
|
|
3175
|
-
}
|
|
3176
|
-
if (min > max) out.push(["range", `${at} has an empty range: the min ${min} is more than the max ${max}`]);
|
|
3177
|
-
if (p.kind === "int" && (!Number.isInteger(min) || !Number.isInteger(max))) out.push(["range", `${at} is an int with a min or a max that is not whole`]);
|
|
3178
|
-
if (step !== void 0 && (!Number.isFinite(step) || step <= 0 || p.kind === "int" && !Number.isInteger(step))) out.push(["range", `${at} has the step ${step}, which is not a ${p.kind === "int" ? "whole " : ""}number above 0`]);
|
|
3179
|
-
if (p.scale === "log" && min <= 0) out.push(["scale", `${at} has the scale log with the min ${min}, which is not above 0`]);
|
|
3180
|
-
} else {
|
|
3181
|
-
if (p.scale !== void 0) out.push(["scale", `${at} is a ${p.kind} param with a scale`]);
|
|
3182
|
-
if (p.kind === "enum") {
|
|
3183
|
-
if (!Array.isArray(p.values) || p.values.length === 0) out.push(["values", `${at} is an enum without values`]);
|
|
3184
|
-
else {
|
|
3185
|
-
const seen = /* @__PURE__ */ new Set();
|
|
3186
|
-
for (const v of p.values) {
|
|
3187
|
-
if (seen.has(v)) out.push(["duplicate", `${at} has the value ${show(v)} two times`]);
|
|
3188
|
-
seen.add(v);
|
|
3189
|
-
}
|
|
3190
|
-
}
|
|
3191
|
-
}
|
|
3192
|
-
}
|
|
3193
|
-
if (out.length === 0) {
|
|
3194
|
-
const problem = valueProblem(p, p.default);
|
|
3195
|
-
if (problem !== void 0) out.push(["default", `the default of ${at} is not in its range: ${problem}`]);
|
|
3196
|
-
}
|
|
3197
|
-
return out;
|
|
3198
|
-
}
|
|
3199
|
-
/** This function gives the problems of one stage option. */
|
|
3200
|
-
function optionProblems(stage, o) {
|
|
3201
|
-
const out = [];
|
|
3202
|
-
const at = `the option ${stage.name}.${o.id}`;
|
|
3203
|
-
if (!Array.isArray(o.params)) return [["config", `${at} has no list of params`]];
|
|
3204
|
-
const names = o.params.map((p) => p.name);
|
|
3205
|
-
o.params.forEach((p, i) => {
|
|
3206
|
-
const pat = `the param ${stage.name}.${o.id}.${p.name}`;
|
|
3207
|
-
if (names.indexOf(p.name) !== i) out.push(["duplicate", `${at} has the param ${p.name} two times`]);
|
|
3208
|
-
out.push(...paramProblems(p, pat));
|
|
3209
|
-
if (p.when === void 0) return;
|
|
3210
|
-
const j = names.indexOf(p.when.param);
|
|
3211
|
-
if (j < 0) out.push(["unknown", `${pat} has a when with the unknown param ${p.when.param}`]);
|
|
3212
|
-
else if (j >= i) out.push(["when", `${pat} has a when with the param ${p.when.param}, which does not come before it`]);
|
|
3213
|
-
else if (!Array.isArray(p.when.in) || p.when.in.length === 0) out.push(["when", `${pat} has a when with no values`]);
|
|
3214
|
-
else for (const v of p.when.in) {
|
|
3215
|
-
const problem = valueProblem(o.params[j], v);
|
|
3216
|
-
if (problem !== void 0) out.push(["when", `${pat} has a when value that is not a value of ${p.when.param}: ${problem}`]);
|
|
3217
|
-
}
|
|
3218
|
-
});
|
|
3219
|
-
if (o.supports !== void 0) out.push(...supportsProblems(o.supports, at));
|
|
3220
|
-
if (o.members !== void 0) {
|
|
3221
|
-
const { min, max, ordered } = o.members;
|
|
3222
|
-
const noList = min === 0 && max === 0;
|
|
3223
|
-
if (!Number.isInteger(min) || !Number.isInteger(max) || !noList && (min < 1 || min > max) || typeof ordered !== "boolean") out.push(["members", `${at} has a member rule without whole counts 1 <= min <= max or min = max = 0, and a boolean ordered`]);
|
|
3224
|
-
if (noList && (o.members.pool !== void 0 || o.members.view !== void 0)) out.push(["members", `${at} has no member list, so its member rule cannot have a pool or a view`]);
|
|
3225
|
-
const pool = o.members.pool;
|
|
3226
|
-
if (pool !== void 0 && (!Array.isArray(pool) || pool.length === 0 || !pool.every((entry) => Array.isArray(entry)))) out.push(["pool", `${at} has a pool that is not a list of one or more member lists`]);
|
|
3227
|
-
const view = o.members.view;
|
|
3228
|
-
if (view !== void 0 && view !== "bounds") out.push(["view", `${at} has the member view ${show(view)}, not "bounds"`]);
|
|
3229
|
-
}
|
|
3230
|
-
return out;
|
|
3231
|
-
}
|
|
3232
|
-
/** This function gives the problems of the `supports` of an option. */
|
|
3233
|
-
function supportsProblems(supports, at) {
|
|
3234
|
-
if (!isObject(supports)) return [["shape", `the supports of ${at} are not an object`]];
|
|
3235
|
-
const shape = supports["shape"];
|
|
3236
|
-
if (shape === void 0) return [];
|
|
3237
|
-
if (!Array.isArray(shape) || shape.length === 0) return [["shape", `${at} supports no shape kind. Remove shape to support each kind`]];
|
|
3238
|
-
const out = [];
|
|
3239
|
-
shape.forEach((k, i) => {
|
|
3240
|
-
if (!isShapeKind(k)) out.push(["shape", `${at} supports the shape kind ${show(k)}, which is not one of ${SHAPE_KIND_IDS.join(", ")}`]);
|
|
3241
|
-
else if (shape.indexOf(k) !== i) out.push(["duplicate", `${at} has the shape kind ${k} two times`]);
|
|
3242
|
-
});
|
|
3243
|
-
return out;
|
|
3244
|
-
}
|
|
3245
|
-
/** This function tells if an option supports each shape kind of the list. */
|
|
3246
|
-
const supportsShapes = (o, shapes) => shapes === void 0 || o.supports?.shape === void 0 || shapes.every((k) => o.supports.shape.includes(k));
|
|
3247
|
-
/** The shape kinds that the members of an option with `view: "bounds"` see: a bounding box is a `rect`. */
|
|
3248
|
-
const BOUNDS_VIEW = /* @__PURE__ */ Object.freeze(["rect"]);
|
|
3249
|
-
/**
|
|
3250
|
-
* This function gives the shape kinds of the members of a meta option. With `view: "bounds"`, a list with a kind becomes `BOUNDS_VIEW`.
|
|
3251
|
-
* Without a list, or with an empty list, the value does not change.
|
|
3252
|
-
*/
|
|
3253
|
-
const memberShapes = (rule, shapes) => rule.view === "bounds" && shapes !== void 0 && shapes.length > 0 ? BOUNDS_VIEW : shapes;
|
|
3254
|
-
/** This function tells if two lists have the same shape kinds. */
|
|
3255
|
-
const sameKinds = (a, b) => a.every((k) => b.includes(k)) && b.every((k) => a.includes(k));
|
|
3256
|
-
/** This function gives the problems of the params of one stage choice. */
|
|
3257
|
-
function choiceParamProblems(o, params, at) {
|
|
3258
|
-
if (params !== void 0 && !isObject(params)) return [["config", `the params of ${at} are not an object`]];
|
|
3259
|
-
const given = params ?? {};
|
|
3260
|
-
const out = [];
|
|
3261
|
-
for (const key of Object.keys(given)) if (!o.params.some((p) => p.name === key)) out.push(["unknown", `${at} has the unknown param ${key}`]);
|
|
3262
|
-
const values = {};
|
|
3263
|
-
for (const p of o.params) {
|
|
3264
|
-
const has = Object.hasOwn(given, p.name);
|
|
3265
|
-
if (!isActive(p, values)) {
|
|
3266
|
-
if (has) out.push(["inactive", `${at} sets the param ${p.name}, which is not active`]);
|
|
3267
|
-
continue;
|
|
3268
|
-
}
|
|
3269
|
-
const v = has ? given[p.name] : p.default;
|
|
3270
|
-
const problem = valueProblem(p, v);
|
|
3271
|
-
if (problem !== void 0) out.push(["param", `the param ${p.name} of ${at} is not in its range: ${problem}`]);
|
|
3272
|
-
else values[p.name] = normalize(p, v);
|
|
3273
|
-
}
|
|
3274
|
-
return out;
|
|
3275
|
-
}
|
|
3276
|
-
/** This function gives the problems of a config of a space. A member of a meta option has `member: true`. */
|
|
3277
|
-
function configProblems(space, config, member, shapes) {
|
|
3278
|
-
if (!isObject(config) || !isObject(config["stages"])) return [["config", "the config is not an object with stages"]];
|
|
3279
|
-
const out = [];
|
|
3280
|
-
const stages = config["stages"];
|
|
3281
|
-
if (config["space"] !== space.engine) out.push(["space", `the space of the config is ${show(config["space"])}, not ${show(space.engine)}`]);
|
|
3282
|
-
for (const key of Object.keys(stages)) if (!space.stages.some((s) => s.name === key)) out.push(["unknown", `the config has the unknown stage ${key}`]);
|
|
3283
|
-
const optionOf = (s) => {
|
|
3284
|
-
const c = stages[s.name];
|
|
3285
|
-
return isObject(c) ? s.options.find((o) => o.id === c["option"]) : void 0;
|
|
3286
|
-
};
|
|
3287
|
-
const prefix = prefixes(space);
|
|
3288
|
-
const metaStage = /* @__PURE__ */ new Map();
|
|
3289
|
-
for (const s of space.stages) {
|
|
3290
|
-
if (optionOf(s)?.members === void 0) continue;
|
|
3291
|
-
const p = prefix.get(s.name);
|
|
3292
|
-
if (metaStage.has(p)) out.push(["meta", `the stages ${metaStage.get(p)} and ${s.name} both have a meta option`]);
|
|
3293
|
-
else metaStage.set(p, s.name);
|
|
3294
|
-
}
|
|
3295
|
-
const chosen = /* @__PURE__ */ new Map();
|
|
3296
|
-
for (const s of space.stages) {
|
|
3297
|
-
const c = Object.hasOwn(stages, s.name) ? stages[s.name] : void 0;
|
|
3298
|
-
const meta = metaStage.get(prefix.get(s.name));
|
|
3299
|
-
chosen.set(s.name, null);
|
|
3300
|
-
if (c === void 0 || c === null) {
|
|
3301
|
-
if (!s.optional && meta === void 0) out.push(["missing", `the config has no option for the stage ${s.name}`]);
|
|
3302
|
-
continue;
|
|
3303
|
-
}
|
|
3304
|
-
if (!isObject(c) || typeof c["option"] !== "string") {
|
|
3305
|
-
out.push(["config", `the choice of the stage ${s.name} has no option`]);
|
|
3306
|
-
continue;
|
|
3307
|
-
}
|
|
3308
|
-
const o = s.options.find((x) => x.id === c["option"]);
|
|
3309
|
-
if (o === void 0) {
|
|
3310
|
-
out.push(["unknown", `the stage ${s.name} has no option ${c["option"]}`]);
|
|
3311
|
-
continue;
|
|
3312
|
-
}
|
|
3313
|
-
chosen.set(s.name, o.id);
|
|
3314
|
-
const at = `the option ${s.name}.${o.id}`;
|
|
3315
|
-
if (!supportsShapes(o, shapes)) {
|
|
3316
|
-
const missing = shapes.filter((k) => !o.supports.shape.includes(k));
|
|
3317
|
-
out.push(["shape", `${at} supports the shape kinds ${o.supports.shape.join(", ")}, not ${missing.join(", ")}`]);
|
|
3318
|
-
}
|
|
3319
|
-
if (meta !== void 0 && meta !== s.name) out.push(["meta", `the stage ${s.name} is set, but the meta option of ${meta} replaces the other stages of its part`]);
|
|
3320
|
-
out.push(...choiceParamProblems(o, c["params"], at));
|
|
3321
|
-
if (o.members === void 0) {
|
|
3322
|
-
if (c["members"] !== void 0) out.push(["members", `${at} is not a meta option, but the config gives members`]);
|
|
3323
|
-
continue;
|
|
3324
|
-
}
|
|
3325
|
-
if (member) {
|
|
3326
|
-
out.push(["depth", `a member has the meta option ${s.name}.${o.id}. The depth of a meta config is 1`]);
|
|
3327
|
-
continue;
|
|
3328
|
-
}
|
|
3329
|
-
out.push(...memberProblems(partOf(space, s.name).space, o.members, c["members"], at, memberShapes(o.members, shapes)));
|
|
3330
|
-
}
|
|
3331
|
-
for (const c of space.constraints ?? []) if (!holds(c, chosen)) out.push(["constraint", `the config breaks the constraint ${describe(c)}`]);
|
|
3332
|
-
return out;
|
|
3333
|
-
}
|
|
3334
|
-
/** This function gives the problems of the members of a meta option. `part` is the space of the members. */
|
|
3335
|
-
function memberProblems(part, rule, members, at, shapes) {
|
|
3336
|
-
if (!Array.isArray(members)) return [["members", `${at} is a meta option without a list of members`]];
|
|
3337
|
-
const out = [];
|
|
3338
|
-
if (members.length < rule.min || members.length > rule.max) out.push(["members", `${at} has ${members.length} members, not ${rule.min} to ${rule.max}`]);
|
|
3339
|
-
const ids = /* @__PURE__ */ new Map();
|
|
3340
|
-
members.forEach((m, k) => {
|
|
3341
|
-
const inner = configProblems(part, m, true, shapes);
|
|
3342
|
-
for (const [claim, detail] of inner) out.push([claim, `the member ${k} of ${at}: ${detail}`]);
|
|
3343
|
-
if (inner.length > 0) return;
|
|
3344
|
-
const id = idOf(canonicalOf(part, m, true));
|
|
3345
|
-
if (ids.has(id)) out.push(["duplicate", `the members ${ids.get(id)} and ${k} of ${at} are the same config ${id}`]);
|
|
3346
|
-
else ids.set(id, k);
|
|
3347
|
-
});
|
|
3348
|
-
if (rule.pool !== void 0 && out.length === 0) {
|
|
3349
|
-
const key = memberKey(part, rule, members);
|
|
3350
|
-
if (!rule.pool.some((entry) => memberKey(part, rule, entry) === key)) out.push(["pool", `${at} has a member list that is not in its pool of ${rule.pool.length} ${rule.pool.length === 1 ? "list" : "lists"}`]);
|
|
3351
|
-
}
|
|
3352
|
-
return out;
|
|
3353
|
-
}
|
|
3354
|
-
/** This function gives the key of a good member list: the `configId` of each member, sorted when the rule is not ordered. */
|
|
3355
|
-
function memberKey(part, rule, members) {
|
|
3356
|
-
const ids = members.map((m) => idOf(canonicalOf(part, m, true)));
|
|
3357
|
-
if (!rule.ordered) ids.sort();
|
|
3358
|
-
return ids.join(",");
|
|
3359
|
-
}
|
|
3360
|
-
/** This function gives the problems of the pool of a meta option of a good space. `part` is the space of the members. */
|
|
3361
|
-
function poolProblems(part, rule, at) {
|
|
3362
|
-
const out = [];
|
|
3363
|
-
const keys = /* @__PURE__ */ new Map();
|
|
3364
|
-
const plain = {
|
|
3365
|
-
min: rule.min,
|
|
3366
|
-
max: rule.max,
|
|
3367
|
-
ordered: rule.ordered
|
|
3368
|
-
};
|
|
3369
|
-
(rule.pool ?? []).forEach((entry, j) => {
|
|
3370
|
-
const inner = memberProblems(part, plain, entry, `the pool entry ${j} of ${at}`);
|
|
3371
|
-
out.push(...inner);
|
|
3372
|
-
if (inner.length > 0) return;
|
|
3373
|
-
const key = memberKey(part, rule, entry);
|
|
3374
|
-
if (keys.has(key)) out.push(["duplicate", `the pool entries ${keys.get(key)} and ${j} of ${at} have the same members`]);
|
|
3375
|
-
else keys.set(key, j);
|
|
3376
|
-
});
|
|
3377
|
-
return out;
|
|
3378
|
-
}
|
|
3379
|
-
/** This function tells if a member list fits a space: each member is a good config of the space. A shape filter removes options from the space. */
|
|
3380
|
-
const fits = (part, entry) => entry.every((m) => configProblems(part, m, true).length === 0);
|
|
3381
|
-
const toBreaks = (problems, inputs = []) => problems.map(([claim, detail]) => ({
|
|
3382
|
-
claim,
|
|
3383
|
-
detail,
|
|
3384
|
-
inputs
|
|
3385
|
-
}));
|
|
3386
|
-
/**
|
|
3387
|
-
* This function checks one config of a space. It gives one break for each problem. An empty list tells that the config is good.
|
|
3388
|
-
* The `claim` of a break is the kind of the problem, for example `missing`, `unknown`, `inactive`, `param`, `constraint`, `members` or `depth`.
|
|
3389
|
-
*
|
|
3390
|
-
* A member list that is not an entry of the `pool` of its option gives the claim `pool`.
|
|
3391
|
-
* With `shapes`, an option that does not support a kind of the list gives the claim `shape`. A member of a meta option gets the same check.
|
|
3392
|
-
* A member of an option with `view: "bounds"` gets the check with the kind `rect`.
|
|
3393
|
-
*/
|
|
3394
|
-
function checkConfig(space, config, options = {}) {
|
|
3395
|
-
return toBreaks(configProblems(space, config, false, options.shapes));
|
|
3396
|
-
}
|
|
3397
|
-
/**
|
|
3398
|
-
* This function checks a space. It gives one break for each problem. An empty list tells that the space is good.
|
|
3399
|
-
* It finds duplicate names, empty ranges, defaults out of range, enums without values, bad log scales, unknown names and bad named configs.
|
|
3400
|
-
* It also checks each entry of a `pool`: the count of its members, each member and a repeated entry.
|
|
3401
|
-
* The `inputs` of a break of a named config have the name of the config.
|
|
3402
|
-
*/
|
|
3403
|
-
function checkSpace(space) {
|
|
3404
|
-
if (!isObject(space) || !Array.isArray(space.stages)) return toBreaks([["schema", "the space is not an object with stages"]]);
|
|
3405
|
-
const out = [];
|
|
3406
|
-
if (space.schema !== "strategy-space/1") out.push(["schema", `the schema is ${show(space.schema)}, not ${STRATEGY_SPACE_SCHEMA}`]);
|
|
3407
|
-
if (typeof space.engine !== "string" || space.engine === "") out.push(["schema", "the space has no engine name"]);
|
|
3408
|
-
if (typeof space.version !== "string") out.push(["schema", "the space has no version"]);
|
|
3409
|
-
const names = space.stages.map((s) => s.name);
|
|
3410
|
-
space.stages.forEach((s, i) => {
|
|
3411
|
-
if (names.indexOf(s.name) !== i) out.push(["duplicate", `the space has the stage ${s.name} two times`]);
|
|
3412
|
-
const options = Array.isArray(s.options) ? s.options : [];
|
|
3413
|
-
if (options.length === 0) {
|
|
3414
|
-
out.push(["options", `the stage ${s.name} has no options`]);
|
|
3415
|
-
return;
|
|
3416
|
-
}
|
|
3417
|
-
const ids = options.map((o) => o.id);
|
|
3418
|
-
options.forEach((o, j) => {
|
|
3419
|
-
if (ids.indexOf(o.id) !== j) out.push(["duplicate", `the stage ${s.name} has the option ${o.id} two times`]);
|
|
3420
|
-
out.push(...optionProblems(s, o));
|
|
3421
|
-
});
|
|
3422
|
-
if (!s.optional && options.every((o) => o.members !== void 0)) out.push(["meta", `the stage ${s.name} is not optional and has only meta options, so no base config exists`]);
|
|
3423
|
-
});
|
|
3424
|
-
const known = (ref, at) => {
|
|
3425
|
-
const s = space.stages.find((x) => x.name === ref?.stage);
|
|
3426
|
-
if (s === void 0) out.push(["unknown", `${at} has the unknown stage ${ref?.stage}`]);
|
|
3427
|
-
else if (ref.option === null ? !s.optional : !s.options.some((o) => o.id === ref.option)) out.push(["unknown", `${at} has the unknown option ${s.name}=${ref.option ?? "null"}`]);
|
|
3428
|
-
};
|
|
3429
|
-
(space.constraints ?? []).forEach((c, k) => {
|
|
3430
|
-
known(c.if, `the constraint ${k}`);
|
|
3431
|
-
known("not" in c.then ? c.then.not : c.then, `the constraint ${k}`);
|
|
3432
|
-
});
|
|
3433
|
-
for (const part of space.parts ?? []) for (const b of checkSpace(part)) out.push([b.claim, `the part ${part.engine}: ${b.detail}`]);
|
|
3434
|
-
if (space.parts !== void 0) {
|
|
3435
|
-
for (const s of space.stages) if (partOf(space, s.name).prefix === "") out.push(["parts", `the stage ${s.name} is not a stage of a part`]);
|
|
3436
|
-
}
|
|
3437
|
-
const breaks = toBreaks(out);
|
|
3438
|
-
if (breaks.length > 0) return breaks;
|
|
3439
|
-
for (const s of space.stages) for (const o of s.options) if (o.members?.pool !== void 0) breaks.push(...toBreaks(poolProblems(partOf(space, s.name).space, o.members, `the option ${s.name}.${o.id}`)));
|
|
3440
|
-
if (breaks.length > 0) return breaks;
|
|
3441
|
-
for (const [name, config] of Object.entries(space.named ?? {})) breaks.push(...toBreaks(configProblems(space, config, false).map(([c, d]) => [c, `the named config ${name}: ${d}`]), [name]));
|
|
3442
|
-
return breaks;
|
|
3443
|
-
}
|
|
3444
|
-
/** This function gives the canonical form of a config that is known to be good. */
|
|
3445
|
-
function canonicalOf(space, config, member) {
|
|
3446
|
-
const stages = {};
|
|
3447
|
-
for (const s of space.stages) {
|
|
3448
|
-
const c = Object.hasOwn(config.stages, s.name) ? config.stages[s.name] : void 0;
|
|
3449
|
-
if (c === void 0 || c === null) {
|
|
3450
|
-
stages[s.name] = null;
|
|
3451
|
-
continue;
|
|
3452
|
-
}
|
|
3453
|
-
const o = s.options.find((x) => x.id === c.option);
|
|
3454
|
-
const given = c.params ?? {};
|
|
3455
|
-
const params = {};
|
|
3456
|
-
for (const p of o.params) if (isActive(p, params)) params[p.name] = normalize(p, Object.hasOwn(given, p.name) ? given[p.name] : p.default);
|
|
3457
|
-
if (o.members === void 0 || member) {
|
|
3458
|
-
stages[s.name] = {
|
|
3459
|
-
option: o.id,
|
|
3460
|
-
params
|
|
3461
|
-
};
|
|
3462
|
-
continue;
|
|
3463
|
-
}
|
|
3464
|
-
const part = partOf(space, s.name).space;
|
|
3465
|
-
const members = (c.members ?? []).map((m) => canonicalOf(part, m, true));
|
|
3466
|
-
if (!o.members.ordered) members.sort((a, b) => idOf(a) < idOf(b) ? -1 : idOf(a) > idOf(b) ? 1 : 0);
|
|
3467
|
-
stages[s.name] = {
|
|
3468
|
-
option: o.id,
|
|
3469
|
-
params,
|
|
3470
|
-
members
|
|
3471
|
-
};
|
|
3472
|
-
}
|
|
3473
|
-
return {
|
|
3474
|
-
space: space.engine,
|
|
3475
|
-
stages
|
|
3476
|
-
};
|
|
3477
|
-
}
|
|
3478
|
-
const idOf = (canonical) => sha256Hex(canonicalJson(canonical)).slice(0, 16);
|
|
3479
|
-
/**
|
|
3480
|
-
* This function gives the canonical form of a config: each stage of the space, the active params only, the defaults, and stable numbers.
|
|
3481
|
-
* An unused optional stage is `null`. The members of an unordered meta option are sorted by their `configId`.
|
|
3482
|
-
* It throws an `Error` for a config with a break of `checkConfig`.
|
|
3483
|
-
*/
|
|
3484
|
-
function canonicalConfig(space, config) {
|
|
3485
|
-
const problems = configProblems(space, config, false);
|
|
3486
|
-
if (problems.length > 0) throw new Error(`The config of the space ${space.engine} is not good: ${problems.map((p) => p[1]).join("; ")}.`);
|
|
3487
|
-
return canonicalOf(space, config, false);
|
|
3488
|
-
}
|
|
3489
|
-
/**
|
|
3490
|
-
* This function gives the id of a config: the first 16 hex characters of the SHA-256 of `canonicalJson` of `canonicalConfig`.
|
|
3491
|
-
* Two spellings of one config get one id. The version of the space is not part of the id.
|
|
3492
|
-
*/
|
|
3493
|
-
function configId(space, config) {
|
|
3494
|
-
return idOf(canonicalConfig(space, config));
|
|
3495
|
-
}
|
|
3496
|
-
/** This function throws an `Error` for a space with a break of `checkSpace`. */
|
|
3497
|
-
function assertSpace(space) {
|
|
3498
|
-
const breaks = checkSpace(space);
|
|
3499
|
-
if (breaks.length > 0) throw new Error(`The space ${String(space?.engine)} is not good: ${breaks.map((b) => b.detail).join("; ")}.`);
|
|
3500
|
-
}
|
|
3501
|
-
/** This function throws a `RangeError` when a value is not a whole number of `min` or more. */
|
|
3502
|
-
function assertWhole(value, name, min) {
|
|
3503
|
-
if (!Number.isInteger(value) || value < min) throw new RangeError(`The option ${name} is ${value}, not a whole number of ${min} or more.`);
|
|
3504
|
-
}
|
|
3505
|
-
/** This function gives the last point of the step grid of a numeric param. */
|
|
3506
|
-
function lastPoint(p) {
|
|
3507
|
-
const min = p.min;
|
|
3508
|
-
const step = p.step ?? (p.kind === "int" ? 1 : void 0);
|
|
3509
|
-
if (step === void 0) return p.max;
|
|
3510
|
-
return min + Math.floor((p.max - min) / step + 1e-9) * step;
|
|
3511
|
-
}
|
|
3512
|
-
/** This function moves a value of a numeric param onto its step and into its range. */
|
|
3513
|
-
function snap(p, x, digits) {
|
|
3514
|
-
const min = p.min;
|
|
3515
|
-
const step = p.step ?? (p.kind === "int" ? 1 : void 0);
|
|
3516
|
-
let v = step === void 0 ? x : min + Math.round((x - min) / step) * step;
|
|
3517
|
-
v = Math.min(lastPoint(p), Math.max(min, v));
|
|
3518
|
-
return p.kind === "int" ? zero(Math.round(v)) : Math.min(p.max, Math.max(min, realForm(v, digits)));
|
|
3519
|
-
}
|
|
3520
|
-
/** This function gives the point at the fraction `t` of the range of a numeric param, on its scale. */
|
|
3521
|
-
function at01(p, t) {
|
|
3522
|
-
const min = p.min;
|
|
3523
|
-
const max = p.max;
|
|
3524
|
-
if (p.scale === "log") return Math.exp(Math.log(min) + t * (Math.log(max) - Math.log(min)));
|
|
3525
|
-
return min + t * (max - min);
|
|
3526
|
-
}
|
|
3527
|
-
/**
|
|
3528
|
-
* This function gives the grid values of one param, in increasing order. An `enum` gives its values, and a `bool` gives `false` and `true`.
|
|
3529
|
-
* With 1 level, a numeric param gives its default. With k levels, it gives k points from `min` to `max` on its scale and step, without repeats.
|
|
3530
|
-
*/
|
|
3531
|
-
function paramLevels(param, levels) {
|
|
3532
|
-
assertWhole(levels, "levels", 1);
|
|
3533
|
-
if (param.kind === "bool") return [false, true];
|
|
3534
|
-
if (param.kind === "enum") return [...param.values ?? []];
|
|
3535
|
-
if (levels === 1 || param.min === param.max) return [normalize(param, param.default)];
|
|
3536
|
-
const out = [];
|
|
3537
|
-
const steps = param.kind === "int" || param.step !== void 0 ? Math.round((lastPoint(param) - param.min) / (param.step ?? 1)) + 1 : Infinity;
|
|
3538
|
-
if (steps <= levels && param.scale !== "log") for (let j = 0; j < steps; j++) out.push(snap(param, param.min + j * (param.step ?? 1), 12));
|
|
3539
|
-
else for (let j = 0; j < levels; j++) out.push(snap(param, at01(param, j / (levels - 1)), 12));
|
|
3540
|
-
return [...new Set(out)];
|
|
3541
|
-
}
|
|
3542
|
-
/** This function tells if a later param of the list reads a param in its `when`. */
|
|
3543
|
-
const readLater = (params, i) => params.slice(i + 1).some((q) => q.when?.param === params[i].name);
|
|
3544
|
-
/** This function gives the count of the grid assignments of the params of an option, with only the active params. */
|
|
3545
|
-
function countParams(params, levels, i = 0, values = {}) {
|
|
3546
|
-
if (i === params.length) return 1n;
|
|
3547
|
-
const p = params[i];
|
|
3548
|
-
if (!isActive(p, values)) return countParams(params, levels, i + 1, values);
|
|
3549
|
-
const lv = paramLevels(p, levels);
|
|
3550
|
-
if (!readLater(params, i)) return BigInt(lv.length) * countParams(params, levels, i + 1, values);
|
|
3551
|
-
let sum = 0n;
|
|
3552
|
-
for (const v of lv) sum += countParams(params, levels, i + 1, {
|
|
3553
|
-
...values,
|
|
3554
|
-
[p.name]: v
|
|
3555
|
-
});
|
|
3556
|
-
return sum;
|
|
3557
|
-
}
|
|
3558
|
-
/** This function gives the grid assignments of the params of an option, in a stable order. */
|
|
3559
|
-
function* gridParams(params, levels, i = 0, values = {}) {
|
|
3560
|
-
if (i === params.length) {
|
|
3561
|
-
yield { ...values };
|
|
3562
|
-
return;
|
|
3563
|
-
}
|
|
3564
|
-
const p = params[i];
|
|
3565
|
-
if (!isActive(p, values)) {
|
|
3566
|
-
yield* gridParams(params, levels, i + 1, values);
|
|
3567
|
-
return;
|
|
3568
|
-
}
|
|
3569
|
-
for (const v of paramLevels(p, levels)) yield* gridParams(params, levels, i + 1, {
|
|
3570
|
-
...values,
|
|
3571
|
-
[p.name]: v
|
|
3572
|
-
});
|
|
3573
|
-
}
|
|
3574
|
-
const optionIds = (choices) => new Map([...choices].map(([k, o]) => [k, o?.id ?? null]));
|
|
3575
|
-
const keeps = (space, choices) => (space.constraints ?? []).every((c) => holds(c, optionIds(choices)));
|
|
3576
|
-
/**
|
|
3577
|
-
* This function gives the constraints of a space for each stage: a constraint goes to the last stage that it names.
|
|
3578
|
-
* The last list holds each constraint that names a stage outside the space, so a walk examines it at the end.
|
|
3579
|
-
*/
|
|
3580
|
-
function constraintsByStage(space) {
|
|
3581
|
-
const place = new Map(space.stages.map((s, i) => [s.name, i]));
|
|
3582
|
-
const lists = Array.from({ length: space.stages.length + 1 }, () => []);
|
|
3583
|
-
for (const c of space.constraints ?? []) {
|
|
3584
|
-
const named = [c.if.stage, "not" in c.then ? c.then.not.stage : c.then.stage];
|
|
3585
|
-
lists[Math.max(...named.map((name) => place.get(name) ?? space.stages.length))].push(c);
|
|
3586
|
-
}
|
|
3587
|
-
return lists;
|
|
3588
|
-
}
|
|
3589
|
-
/** This function gives the count of the member lists of a meta option without a pool from `base` base configs, or `null` above `limit`. */
|
|
3590
|
-
function memberLists(base, rule, limit) {
|
|
3591
|
-
let sum = 0n;
|
|
3592
|
-
for (let m = rule.min; m <= rule.max; m++) {
|
|
3593
|
-
let n = 1n;
|
|
3594
|
-
for (let k = 0; k < m; k++) {
|
|
3595
|
-
n = n * (base - BigInt(k)) / (rule.ordered ? 1n : BigInt(k + 1));
|
|
3596
|
-
if (n <= 0n) break;
|
|
3597
|
-
}
|
|
3598
|
-
if (n <= 0n) break;
|
|
3599
|
-
sum += n;
|
|
3600
|
-
if (sum > limit) return null;
|
|
3601
|
-
}
|
|
3602
|
-
return sum;
|
|
3603
|
-
}
|
|
3604
|
-
/** This function multiplies two counts. A `null` count or a product above `limit` gives `null`. */
|
|
3605
|
-
const times = (a, b, limit) => a === null || b === null ? null : a * b > limit ? null : a * b;
|
|
2877
|
+
/** This function gives the known shape kinds of a check: the list of the caller, or `SHAPE_KIND_IDS`. */
|
|
2878
|
+
const withKinds = (options) => ({
|
|
2879
|
+
kinds: SHAPE_KIND_IDS,
|
|
2880
|
+
...options
|
|
2881
|
+
});
|
|
3606
2882
|
/**
|
|
3607
|
-
* This function
|
|
3608
|
-
*
|
|
2883
|
+
* This function checks a space with `checkSpace` of `@mark1russell7/strategy-space`.
|
|
2884
|
+
* Without `kinds`, a shape kind of a `supports` that is not in `SHAPE_KIND_IDS` gives the claim `shape`.
|
|
3609
2885
|
*/
|
|
3610
|
-
function
|
|
3611
|
-
|
|
3612
|
-
let acc = [{
|
|
3613
|
-
choices: /* @__PURE__ */ new Map(),
|
|
3614
|
-
base: true,
|
|
3615
|
-
weight: 1n
|
|
3616
|
-
}];
|
|
3617
|
-
for (const part of space.parts) {
|
|
3618
|
-
const head = `${part.engine}.`;
|
|
3619
|
-
const own = combos(part, levels, meta, limit);
|
|
3620
|
-
const next = [];
|
|
3621
|
-
for (const a of acc) for (const b of own) {
|
|
3622
|
-
const choices = new Map(a.choices);
|
|
3623
|
-
for (const [k, o] of b.choices) choices.set(head + k, o);
|
|
3624
|
-
const base = a.base && b.base;
|
|
3625
|
-
next.push({
|
|
3626
|
-
choices,
|
|
3627
|
-
base,
|
|
3628
|
-
weight: base ? a.weight * b.weight : times(a.weight, b.weight, limit)
|
|
3629
|
-
});
|
|
3630
|
-
}
|
|
3631
|
-
acc = next;
|
|
3632
|
-
}
|
|
3633
|
-
return acc.filter((c) => keeps(space, c.choices));
|
|
3634
|
-
}
|
|
3635
|
-
const counts = /* @__PURE__ */ new Map();
|
|
3636
|
-
const countOf = (o) => {
|
|
3637
|
-
if (!counts.has(o)) counts.set(o, countParams(o.params, levels));
|
|
3638
|
-
return counts.get(o);
|
|
3639
|
-
};
|
|
3640
|
-
const out = [];
|
|
3641
|
-
const stages = space.stages;
|
|
3642
|
-
const checks = constraintsByStage(space);
|
|
3643
|
-
const ids = /* @__PURE__ */ new Map();
|
|
3644
|
-
const walk = (i, choices, weight) => {
|
|
3645
|
-
if (i === stages.length) {
|
|
3646
|
-
if (checks[i].every((c) => holds(c, ids))) out.push({
|
|
3647
|
-
choices: new Map(choices),
|
|
3648
|
-
base: true,
|
|
3649
|
-
weight
|
|
3650
|
-
});
|
|
3651
|
-
return;
|
|
3652
|
-
}
|
|
3653
|
-
const s = stages[i];
|
|
3654
|
-
const list = s.options.filter((o) => o.members === void 0);
|
|
3655
|
-
if (s.optional) list.push(null);
|
|
3656
|
-
for (const o of list) {
|
|
3657
|
-
choices.set(s.name, o);
|
|
3658
|
-
ids.set(s.name, o?.id ?? null);
|
|
3659
|
-
if (checks[i].every((c) => holds(c, ids))) walk(i + 1, choices, o === null ? weight : weight * countOf(o));
|
|
3660
|
-
}
|
|
3661
|
-
choices.delete(s.name);
|
|
3662
|
-
ids.delete(s.name);
|
|
3663
|
-
};
|
|
3664
|
-
walk(0, /* @__PURE__ */ new Map(), 1n);
|
|
3665
|
-
if (!meta) return out;
|
|
3666
|
-
const base = out.reduce((sum, c) => sum + c.weight, 0n);
|
|
3667
|
-
for (const s of stages) for (const o of s.options) {
|
|
3668
|
-
if (o.members === void 0) continue;
|
|
3669
|
-
const choices = new Map(stages.map((x) => [x.name, x === s ? o : null]));
|
|
3670
|
-
const own = memberSpaces.get(o);
|
|
3671
|
-
const from = own === void 0 ? base : baseCount(own, levels, limit);
|
|
3672
|
-
const lists = o.members.pool === void 0 ? memberLists(from, o.members, limit) : BigInt(o.members.pool.length);
|
|
3673
|
-
if (keeps(space, choices)) out.push({
|
|
3674
|
-
choices,
|
|
3675
|
-
base: false,
|
|
3676
|
-
weight: times(countOf(o), lists, limit)
|
|
3677
|
-
});
|
|
3678
|
-
}
|
|
3679
|
-
return out;
|
|
2886
|
+
function checkSpace(space, kindOptions = {}) {
|
|
2887
|
+
return checkSpace$1(space, withKinds(kindOptions));
|
|
3680
2888
|
}
|
|
3681
2889
|
/**
|
|
3682
|
-
* This function
|
|
3683
|
-
*
|
|
3684
|
-
* A meta option with a `pool` counts each entry of the pool one time.
|
|
3685
|
-
* With `shapes`, it counts only the options that support each kind of the list.
|
|
2890
|
+
* This function counts the grid points of a space with `spaceSize` of `@mark1russell7/strategy-space`.
|
|
2891
|
+
* Without `kinds`, a shape kind that is not in `SHAPE_KIND_IDS` causes an error.
|
|
3686
2892
|
*/
|
|
3687
2893
|
function spaceSize(space, options) {
|
|
3688
|
-
|
|
3689
|
-
assertWhole(options.levels, "levels", 1);
|
|
3690
|
-
assertShapes(options.shapes);
|
|
3691
|
-
return sizeOf(restrict(space, options.shapes), options.levels, options.maxMeta ?? 10n ** 18n);
|
|
2894
|
+
return spaceSize$1(space, withKinds(options));
|
|
3692
2895
|
}
|
|
3693
2896
|
/**
|
|
3694
|
-
* This function gives the
|
|
3695
|
-
*
|
|
3696
|
-
* A pool keeps only the entries whose members fit the smaller space. A meta option without such an entry goes out.
|
|
3697
|
-
*
|
|
3698
|
-
* The members of an option with `view: "bounds"` come from the space for the kinds of `memberShapes`. `memberSpaces` keeps that space.
|
|
3699
|
-
*/
|
|
3700
|
-
function restrict(space, shapes) {
|
|
3701
|
-
if (shapes === void 0) return space;
|
|
3702
|
-
const kept = {
|
|
3703
|
-
...space,
|
|
3704
|
-
stages: space.stages.map((s) => ({
|
|
3705
|
-
...s,
|
|
3706
|
-
options: s.options.filter((o) => supportsShapes(o, shapes))
|
|
3707
|
-
})),
|
|
3708
|
-
...space.parts === void 0 ? {} : { parts: space.parts.map((p) => restrict(p, shapes)) }
|
|
3709
|
-
};
|
|
3710
|
-
const withPool = (s, o) => {
|
|
3711
|
-
if (o.members === void 0) return [o];
|
|
3712
|
-
const inner = memberShapes(o.members, shapes);
|
|
3713
|
-
const own = sameKinds(inner, shapes) ? void 0 : restrict(partOf(space, s.name).space, inner);
|
|
3714
|
-
const mark = (option) => {
|
|
3715
|
-
if (own !== void 0) memberSpaces.set(option, own);
|
|
3716
|
-
return [option];
|
|
3717
|
-
};
|
|
3718
|
-
if (o.members.pool === void 0) return own === void 0 ? [o] : mark({ ...o });
|
|
3719
|
-
const part = own ?? partOf(kept, s.name).space;
|
|
3720
|
-
const pool = o.members.pool.filter((entry) => fits(part, entry));
|
|
3721
|
-
return pool.length === 0 ? [] : mark({
|
|
3722
|
-
...o,
|
|
3723
|
-
members: {
|
|
3724
|
-
...o.members,
|
|
3725
|
-
pool
|
|
3726
|
-
}
|
|
3727
|
-
});
|
|
3728
|
-
};
|
|
3729
|
-
return {
|
|
3730
|
-
...kept,
|
|
3731
|
-
stages: kept.stages.map((s) => ({
|
|
3732
|
-
...s,
|
|
3733
|
-
options: s.options.flatMap((o) => withPool(s, o))
|
|
3734
|
-
}))
|
|
3735
|
-
};
|
|
3736
|
-
}
|
|
3737
|
-
/** The space of the members of a meta option of a restricted space, when it is not the part of the option. Only `view: "bounds"` makes such a space. */
|
|
3738
|
-
const memberSpaces = /* @__PURE__ */ new WeakMap();
|
|
3739
|
-
/**
|
|
3740
|
-
* This function tells if an option of a restricted space has a member list. A base option and a meta option with a `pool` have one.
|
|
3741
|
-
* A different meta option has one when its members have a base config. `restrict` keeps only the pool entries that fit.
|
|
3742
|
-
*/
|
|
3743
|
-
const hasMembers = (space, stage, o) => o.members === void 0 || o.members.max === 0 || o.members.pool !== void 0 || combos(memberSpaces.get(o) ?? partOf(space, stage).space, 1, false, 0n).length > 0;
|
|
3744
|
-
/** This function tells if a restricted space has a config. A base config counts. With `meta`, a meta config counts when each of its meta options has a member list. */
|
|
3745
|
-
const hasConfig = (space, meta) => combos(space, 1, meta, 0n).some((c) => c.base || [...c.choices].every(([stage, o]) => o === null || hasMembers(space, stage, o)));
|
|
3746
|
-
/** This function gives the error of a sample of a space that has no config for the shape kinds `shapes`. `kept` is the restricted space. */
|
|
3747
|
-
function noSample(space, kept, shapes, meta) {
|
|
3748
|
-
const list = (names, one, more) => names.length === 1 ? `${one} ${names[0]}` : `${more} ${names.join(", ")}`;
|
|
3749
|
-
const empty = kept.stages.filter((s) => !s.optional && !s.options.some((o) => meta || o.members === void 0)).map((s) => s.name);
|
|
3750
|
-
const removed = !meta ? [] : space.stages.flatMap((s) => s.options.filter((o) => o.members !== void 0 && o.members.view !== "bounds" && supportsShapes(o, shapes)).filter((o) => {
|
|
3751
|
-
const own = kept.stages.find((x) => x.name === s.name)?.options.find((x) => x.id === o.id);
|
|
3752
|
-
return own === void 0 || !hasMembers(kept, s.name, own);
|
|
3753
|
-
}).map((o) => `${s.name}.${o.id}`));
|
|
3754
|
-
let text = `The space ${space.engine} has no config for a sample with the shape kinds ${shapes.join(", ")}.`;
|
|
3755
|
-
if (empty.length > 0) text += ` ${list(empty, "The stage", "The stages")} ${empty.length === 1 ? "has" : "have"} no option for these kinds.`;
|
|
3756
|
-
if (removed.length > 0) text += ` ${list(removed, "The meta option", "The meta options")} ${removed.length === 1 ? "has" : "have"} no member config for these kinds. Set members.view to "bounds" when the members see only the bounding boxes.`;
|
|
3757
|
-
if (empty.length === 0 && removed.length === 0) text += " The constraints remove each config.";
|
|
3758
|
-
return new RangeError(text);
|
|
3759
|
-
}
|
|
3760
|
-
/** This function gives a constraint in words, for the reason of an error. */
|
|
3761
|
-
function constraintText(c) {
|
|
3762
|
-
const ref = (r) => `${r.stage} ${r.option === null ? "has no option" : `is ${r.option}`}`;
|
|
3763
|
-
return "not" in c.then ? `if ${ref(c.if)}, then not ${ref(c.then.not)}` : `if ${ref(c.if)}, then ${ref(c.then)}`;
|
|
3764
|
-
}
|
|
3765
|
-
/**
|
|
3766
|
-
* This function throws a `RangeError` for a bad `fixed` option of a sample. A stage or an option that the space does not have is bad.
|
|
3767
|
-
* A meta option without `meta: "sample"` is bad, and so are two fixed options that a constraint forbids.
|
|
3768
|
-
*/
|
|
3769
|
-
function assertFixed(space, fixed, meta) {
|
|
3770
|
-
for (const [stage, id] of Object.entries(fixed)) {
|
|
3771
|
-
const s = space.stages.find((x) => x.name === stage);
|
|
3772
|
-
if (s === void 0) throw new RangeError(`The option fixed names the stage ${stage}, and the space ${space.engine} has no such stage.`);
|
|
3773
|
-
const o = s.options.find((x) => x.id === id);
|
|
3774
|
-
if (o === void 0) throw new RangeError(`The option fixed names the option ${String(id)} of the stage ${stage}, and the stage has no such option.`);
|
|
3775
|
-
if (o.members !== void 0 && !meta) throw new RangeError(`The option fixed names the meta option ${stage}.${id}, and a sample with meta skip has no meta option. Set meta to "sample".`);
|
|
3776
|
-
}
|
|
3777
|
-
const ids = new Map(Object.entries(fixed));
|
|
3778
|
-
const other = (c) => "not" in c.then ? c.then.not.stage : c.then.stage;
|
|
3779
|
-
const broken = (space.constraints ?? []).filter((c) => ids.has(c.if.stage) && ids.has(other(c)) && !holds(c, ids));
|
|
3780
|
-
if (broken.length > 0) {
|
|
3781
|
-
const names = Object.entries(fixed).map(([stage, id]) => `${stage}.${id}`);
|
|
3782
|
-
throw new RangeError(`The constraints of the space ${space.engine} forbid the fixed options ${names.join(", ")}. The broken constraints: ${broken.map(constraintText).join("; ")}.`);
|
|
3783
|
-
}
|
|
3784
|
-
}
|
|
3785
|
-
/**
|
|
3786
|
-
* This function gives the restricted space `kept` with only the fixed option at each fixed stage. A fixed stage that was optional stops being optional.
|
|
3787
|
-
* A meta option of another stage of the part of a fixed stage goes out. A sample gives no option to the other stages of the part of its meta option.
|
|
3788
|
-
*/
|
|
3789
|
-
function fixStages(space, kept, fixed, shapes) {
|
|
3790
|
-
const prefix = prefixes(kept);
|
|
3791
|
-
const parts = new Set(Object.keys(fixed).map((stage) => prefix.get(stage)));
|
|
3792
|
-
const stages = kept.stages.map((s) => {
|
|
3793
|
-
const id = fixed[s.name];
|
|
3794
|
-
if (id === void 0) return parts.has(prefix.get(s.name)) ? {
|
|
3795
|
-
...s,
|
|
3796
|
-
options: s.options.filter((o) => o.members === void 0)
|
|
3797
|
-
} : s;
|
|
3798
|
-
const options = s.options.filter((o) => o.id === id).map((o) => o.members === void 0 ? o : withMemberSpace(o, memberSpaces.get(o) ?? partOf(kept, s.name).space));
|
|
3799
|
-
if (options.length === 0) throw new RangeError(`The fixed option ${s.name}.${id} of the space ${space.engine} does not support the shape kinds ${(shapes ?? []).join(", ")}.`);
|
|
3800
|
-
return {
|
|
3801
|
-
...s,
|
|
3802
|
-
options,
|
|
3803
|
-
optional: false
|
|
3804
|
-
};
|
|
3805
|
-
});
|
|
3806
|
-
return {
|
|
3807
|
-
...kept,
|
|
3808
|
-
stages
|
|
3809
|
-
};
|
|
3810
|
-
}
|
|
3811
|
-
/** This function gives a copy of a meta option whose members come from `members`. */
|
|
3812
|
-
function withMemberSpace(o, members) {
|
|
3813
|
-
const copy = { ...o };
|
|
3814
|
-
memberSpaces.set(copy, members);
|
|
3815
|
-
return copy;
|
|
3816
|
-
}
|
|
3817
|
-
/** This function gives the error of a sample whose fixed options keep each constraint between them. The constraints with the other stages remove each config. */
|
|
3818
|
-
function noFixedSample(space, fixed) {
|
|
3819
|
-
const names = Object.entries(fixed).map(([stage, id]) => `${stage}.${id}`);
|
|
3820
|
-
return /* @__PURE__ */ new RangeError(`The space ${space.engine} has no config with the fixed options ${names.join(", ")}. No constraint between two fixed options breaks, so the constraints with the other stages remove each config.`);
|
|
3821
|
-
}
|
|
3822
|
-
/** This function gives the count of the base configs of a space that is known to be good. */
|
|
3823
|
-
const baseCount = (space, levels, limit) => combos(space, levels, false, limit).reduce((sum, c) => sum + c.weight, 0n);
|
|
3824
|
-
/** This function throws a `RangeError` for a list of shape kinds with a kind that is not declared. */
|
|
3825
|
-
function assertShapes(shapes) {
|
|
3826
|
-
if (shapes === void 0) return;
|
|
3827
|
-
if (!Array.isArray(shapes)) throw new RangeError("The option shapes is not a list of shape kinds.");
|
|
3828
|
-
for (const k of shapes) if (!isShapeKind(k)) throw new RangeError(`The shape kind ${show(k)} is not one of ${SHAPE_KIND_IDS.join(", ")}.`);
|
|
3829
|
-
}
|
|
3830
|
-
/** This function gives the count of the grid points of a space that is known to be good. */
|
|
3831
|
-
function sizeOf(space, levels, limit) {
|
|
3832
|
-
let base = 0n;
|
|
3833
|
-
let meta = 0n;
|
|
3834
|
-
for (const c of combos(space, levels, true, limit)) if (c.base) base += c.weight;
|
|
3835
|
-
else meta = meta === null || c.weight === null || meta + c.weight > limit ? null : meta + c.weight;
|
|
3836
|
-
return {
|
|
3837
|
-
base,
|
|
3838
|
-
meta
|
|
3839
|
-
};
|
|
3840
|
-
}
|
|
3841
|
-
/** This function gives the cartesian product of lists, with the last list as the fastest. */
|
|
3842
|
-
function* product(lists, i = 0, acc = []) {
|
|
3843
|
-
if (i === lists.length) {
|
|
3844
|
-
yield [...acc];
|
|
3845
|
-
return;
|
|
3846
|
-
}
|
|
3847
|
-
for (const x of lists[i]) {
|
|
3848
|
-
acc.push(x);
|
|
3849
|
-
yield* product(lists, i + 1, acc);
|
|
3850
|
-
acc.pop();
|
|
3851
|
-
}
|
|
3852
|
-
}
|
|
3853
|
-
/** This function gives the configs of the grid of a space, without meta options. */
|
|
3854
|
-
function gridConfigs(space, levels, limit, shapes) {
|
|
3855
|
-
const kept = restrict(space, shapes);
|
|
3856
|
-
const size = sizeOf(kept, levels, 0n).base;
|
|
3857
|
-
if (size > BigInt(limit)) throw new RangeError(`The grid of the space ${space.engine} has ${size} configs, more than the limit ${limit}.`);
|
|
3858
|
-
const out = [];
|
|
3859
|
-
for (const c of combos(kept, levels, false, 0n)) {
|
|
3860
|
-
const names = [...c.choices.keys()];
|
|
3861
|
-
const lists = names.map((k) => {
|
|
3862
|
-
const o = c.choices.get(k);
|
|
3863
|
-
return o === null ? [null] : [...gridParams(o.params, levels)].map((params) => ({
|
|
3864
|
-
option: o.id,
|
|
3865
|
-
params
|
|
3866
|
-
}));
|
|
3867
|
-
});
|
|
3868
|
-
for (const row of product(lists)) out.push(canonicalOf(space, {
|
|
3869
|
-
space: space.engine,
|
|
3870
|
-
stages: Object.fromEntries(names.map((k, i) => [k, row[i]]))
|
|
3871
|
-
}, false));
|
|
3872
|
-
}
|
|
3873
|
-
return out;
|
|
3874
|
-
}
|
|
3875
|
-
/** This function gives a random order of the numbers from 0 to `n - 1`. */
|
|
3876
|
-
function permutation(n, r) {
|
|
3877
|
-
const out = Array.from({ length: n }, (_, i) => i);
|
|
3878
|
-
for (let i = n - 1; i > 0; i--) {
|
|
3879
|
-
const j = Math.floor(r() * (i + 1));
|
|
3880
|
-
[out[i], out[j]] = [out[j], out[i]];
|
|
3881
|
-
}
|
|
3882
|
-
return out;
|
|
3883
|
-
}
|
|
3884
|
-
/** This function gives the value of a param at the fraction `u` of its range. A sampled real number has 6 significant digits. */
|
|
3885
|
-
function sampleValue(p, u) {
|
|
3886
|
-
if (p.kind === "bool") return u >= .5;
|
|
3887
|
-
if (p.kind === "enum") {
|
|
3888
|
-
const values = p.values;
|
|
3889
|
-
return values[Math.min(values.length - 1, Math.floor(u * values.length))];
|
|
3890
|
-
}
|
|
3891
|
-
if (p.min === p.max) return normalize(p, p.min);
|
|
3892
|
-
if (p.scale !== "log" && (p.kind === "int" || p.step !== void 0)) {
|
|
3893
|
-
const steps = Math.round((lastPoint(p) - p.min) / (p.step ?? 1)) + 1;
|
|
3894
|
-
return snap(p, p.min + Math.min(steps - 1, Math.floor(u * steps)) * (p.step ?? 1), 6);
|
|
3895
|
-
}
|
|
3896
|
-
return snap(p, at01(p, u), 6);
|
|
3897
|
-
}
|
|
3898
|
-
/** This function gives a seed of 32 bits from a seed and a step name. */
|
|
3899
|
-
const subSeed = (seed, step) => Math.floor(stepRandom(seed, step)() * 4294967296);
|
|
3900
|
-
/**
|
|
3901
|
-
* This function gives a sample of a space: Latin hypercube strata over the options, the params and the member counts.
|
|
3902
|
-
* `canon` is the space of `canonicalConfig`: the space before a shape filter. A member of an option with `view: "bounds"` can have an option that the filter removed.
|
|
3903
|
-
*/
|
|
3904
|
-
function sampleConfigs(space, n, seed, meta, maxTries, canon = space, walk = false, first = /* @__PURE__ */ new Set()) {
|
|
3905
|
-
const stages = space.stages;
|
|
3906
|
-
const prefix = prefixes(space);
|
|
3907
|
-
const bounded = new Set(stages.filter((s) => meta && s.options.some((o) => memberSpaces.has(o) || o.members?.max === 0)).map((s) => prefix.get(s.name)));
|
|
3908
|
-
const lists = stages.map((s) => {
|
|
3909
|
-
const list = s.options.filter((o) => meta || o.members === void 0);
|
|
3910
|
-
if (s.optional) list.push(null);
|
|
3911
|
-
if (list.length === 0 && bounded.has(prefix.get(s.name))) list.push(null);
|
|
3912
|
-
if (list.length === 0) throw new Error(`The stage ${s.name} of the space ${space.engine} has no option for a sample. A shape filter can remove all of its options.`);
|
|
3913
|
-
return list;
|
|
3914
|
-
});
|
|
3915
|
-
const usable = /* @__PURE__ */ new Map();
|
|
3916
|
-
const withMembers = (s, o) => {
|
|
3917
|
-
if (o === null || o === void 0 || o.members === void 0) return true;
|
|
3918
|
-
if (!usable.has(o)) usable.set(o, hasMembers(space, s.name, o));
|
|
3919
|
-
return usable.get(o);
|
|
3920
|
-
};
|
|
3921
|
-
const plan = walk ? walkPlan(stages, lists, space.constraints ?? [], n, first) : null;
|
|
3922
|
-
const points = [];
|
|
3923
|
-
let tries = 0;
|
|
3924
|
-
for (let round = 0; points.length < n; round++) {
|
|
3925
|
-
const r = stepRandom(seed, `options:${round}`);
|
|
3926
|
-
const picks = walk ? lists.map(() => permutation(n, r)) : [];
|
|
3927
|
-
const columns = walk ? [] : lists.map((list) => permutation(n, r).map((p) => list[Math.floor(p * list.length / n)]));
|
|
3928
|
-
for (let i = 0; i < n && points.length < n; i++) {
|
|
3929
|
-
if (tries >= maxTries) throw new Error(`The constraints of the space ${space.engine} remove too many points: ${points.length} of ${n} configs after ${tries} tries.${walk ? "" : " The option draw: \"walk\" gets a sample of a space with many constraints."}`);
|
|
3930
|
-
tries++;
|
|
3931
|
-
const choices = plan !== null ? walkPoint(plan, picks, i) : new Map(stages.map((s, k) => [s.name, columns[k][i]]));
|
|
3932
|
-
const metaPart = /* @__PURE__ */ new Map();
|
|
3933
|
-
for (const s of stages) if (choices.get(s.name)?.members !== void 0 && !metaPart.has(prefix.get(s.name))) metaPart.set(prefix.get(s.name), s.name);
|
|
3934
|
-
for (const s of stages) {
|
|
3935
|
-
const m = metaPart.get(prefix.get(s.name));
|
|
3936
|
-
if (m !== void 0 && m !== s.name) choices.set(s.name, null);
|
|
3937
|
-
}
|
|
3938
|
-
if (stages.every((s) => s.optional === true || choices.get(s.name) !== null || metaPart.has(prefix.get(s.name))) && keeps(space, choices) && stages.every((s) => withMembers(s, choices.get(s.name)))) points.push(choices);
|
|
3939
|
-
}
|
|
3940
|
-
}
|
|
3941
|
-
const values = points.map(() => stages.map(() => ({})));
|
|
3942
|
-
const members = points.map(() => stages.map(() => []));
|
|
3943
|
-
stages.forEach((s, k) => {
|
|
3944
|
-
for (const o of s.options) {
|
|
3945
|
-
const users = points.flatMap((c, i) => c.get(s.name) === o ? [i] : []);
|
|
3946
|
-
if (users.length === 0) continue;
|
|
3947
|
-
for (const p of o.params) {
|
|
3948
|
-
const active = users.filter((i) => isActive(p, values[i][k]));
|
|
3949
|
-
const r = stepRandom(seed, `param:${s.name}:${o.id}:${p.name}`);
|
|
3950
|
-
permutation(active.length, r).forEach((stratum, j) => {
|
|
3951
|
-
values[active[j]][k][p.name] = sampleValue(p, (stratum + r()) / active.length);
|
|
3952
|
-
});
|
|
3953
|
-
}
|
|
3954
|
-
if (o.members === void 0 || o.members.max === 0) continue;
|
|
3955
|
-
const fixed = o.members.pool;
|
|
3956
|
-
if (fixed !== void 0) {
|
|
3957
|
-
const r = stepRandom(seed, `pool-entry:${s.name}:${o.id}`);
|
|
3958
|
-
permutation(users.length, r).forEach((stratum, j) => {
|
|
3959
|
-
members[users[j]][k] = [...fixed[Math.min(fixed.length - 1, Math.floor((stratum + r()) / users.length * fixed.length))]];
|
|
3960
|
-
});
|
|
3961
|
-
continue;
|
|
3962
|
-
}
|
|
3963
|
-
const part = memberSpaces.get(o) ?? partOf(space, s.name).space;
|
|
3964
|
-
const poolSize = Math.max(n, 4 * o.members.max, 8);
|
|
3965
|
-
const seen = /* @__PURE__ */ new Set();
|
|
3966
|
-
const pool = sampleConfigs(part, poolSize, subSeed(seed, `pool:${s.name}:${o.id}`), false, Math.max(1e3, 100 * poolSize), part, walk).filter((c) => {
|
|
3967
|
-
const id = idOf(c);
|
|
3968
|
-
return seen.has(id) ? false : (seen.add(id), true);
|
|
3969
|
-
});
|
|
3970
|
-
const r = stepRandom(seed, `members:${s.name}:${o.id}`);
|
|
3971
|
-
const { min, max } = o.members;
|
|
3972
|
-
permutation(users.length, r).forEach((stratum, j) => {
|
|
3973
|
-
const count = min + Math.min(max - min, Math.floor((stratum + r()) / users.length * (max - min + 1)));
|
|
3974
|
-
if (pool.length < count) throw new Error(`The space ${part.engine} has only ${pool.length} distinct base samples, fewer than the ${count} members of ${s.name}.${o.id}.`);
|
|
3975
|
-
const picked = permutation(pool.length, r).slice(0, count);
|
|
3976
|
-
members[users[j]][k] = picked.map((x) => pool[x]);
|
|
3977
|
-
});
|
|
3978
|
-
}
|
|
3979
|
-
});
|
|
3980
|
-
return points.map((choices, i) => {
|
|
3981
|
-
const out = {};
|
|
3982
|
-
stages.forEach((s, k) => {
|
|
3983
|
-
const o = choices.get(s.name);
|
|
3984
|
-
out[s.name] = o === null ? null : o.members === void 0 ? {
|
|
3985
|
-
option: o.id,
|
|
3986
|
-
params: values[i][k]
|
|
3987
|
-
} : {
|
|
3988
|
-
option: o.id,
|
|
3989
|
-
params: values[i][k],
|
|
3990
|
-
members: members[i][k]
|
|
3991
|
-
};
|
|
3992
|
-
});
|
|
3993
|
-
return canonicalConfig(canon, {
|
|
3994
|
-
space: space.engine,
|
|
3995
|
-
stages: out
|
|
3996
|
-
});
|
|
3997
|
-
});
|
|
3998
|
-
}
|
|
3999
|
-
/**
|
|
4000
|
-
* This function gives configs of a space, in their canonical form. Each config keeps the constraints. The result is deterministic.
|
|
4001
|
-
*
|
|
4002
|
-
* - The mode `grid` gives each grid point without a meta option, in a stable order. Stages, options and values go in the order of the space.
|
|
4003
|
-
* - The mode `sample` gives `n` configs of a seeded Latin hypercube. The options of a stage get equal strata, and each active param gets its own strata.
|
|
4004
|
-
* - A sample draws again when a point breaks a constraint. After `maxTries` draws, it throws an `Error`. Two sampled configs can be equal.
|
|
4005
|
-
* - With `draw: "walk"`, each stage draws only from the options that keep the constraints with the stages before it. The largest stage keeps its equal strata.
|
|
4006
|
-
* - With `shapes`, only the options that support each kind of the list count. Without `shapes`, the result does not change.
|
|
4007
|
-
* - A meta option with a `pool` gives only the entries of its pool. With `shapes`, only the entries whose members support each kind count.
|
|
4008
|
-
* - A sample draws again when a point chooses a meta option without a member list. `spaceSize` gives such an option 0.
|
|
4009
|
-
* - With `shapes`, a sample throws a `RangeError` when the space has no config for the kinds. The error names the stages without an option and the meta options without members.
|
|
4010
|
-
* - With `fixed`, each config has the fixed option at each named stage. A fixed option that the space, the shape filter or the constraints do not permit gets a `RangeError` with the reason.
|
|
2897
|
+
* This function gives the configs of a grid or a sample with `enumerateSpace` of `@mark1russell7/strategy-space`.
|
|
2898
|
+
* Without `kinds`, a shape kind that is not in `SHAPE_KIND_IDS` causes an error.
|
|
4011
2899
|
*/
|
|
4012
2900
|
function enumerateSpace(space, options) {
|
|
4013
|
-
|
|
4014
|
-
assertShapes(options.shapes);
|
|
4015
|
-
if (options.mode === "grid") {
|
|
4016
|
-
assertWhole(options.levels, "levels", 1);
|
|
4017
|
-
return gridConfigs(space, options.levels, options.limit ?? 1e5, options.shapes);
|
|
4018
|
-
}
|
|
4019
|
-
if (options.mode !== "sample") throw new RangeError(`The mode ${String(options.mode)} is not grid or sample.`);
|
|
4020
|
-
assertWhole(options.n, "n", 0);
|
|
4021
|
-
if (!Number.isInteger(options.seed)) throw new RangeError(`The seed ${options.seed} is not a whole number.`);
|
|
4022
|
-
const meta = options.meta ?? "skip";
|
|
4023
|
-
if (meta !== "skip" && meta !== "sample") throw new RangeError(`The option meta is ${String(meta)}, not skip or sample.`);
|
|
4024
|
-
const draw = options.draw ?? "reject";
|
|
4025
|
-
if (draw !== "reject" && draw !== "walk") throw new RangeError(`The option draw is ${String(draw)}, not reject or walk.`);
|
|
4026
|
-
const fixed = options.fixed;
|
|
4027
|
-
if (fixed !== void 0) assertFixed(space, fixed, meta === "sample");
|
|
4028
|
-
if (options.n === 0) return [];
|
|
4029
|
-
const kept = restrict(space, options.shapes);
|
|
4030
|
-
const drawn = fixed === void 0 ? kept : fixStages(space, kept, fixed, options.shapes);
|
|
4031
|
-
try {
|
|
4032
|
-
return sampleConfigs(drawn, options.n, options.seed, meta === "sample", options.maxTries ?? Math.max(1e3, 100 * options.n), space, draw === "walk", new Set(Object.keys(fixed ?? {})));
|
|
4033
|
-
} catch (error) {
|
|
4034
|
-
if (options.shapes !== void 0 && !hasConfig(kept, meta === "sample")) throw noSample(space, kept, options.shapes, meta === "sample");
|
|
4035
|
-
if (fixed !== void 0 && !hasConfig(drawn, meta === "sample")) throw noFixedSample(space, fixed);
|
|
4036
|
-
throw error;
|
|
4037
|
-
}
|
|
4038
|
-
}
|
|
4039
|
-
/**
|
|
4040
|
-
* This function gives the shape kinds that a space accepts, in the order of `SHAPE_KINDS`. The value comes from the `supports` of the options.
|
|
4041
|
-
* A space accepts a kind when it has at least one config without a meta option on that kind.
|
|
4042
|
-
*/
|
|
4043
|
-
function spaceShapeKinds(space) {
|
|
4044
|
-
assertSpace(space);
|
|
4045
|
-
return SHAPE_KIND_IDS.filter((k) => sizeOf(restrict(space, [k]), 1, 0n).base > 0n);
|
|
4046
|
-
}
|
|
4047
|
-
/**
|
|
4048
|
-
* This function joins configs of the parts of a product space into one config of the space `space`.
|
|
4049
|
-
* The name of each stage gets the prefix `<space of the part>.`. The members of a meta option do not change.
|
|
4050
|
-
*/
|
|
4051
|
-
function joinConfigs(space, parts) {
|
|
4052
|
-
const stages = {};
|
|
4053
|
-
for (const part of parts) for (const [k, c] of Object.entries(part.stages)) stages[`${part.space}.${k}`] = c;
|
|
4054
|
-
return {
|
|
4055
|
-
space,
|
|
4056
|
-
stages
|
|
4057
|
-
};
|
|
4058
|
-
}
|
|
4059
|
-
/** This function gives the config of the part `engine` of a joint config. It throws a `RangeError` when no stage has the prefix `<engine>.`. */
|
|
4060
|
-
function splitConfig(config, engine) {
|
|
4061
|
-
const head = `${engine}.`;
|
|
4062
|
-
const entries = Object.entries(config.stages).filter(([k]) => k.startsWith(head));
|
|
4063
|
-
if (entries.length === 0) throw new RangeError(`The config of the space ${config.space} has no stage of the part ${engine}.`);
|
|
4064
|
-
return {
|
|
4065
|
-
space: engine,
|
|
4066
|
-
stages: Object.fromEntries(entries.map(([k, c]) => [k.slice(head.length), c]))
|
|
4067
|
-
};
|
|
4068
|
-
}
|
|
4069
|
-
/**
|
|
4070
|
-
* This function joins two spaces, for example a packer and a router, into the space `name`. The stages of `a` and `b` get the prefixes `<a.engine>.` and `<b.engine>.`.
|
|
4071
|
-
* The constraints get the same prefixes. The named configs are the pairs `<name of a>+<name of b>`. It throws a `RangeError` when the two engines have the same name.
|
|
4072
|
-
*/
|
|
4073
|
-
function productSpace(a, b, name) {
|
|
4074
|
-
if (a.engine === b.engine) throw new RangeError(`The two spaces have the same engine ${a.engine}.`);
|
|
4075
|
-
if (name === "") throw new RangeError("The product space has no name.");
|
|
4076
|
-
const stagesOf = (s) => s.stages.map((x) => ({
|
|
4077
|
-
...x,
|
|
4078
|
-
name: `${s.engine}.${x.name}`
|
|
4079
|
-
}));
|
|
4080
|
-
const ref = (s, r) => ({
|
|
4081
|
-
stage: `${s.engine}.${r.stage}`,
|
|
4082
|
-
option: r.option
|
|
4083
|
-
});
|
|
4084
|
-
const constraintsOf = (s) => (s.constraints ?? []).map((c) => ({
|
|
4085
|
-
if: ref(s, c.if),
|
|
4086
|
-
then: "not" in c.then ? { not: ref(s, c.then.not) } : ref(s, c.then)
|
|
4087
|
-
}));
|
|
4088
|
-
const named = {};
|
|
4089
|
-
for (const [ka, ca] of Object.entries(a.named ?? {})) for (const [kb, cb] of Object.entries(b.named ?? {})) named[`${ka}+${kb}`] = joinConfigs(name, [ca, cb]);
|
|
4090
|
-
const constraints = [...constraintsOf(a), ...constraintsOf(b)];
|
|
4091
|
-
return {
|
|
4092
|
-
schema: STRATEGY_SPACE_SCHEMA,
|
|
4093
|
-
engine: name,
|
|
4094
|
-
version: `${a.engine}@${a.version}+${b.engine}@${b.version}`,
|
|
4095
|
-
stages: [...stagesOf(a), ...stagesOf(b)],
|
|
4096
|
-
...constraints.length > 0 ? { constraints } : {},
|
|
4097
|
-
...Object.keys(named).length > 0 ? { named } : {},
|
|
4098
|
-
parts: [a, b]
|
|
4099
|
-
};
|
|
4100
|
-
}
|
|
4101
|
-
// src/strategy-options.ts
|
|
4102
|
-
/** The number of values of a sweep on a range when the call and the declaration give no number. */
|
|
4103
|
-
const SWEEP_COUNT = 5;
|
|
4104
|
-
/** This function gives the declarations of a strategy as a record. A strategy without a schema gives an empty record. */
|
|
4105
|
-
function schemaOf(strategy) {
|
|
4106
|
-
return strategy.schema ?? {};
|
|
4107
|
-
}
|
|
4108
|
-
/** This function gives the declared options of a strategy, in the order of its schema, with the effective default of each one. */
|
|
4109
|
-
function listOptions(strategy) {
|
|
4110
|
-
const defaults = optionDefaults(strategy);
|
|
4111
|
-
return Object.entries(schemaOf(strategy)).map(([key, declaration]) => ({
|
|
4112
|
-
key,
|
|
4113
|
-
declaration,
|
|
4114
|
-
value: defaults[key]
|
|
4115
|
-
}));
|
|
4116
|
-
}
|
|
4117
|
-
/**
|
|
4118
|
-
* This function gives the effective default of each declared option: the value of `defaults`, else the `default` of the declaration.
|
|
4119
|
-
* An option without either value stays out. The strategy gives the same output with this object as with no options.
|
|
4120
|
-
*/
|
|
4121
|
-
function optionDefaults(strategy) {
|
|
4122
|
-
const out = {};
|
|
4123
|
-
const defaults = strategy.defaults;
|
|
4124
|
-
for (const [key, declaration] of Object.entries(schemaOf(strategy))) if (key in defaults) out[key] = defaults[key];
|
|
4125
|
-
else if (declaration.default !== void 0) out[key] = declaration.default;
|
|
4126
|
-
for (const key of Object.keys(defaults)) if (!(key in out)) out[key] = defaults[key];
|
|
4127
|
-
return out;
|
|
4128
|
-
}
|
|
4129
|
-
/** This function gives `true` if the number is one of the special values of the declaration. */
|
|
4130
|
-
const isSpecial = (d, v) => (d.special ?? []).some((s) => Object.is(s, v) || s === v);
|
|
4131
|
-
/** This function gives the problem of one value against its declaration, or `undefined` when the value fits. */
|
|
4132
|
-
function optionIssue(key, value, d) {
|
|
4133
|
-
const issue = (problem, message) => ({
|
|
4134
|
-
key,
|
|
4135
|
-
value,
|
|
4136
|
-
problem,
|
|
4137
|
-
message
|
|
4138
|
-
});
|
|
4139
|
-
if (value === void 0) return void 0;
|
|
4140
|
-
switch (d.type) {
|
|
4141
|
-
case "number":
|
|
4142
|
-
case "integer":
|
|
4143
|
-
if (typeof value !== "number" || Number.isNaN(value)) return issue("type", `The option "${key}" takes a number.`);
|
|
4144
|
-
if (isSpecial(d, value)) return void 0;
|
|
4145
|
-
if (d.values !== void 0 && !d.values.includes(value)) return issue("value", `The option "${key}" takes ${d.values.join(", ")}, not ${value}.`);
|
|
4146
|
-
if (value < d.min || value > d.max) return issue("range", `The option "${key}" is ${value}, out of the range ${d.min} to ${d.max}.`);
|
|
4147
|
-
if (d.type === "integer" && !Number.isInteger(value)) return issue("integer", `The option "${key}" takes a whole number, not ${value}.`);
|
|
4148
|
-
return;
|
|
4149
|
-
case "boolean": return typeof value === "boolean" ? void 0 : issue("type", `The option "${key}" takes true or false.`);
|
|
4150
|
-
case "enum":
|
|
4151
|
-
if (d.custom === "function" && typeof value === "function") return void 0;
|
|
4152
|
-
return d.values.includes(value) ? void 0 : issue("value", `The option "${key}" takes ${d.values.join(", ")}, not ${String(value)}.`);
|
|
4153
|
-
case "function": return typeof value === "function" ? void 0 : issue("type", `The option "${key}" takes a function.`);
|
|
4154
|
-
case "list": return Array.isArray(value) ? void 0 : issue("type", `The option "${key}" takes a list.`);
|
|
4155
|
-
default: return typeof value === "object" && value !== null ? void 0 : issue("type", `The option "${key}" takes an object.`);
|
|
4156
|
-
}
|
|
4157
|
-
}
|
|
4158
|
-
/**
|
|
4159
|
-
* This function examines option values against the schema of a strategy. It gives one issue for each key without a declaration.
|
|
4160
|
-
* It also gives one issue for each value of the wrong type, out of the range, not whole or not one of the values.
|
|
4161
|
-
* It changes no value.
|
|
4162
|
-
* A strategy without a schema gives no issue.
|
|
4163
|
-
*/
|
|
4164
|
-
function checkOptions(strategy, options) {
|
|
4165
|
-
if (strategy.schema === void 0) return [];
|
|
4166
|
-
const schema = schemaOf(strategy);
|
|
4167
|
-
const out = [];
|
|
4168
|
-
for (const [key, value] of Object.entries(options)) {
|
|
4169
|
-
const d = schema[key];
|
|
4170
|
-
const issue = d ? optionIssue(key, value, d) : {
|
|
4171
|
-
key,
|
|
4172
|
-
value,
|
|
4173
|
-
problem: "unknown",
|
|
4174
|
-
message: `The strategy "${strategy.name}" has no option "${key}".`
|
|
4175
|
-
};
|
|
4176
|
-
if (issue) out.push(issue);
|
|
4177
|
-
}
|
|
4178
|
-
return out;
|
|
4179
|
-
}
|
|
4180
|
-
/**
|
|
4181
|
-
* This function moves each number option into the range of its declaration, for a control. An integer option gets the nearest whole number.
|
|
4182
|
-
* An option with a list of `values` gets the nearest value of the list. A special value stays. The function keeps each other value as it is. `checkOptions` reports the values that this function cannot correct.
|
|
4183
|
-
*/
|
|
4184
|
-
function clampOptions(strategy, options) {
|
|
4185
|
-
const schema = schemaOf(strategy);
|
|
4186
|
-
const out = { ...options };
|
|
4187
|
-
for (const [key, value] of Object.entries(options)) {
|
|
4188
|
-
const d = schema[key];
|
|
4189
|
-
if (!d || d.type !== "number" && d.type !== "integer" || typeof value !== "number" || Number.isNaN(value) || isSpecial(d, value)) continue;
|
|
4190
|
-
if (d.values !== void 0 && d.values.length > 0) {
|
|
4191
|
-
out[key] = d.values.reduce((best, v) => Math.abs(v - value) < Math.abs(best - value) ? v : best);
|
|
4192
|
-
continue;
|
|
4193
|
-
}
|
|
4194
|
-
const v = Math.min(d.max, Math.max(d.min, value));
|
|
4195
|
-
out[key] = d.type === "integer" ? Math.min(d.max, Math.max(d.min, Math.round(v))) : v;
|
|
4196
|
-
}
|
|
4197
|
-
return out;
|
|
4198
|
-
}
|
|
4199
|
-
/** This function gives `count` values from `min` to `max` on the scale, on the grain of the step, without repeats. */
|
|
4200
|
-
function rangeValues(d, count) {
|
|
4201
|
-
const n = Math.max(1, Math.floor(count));
|
|
4202
|
-
const log = d.scale === "log" && d.min > 0;
|
|
4203
|
-
const at = (t) => log ? d.min * (d.max / d.min) ** t : d.min + (d.max - d.min) * t;
|
|
4204
|
-
const step = d.step ?? (d.type === "integer" ? 1 : void 0);
|
|
4205
|
-
const snap = (v) => {
|
|
4206
|
-
const s = step === void 0 ? v : d.min + Math.round((v - d.min) / step) * step;
|
|
4207
|
-
const r = Number(s.toPrecision(12));
|
|
4208
|
-
return Math.min(d.max, Math.max(d.min, d.type === "integer" ? Math.round(r) : r));
|
|
4209
|
-
};
|
|
4210
|
-
const out = [];
|
|
4211
|
-
for (let i = 0; i < n; i++) {
|
|
4212
|
-
const v = snap(at(n === 1 ? 0 : i / (n - 1)));
|
|
4213
|
-
if (!out.includes(v)) out.push(v);
|
|
4214
|
-
}
|
|
4215
|
-
return out;
|
|
4216
|
-
}
|
|
4217
|
-
/**
|
|
4218
|
-
* This function gives the values of a sweep of one option declaration.
|
|
4219
|
-
* A number option gives its `marks`, or its list of `values`, or `count` values on its range and its scale. A boolean gives `false` and `true`. An enum gives its values.
|
|
4220
|
-
* An option of another type has no sweep, so the function throws an error.
|
|
4221
|
-
*/
|
|
4222
|
-
function sweepValues(declaration, options = {}) {
|
|
4223
|
-
switch (declaration.type) {
|
|
4224
|
-
case "number":
|
|
4225
|
-
case "integer": {
|
|
4226
|
-
if (options.count === void 0 && declaration.marks !== void 0) return [...declaration.marks];
|
|
4227
|
-
if (declaration.values !== void 0) return [...declaration.values];
|
|
4228
|
-
const values = rangeValues(declaration, options.count ?? SWEEP_COUNT);
|
|
4229
|
-
return options.special ? [...values, ...(declaration.special ?? []).filter((s) => !values.includes(s))] : values;
|
|
4230
|
-
}
|
|
4231
|
-
case "boolean": return [false, true];
|
|
4232
|
-
case "enum": return [...declaration.values];
|
|
4233
|
-
default: throw new Error(`An option of the type "${declaration.type}" has no sweep. A sweep takes a number, an integer, a boolean or an enum.`);
|
|
4234
|
-
}
|
|
4235
|
-
}
|
|
4236
|
-
/** This function gives the declaration of one option of a strategy. When the strategy has no such declaration, the function throws an error. */
|
|
4237
|
-
function declarationOf(strategy, key) {
|
|
4238
|
-
const d = schemaOf(strategy)[key];
|
|
4239
|
-
if (!d) throw new Error(`The strategy "${strategy.name}" has no declaration of the option "${key}".`);
|
|
4240
|
-
return d;
|
|
4241
|
-
}
|
|
4242
|
-
/**
|
|
4243
|
-
* This function gives the settings of a sweep of one option of a strategy, from its declaration only.
|
|
4244
|
-
* The name of each setting is the key and the value, for example `stretch 1.5`.
|
|
4245
|
-
*/
|
|
4246
|
-
function sweepOf(strategy, key, options = {}) {
|
|
4247
|
-
return sweepValues(declarationOf(strategy, key), options).map((value) => ({
|
|
4248
|
-
name: `${key} ${String(value)}`,
|
|
4249
|
-
options: { [key]: value }
|
|
4250
|
-
}));
|
|
4251
|
-
}
|
|
4252
|
-
/**
|
|
4253
|
-
* This function gives the settings of a grid of options: each combination of the values of the sweeps of the keys.
|
|
4254
|
-
* The name of a setting joins the names of its values with commas, in the order of the keys.
|
|
4255
|
-
*/
|
|
4256
|
-
function sweepGrid(strategy, keys, options = {}) {
|
|
4257
|
-
let out = [{
|
|
4258
|
-
name: "",
|
|
4259
|
-
options: {}
|
|
4260
|
-
}];
|
|
4261
|
-
for (const key of keys) {
|
|
4262
|
-
const sweep = sweepOf(strategy, key, options);
|
|
4263
|
-
out = out.flatMap((s) => sweep.map((t) => ({
|
|
4264
|
-
name: s.name === "" ? t.name : `${s.name}, ${t.name}`,
|
|
4265
|
-
options: {
|
|
4266
|
-
...s.options,
|
|
4267
|
-
...t.options
|
|
4268
|
-
}
|
|
4269
|
-
})));
|
|
4270
|
-
}
|
|
4271
|
-
return keys.length === 0 ? [] : out;
|
|
4272
|
-
}
|
|
4273
|
-
/** This function gives the problems of one declaration: a bad range, a bad step, or marks and a default that do not fit. */
|
|
4274
|
-
function declarationIssues(key, d) {
|
|
4275
|
-
const out = [];
|
|
4276
|
-
if (d.describe.trim().length === 0) out.push(`The option "${key}" has no description.`);
|
|
4277
|
-
if (d.type === "number" || d.type === "integer") {
|
|
4278
|
-
if (!Number.isFinite(d.min) || !Number.isFinite(d.max) || d.min > d.max) out.push(`The option "${key}" has the bad range ${d.min} to ${d.max}.`);
|
|
4279
|
-
if (d.type === "integer" && (!Number.isInteger(d.min) || !Number.isInteger(d.max))) out.push(`The integer option "${key}" has a range that is not whole.`);
|
|
4280
|
-
if (d.step !== void 0 && !(d.step > 0)) out.push(`The option "${key}" has the step ${d.step}. A step is more than 0.`);
|
|
4281
|
-
if (d.scale === "log" && !(d.min > 0)) out.push(`The option "${key}" has the scale log and the min ${d.min}. The scale log needs a min above 0.`);
|
|
4282
|
-
if (d.values !== void 0 && d.values.length === 0) out.push(`The number option "${key}" has an empty list of values.`);
|
|
4283
|
-
for (const v of d.values ?? []) if (v < d.min || v > d.max || d.type === "integer" && !Number.isInteger(v)) out.push(`The value ${v} of the option "${key}" is not on its range ${d.min} to ${d.max}.`);
|
|
4284
|
-
for (const v of [...d.marks ?? [], ...d.default === void 0 ? [] : [d.default]]) {
|
|
4285
|
-
const issue = optionIssue(key, v, d);
|
|
4286
|
-
if (issue) out.push(issue.message);
|
|
4287
|
-
}
|
|
4288
|
-
} else if (d.type === "enum") {
|
|
4289
|
-
if (d.values.length === 0) out.push(`The enum option "${key}" has no values.`);
|
|
4290
|
-
if (d.default !== void 0 && !d.values.includes(d.default)) out.push(`The default of the option "${key}" is not one of its values.`);
|
|
4291
|
-
} else if (d.type === "boolean") {
|
|
4292
|
-
if (d.default !== void 0 && typeof d.default !== "boolean") out.push(`The default of the option "${key}" is not true or false.`);
|
|
4293
|
-
}
|
|
4294
|
-
return out;
|
|
2901
|
+
return enumerateSpace$1(space, withKinds(options));
|
|
4295
2902
|
}
|
|
4296
2903
|
/**
|
|
4297
|
-
* This function
|
|
4298
|
-
*
|
|
4299
|
-
* A good schema gives an empty list.
|
|
2904
|
+
* This function gives the shape kinds that a space accepts, with `spaceShapeKinds` of `@mark1russell7/strategy-space`.
|
|
2905
|
+
* Without `kinds`, it examines the kinds of `SHAPE_KIND_IDS`, in their order.
|
|
4300
2906
|
*/
|
|
4301
|
-
function
|
|
4302
|
-
|
|
4303
|
-
const out = [];
|
|
4304
|
-
for (const key of Object.keys(strategy.defaults)) if (!(key in schema)) out.push(`The strategy "${strategy.name}" has the option "${key}" in its defaults, without a declaration.`);
|
|
4305
|
-
for (const [key, d] of Object.entries(schema)) out.push(...declarationIssues(key, d).map((m) => `The strategy "${strategy.name}": ${m}`));
|
|
4306
|
-
out.push(...checkOptions(strategy, strategy.defaults).filter((i) => i.problem !== "unknown").map((i) => `The strategy "${strategy.name}": ${i.message}`));
|
|
4307
|
-
return out;
|
|
2907
|
+
function spaceShapeKinds(space, options = {}) {
|
|
2908
|
+
return spaceShapeKinds$1(space, withKinds(options));
|
|
4308
2909
|
}
|
|
4309
2910
|
// src/strategy-catalog.ts
|
|
4310
2911
|
/** The newest schema of a strategy catalog. A new field is a new version of the schema. */
|
|
@@ -5214,37 +3815,26 @@ const VERDICT_DRAWS = 2e4;
|
|
|
5214
3815
|
const VERDICT_SEED = 1;
|
|
5215
3816
|
/** This function gives the mean of a list that is not empty. */
|
|
5216
3817
|
const meanOf = (values) => values.reduce((s, v) => s + v, 0) / values.length;
|
|
5217
|
-
/** This function gives the
|
|
5218
|
-
function
|
|
5219
|
-
|
|
5220
|
-
const
|
|
5221
|
-
|
|
5222
|
-
|
|
5223
|
-
|
|
5224
|
-
|
|
5225
|
-
|
|
5226
|
-
|
|
5227
|
-
return
|
|
5228
|
-
|
|
5229
|
-
|
|
5230
|
-
|
|
5231
|
-
|
|
5232
|
-
|
|
3818
|
+
/** This function gives the mean of a sample of the differences, in the order of the sample. */
|
|
3819
|
+
function sampleMean(list) {
|
|
3820
|
+
let s = 0;
|
|
3821
|
+
for (const d of list) s += d;
|
|
3822
|
+
return s / list.length;
|
|
3823
|
+
}
|
|
3824
|
+
/** This function gives the mean of the means of the samples of the families. */
|
|
3825
|
+
function meanOfMeans(lists) {
|
|
3826
|
+
let sum = 0;
|
|
3827
|
+
for (const list of lists) sum += sampleMean(list);
|
|
3828
|
+
return sum / lists.length;
|
|
3829
|
+
}
|
|
3830
|
+
/** This function gives the interval of a bootstrap, with the mean of the data. */
|
|
3831
|
+
const intervalOf = (mean, i) => ({
|
|
3832
|
+
mean,
|
|
3833
|
+
lo: i.low,
|
|
3834
|
+
hi: i.high
|
|
3835
|
+
});
|
|
5233
3836
|
/** This function tells if an interval is clear of 0 on the side of the sign `sign`. */
|
|
5234
3837
|
const clearOn = (i, sign) => sign > 0 ? i.lo > 0 : sign < 0 ? i.hi < 0 : false;
|
|
5235
|
-
/** This function gives the two-sided p of the exact binomial sign test, with `p = 0.5`. No count gives 1. */
|
|
5236
|
-
function signP(wins, losses) {
|
|
5237
|
-
const n = wins + losses;
|
|
5238
|
-
if (n === 0) return 1;
|
|
5239
|
-
const k = Math.min(wins, losses);
|
|
5240
|
-
let tail = 0;
|
|
5241
|
-
let c = 1;
|
|
5242
|
-
for (let i = 0; i <= k; i++) {
|
|
5243
|
-
tail += c;
|
|
5244
|
-
c = c * (n - i) / (i + 1);
|
|
5245
|
-
}
|
|
5246
|
-
return Math.min(1, 2 * tail / 2 ** n);
|
|
5247
|
-
}
|
|
5248
3838
|
/** This function gives the breadth of a verdict with the side `side`: 1, -1, or 0 for no side. */
|
|
5249
3839
|
function breadthOf(families, side) {
|
|
5250
3840
|
const ties = families.filter((f) => f.sign === 0).length;
|
|
@@ -5356,63 +3946,39 @@ function verdict(records, options) {
|
|
|
5356
3946
|
sign: verdictOf === "better" ? 1 : verdictOf === "worse" ? -1 : 0
|
|
5357
3947
|
};
|
|
5358
3948
|
});
|
|
3949
|
+
const ends = [level / 2, 1 - level / 2];
|
|
3950
|
+
const diffs = lists.map((list) => list.map((p) => p.d));
|
|
5359
3951
|
const stratMean = meanOf(families.map((f) => f.mean));
|
|
5360
|
-
const
|
|
5361
|
-
|
|
5362
|
-
|
|
5363
|
-
|
|
5364
|
-
|
|
5365
|
-
let s = 0;
|
|
5366
|
-
for (let j = 0; j < list.length; j++) s += list[Math.floor(rs() * list.length)].d;
|
|
5367
|
-
sum += s / list.length;
|
|
5368
|
-
}
|
|
5369
|
-
stratSamples.push(sum / lists.length);
|
|
5370
|
-
}
|
|
5371
|
-
const stratified = intervalOf(stratMean, stratSamples, level);
|
|
3952
|
+
const stratified = intervalOf(stratMean, bootstrapStrata(diffs, meanOfMeans, {
|
|
3953
|
+
runs,
|
|
3954
|
+
ends,
|
|
3955
|
+
seed: deriveSeed(seed, "verdict.stratified")
|
|
3956
|
+
}));
|
|
5372
3957
|
const pooledMean = meanOf(pairs.map((p) => p.d));
|
|
5373
|
-
const
|
|
5374
|
-
|
|
5375
|
-
|
|
5376
|
-
|
|
5377
|
-
|
|
5378
|
-
pooledSamples.push(s / n);
|
|
5379
|
-
}
|
|
5380
|
-
const pooled = intervalOf(pooledMean, pooledSamples, level);
|
|
3958
|
+
const pooled = intervalOf(pooledMean, bootstrap(diffs.flat(), sampleMean, {
|
|
3959
|
+
runs,
|
|
3960
|
+
ends,
|
|
3961
|
+
seed: deriveSeed(seed, "verdict.pooled")
|
|
3962
|
+
}));
|
|
5381
3963
|
const terms = lists.flatMap((list) => list.map((p) => p.d / (lists.length * list.length)));
|
|
5382
|
-
const
|
|
5383
|
-
|
|
5384
|
-
|
|
5385
|
-
|
|
5386
|
-
|
|
5387
|
-
if (exact) {
|
|
5388
|
-
total = 2 ** n;
|
|
5389
|
-
for (let mask = 0; mask < total; mask++) {
|
|
5390
|
-
let t = 0;
|
|
5391
|
-
for (let i = 0; i < n; i++) t += mask & 1 << i ? -terms[i] : terms[i];
|
|
5392
|
-
if (Math.abs(t) >= observed - slack) hits++;
|
|
5393
|
-
}
|
|
5394
|
-
} else {
|
|
5395
|
-
total = draws;
|
|
5396
|
-
const rf = stepRandom(seed, "verdict.permutation");
|
|
5397
|
-
for (let k = 0; k < draws; k++) {
|
|
5398
|
-
let t = 0;
|
|
5399
|
-
for (let i = 0; i < n; i++) t += rf() < .5 ? -terms[i] : terms[i];
|
|
5400
|
-
if (Math.abs(t) >= observed - slack) hits++;
|
|
5401
|
-
}
|
|
5402
|
-
}
|
|
3964
|
+
const flips = signFlipTest(terms, {
|
|
3965
|
+
exactMax,
|
|
3966
|
+
runs: draws,
|
|
3967
|
+
seed: deriveSeed(seed, "verdict.permutation")
|
|
3968
|
+
});
|
|
5403
3969
|
const permutation = {
|
|
5404
|
-
p:
|
|
5405
|
-
exact,
|
|
5406
|
-
draws:
|
|
3970
|
+
p: flips.p,
|
|
3971
|
+
exact: flips.exact,
|
|
3972
|
+
draws: flips.runs,
|
|
5407
3973
|
seed
|
|
5408
3974
|
};
|
|
5409
3975
|
const wins = families.filter((f) => f.sign === 1).length;
|
|
5410
3976
|
const losses = families.filter((f) => f.sign === -1).length;
|
|
5411
|
-
const
|
|
3977
|
+
const signs = {
|
|
5412
3978
|
wins,
|
|
5413
3979
|
losses,
|
|
5414
3980
|
ties: families.length - wins - losses,
|
|
5415
|
-
p:
|
|
3981
|
+
p: signTest(wins, losses, 2)
|
|
5416
3982
|
};
|
|
5417
3983
|
const sign = Math.sign(stratified.mean);
|
|
5418
3984
|
const agree = sign !== 0 && sign === Math.sign(pooled.mean) && clearOn(stratified, sign) && clearOn(pooled, sign) && permutation.p < level;
|
|
@@ -5426,7 +3992,7 @@ function verdict(records, options) {
|
|
|
5426
3992
|
pooled,
|
|
5427
3993
|
permutation,
|
|
5428
3994
|
families,
|
|
5429
|
-
signTest,
|
|
3995
|
+
signTest: signs,
|
|
5430
3996
|
agree,
|
|
5431
3997
|
label,
|
|
5432
3998
|
scope: "these families",
|