@mark1russell7/layout-contract 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.d.ts +9 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +53 -12
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/space-walk.ts +63 -0
- package/src/strategy-space.ts +20 -14
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/layout-contract",
|
|
4
|
-
"version": "0.5.
|
|
4
|
+
"version": "0.5.2",
|
|
5
5
|
"description": "The shared contract of the packing and highways layout engines: the input and record formats, the turn rule, margins, tolerances and hard rules.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"layout",
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// The walk draw of a sample of a strategy space (`docs/STRATEGY-SPACE.md`). The package root does not export this file.
|
|
2
|
+
import type { OptionRef, SpaceConstraint, SpaceStage, StageOption } from "./strategy-space.ts";
|
|
3
|
+
|
|
4
|
+
const refMatches = (ref: OptionRef, choices: ReadonlyMap<string, string | null>): boolean => (choices.get(ref.stage) ?? null) === ref.option;
|
|
5
|
+
|
|
6
|
+
/** This function tells if the option choices keep a constraint. */
|
|
7
|
+
export function holds(c: SpaceConstraint, choices: ReadonlyMap<string, string | null>): boolean {
|
|
8
|
+
if (!refMatches(c.if, choices)) return true;
|
|
9
|
+
return "not" in c.then ? !refMatches(c.then.not, choices) : refMatches(c.then, choices);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const otherStage = (c: SpaceConstraint): string => ("not" in c.then ? c.then.not.stage : c.then.stage);
|
|
13
|
+
|
|
14
|
+
/** This function tells if an option of a stage keeps each constraint between that stage and the stages that a walk has fixed. */
|
|
15
|
+
export function fitsWalk(constraints: readonly SpaceConstraint[], fixed: Map<string, string | null>, stage: string, id: string | null): boolean {
|
|
16
|
+
fixed.set(stage, id);
|
|
17
|
+
const ok = constraints.every((c) => !fixed.has(c.if.stage === stage ? otherStage(c) : c.if.stage) || holds(c, fixed));
|
|
18
|
+
fixed.delete(stage);
|
|
19
|
+
return ok;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The order of the stages of a walk, and the constraints that name each stage. */
|
|
23
|
+
export interface WalkPlan {
|
|
24
|
+
/** The stages of the space. */
|
|
25
|
+
readonly stages: readonly SpaceStage[];
|
|
26
|
+
/** The options of each stage for a draw. */
|
|
27
|
+
readonly lists: readonly (readonly (StageOption | null)[])[];
|
|
28
|
+
/** The indexes of the stages, from the largest stage to the smallest. */
|
|
29
|
+
readonly order: readonly number[];
|
|
30
|
+
/** The constraints that name each stage. */
|
|
31
|
+
readonly naming: readonly (readonly SpaceConstraint[])[];
|
|
32
|
+
/** The count of points of a round. */
|
|
33
|
+
readonly n: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** This function gives the plan of a walk. Stages of equal size keep the order of the space. */
|
|
37
|
+
export function walkPlan(stages: readonly SpaceStage[], lists: readonly (readonly (StageOption | null)[])[], constraints: readonly SpaceConstraint[], n: number): WalkPlan {
|
|
38
|
+
return {
|
|
39
|
+
stages,
|
|
40
|
+
lists,
|
|
41
|
+
n,
|
|
42
|
+
order: stages.map((_, k) => k).sort((a, b) => lists[b]!.length - lists[a]!.length || a - b),
|
|
43
|
+
naming: stages.map((s) => constraints.filter((c) => c.if.stage === s.name || otherStage(c) === s.name)),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 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.
|
|
49
|
+
* 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.
|
|
50
|
+
* The argument `fits` lets a test give a fault. The default is `fitsWalk`.
|
|
51
|
+
*/
|
|
52
|
+
export function walkPoint(plan: WalkPlan, picks: readonly (readonly number[])[], i: number, fits: typeof fitsWalk = fitsWalk): Map<string, StageOption | null> {
|
|
53
|
+
const fixed = new Map<string, string | null>();
|
|
54
|
+
const out = new Map<string, StageOption | null>();
|
|
55
|
+
for (const k of plan.order) {
|
|
56
|
+
const s = plan.stages[k]!;
|
|
57
|
+
const open = plan.lists[k]!.filter((o) => fits(plan.naming[k]!, fixed, s.name, o?.id ?? null));
|
|
58
|
+
const o = open[Math.floor((picks[k]![i]! * open.length) / plan.n)] ?? null;
|
|
59
|
+
fixed.set(s.name, o?.id ?? null);
|
|
60
|
+
out.set(s.name, o);
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
package/src/strategy-space.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import type { ClaimBreak } from "./corpus.ts";
|
|
3
3
|
import { canonicalJson, sha256Hex } from "./hash.ts";
|
|
4
4
|
import { stepRandom } from "./random.ts";
|
|
5
|
+
import { holds, walkPlan, walkPoint } from "./space-walk.ts";
|
|
5
6
|
import { isShapeKind, SHAPE_KIND_IDS, type ShapeKind } from "./shapes.ts";
|
|
6
7
|
|
|
7
8
|
/** The schema of a strategy space. */
|
|
@@ -164,6 +165,14 @@ export interface SampleOptions extends ShapeFilter {
|
|
|
164
165
|
readonly meta?: "skip" | "sample";
|
|
165
166
|
/** The largest count of option draws. The default is the larger value of 1000 and `100 * n`. */
|
|
166
167
|
readonly maxTries?: number;
|
|
168
|
+
/**
|
|
169
|
+
* How a point keeps the constraints. With `reject` (the default), a point that breaks a constraint draws again.
|
|
170
|
+
* With `walk`, a point draws its stages from the largest to the smallest. Each stage draws only from the options that keep the constraints with the stages before it.
|
|
171
|
+
* A space whose constraints reject most draws gets its sample with `walk`. The two values give different configs for one seed.
|
|
172
|
+
* A sample of `walk` is not uniform over the valid configs. Each option of the largest stage gets an equal share, whatever count of valid configs it has.
|
|
173
|
+
* Thus a mean over this sample is not a mean of the space.
|
|
174
|
+
*/
|
|
175
|
+
readonly draw?: "reject" | "walk";
|
|
167
176
|
}
|
|
168
177
|
|
|
169
178
|
/** The options of `enumerateSpace`. */
|
|
@@ -245,14 +254,6 @@ function partOf(space: StrategySpace, stage: string): { readonly space: Strategy
|
|
|
245
254
|
/** This function gives the prefix of the innermost part of each stage. */
|
|
246
255
|
const prefixes = (space: StrategySpace): Map<string, string> => new Map(space.stages.map((s) => [s.name, partOf(space, s.name).prefix]));
|
|
247
256
|
|
|
248
|
-
const refMatches = (ref: OptionRef, choices: ReadonlyMap<string, string | null>): boolean => (choices.get(ref.stage) ?? null) === ref.option;
|
|
249
|
-
|
|
250
|
-
/** This function tells if the option choices keep a constraint. */
|
|
251
|
-
function holds(c: SpaceConstraint, choices: ReadonlyMap<string, string | null>): boolean {
|
|
252
|
-
if (!refMatches(c.if, choices)) return true;
|
|
253
|
-
return "not" in c.then ? !refMatches(c.then.not, choices) : refMatches(c.then, choices);
|
|
254
|
-
}
|
|
255
|
-
|
|
256
257
|
const describe = (c: SpaceConstraint): string => {
|
|
257
258
|
const ref = (r: OptionRef): string => `${r.stage}=${r.option ?? "null"}`;
|
|
258
259
|
return "not" in c.then ? `if ${ref(c.if)} then not ${ref(c.then.not)}` : `if ${ref(c.if)} then ${ref(c.then)}`;
|
|
@@ -949,7 +950,7 @@ const subSeed = (seed: number, step: string): number => Math.floor(stepRandom(se
|
|
|
949
950
|
* This function gives a sample of a space: Latin hypercube strata over the options, the params and the member counts.
|
|
950
951
|
* `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.
|
|
951
952
|
*/
|
|
952
|
-
function sampleConfigs(space: StrategySpace, n: number, seed: number, meta: boolean, maxTries: number, canon: StrategySpace = space): StrategyConfig[] {
|
|
953
|
+
function sampleConfigs(space: StrategySpace, n: number, seed: number, meta: boolean, maxTries: number, canon: StrategySpace = space, walk = false): StrategyConfig[] {
|
|
953
954
|
const stages = space.stages;
|
|
954
955
|
const prefix = prefixes(space);
|
|
955
956
|
// A stage without options can stay empty in a part with a meta option of `view: "bounds"`, because that option replaces the stage.
|
|
@@ -968,15 +969,17 @@ function sampleConfigs(space: StrategySpace, n: number, seed: number, meta: bool
|
|
|
968
969
|
if (!usable.has(o)) usable.set(o, hasMembers(space, s.name, o));
|
|
969
970
|
return usable.get(o)!;
|
|
970
971
|
};
|
|
972
|
+
const plan = walk ? walkPlan(stages, lists, space.constraints ?? [], n) : null;
|
|
971
973
|
const points: Map<string, StageOption | null>[] = [];
|
|
972
974
|
let tries = 0;
|
|
973
975
|
for (let round = 0; points.length < n; round++) {
|
|
974
976
|
const r = stepRandom(seed, `options:${round}`);
|
|
975
|
-
const
|
|
977
|
+
const picks = walk ? lists.map(() => permutation(n, r)) : [];
|
|
978
|
+
const columns = walk ? [] : lists.map((list) => permutation(n, r).map((p) => list[Math.floor((p * list.length) / n)]!));
|
|
976
979
|
for (let i = 0; i < n && points.length < n; i++) {
|
|
977
|
-
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
|
|
980
|
+
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.'}`);
|
|
978
981
|
tries++;
|
|
979
|
-
const choices = new Map<string, StageOption | null>(stages.map((s, k) => [s.name, columns[k]![i]!]));
|
|
982
|
+
const choices = plan !== null ? walkPoint(plan, picks, i) : new Map<string, StageOption | null>(stages.map((s, k) => [s.name, columns[k]![i]!]));
|
|
980
983
|
const metaPart = new Map<string, string>();
|
|
981
984
|
for (const s of stages) {
|
|
982
985
|
const o = choices.get(s.name);
|
|
@@ -1015,7 +1018,7 @@ function sampleConfigs(space: StrategySpace, n: number, seed: number, meta: bool
|
|
|
1015
1018
|
const part = memberSpaces.get(o) ?? partOf(space, s.name).space;
|
|
1016
1019
|
const poolSize = Math.max(n, 4 * o.members.max, 8);
|
|
1017
1020
|
const seen = new Set<string>();
|
|
1018
|
-
const pool = sampleConfigs(part, poolSize, subSeed(seed, `pool:${s.name}:${o.id}`), false, Math.max(1000, 100 * poolSize)).filter((c) => {
|
|
1021
|
+
const pool = sampleConfigs(part, poolSize, subSeed(seed, `pool:${s.name}:${o.id}`), false, Math.max(1000, 100 * poolSize), part, walk).filter((c) => {
|
|
1019
1022
|
const id = idOf(c);
|
|
1020
1023
|
return seen.has(id) ? false : (seen.add(id), true);
|
|
1021
1024
|
});
|
|
@@ -1045,6 +1048,7 @@ function sampleConfigs(space: StrategySpace, n: number, seed: number, meta: bool
|
|
|
1045
1048
|
* - 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.
|
|
1046
1049
|
* - 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.
|
|
1047
1050
|
* - A sample draws again when a point breaks a constraint. After `maxTries` draws, it throws an `Error`. Two sampled configs can be equal.
|
|
1051
|
+
* - 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.
|
|
1048
1052
|
* - With `shapes`, only the options that support each kind of the list count. Without `shapes`, the result does not change.
|
|
1049
1053
|
* - A meta option with a `pool` gives only the entries of its pool. With `shapes`, only the entries whose members support each kind count.
|
|
1050
1054
|
* - A sample draws again when a point chooses a meta option without a member list. `spaceSize` gives such an option 0.
|
|
@@ -1062,10 +1066,12 @@ export function enumerateSpace(space: StrategySpace, options: EnumerateOptions):
|
|
|
1062
1066
|
if (!Number.isInteger(options.seed)) throw new RangeError(`The seed ${options.seed} is not a whole number.`);
|
|
1063
1067
|
const meta = options.meta ?? "skip";
|
|
1064
1068
|
if (meta !== "skip" && meta !== "sample") throw new RangeError(`The option meta is ${String(meta)}, not skip or sample.`);
|
|
1069
|
+
const draw = options.draw ?? "reject";
|
|
1070
|
+
if (draw !== "reject" && draw !== "walk") throw new RangeError(`The option draw is ${String(draw)}, not reject or walk.`);
|
|
1065
1071
|
if (options.n === 0) return [];
|
|
1066
1072
|
const kept = restrict(space, options.shapes);
|
|
1067
1073
|
try {
|
|
1068
|
-
return sampleConfigs(kept, options.n, options.seed, meta === "sample", options.maxTries ?? Math.max(1000, 100 * options.n), space);
|
|
1074
|
+
return sampleConfigs(kept, options.n, options.seed, meta === "sample", options.maxTries ?? Math.max(1000, 100 * options.n), space, draw === "walk");
|
|
1069
1075
|
} catch (error) {
|
|
1070
1076
|
// Only a space without a config for the shape kinds gives the RangeError. A different failure keeps its own error.
|
|
1071
1077
|
if (options.shapes !== undefined && !hasConfig(kept, meta === "sample")) throw noSample(space, kept, options.shapes, meta === "sample");
|