@mark1russell7/strategy-space 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +49 -2
- package/build/claim.d.ts +10 -0
- package/build/claim.d.ts.map +1 -0
- package/build/distinct.d.ts +49 -0
- package/build/distinct.d.ts.map +1 -0
- package/build/distinct.js +61 -0
- package/build/distinct.js.map +1 -0
- package/build/index.d.ts +5 -0
- package/build/index.js +4 -0
- package/build/space-walk.js +48 -0
- package/build/space-walk.js.map +1 -0
- package/build/strategy-options.d.ts +189 -0
- package/build/strategy-options.d.ts.map +1 -0
- package/build/strategy-options.js +211 -0
- package/build/strategy-options.js.map +1 -0
- package/build/strategy-space.d.ts +251 -0
- package/build/strategy-space.d.ts.map +1 -0
- package/build/strategy-space.js +1029 -0
- package/build/strategy-space.js.map +1 -0
- package/package.json +60 -3
|
@@ -0,0 +1,1029 @@
|
|
|
1
|
+
import { holds, walkPlan, walkPoint } from "./space-walk.js";
|
|
2
|
+
import { canonicalJson, fnv1a32, mulberry32, sha256Hex } from "@mark1russell7/rigor-generators";
|
|
3
|
+
// src/strategy-space.ts
|
|
4
|
+
/** The schema of a strategy space. */
|
|
5
|
+
const STRATEGY_SPACE_SCHEMA = "strategy-space/1";
|
|
6
|
+
/**
|
|
7
|
+
* This function gives the generator of one step of a seeded draw: `mulberry32` of the `fnv1a32` hash of `"<seed>:<step>"`.
|
|
8
|
+
* The hash reads the WTF-8 bytes, so a lone surrogate in a name gives the same stream as `stepRandom` of `layout-contract`.
|
|
9
|
+
*/
|
|
10
|
+
const stepRandom = (seed, step) => mulberry32(fnv1a32(`${seed}:${step}`, { input: "wtf8" }));
|
|
11
|
+
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
12
|
+
const show = (value) => typeof value === "string" ? `"${value}"` : String(value);
|
|
13
|
+
const isNumeric = (p) => p.kind === "int" || p.kind === "real";
|
|
14
|
+
const zero = (v) => v === 0 ? 0 : v;
|
|
15
|
+
/** This function gives the stable form of a real number: 12 significant digits, with `-0` as `0`. */
|
|
16
|
+
const realForm = (v, digits = 12) => zero(Number(v.toPrecision(digits)));
|
|
17
|
+
/** This function gives the stable form of a value of a param. */
|
|
18
|
+
function normalize(p, v) {
|
|
19
|
+
if (typeof v !== "number") return v;
|
|
20
|
+
if (p.kind !== "real") return zero(v);
|
|
21
|
+
return Math.min(p.max ?? Infinity, Math.max(p.min ?? -Infinity, realForm(v)));
|
|
22
|
+
}
|
|
23
|
+
/** This function tells if a value is on the step of a param. */
|
|
24
|
+
function onStep(p, v) {
|
|
25
|
+
if (p.step === void 0 || p.min === void 0) return true;
|
|
26
|
+
const q = (v - p.min) / p.step;
|
|
27
|
+
return Math.abs(q - Math.round(q)) <= 1e-9 * Math.max(1, Math.abs(q));
|
|
28
|
+
}
|
|
29
|
+
/** This function gives the problem of a value of a param, or `undefined` for a value in the range. */
|
|
30
|
+
function valueProblem(p, v) {
|
|
31
|
+
if (p.kind === "bool") return typeof v === "boolean" ? void 0 : `${show(v)} is not true or false`;
|
|
32
|
+
if (p.kind === "enum") return (p.values ?? []).some((x) => x === v) ? void 0 : `${show(v)} is not one of ${(p.values ?? []).map(show).join(", ")}`;
|
|
33
|
+
if (typeof v !== "number" || !Number.isFinite(v)) return `${show(v)} is not a finite number`;
|
|
34
|
+
if (p.kind === "int" && !Number.isInteger(v)) return `${v} is not a whole number`;
|
|
35
|
+
if (p.min !== void 0 && v < p.min) return `${v} is less than the min ${p.min}`;
|
|
36
|
+
if (p.max !== void 0 && v > p.max) return `${v} is more than the max ${p.max}`;
|
|
37
|
+
if (!onStep(p, v)) return `${v} is not on the step ${p.step} from ${p.min}`;
|
|
38
|
+
}
|
|
39
|
+
/** This function tells if a param is active for the values of the earlier params. */
|
|
40
|
+
function isActive(p, values) {
|
|
41
|
+
if (p.when === void 0) return true;
|
|
42
|
+
if (!Object.hasOwn(values, p.when.param)) return false;
|
|
43
|
+
const v = values[p.when.param];
|
|
44
|
+
return p.when.in.some((x) => x === v);
|
|
45
|
+
}
|
|
46
|
+
/** 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. */
|
|
47
|
+
function paramProblem(param, value) {
|
|
48
|
+
return valueProblem(param, value);
|
|
49
|
+
}
|
|
50
|
+
/** This function tells if a param is active for the values of the earlier params. A param without `when` is always active. */
|
|
51
|
+
function paramActive(param, values) {
|
|
52
|
+
return isActive(param, values);
|
|
53
|
+
}
|
|
54
|
+
/** This function gives the stable form of a value of a param: a real number gets 12 significant digits, and `-0` becomes `0`. */
|
|
55
|
+
function paramValue(param, value) {
|
|
56
|
+
return normalize(param, value);
|
|
57
|
+
}
|
|
58
|
+
/** This function gives the part of a space that has a stage, and the prefix of the names of its stages. */
|
|
59
|
+
function partOf(space, stage) {
|
|
60
|
+
for (const part of space.parts ?? []) {
|
|
61
|
+
const head = `${part.engine}.`;
|
|
62
|
+
if (stage.startsWith(head)) {
|
|
63
|
+
const inner = partOf(part, stage.slice(head.length));
|
|
64
|
+
return {
|
|
65
|
+
space: inner.space,
|
|
66
|
+
prefix: head + inner.prefix
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
space,
|
|
72
|
+
prefix: ""
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** This function gives the prefix of the innermost part of each stage. */
|
|
76
|
+
const prefixes = (space) => new Map(space.stages.map((s) => [s.name, partOf(space, s.name).prefix]));
|
|
77
|
+
const describe = (c) => {
|
|
78
|
+
const ref = (r) => `${r.stage}=${r.option ?? "null"}`;
|
|
79
|
+
return "not" in c.then ? `if ${ref(c.if)} then not ${ref(c.then.not)}` : `if ${ref(c.if)} then ${ref(c.then)}`;
|
|
80
|
+
};
|
|
81
|
+
/** This function gives the problems of the range and the default of one param. */
|
|
82
|
+
function paramProblems(p, at) {
|
|
83
|
+
const out = [];
|
|
84
|
+
if (isNumeric(p)) {
|
|
85
|
+
const { min, max, step } = p;
|
|
86
|
+
if (typeof min !== "number" || typeof max !== "number" || !Number.isFinite(min) || !Number.isFinite(max)) {
|
|
87
|
+
out.push(["range", `${at} has no finite min and max`]);
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
if (min > max) out.push(["range", `${at} has an empty range: the min ${min} is more than the max ${max}`]);
|
|
91
|
+
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`]);
|
|
92
|
+
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`]);
|
|
93
|
+
if (p.scale === "log" && min <= 0) out.push(["scale", `${at} has the scale log with the min ${min}, which is not above 0`]);
|
|
94
|
+
} else {
|
|
95
|
+
if (p.scale !== void 0) out.push(["scale", `${at} is a ${p.kind} param with a scale`]);
|
|
96
|
+
if (p.kind === "enum") {
|
|
97
|
+
if (!Array.isArray(p.values) || p.values.length === 0) out.push(["values", `${at} is an enum without values`]);
|
|
98
|
+
else {
|
|
99
|
+
const seen = /* @__PURE__ */ new Set();
|
|
100
|
+
for (const v of p.values) {
|
|
101
|
+
if (seen.has(v)) out.push(["duplicate", `${at} has the value ${show(v)} two times`]);
|
|
102
|
+
seen.add(v);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (out.length === 0) {
|
|
108
|
+
const problem = valueProblem(p, p.default);
|
|
109
|
+
if (problem !== void 0) out.push(["default", `the default of ${at} is not in its range: ${problem}`]);
|
|
110
|
+
}
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
/** This function tells why a value is not a known shape kind, or gives `undefined` for a good kind. */
|
|
114
|
+
function kindProblem(k, kinds) {
|
|
115
|
+
if (typeof k !== "string" || k === "") return "is not a string with text";
|
|
116
|
+
if (kinds !== void 0 && !kinds.includes(k)) return `is not one of ${kinds.join(", ")}`;
|
|
117
|
+
}
|
|
118
|
+
/** This function gives the problems of one stage option. */
|
|
119
|
+
function optionProblems(stage, o, kinds) {
|
|
120
|
+
const out = [];
|
|
121
|
+
const at = `the option ${stage.name}.${o.id}`;
|
|
122
|
+
if (!Array.isArray(o.params)) return [["config", `${at} has no list of params`]];
|
|
123
|
+
const names = o.params.map((p) => p.name);
|
|
124
|
+
o.params.forEach((p, i) => {
|
|
125
|
+
const pat = `the param ${stage.name}.${o.id}.${p.name}`;
|
|
126
|
+
if (names.indexOf(p.name) !== i) out.push(["duplicate", `${at} has the param ${p.name} two times`]);
|
|
127
|
+
out.push(...paramProblems(p, pat));
|
|
128
|
+
if (p.when === void 0) return;
|
|
129
|
+
const j = names.indexOf(p.when.param);
|
|
130
|
+
if (j < 0) out.push(["unknown", `${pat} has a when with the unknown param ${p.when.param}`]);
|
|
131
|
+
else if (j >= i) out.push(["when", `${pat} has a when with the param ${p.when.param}, which does not come before it`]);
|
|
132
|
+
else if (!Array.isArray(p.when.in) || p.when.in.length === 0) out.push(["when", `${pat} has a when with no values`]);
|
|
133
|
+
else for (const v of p.when.in) {
|
|
134
|
+
const problem = valueProblem(o.params[j], v);
|
|
135
|
+
if (problem !== void 0) out.push(["when", `${pat} has a when value that is not a value of ${p.when.param}: ${problem}`]);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
if (o.supports !== void 0) out.push(...supportsProblems(o.supports, at, kinds));
|
|
139
|
+
if (o.members !== void 0) {
|
|
140
|
+
const { min, max, ordered } = o.members;
|
|
141
|
+
const noList = min === 0 && max === 0;
|
|
142
|
+
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`]);
|
|
143
|
+
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`]);
|
|
144
|
+
const pool = o.members.pool;
|
|
145
|
+
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`]);
|
|
146
|
+
const view = o.members.view;
|
|
147
|
+
if (view !== void 0 && view !== "bounds") out.push(["view", `${at} has the member view ${show(view)}, not "bounds"`]);
|
|
148
|
+
}
|
|
149
|
+
return out;
|
|
150
|
+
}
|
|
151
|
+
/** This function gives the problems of the `supports` of an option. */
|
|
152
|
+
function supportsProblems(supports, at, kinds) {
|
|
153
|
+
if (!isObject(supports)) return [["shape", `the supports of ${at} are not an object`]];
|
|
154
|
+
const shape = supports["shape"];
|
|
155
|
+
if (shape === void 0) return [];
|
|
156
|
+
if (!Array.isArray(shape) || shape.length === 0) return [["shape", `${at} supports no shape kind. Remove shape to support each kind`]];
|
|
157
|
+
const out = [];
|
|
158
|
+
shape.forEach((k, i) => {
|
|
159
|
+
const problem = kindProblem(k, kinds);
|
|
160
|
+
if (problem !== void 0) out.push(["shape", `${at} supports the shape kind ${show(k)}, which ${problem}`]);
|
|
161
|
+
else if (shape.indexOf(k) !== i) out.push(["duplicate", `${at} has the shape kind ${k} two times`]);
|
|
162
|
+
});
|
|
163
|
+
return out;
|
|
164
|
+
}
|
|
165
|
+
/** This function tells if an option supports each shape kind of the list. */
|
|
166
|
+
const supportsShapes = (o, shapes) => shapes === void 0 || o.supports?.shape === void 0 || shapes.every((k) => o.supports.shape.includes(k));
|
|
167
|
+
/** The shape kinds that the members of an option with `view: "bounds"` see: a bounding box is a `rect`. */
|
|
168
|
+
const BOUNDS_VIEW = /* @__PURE__ */ Object.freeze(["rect"]);
|
|
169
|
+
/**
|
|
170
|
+
* This function gives the shape kinds of the members of a meta option. With `view: "bounds"`, a list with a kind becomes `BOUNDS_VIEW`.
|
|
171
|
+
* Without a list, or with an empty list, the value does not change.
|
|
172
|
+
*/
|
|
173
|
+
const memberShapes = (rule, shapes) => rule.view === "bounds" && shapes !== void 0 && shapes.length > 0 ? BOUNDS_VIEW : shapes;
|
|
174
|
+
/** This function tells if two lists have the same shape kinds. */
|
|
175
|
+
const sameKinds = (a, b) => a.every((k) => b.includes(k)) && b.every((k) => a.includes(k));
|
|
176
|
+
/** This function gives the problems of the params of one stage choice. */
|
|
177
|
+
function choiceParamProblems(o, params, at) {
|
|
178
|
+
if (params !== void 0 && !isObject(params)) return [["config", `the params of ${at} are not an object`]];
|
|
179
|
+
const given = params ?? {};
|
|
180
|
+
const out = [];
|
|
181
|
+
for (const key of Object.keys(given)) if (!o.params.some((p) => p.name === key)) out.push(["unknown", `${at} has the unknown param ${key}`]);
|
|
182
|
+
const values = {};
|
|
183
|
+
for (const p of o.params) {
|
|
184
|
+
const has = Object.hasOwn(given, p.name);
|
|
185
|
+
if (!isActive(p, values)) {
|
|
186
|
+
if (has) out.push(["inactive", `${at} sets the param ${p.name}, which is not active`]);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
const v = has ? given[p.name] : p.default;
|
|
190
|
+
const problem = valueProblem(p, v);
|
|
191
|
+
if (problem !== void 0) out.push(["param", `the param ${p.name} of ${at} is not in its range: ${problem}`]);
|
|
192
|
+
else values[p.name] = normalize(p, v);
|
|
193
|
+
}
|
|
194
|
+
return out;
|
|
195
|
+
}
|
|
196
|
+
/** This function gives the problems of a config of a space. A member of a meta option has `member: true`. */
|
|
197
|
+
function configProblems(space, config, member, shapes) {
|
|
198
|
+
if (!isObject(config) || !isObject(config["stages"])) return [["config", "the config is not an object with stages"]];
|
|
199
|
+
const out = [];
|
|
200
|
+
const stages = config["stages"];
|
|
201
|
+
if (config["space"] !== space.engine) out.push(["space", `the space of the config is ${show(config["space"])}, not ${show(space.engine)}`]);
|
|
202
|
+
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}`]);
|
|
203
|
+
const optionOf = (s) => {
|
|
204
|
+
const c = stages[s.name];
|
|
205
|
+
return isObject(c) ? s.options.find((o) => o.id === c["option"]) : void 0;
|
|
206
|
+
};
|
|
207
|
+
const prefix = prefixes(space);
|
|
208
|
+
const metaStage = /* @__PURE__ */ new Map();
|
|
209
|
+
for (const s of space.stages) {
|
|
210
|
+
if (optionOf(s)?.members === void 0) continue;
|
|
211
|
+
const p = prefix.get(s.name);
|
|
212
|
+
if (metaStage.has(p)) out.push(["meta", `the stages ${metaStage.get(p)} and ${s.name} both have a meta option`]);
|
|
213
|
+
else metaStage.set(p, s.name);
|
|
214
|
+
}
|
|
215
|
+
const chosen = /* @__PURE__ */ new Map();
|
|
216
|
+
for (const s of space.stages) {
|
|
217
|
+
const c = Object.hasOwn(stages, s.name) ? stages[s.name] : void 0;
|
|
218
|
+
const meta = metaStage.get(prefix.get(s.name));
|
|
219
|
+
chosen.set(s.name, null);
|
|
220
|
+
if (c === void 0 || c === null) {
|
|
221
|
+
if (!s.optional && meta === void 0) out.push(["missing", `the config has no option for the stage ${s.name}`]);
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
if (!isObject(c) || typeof c["option"] !== "string") {
|
|
225
|
+
out.push(["config", `the choice of the stage ${s.name} has no option`]);
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
const o = s.options.find((x) => x.id === c["option"]);
|
|
229
|
+
if (o === void 0) {
|
|
230
|
+
out.push(["unknown", `the stage ${s.name} has no option ${c["option"]}`]);
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
chosen.set(s.name, o.id);
|
|
234
|
+
const at = `the option ${s.name}.${o.id}`;
|
|
235
|
+
if (!supportsShapes(o, shapes)) {
|
|
236
|
+
const missing = shapes.filter((k) => !o.supports.shape.includes(k));
|
|
237
|
+
out.push(["shape", `${at} supports the shape kinds ${o.supports.shape.join(", ")}, not ${missing.join(", ")}`]);
|
|
238
|
+
}
|
|
239
|
+
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`]);
|
|
240
|
+
out.push(...choiceParamProblems(o, c["params"], at));
|
|
241
|
+
if (o.members === void 0) {
|
|
242
|
+
if (c["members"] !== void 0) out.push(["members", `${at} is not a meta option, but the config gives members`]);
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (member) {
|
|
246
|
+
out.push(["depth", `a member has the meta option ${s.name}.${o.id}. The depth of a meta config is 1`]);
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
out.push(...memberProblems(partOf(space, s.name).space, o.members, c["members"], at, memberShapes(o.members, shapes)));
|
|
250
|
+
}
|
|
251
|
+
for (const c of space.constraints ?? []) if (!holds(c, chosen)) out.push(["constraint", `the config breaks the constraint ${describe(c)}`]);
|
|
252
|
+
return out;
|
|
253
|
+
}
|
|
254
|
+
/** This function gives the problems of the members of a meta option. `part` is the space of the members. */
|
|
255
|
+
function memberProblems(part, rule, members, at, shapes) {
|
|
256
|
+
if (!Array.isArray(members)) return [["members", `${at} is a meta option without a list of members`]];
|
|
257
|
+
const out = [];
|
|
258
|
+
if (members.length < rule.min || members.length > rule.max) out.push(["members", `${at} has ${members.length} members, not ${rule.min} to ${rule.max}`]);
|
|
259
|
+
const ids = /* @__PURE__ */ new Map();
|
|
260
|
+
members.forEach((m, k) => {
|
|
261
|
+
const inner = configProblems(part, m, true, shapes);
|
|
262
|
+
for (const [claim, detail] of inner) out.push([claim, `the member ${k} of ${at}: ${detail}`]);
|
|
263
|
+
if (inner.length > 0) return;
|
|
264
|
+
const id = idOf(canonicalOf(part, m, true));
|
|
265
|
+
if (ids.has(id)) out.push(["duplicate", `the members ${ids.get(id)} and ${k} of ${at} are the same config ${id}`]);
|
|
266
|
+
else ids.set(id, k);
|
|
267
|
+
});
|
|
268
|
+
if (rule.pool !== void 0 && out.length === 0) {
|
|
269
|
+
const key = memberKey(part, rule, members);
|
|
270
|
+
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"}`]);
|
|
271
|
+
}
|
|
272
|
+
return out;
|
|
273
|
+
}
|
|
274
|
+
/** This function gives the key of a good member list: the `configId` of each member, sorted when the rule is not ordered. */
|
|
275
|
+
function memberKey(part, rule, members) {
|
|
276
|
+
const ids = members.map((m) => idOf(canonicalOf(part, m, true)));
|
|
277
|
+
if (!rule.ordered) ids.sort();
|
|
278
|
+
return ids.join(",");
|
|
279
|
+
}
|
|
280
|
+
/** This function gives the problems of the pool of a meta option of a good space. `part` is the space of the members. */
|
|
281
|
+
function poolProblems(part, rule, at) {
|
|
282
|
+
const out = [];
|
|
283
|
+
const keys = /* @__PURE__ */ new Map();
|
|
284
|
+
const plain = {
|
|
285
|
+
min: rule.min,
|
|
286
|
+
max: rule.max,
|
|
287
|
+
ordered: rule.ordered
|
|
288
|
+
};
|
|
289
|
+
(rule.pool ?? []).forEach((entry, j) => {
|
|
290
|
+
const inner = memberProblems(part, plain, entry, `the pool entry ${j} of ${at}`);
|
|
291
|
+
out.push(...inner);
|
|
292
|
+
if (inner.length > 0) return;
|
|
293
|
+
const key = memberKey(part, rule, entry);
|
|
294
|
+
if (keys.has(key)) out.push(["duplicate", `the pool entries ${keys.get(key)} and ${j} of ${at} have the same members`]);
|
|
295
|
+
else keys.set(key, j);
|
|
296
|
+
});
|
|
297
|
+
return out;
|
|
298
|
+
}
|
|
299
|
+
/** 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. */
|
|
300
|
+
const fits = (part, entry) => entry.every((m) => configProblems(part, m, true).length === 0);
|
|
301
|
+
const toBreaks = (problems, inputs = []) => problems.map(([claim, detail]) => ({
|
|
302
|
+
claim,
|
|
303
|
+
detail,
|
|
304
|
+
inputs
|
|
305
|
+
}));
|
|
306
|
+
/**
|
|
307
|
+
* This function checks one config of a space. It gives one break for each problem. An empty list tells that the config is good.
|
|
308
|
+
* The `claim` of a break is the kind of the problem, for example `missing`, `unknown`, `inactive`, `param`, `constraint`, `members` or `depth`.
|
|
309
|
+
*
|
|
310
|
+
* A member list that is not an entry of the `pool` of its option gives the claim `pool`.
|
|
311
|
+
* 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.
|
|
312
|
+
* A member of an option with `view: "bounds"` gets the check with the kind `rect`.
|
|
313
|
+
*/
|
|
314
|
+
function checkConfig(space, config, options = {}) {
|
|
315
|
+
return toBreaks(configProblems(space, config, false, options.shapes));
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* This function checks a space. It gives one break for each problem. An empty list tells that the space is good.
|
|
319
|
+
* It finds duplicate names, empty ranges, defaults out of range, enums without values, bad log scales, unknown names and bad named configs.
|
|
320
|
+
* It also checks each entry of a `pool`: the count of its members, each member and a repeated entry.
|
|
321
|
+
* The `inputs` of a break of a named config have the name of the config.
|
|
322
|
+
* With `kinds`, a shape kind of a `supports` that is not in the list gives the claim `shape`.
|
|
323
|
+
*/
|
|
324
|
+
function checkSpace(space, kindOptions = {}) {
|
|
325
|
+
if (!isObject(space) || !Array.isArray(space.stages)) return toBreaks([["schema", "the space is not an object with stages"]]);
|
|
326
|
+
const out = [];
|
|
327
|
+
if (space.schema !== "strategy-space/1") out.push(["schema", `the schema is ${show(space.schema)}, not ${STRATEGY_SPACE_SCHEMA}`]);
|
|
328
|
+
if (typeof space.engine !== "string" || space.engine === "") out.push(["schema", "the space has no engine name"]);
|
|
329
|
+
if (typeof space.version !== "string") out.push(["schema", "the space has no version"]);
|
|
330
|
+
const names = space.stages.map((s) => s.name);
|
|
331
|
+
space.stages.forEach((s, i) => {
|
|
332
|
+
if (names.indexOf(s.name) !== i) out.push(["duplicate", `the space has the stage ${s.name} two times`]);
|
|
333
|
+
const options = Array.isArray(s.options) ? s.options : [];
|
|
334
|
+
if (options.length === 0) {
|
|
335
|
+
out.push(["options", `the stage ${s.name} has no options`]);
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
const ids = options.map((o) => o.id);
|
|
339
|
+
options.forEach((o, j) => {
|
|
340
|
+
if (ids.indexOf(o.id) !== j) out.push(["duplicate", `the stage ${s.name} has the option ${o.id} two times`]);
|
|
341
|
+
out.push(...optionProblems(s, o, kindOptions.kinds));
|
|
342
|
+
});
|
|
343
|
+
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`]);
|
|
344
|
+
});
|
|
345
|
+
const known = (ref, at) => {
|
|
346
|
+
const s = space.stages.find((x) => x.name === ref?.stage);
|
|
347
|
+
if (s === void 0) out.push(["unknown", `${at} has the unknown stage ${ref?.stage}`]);
|
|
348
|
+
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"}`]);
|
|
349
|
+
};
|
|
350
|
+
(space.constraints ?? []).forEach((c, k) => {
|
|
351
|
+
known(c.if, `the constraint ${k}`);
|
|
352
|
+
known("not" in c.then ? c.then.not : c.then, `the constraint ${k}`);
|
|
353
|
+
});
|
|
354
|
+
for (const part of space.parts ?? []) for (const b of checkSpace(part, kindOptions)) out.push([b.claim, `the part ${part.engine}: ${b.detail}`]);
|
|
355
|
+
if (space.parts !== void 0) {
|
|
356
|
+
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`]);
|
|
357
|
+
}
|
|
358
|
+
const breaks = toBreaks(out);
|
|
359
|
+
if (breaks.length > 0) return breaks;
|
|
360
|
+
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}`)));
|
|
361
|
+
if (breaks.length > 0) return breaks;
|
|
362
|
+
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]));
|
|
363
|
+
return breaks;
|
|
364
|
+
}
|
|
365
|
+
/** This function gives the canonical form of a config that is known to be good. */
|
|
366
|
+
function canonicalOf(space, config, member) {
|
|
367
|
+
const stages = {};
|
|
368
|
+
for (const s of space.stages) {
|
|
369
|
+
const c = Object.hasOwn(config.stages, s.name) ? config.stages[s.name] : void 0;
|
|
370
|
+
if (c === void 0 || c === null) {
|
|
371
|
+
stages[s.name] = null;
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
const o = s.options.find((x) => x.id === c.option);
|
|
375
|
+
const given = c.params ?? {};
|
|
376
|
+
const params = {};
|
|
377
|
+
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);
|
|
378
|
+
if (o.members === void 0 || member) {
|
|
379
|
+
stages[s.name] = {
|
|
380
|
+
option: o.id,
|
|
381
|
+
params
|
|
382
|
+
};
|
|
383
|
+
continue;
|
|
384
|
+
}
|
|
385
|
+
const part = partOf(space, s.name).space;
|
|
386
|
+
const members = (c.members ?? []).map((m) => canonicalOf(part, m, true));
|
|
387
|
+
if (!o.members.ordered) members.sort((a, b) => idOf(a) < idOf(b) ? -1 : idOf(a) > idOf(b) ? 1 : 0);
|
|
388
|
+
stages[s.name] = {
|
|
389
|
+
option: o.id,
|
|
390
|
+
params,
|
|
391
|
+
members
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
return {
|
|
395
|
+
space: space.engine,
|
|
396
|
+
stages
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
const idOf = (canonical) => sha256Hex(canonicalJson(canonical, { form: "json" }), { input: "wtf8" }).slice(0, 16);
|
|
400
|
+
/**
|
|
401
|
+
* This function gives the canonical form of a config: each stage of the space, the active params only, the defaults, and stable numbers.
|
|
402
|
+
* An unused optional stage is `null`. The members of an unordered meta option are sorted by their `configId`.
|
|
403
|
+
* It throws an `Error` for a config with a break of `checkConfig`.
|
|
404
|
+
*/
|
|
405
|
+
function canonicalConfig(space, config) {
|
|
406
|
+
const problems = configProblems(space, config, false);
|
|
407
|
+
if (problems.length > 0) throw new Error(`The config of the space ${space.engine} is not good: ${problems.map((p) => p[1]).join("; ")}.`);
|
|
408
|
+
return canonicalOf(space, config, false);
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* This function gives the id of a config: the first 16 hex characters of the SHA-256 of `canonicalJson` of `canonicalConfig`.
|
|
412
|
+
* Two spellings of one config get one id. The version of the space is not part of the id.
|
|
413
|
+
*/
|
|
414
|
+
function configId(space, config) {
|
|
415
|
+
return idOf(canonicalConfig(space, config));
|
|
416
|
+
}
|
|
417
|
+
/** This function throws an `Error` for a space with a break of `checkSpace`. */
|
|
418
|
+
function assertSpace(space, kinds) {
|
|
419
|
+
const breaks = checkSpace(space, kinds === void 0 ? {} : { kinds });
|
|
420
|
+
if (breaks.length > 0) throw new Error(`The space ${String(space?.engine)} is not good: ${breaks.map((b) => b.detail).join("; ")}.`);
|
|
421
|
+
}
|
|
422
|
+
/** This function throws a `RangeError` when a value is not a whole number of `min` or more. */
|
|
423
|
+
function assertWhole(value, name, min) {
|
|
424
|
+
if (!Number.isInteger(value) || value < min) throw new RangeError(`The option ${name} is ${value}, not a whole number of ${min} or more.`);
|
|
425
|
+
}
|
|
426
|
+
/** This function gives the last point of the step grid of a numeric param. */
|
|
427
|
+
function lastPoint(p) {
|
|
428
|
+
const min = p.min;
|
|
429
|
+
const step = p.step ?? (p.kind === "int" ? 1 : void 0);
|
|
430
|
+
if (step === void 0) return p.max;
|
|
431
|
+
return min + Math.floor((p.max - min) / step + 1e-9) * step;
|
|
432
|
+
}
|
|
433
|
+
/** This function moves a value of a numeric param onto its step and into its range. */
|
|
434
|
+
function snap(p, x, digits) {
|
|
435
|
+
const min = p.min;
|
|
436
|
+
const step = p.step ?? (p.kind === "int" ? 1 : void 0);
|
|
437
|
+
let v = step === void 0 ? x : min + Math.round((x - min) / step) * step;
|
|
438
|
+
v = Math.min(lastPoint(p), Math.max(min, v));
|
|
439
|
+
return p.kind === "int" ? zero(Math.round(v)) : Math.min(p.max, Math.max(min, realForm(v, digits)));
|
|
440
|
+
}
|
|
441
|
+
/** This function gives the point at the fraction `t` of the range of a numeric param, on its scale. */
|
|
442
|
+
function at01(p, t) {
|
|
443
|
+
const min = p.min;
|
|
444
|
+
const max = p.max;
|
|
445
|
+
if (p.scale === "log") return Math.exp(Math.log(min) + t * (Math.log(max) - Math.log(min)));
|
|
446
|
+
return min + t * (max - min);
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* This function gives the grid values of one param, in increasing order. An `enum` gives its values, and a `bool` gives `false` and `true`.
|
|
450
|
+
* 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.
|
|
451
|
+
*/
|
|
452
|
+
function paramLevels(param, levels) {
|
|
453
|
+
assertWhole(levels, "levels", 1);
|
|
454
|
+
if (param.kind === "bool") return [false, true];
|
|
455
|
+
if (param.kind === "enum") return [...param.values ?? []];
|
|
456
|
+
if (levels === 1 || param.min === param.max) return [normalize(param, param.default)];
|
|
457
|
+
const out = [];
|
|
458
|
+
const steps = param.kind === "int" || param.step !== void 0 ? Math.round((lastPoint(param) - param.min) / (param.step ?? 1)) + 1 : Infinity;
|
|
459
|
+
if (steps <= levels && param.scale !== "log") for (let j = 0; j < steps; j++) out.push(snap(param, param.min + j * (param.step ?? 1), 12));
|
|
460
|
+
else for (let j = 0; j < levels; j++) out.push(snap(param, at01(param, j / (levels - 1)), 12));
|
|
461
|
+
return [...new Set(out)];
|
|
462
|
+
}
|
|
463
|
+
/** This function tells if a later param of the list reads a param in its `when`. */
|
|
464
|
+
const readLater = (params, i) => params.slice(i + 1).some((q) => q.when?.param === params[i].name);
|
|
465
|
+
/** This function gives the count of the grid assignments of the params of an option, with only the active params. */
|
|
466
|
+
function countParams(params, levels, i = 0, values = {}) {
|
|
467
|
+
if (i === params.length) return 1n;
|
|
468
|
+
const p = params[i];
|
|
469
|
+
if (!isActive(p, values)) return countParams(params, levels, i + 1, values);
|
|
470
|
+
const lv = paramLevels(p, levels);
|
|
471
|
+
if (!readLater(params, i)) return BigInt(lv.length) * countParams(params, levels, i + 1, values);
|
|
472
|
+
let sum = 0n;
|
|
473
|
+
for (const v of lv) sum += countParams(params, levels, i + 1, {
|
|
474
|
+
...values,
|
|
475
|
+
[p.name]: v
|
|
476
|
+
});
|
|
477
|
+
return sum;
|
|
478
|
+
}
|
|
479
|
+
/** This function gives the grid assignments of the params of an option, in a stable order. */
|
|
480
|
+
function* gridParams(params, levels, i = 0, values = {}) {
|
|
481
|
+
if (i === params.length) {
|
|
482
|
+
yield { ...values };
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
const p = params[i];
|
|
486
|
+
if (!isActive(p, values)) {
|
|
487
|
+
yield* gridParams(params, levels, i + 1, values);
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
for (const v of paramLevels(p, levels)) yield* gridParams(params, levels, i + 1, {
|
|
491
|
+
...values,
|
|
492
|
+
[p.name]: v
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
const optionIds = (choices) => new Map([...choices].map(([k, o]) => [k, o?.id ?? null]));
|
|
496
|
+
const keeps = (space, choices) => (space.constraints ?? []).every((c) => holds(c, optionIds(choices)));
|
|
497
|
+
/**
|
|
498
|
+
* This function gives the constraints of a space for each stage: a constraint goes to the last stage that it names.
|
|
499
|
+
* The last list holds each constraint that names a stage outside the space, so a walk examines it at the end.
|
|
500
|
+
*/
|
|
501
|
+
function constraintsByStage(space) {
|
|
502
|
+
const place = new Map(space.stages.map((s, i) => [s.name, i]));
|
|
503
|
+
const lists = Array.from({ length: space.stages.length + 1 }, () => []);
|
|
504
|
+
for (const c of space.constraints ?? []) {
|
|
505
|
+
const named = [c.if.stage, "not" in c.then ? c.then.not.stage : c.then.stage];
|
|
506
|
+
lists[Math.max(...named.map((name) => place.get(name) ?? space.stages.length))].push(c);
|
|
507
|
+
}
|
|
508
|
+
return lists;
|
|
509
|
+
}
|
|
510
|
+
/** This function gives the count of the member lists of a meta option without a pool from `base` base configs, or `null` above `limit`. */
|
|
511
|
+
function memberLists(base, rule, limit) {
|
|
512
|
+
let sum = 0n;
|
|
513
|
+
for (let m = rule.min; m <= rule.max; m++) {
|
|
514
|
+
let n = 1n;
|
|
515
|
+
for (let k = 0; k < m; k++) {
|
|
516
|
+
n = n * (base - BigInt(k)) / (rule.ordered ? 1n : BigInt(k + 1));
|
|
517
|
+
if (n <= 0n) break;
|
|
518
|
+
}
|
|
519
|
+
if (n <= 0n) break;
|
|
520
|
+
sum += n;
|
|
521
|
+
if (sum > limit) return null;
|
|
522
|
+
}
|
|
523
|
+
return sum;
|
|
524
|
+
}
|
|
525
|
+
/** This function multiplies two counts. A `null` count or a product above `limit` gives `null`. */
|
|
526
|
+
const times = (a, b, limit) => a === null || b === null ? null : a * b > limit ? null : a * b;
|
|
527
|
+
/**
|
|
528
|
+
* This function gives the combinations of options of a space and their grid counts.
|
|
529
|
+
* A product space joins the combinations of its parts. Each combination keeps the constraints.
|
|
530
|
+
*/
|
|
531
|
+
function combos(space, levels, meta, limit) {
|
|
532
|
+
if (space.parts !== void 0 && space.parts.length > 0) {
|
|
533
|
+
let acc = [{
|
|
534
|
+
choices: /* @__PURE__ */ new Map(),
|
|
535
|
+
base: true,
|
|
536
|
+
weight: 1n
|
|
537
|
+
}];
|
|
538
|
+
for (const part of space.parts) {
|
|
539
|
+
const head = `${part.engine}.`;
|
|
540
|
+
const own = combos(part, levels, meta, limit);
|
|
541
|
+
const next = [];
|
|
542
|
+
for (const a of acc) for (const b of own) {
|
|
543
|
+
const choices = new Map(a.choices);
|
|
544
|
+
for (const [k, o] of b.choices) choices.set(head + k, o);
|
|
545
|
+
const base = a.base && b.base;
|
|
546
|
+
next.push({
|
|
547
|
+
choices,
|
|
548
|
+
base,
|
|
549
|
+
weight: base ? a.weight * b.weight : times(a.weight, b.weight, limit)
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
acc = next;
|
|
553
|
+
}
|
|
554
|
+
return acc.filter((c) => keeps(space, c.choices));
|
|
555
|
+
}
|
|
556
|
+
const counts = /* @__PURE__ */ new Map();
|
|
557
|
+
const countOf = (o) => {
|
|
558
|
+
if (!counts.has(o)) counts.set(o, countParams(o.params, levels));
|
|
559
|
+
return counts.get(o);
|
|
560
|
+
};
|
|
561
|
+
const out = [];
|
|
562
|
+
const stages = space.stages;
|
|
563
|
+
const checks = constraintsByStage(space);
|
|
564
|
+
const ids = /* @__PURE__ */ new Map();
|
|
565
|
+
const walk = (i, choices, weight) => {
|
|
566
|
+
if (i === stages.length) {
|
|
567
|
+
if (checks[i].every((c) => holds(c, ids))) out.push({
|
|
568
|
+
choices: new Map(choices),
|
|
569
|
+
base: true,
|
|
570
|
+
weight
|
|
571
|
+
});
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
const s = stages[i];
|
|
575
|
+
const list = s.options.filter((o) => o.members === void 0);
|
|
576
|
+
if (s.optional) list.push(null);
|
|
577
|
+
for (const o of list) {
|
|
578
|
+
choices.set(s.name, o);
|
|
579
|
+
ids.set(s.name, o?.id ?? null);
|
|
580
|
+
if (checks[i].every((c) => holds(c, ids))) walk(i + 1, choices, o === null ? weight : weight * countOf(o));
|
|
581
|
+
}
|
|
582
|
+
choices.delete(s.name);
|
|
583
|
+
ids.delete(s.name);
|
|
584
|
+
};
|
|
585
|
+
walk(0, /* @__PURE__ */ new Map(), 1n);
|
|
586
|
+
if (!meta) return out;
|
|
587
|
+
const base = out.reduce((sum, c) => sum + c.weight, 0n);
|
|
588
|
+
for (const s of stages) for (const o of s.options) {
|
|
589
|
+
if (o.members === void 0) continue;
|
|
590
|
+
const choices = new Map(stages.map((x) => [x.name, x === s ? o : null]));
|
|
591
|
+
const own = memberSpaces.get(o);
|
|
592
|
+
const from = own === void 0 ? base : baseCount(own, levels, limit);
|
|
593
|
+
const lists = o.members.pool === void 0 ? memberLists(from, o.members, limit) : BigInt(o.members.pool.length);
|
|
594
|
+
if (keeps(space, choices)) out.push({
|
|
595
|
+
choices,
|
|
596
|
+
base: false,
|
|
597
|
+
weight: times(countOf(o), lists, limit)
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
return out;
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* This function gives the count of the grid points of a space, with `levels` levels for each numeric param.
|
|
604
|
+
* It counts only the active params and the combinations that keep the constraints. It gives the meta configs apart.
|
|
605
|
+
* A meta option with a `pool` counts each entry of the pool one time.
|
|
606
|
+
* With `shapes`, it counts only the options that support each kind of the list.
|
|
607
|
+
*/
|
|
608
|
+
function spaceSize(space, options) {
|
|
609
|
+
assertSpace(space, options.kinds);
|
|
610
|
+
assertWhole(options.levels, "levels", 1);
|
|
611
|
+
assertShapes(options.shapes, options.kinds);
|
|
612
|
+
return sizeOf(restrict(space, options.shapes), options.levels, options.maxMeta ?? 10n ** 18n);
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* This function gives the space with only the options that support each shape kind of `shapes`. Without `shapes`, it gives the same space.
|
|
616
|
+
* A stage can lose all of its options. Then the space has no config with that stage.
|
|
617
|
+
* A pool keeps only the entries whose members fit the smaller space. A meta option without such an entry goes out.
|
|
618
|
+
*
|
|
619
|
+
* The members of an option with `view: "bounds"` come from the space for the kinds of `memberShapes`. `memberSpaces` keeps that space.
|
|
620
|
+
*/
|
|
621
|
+
function restrict(space, shapes) {
|
|
622
|
+
if (shapes === void 0) return space;
|
|
623
|
+
const kept = {
|
|
624
|
+
...space,
|
|
625
|
+
stages: space.stages.map((s) => ({
|
|
626
|
+
...s,
|
|
627
|
+
options: s.options.filter((o) => supportsShapes(o, shapes))
|
|
628
|
+
})),
|
|
629
|
+
...space.parts === void 0 ? {} : { parts: space.parts.map((p) => restrict(p, shapes)) }
|
|
630
|
+
};
|
|
631
|
+
const withPool = (s, o) => {
|
|
632
|
+
if (o.members === void 0) return [o];
|
|
633
|
+
const inner = memberShapes(o.members, shapes);
|
|
634
|
+
const own = sameKinds(inner, shapes) ? void 0 : restrict(partOf(space, s.name).space, inner);
|
|
635
|
+
const mark = (option) => {
|
|
636
|
+
if (own !== void 0) memberSpaces.set(option, own);
|
|
637
|
+
return [option];
|
|
638
|
+
};
|
|
639
|
+
if (o.members.pool === void 0) return own === void 0 ? [o] : mark({ ...o });
|
|
640
|
+
const part = own ?? partOf(kept, s.name).space;
|
|
641
|
+
const pool = o.members.pool.filter((entry) => fits(part, entry));
|
|
642
|
+
return pool.length === 0 ? [] : mark({
|
|
643
|
+
...o,
|
|
644
|
+
members: {
|
|
645
|
+
...o.members,
|
|
646
|
+
pool
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
};
|
|
650
|
+
return {
|
|
651
|
+
...kept,
|
|
652
|
+
stages: kept.stages.map((s) => ({
|
|
653
|
+
...s,
|
|
654
|
+
options: s.options.flatMap((o) => withPool(s, o))
|
|
655
|
+
}))
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
/** 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. */
|
|
659
|
+
const memberSpaces = /* @__PURE__ */ new WeakMap();
|
|
660
|
+
/**
|
|
661
|
+
* 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.
|
|
662
|
+
* A different meta option has one when its members have a base config. `restrict` keeps only the pool entries that fit.
|
|
663
|
+
*/
|
|
664
|
+
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;
|
|
665
|
+
/** 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. */
|
|
666
|
+
const hasConfig = (space, meta) => combos(space, 1, meta, 0n).some((c) => c.base || [...c.choices].every(([stage, o]) => o === null || hasMembers(space, stage, o)));
|
|
667
|
+
/** 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. */
|
|
668
|
+
function noSample(space, kept, shapes, meta) {
|
|
669
|
+
const list = (names, one, more) => names.length === 1 ? `${one} ${names[0]}` : `${more} ${names.join(", ")}`;
|
|
670
|
+
const empty = kept.stages.filter((s) => !s.optional && !s.options.some((o) => meta || o.members === void 0)).map((s) => s.name);
|
|
671
|
+
const removed = !meta ? [] : space.stages.flatMap((s) => s.options.filter((o) => o.members !== void 0 && o.members.view !== "bounds" && supportsShapes(o, shapes)).filter((o) => {
|
|
672
|
+
const own = kept.stages.find((x) => x.name === s.name)?.options.find((x) => x.id === o.id);
|
|
673
|
+
return own === void 0 || !hasMembers(kept, s.name, own);
|
|
674
|
+
}).map((o) => `${s.name}.${o.id}`));
|
|
675
|
+
let text = `The space ${space.engine} has no config for a sample with the shape kinds ${shapes.join(", ")}.`;
|
|
676
|
+
if (empty.length > 0) text += ` ${list(empty, "The stage", "The stages")} ${empty.length === 1 ? "has" : "have"} no option for these kinds.`;
|
|
677
|
+
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.`;
|
|
678
|
+
if (empty.length === 0 && removed.length === 0) text += " The constraints remove each config.";
|
|
679
|
+
return new RangeError(text);
|
|
680
|
+
}
|
|
681
|
+
/** This function gives a constraint in words, for the reason of an error. */
|
|
682
|
+
function constraintText(c) {
|
|
683
|
+
const ref = (r) => `${r.stage} ${r.option === null ? "has no option" : `is ${r.option}`}`;
|
|
684
|
+
return "not" in c.then ? `if ${ref(c.if)}, then not ${ref(c.then.not)}` : `if ${ref(c.if)}, then ${ref(c.then)}`;
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* 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.
|
|
688
|
+
* A meta option without `meta: "sample"` is bad, and so are two fixed options that a constraint forbids.
|
|
689
|
+
*/
|
|
690
|
+
function assertFixed(space, fixed, meta) {
|
|
691
|
+
for (const [stage, id] of Object.entries(fixed)) {
|
|
692
|
+
const s = space.stages.find((x) => x.name === stage);
|
|
693
|
+
if (s === void 0) throw new RangeError(`The option fixed names the stage ${stage}, and the space ${space.engine} has no such stage.`);
|
|
694
|
+
const o = s.options.find((x) => x.id === id);
|
|
695
|
+
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.`);
|
|
696
|
+
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".`);
|
|
697
|
+
}
|
|
698
|
+
const ids = new Map(Object.entries(fixed));
|
|
699
|
+
const other = (c) => "not" in c.then ? c.then.not.stage : c.then.stage;
|
|
700
|
+
const broken = (space.constraints ?? []).filter((c) => ids.has(c.if.stage) && ids.has(other(c)) && !holds(c, ids));
|
|
701
|
+
if (broken.length > 0) {
|
|
702
|
+
const names = Object.entries(fixed).map(([stage, id]) => `${stage}.${id}`);
|
|
703
|
+
throw new RangeError(`The constraints of the space ${space.engine} forbid the fixed options ${names.join(", ")}. The broken constraints: ${broken.map(constraintText).join("; ")}.`);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
* 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.
|
|
708
|
+
* 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.
|
|
709
|
+
*/
|
|
710
|
+
function fixStages(space, kept, fixed, shapes) {
|
|
711
|
+
const prefix = prefixes(kept);
|
|
712
|
+
const parts = new Set(Object.keys(fixed).map((stage) => prefix.get(stage)));
|
|
713
|
+
const stages = kept.stages.map((s) => {
|
|
714
|
+
const id = fixed[s.name];
|
|
715
|
+
if (id === void 0) return parts.has(prefix.get(s.name)) ? {
|
|
716
|
+
...s,
|
|
717
|
+
options: s.options.filter((o) => o.members === void 0)
|
|
718
|
+
} : s;
|
|
719
|
+
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));
|
|
720
|
+
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(", ")}.`);
|
|
721
|
+
return {
|
|
722
|
+
...s,
|
|
723
|
+
options,
|
|
724
|
+
optional: false
|
|
725
|
+
};
|
|
726
|
+
});
|
|
727
|
+
return {
|
|
728
|
+
...kept,
|
|
729
|
+
stages
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
/** This function gives a copy of a meta option whose members come from `members`. */
|
|
733
|
+
function withMemberSpace(o, members) {
|
|
734
|
+
const copy = { ...o };
|
|
735
|
+
memberSpaces.set(copy, members);
|
|
736
|
+
return copy;
|
|
737
|
+
}
|
|
738
|
+
/** 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. */
|
|
739
|
+
function noFixedSample(space, fixed) {
|
|
740
|
+
const names = Object.entries(fixed).map(([stage, id]) => `${stage}.${id}`);
|
|
741
|
+
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.`);
|
|
742
|
+
}
|
|
743
|
+
/** This function gives the count of the base configs of a space that is known to be good. */
|
|
744
|
+
const baseCount = (space, levels, limit) => combos(space, levels, false, limit).reduce((sum, c) => sum + c.weight, 0n);
|
|
745
|
+
/** This function throws a `RangeError` for a list of shape kinds with a kind that is not known. */
|
|
746
|
+
function assertShapes(shapes, kinds) {
|
|
747
|
+
if (shapes === void 0) return;
|
|
748
|
+
if (!Array.isArray(shapes)) throw new RangeError("The option shapes is not a list of shape kinds.");
|
|
749
|
+
for (const k of shapes) {
|
|
750
|
+
const problem = kindProblem(k, kinds);
|
|
751
|
+
if (problem !== void 0) throw new RangeError(`The shape kind ${show(k)} ${problem}.`);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
/** This function gives the count of the grid points of a space that is known to be good. */
|
|
755
|
+
function sizeOf(space, levels, limit) {
|
|
756
|
+
let base = 0n;
|
|
757
|
+
let meta = 0n;
|
|
758
|
+
for (const c of combos(space, levels, true, limit)) if (c.base) base += c.weight;
|
|
759
|
+
else meta = meta === null || c.weight === null || meta + c.weight > limit ? null : meta + c.weight;
|
|
760
|
+
return {
|
|
761
|
+
base,
|
|
762
|
+
meta
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
/** This function gives the cartesian product of lists, with the last list as the fastest. */
|
|
766
|
+
function* product(lists, i = 0, acc = []) {
|
|
767
|
+
if (i === lists.length) {
|
|
768
|
+
yield [...acc];
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
for (const x of lists[i]) {
|
|
772
|
+
acc.push(x);
|
|
773
|
+
yield* product(lists, i + 1, acc);
|
|
774
|
+
acc.pop();
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
/** This function gives the configs of the grid of a space, without meta options. */
|
|
778
|
+
function gridConfigs(space, levels, limit, shapes) {
|
|
779
|
+
const kept = restrict(space, shapes);
|
|
780
|
+
const size = sizeOf(kept, levels, 0n).base;
|
|
781
|
+
if (size > BigInt(limit)) throw new RangeError(`The grid of the space ${space.engine} has ${size} configs, more than the limit ${limit}.`);
|
|
782
|
+
const out = [];
|
|
783
|
+
for (const c of combos(kept, levels, false, 0n)) {
|
|
784
|
+
const names = [...c.choices.keys()];
|
|
785
|
+
const lists = names.map((k) => {
|
|
786
|
+
const o = c.choices.get(k);
|
|
787
|
+
return o === null ? [null] : [...gridParams(o.params, levels)].map((params) => ({
|
|
788
|
+
option: o.id,
|
|
789
|
+
params
|
|
790
|
+
}));
|
|
791
|
+
});
|
|
792
|
+
for (const row of product(lists)) out.push(canonicalOf(space, {
|
|
793
|
+
space: space.engine,
|
|
794
|
+
stages: Object.fromEntries(names.map((k, i) => [k, row[i]]))
|
|
795
|
+
}, false));
|
|
796
|
+
}
|
|
797
|
+
return out;
|
|
798
|
+
}
|
|
799
|
+
/** This function gives a random order of the numbers from 0 to `n - 1`. */
|
|
800
|
+
function permutation(n, r) {
|
|
801
|
+
const out = Array.from({ length: n }, (_, i) => i);
|
|
802
|
+
for (let i = n - 1; i > 0; i--) {
|
|
803
|
+
const j = Math.floor(r() * (i + 1));
|
|
804
|
+
[out[i], out[j]] = [out[j], out[i]];
|
|
805
|
+
}
|
|
806
|
+
return out;
|
|
807
|
+
}
|
|
808
|
+
/** This function gives the value of a param at the fraction `u` of its range. A sampled real number has 6 significant digits. */
|
|
809
|
+
function sampleValue(p, u) {
|
|
810
|
+
if (p.kind === "bool") return u >= .5;
|
|
811
|
+
if (p.kind === "enum") {
|
|
812
|
+
const values = p.values;
|
|
813
|
+
return values[Math.min(values.length - 1, Math.floor(u * values.length))];
|
|
814
|
+
}
|
|
815
|
+
if (p.min === p.max) return normalize(p, p.min);
|
|
816
|
+
if (p.scale !== "log" && (p.kind === "int" || p.step !== void 0)) {
|
|
817
|
+
const steps = Math.round((lastPoint(p) - p.min) / (p.step ?? 1)) + 1;
|
|
818
|
+
return snap(p, p.min + Math.min(steps - 1, Math.floor(u * steps)) * (p.step ?? 1), 6);
|
|
819
|
+
}
|
|
820
|
+
return snap(p, at01(p, u), 6);
|
|
821
|
+
}
|
|
822
|
+
/** This function gives a seed of 32 bits from a seed and a step name. */
|
|
823
|
+
const subSeed = (seed, step) => Math.floor(stepRandom(seed, step)() * 4294967296);
|
|
824
|
+
/**
|
|
825
|
+
* This function gives a sample of a space: Latin hypercube strata over the options, the params and the member counts.
|
|
826
|
+
* `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.
|
|
827
|
+
*/
|
|
828
|
+
function sampleConfigs(space, n, seed, meta, maxTries, canon = space, walk = false, first = /* @__PURE__ */ new Set()) {
|
|
829
|
+
const stages = space.stages;
|
|
830
|
+
const prefix = prefixes(space);
|
|
831
|
+
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)));
|
|
832
|
+
const lists = stages.map((s) => {
|
|
833
|
+
const list = s.options.filter((o) => meta || o.members === void 0);
|
|
834
|
+
if (s.optional) list.push(null);
|
|
835
|
+
if (list.length === 0 && bounded.has(prefix.get(s.name))) list.push(null);
|
|
836
|
+
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.`);
|
|
837
|
+
return list;
|
|
838
|
+
});
|
|
839
|
+
const usable = /* @__PURE__ */ new Map();
|
|
840
|
+
const withMembers = (s, o) => {
|
|
841
|
+
if (o === null || o === void 0 || o.members === void 0) return true;
|
|
842
|
+
if (!usable.has(o)) usable.set(o, hasMembers(space, s.name, o));
|
|
843
|
+
return usable.get(o);
|
|
844
|
+
};
|
|
845
|
+
const plan = walk ? walkPlan(stages, lists, space.constraints ?? [], n, first) : null;
|
|
846
|
+
const points = [];
|
|
847
|
+
let tries = 0;
|
|
848
|
+
for (let round = 0; points.length < n; round++) {
|
|
849
|
+
const r = stepRandom(seed, `options:${round}`);
|
|
850
|
+
const picks = walk ? lists.map(() => permutation(n, r)) : [];
|
|
851
|
+
const columns = walk ? [] : lists.map((list) => permutation(n, r).map((p) => list[Math.floor(p * list.length / n)]));
|
|
852
|
+
for (let i = 0; i < n && points.length < n; i++) {
|
|
853
|
+
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."}`);
|
|
854
|
+
tries++;
|
|
855
|
+
const choices = plan !== null ? walkPoint(plan, picks, i) : new Map(stages.map((s, k) => [s.name, columns[k][i]]));
|
|
856
|
+
const metaPart = /* @__PURE__ */ new Map();
|
|
857
|
+
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);
|
|
858
|
+
for (const s of stages) {
|
|
859
|
+
const m = metaPart.get(prefix.get(s.name));
|
|
860
|
+
if (m !== void 0 && m !== s.name) choices.set(s.name, null);
|
|
861
|
+
}
|
|
862
|
+
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);
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
const values = points.map(() => stages.map(() => ({})));
|
|
866
|
+
const members = points.map(() => stages.map(() => []));
|
|
867
|
+
stages.forEach((s, k) => {
|
|
868
|
+
for (const o of s.options) {
|
|
869
|
+
const users = points.flatMap((c, i) => c.get(s.name) === o ? [i] : []);
|
|
870
|
+
if (users.length === 0) continue;
|
|
871
|
+
for (const p of o.params) {
|
|
872
|
+
const active = users.filter((i) => isActive(p, values[i][k]));
|
|
873
|
+
const r = stepRandom(seed, `param:${s.name}:${o.id}:${p.name}`);
|
|
874
|
+
permutation(active.length, r).forEach((stratum, j) => {
|
|
875
|
+
values[active[j]][k][p.name] = sampleValue(p, (stratum + r()) / active.length);
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
if (o.members === void 0 || o.members.max === 0) continue;
|
|
879
|
+
const fixed = o.members.pool;
|
|
880
|
+
if (fixed !== void 0) {
|
|
881
|
+
const r = stepRandom(seed, `pool-entry:${s.name}:${o.id}`);
|
|
882
|
+
permutation(users.length, r).forEach((stratum, j) => {
|
|
883
|
+
members[users[j]][k] = [...fixed[Math.min(fixed.length - 1, Math.floor((stratum + r()) / users.length * fixed.length))]];
|
|
884
|
+
});
|
|
885
|
+
continue;
|
|
886
|
+
}
|
|
887
|
+
const part = memberSpaces.get(o) ?? partOf(space, s.name).space;
|
|
888
|
+
const poolSize = Math.max(n, 4 * o.members.max, 8);
|
|
889
|
+
const seen = /* @__PURE__ */ new Set();
|
|
890
|
+
const pool = sampleConfigs(part, poolSize, subSeed(seed, `pool:${s.name}:${o.id}`), false, Math.max(1e3, 100 * poolSize), part, walk).filter((c) => {
|
|
891
|
+
const id = idOf(c);
|
|
892
|
+
return seen.has(id) ? false : (seen.add(id), true);
|
|
893
|
+
});
|
|
894
|
+
const r = stepRandom(seed, `members:${s.name}:${o.id}`);
|
|
895
|
+
const { min, max } = o.members;
|
|
896
|
+
permutation(users.length, r).forEach((stratum, j) => {
|
|
897
|
+
const count = min + Math.min(max - min, Math.floor((stratum + r()) / users.length * (max - min + 1)));
|
|
898
|
+
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}.`);
|
|
899
|
+
const picked = permutation(pool.length, r).slice(0, count);
|
|
900
|
+
members[users[j]][k] = picked.map((x) => pool[x]);
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
});
|
|
904
|
+
return points.map((choices, i) => {
|
|
905
|
+
const out = {};
|
|
906
|
+
stages.forEach((s, k) => {
|
|
907
|
+
const o = choices.get(s.name);
|
|
908
|
+
out[s.name] = o === null ? null : o.members === void 0 ? {
|
|
909
|
+
option: o.id,
|
|
910
|
+
params: values[i][k]
|
|
911
|
+
} : {
|
|
912
|
+
option: o.id,
|
|
913
|
+
params: values[i][k],
|
|
914
|
+
members: members[i][k]
|
|
915
|
+
};
|
|
916
|
+
});
|
|
917
|
+
return canonicalConfig(canon, {
|
|
918
|
+
space: space.engine,
|
|
919
|
+
stages: out
|
|
920
|
+
});
|
|
921
|
+
});
|
|
922
|
+
}
|
|
923
|
+
/**
|
|
924
|
+
* This function gives configs of a space, in their canonical form. Each config keeps the constraints. The result is deterministic.
|
|
925
|
+
*
|
|
926
|
+
* - 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.
|
|
927
|
+
* - 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.
|
|
928
|
+
* - A sample draws again when a point breaks a constraint. After `maxTries` draws, it throws an `Error`. Two sampled configs can be equal.
|
|
929
|
+
* - 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.
|
|
930
|
+
* - With `shapes`, only the options that support each kind of the list count. Without `shapes`, the result does not change.
|
|
931
|
+
* - A meta option with a `pool` gives only the entries of its pool. With `shapes`, only the entries whose members support each kind count.
|
|
932
|
+
* - A sample draws again when a point chooses a meta option without a member list. `spaceSize` gives such an option 0.
|
|
933
|
+
* - 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.
|
|
934
|
+
* - 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.
|
|
935
|
+
*/
|
|
936
|
+
function enumerateSpace(space, options) {
|
|
937
|
+
assertSpace(space, options.kinds);
|
|
938
|
+
assertShapes(options.shapes, options.kinds);
|
|
939
|
+
if (options.mode === "grid") {
|
|
940
|
+
assertWhole(options.levels, "levels", 1);
|
|
941
|
+
return gridConfigs(space, options.levels, options.limit ?? 1e5, options.shapes);
|
|
942
|
+
}
|
|
943
|
+
if (options.mode !== "sample") throw new RangeError(`The mode ${String(options.mode)} is not grid or sample.`);
|
|
944
|
+
assertWhole(options.n, "n", 0);
|
|
945
|
+
if (!Number.isInteger(options.seed)) throw new RangeError(`The seed ${options.seed} is not a whole number.`);
|
|
946
|
+
const meta = options.meta ?? "skip";
|
|
947
|
+
if (meta !== "skip" && meta !== "sample") throw new RangeError(`The option meta is ${String(meta)}, not skip or sample.`);
|
|
948
|
+
const draw = options.draw ?? "reject";
|
|
949
|
+
if (draw !== "reject" && draw !== "walk") throw new RangeError(`The option draw is ${String(draw)}, not reject or walk.`);
|
|
950
|
+
const fixed = options.fixed;
|
|
951
|
+
if (fixed !== void 0) assertFixed(space, fixed, meta === "sample");
|
|
952
|
+
if (options.n === 0) return [];
|
|
953
|
+
const kept = restrict(space, options.shapes);
|
|
954
|
+
const drawn = fixed === void 0 ? kept : fixStages(space, kept, fixed, options.shapes);
|
|
955
|
+
try {
|
|
956
|
+
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 ?? {})));
|
|
957
|
+
} catch (error) {
|
|
958
|
+
if (options.shapes !== void 0 && !hasConfig(kept, meta === "sample")) throw noSample(space, kept, options.shapes, meta === "sample");
|
|
959
|
+
if (fixed !== void 0 && !hasConfig(drawn, meta === "sample")) throw noFixedSample(space, fixed);
|
|
960
|
+
throw error;
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* This function gives the shape kinds that a space accepts. The value comes from the `supports` of the options.
|
|
965
|
+
* A space accepts a kind when it has at least one config without a meta option on that kind.
|
|
966
|
+
* With `kinds`, it examines the kinds of that list, in its order. Without `kinds`, it examines the kinds that the `supports` of the space name, in the order of their first use.
|
|
967
|
+
*/
|
|
968
|
+
function spaceShapeKinds(space, options = {}) {
|
|
969
|
+
assertSpace(space, options.kinds);
|
|
970
|
+
const named = [...new Set(space.stages.flatMap((s) => s.options.flatMap((o) => o.supports?.shape ?? [])))];
|
|
971
|
+
return (options.kinds ?? named).filter((k) => sizeOf(restrict(space, [k]), 1, 0n).base > 0n);
|
|
972
|
+
}
|
|
973
|
+
/**
|
|
974
|
+
* This function joins configs of the parts of a product space into one config of the space `space`.
|
|
975
|
+
* The name of each stage gets the prefix `<space of the part>.`. The members of a meta option do not change.
|
|
976
|
+
*/
|
|
977
|
+
function joinConfigs(space, parts) {
|
|
978
|
+
const stages = {};
|
|
979
|
+
for (const part of parts) for (const [k, c] of Object.entries(part.stages)) stages[`${part.space}.${k}`] = c;
|
|
980
|
+
return {
|
|
981
|
+
space,
|
|
982
|
+
stages
|
|
983
|
+
};
|
|
984
|
+
}
|
|
985
|
+
/** This function gives the config of the part `engine` of a joint config. It throws a `RangeError` when no stage has the prefix `<engine>.`. */
|
|
986
|
+
function splitConfig(config, engine) {
|
|
987
|
+
const head = `${engine}.`;
|
|
988
|
+
const entries = Object.entries(config.stages).filter(([k]) => k.startsWith(head));
|
|
989
|
+
if (entries.length === 0) throw new RangeError(`The config of the space ${config.space} has no stage of the part ${engine}.`);
|
|
990
|
+
return {
|
|
991
|
+
space: engine,
|
|
992
|
+
stages: Object.fromEntries(entries.map(([k, c]) => [k.slice(head.length), c]))
|
|
993
|
+
};
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* 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>.`.
|
|
997
|
+
* 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.
|
|
998
|
+
*/
|
|
999
|
+
function productSpace(a, b, name) {
|
|
1000
|
+
if (a.engine === b.engine) throw new RangeError(`The two spaces have the same engine ${a.engine}.`);
|
|
1001
|
+
if (name === "") throw new RangeError("The product space has no name.");
|
|
1002
|
+
const stagesOf = (s) => s.stages.map((x) => ({
|
|
1003
|
+
...x,
|
|
1004
|
+
name: `${s.engine}.${x.name}`
|
|
1005
|
+
}));
|
|
1006
|
+
const ref = (s, r) => ({
|
|
1007
|
+
stage: `${s.engine}.${r.stage}`,
|
|
1008
|
+
option: r.option
|
|
1009
|
+
});
|
|
1010
|
+
const constraintsOf = (s) => (s.constraints ?? []).map((c) => ({
|
|
1011
|
+
if: ref(s, c.if),
|
|
1012
|
+
then: "not" in c.then ? { not: ref(s, c.then.not) } : ref(s, c.then)
|
|
1013
|
+
}));
|
|
1014
|
+
const named = {};
|
|
1015
|
+
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]);
|
|
1016
|
+
const constraints = [...constraintsOf(a), ...constraintsOf(b)];
|
|
1017
|
+
return {
|
|
1018
|
+
schema: STRATEGY_SPACE_SCHEMA,
|
|
1019
|
+
engine: name,
|
|
1020
|
+
version: `${a.engine}@${a.version}+${b.engine}@${b.version}`,
|
|
1021
|
+
stages: [...stagesOf(a), ...stagesOf(b)],
|
|
1022
|
+
...constraints.length > 0 ? { constraints } : {},
|
|
1023
|
+
...Object.keys(named).length > 0 ? { named } : {},
|
|
1024
|
+
parts: [a, b]
|
|
1025
|
+
};
|
|
1026
|
+
}
|
|
1027
|
+
export { STRATEGY_SPACE_SCHEMA, canonicalConfig, checkConfig, checkSpace, configId, enumerateSpace, joinConfigs, paramActive, paramLevels, paramProblem, paramValue, productSpace, spaceShapeKinds, spaceSize, splitConfig };
|
|
1028
|
+
|
|
1029
|
+
//# sourceMappingURL=strategy-space.js.map
|