@altnovatrail/widgets 0.1.3 → 0.2.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/README.md +1 -1
- package/kinds/cells/cells.handles.json +3 -3
- package/kinds/cells/cells.schema.json +69 -9
- package/kinds/cells/vectors/knight-after-no-parity.json +31 -0
- package/kinds/cells/vectors/knight-par-56-d2-2.json +61 -6
- package/kinds/cells/vectors/paper-drawn-lines.json +36 -1
- package/kinds/cells/vectors/polyomino-one-pieces.json +28 -0
- package/kinds/line/line.handles.json +195 -0
- package/kinds/line/line.schema.json +321 -0
- package/kinds/line/vectors/cases.json +213 -0
- package/kinds/line/vectors/gaps-12-d1-1.json +140 -0
- package/kinds/line/vectors/gaps-12-d1-2.json +95 -0
- package/kinds/line/vectors/gaps-12-d1-3.json +122 -0
- package/kinds/line/vectors/gaps-12-d1-4.json +121 -0
- package/kinds/line/vectors/gaps-12-d1-5.json +87 -0
- package/kinds/line/vectors/gaps-12-d2-1.json +173 -0
- package/kinds/line/vectors/gaps-12-d2-6.json +87 -0
- package/kinds/line/vectors/gaps-12-d3-2.json +173 -0
- package/kinds/line/vectors/gaps-12-d4-4.json +168 -0
- package/kinds/line/vectors/gaps-12-d5-3.json +99 -0
- package/kinds/line/vectors/gaps-12-d5-4.json +215 -0
- package/kinds/line/vectors/gaps-34-d1-1.json +37 -0
- package/kinds/line/vectors/gaps-34-d1-2.json +130 -0
- package/kinds/line/vectors/gaps-34-d1-4.json +155 -0
- package/kinds/line/vectors/gaps-34-d1-5.json +43 -0
- package/kinds/line/vectors/gaps-34-d3-2.json +38 -0
- package/kinds/line/vectors/gaps-34-d3-5.json +177 -0
- package/kinds/line/vectors/gaps-34-d5-3.json +39 -0
- package/kinds/line/vectors/invalid-line.json +34 -0
- package/kinds/line/vectors/invalid-ring.json +34 -0
- package/kinds/line/vectors/invalid.json +43 -0
- package/kinds/line/vectors/jump-unreachable.json +104 -0
- package/kinds/line/vectors/limit.json +40 -0
- package/kinds/line/vectors/op-not-in-scene.json +47 -0
- package/kinds/line/vectors/ring-cut.json +104 -0
- package/kinds/line/vectors/ring-every.json +107 -0
- package/kinds/line/vectors/ring-jump.json +135 -0
- package/lib/cores/arith.d.ts +62 -0
- package/lib/cores/arith.js +197 -0
- package/lib/cores/enumerate.js +2 -2
- package/lib/cores/kind.d.ts +28 -3
- package/lib/cores/kind.js +2 -2
- package/lib/cores/registry.d.ts +43 -0
- package/lib/cores/registry.js +87 -0
- package/lib/cores/search.d.ts +21 -3
- package/lib/cores/search.js +80 -17
- package/lib/kinds/cells/bishop.d.ts +1 -1
- package/lib/kinds/cells/index.d.ts +8 -0
- package/lib/kinds/cells/index.js +10 -2
- package/lib/kinds/cells/kind.d.ts +3 -2
- package/lib/kinds/cells/kind.js +26 -66
- package/lib/kinds/cells/knight.d.ts +1 -1
- package/lib/kinds/cells/operation.d.ts +21 -1
- package/lib/kinds/cells/paper.js +27 -13
- package/lib/kinds/cells/piece.d.ts +17 -6
- package/lib/kinds/cells/piece.js +119 -38
- package/lib/kinds/cells/polyomino.d.ts +14 -0
- package/lib/kinds/cells/polyomino.js +122 -35
- package/lib/kinds/cells/registry.js +1 -1
- package/lib/kinds/cells/schema.js +4 -1
- package/lib/kinds/cells/step.d.ts +1 -1
- package/lib/kinds/line/cut.d.ts +2 -0
- package/lib/kinds/line/cut.js +63 -0
- package/lib/kinds/line/goals.d.ts +22 -0
- package/lib/kinds/line/goals.js +137 -0
- package/lib/kinds/line/index.d.ts +13 -0
- package/lib/kinds/line/index.js +17 -0
- package/lib/kinds/line/jump.d.ts +10 -0
- package/lib/kinds/line/jump.js +95 -0
- package/lib/kinds/line/kind.d.ts +25 -0
- package/lib/kinds/line/kind.js +125 -0
- package/lib/kinds/line/line.d.ts +63 -0
- package/lib/kinds/line/line.js +157 -0
- package/lib/kinds/line/operation.d.ts +69 -0
- package/lib/kinds/line/operation.js +1 -0
- package/lib/kinds/line/posts.d.ts +11 -0
- package/lib/kinds/line/posts.js +85 -0
- package/lib/kinds/line/registry.d.ts +2 -0
- package/lib/kinds/line/registry.js +11 -0
- package/lib/kinds/line/schema.d.ts +4 -0
- package/lib/kinds/line/schema.js +157 -0
- package/package.json +9 -2
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pattern of a fraction as JSON writes it in a scene and a state: the one form module-kit's
|
|
3
|
+
* solver answers in, an integer or `p/q` in lowest terms with the sign before p, as "0", "-3" or
|
|
4
|
+
* "7/2". A module's key may write a fraction in any form of its value, but a scene and a state
|
|
5
|
+
* write it in this one alone, and "2/4" or "3/1" there is an error. The pattern cannot see lowest
|
|
6
|
+
* terms or the bounds of a safe integer; parseFraction does.
|
|
7
|
+
*/
|
|
8
|
+
export const fractionPattern = "^(0|-?[1-9][0-9]{0,15}(/([2-9]|[1-9][0-9]{1,15}))?)$";
|
|
9
|
+
/** The part of a kind's schema for a fraction in a scene or a state. */
|
|
10
|
+
export const fractionSchema = {
|
|
11
|
+
type: "string",
|
|
12
|
+
pattern: fractionPattern,
|
|
13
|
+
description: 'A number, exact: an integer or a fraction in lowest terms with its sign before the numerator, as "0", "-3" or "7/2".',
|
|
14
|
+
};
|
|
15
|
+
const NOTATION = /^(-?[1-9][0-9]{0,15})(?:\/([1-9][0-9]{0,15}))?$/;
|
|
16
|
+
/** The fraction num/den in lowest terms; a denominator of 0 or a part not a safe integer throws. */
|
|
17
|
+
export function fraction(num, den = 1) {
|
|
18
|
+
safe(num);
|
|
19
|
+
safe(den);
|
|
20
|
+
if (den === 0) {
|
|
21
|
+
throw new RangeError("a fraction with the denominator 0");
|
|
22
|
+
}
|
|
23
|
+
const divisor = gcd(num, den);
|
|
24
|
+
const sign = den < 0 ? -1 : 1;
|
|
25
|
+
return made((sign * num) / divisor, (sign * den) / divisor);
|
|
26
|
+
}
|
|
27
|
+
/** a + b. */
|
|
28
|
+
export function add(a, b) {
|
|
29
|
+
// Over the least common denominator, then reduced by what the numerator still shares with it.
|
|
30
|
+
const shared = gcd(a.den, b.den);
|
|
31
|
+
const left = product(a.num, b.den / shared);
|
|
32
|
+
const right = product(b.num, a.den / shared);
|
|
33
|
+
const den = product(a.den, b.den / shared);
|
|
34
|
+
const num = left === undefined || right === undefined ? undefined : left + right;
|
|
35
|
+
if (den === undefined || num === undefined || !Number.isSafeInteger(num)) {
|
|
36
|
+
return exact(BigInt(a.num) * BigInt(b.den) + BigInt(b.num) * BigInt(a.den), BigInt(a.den) * BigInt(b.den));
|
|
37
|
+
}
|
|
38
|
+
const divisor = gcd(num, shared);
|
|
39
|
+
return made(num / divisor, den / divisor);
|
|
40
|
+
}
|
|
41
|
+
/** a − b. */
|
|
42
|
+
export function subtract(a, b) {
|
|
43
|
+
return add(a, made(-b.num, b.den));
|
|
44
|
+
}
|
|
45
|
+
/** a · b. */
|
|
46
|
+
export function multiply(a, b) {
|
|
47
|
+
return times(a, b, "·", b);
|
|
48
|
+
}
|
|
49
|
+
/** a ÷ b; b of 0 throws. */
|
|
50
|
+
export function divide(a, b) {
|
|
51
|
+
if (b.num === 0) {
|
|
52
|
+
throw new RangeError("a division by 0");
|
|
53
|
+
}
|
|
54
|
+
const reciprocal = b.num < 0 ? made(-b.den, -b.num) : made(b.den, b.num);
|
|
55
|
+
return times(a, reciprocal, "÷", b);
|
|
56
|
+
}
|
|
57
|
+
// The operation and its right operand as asked only name them in the error, written only if thrown,
|
|
58
|
+
// since the product runs on every edge of a state graph.
|
|
59
|
+
function times(a, b, sign, asked) {
|
|
60
|
+
// Each numerator reduced by the other's denominator first, so the product is in lowest terms
|
|
61
|
+
// and no part of it is larger than the result's own.
|
|
62
|
+
const first = gcd(a.num, b.den);
|
|
63
|
+
const second = gcd(b.num, a.den);
|
|
64
|
+
const num = product(a.num / first, b.num / second);
|
|
65
|
+
const den = product(a.den / second, b.den / first);
|
|
66
|
+
if (num === undefined || den === undefined) {
|
|
67
|
+
throw new RangeError(`not a safe integer in the result of ${fractionText(a)} ${sign} ${fractionText(asked)}`);
|
|
68
|
+
}
|
|
69
|
+
return made(num, den);
|
|
70
|
+
}
|
|
71
|
+
/** −1, 0 or 1 as a is less than, equal to or greater than b. */
|
|
72
|
+
export function compare(a, b) {
|
|
73
|
+
const left = product(a.num, b.den);
|
|
74
|
+
const right = product(b.num, a.den);
|
|
75
|
+
if (left !== undefined && right !== undefined) {
|
|
76
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
77
|
+
}
|
|
78
|
+
const difference = BigInt(a.num) * BigInt(b.den) - BigInt(b.num) * BigInt(a.den);
|
|
79
|
+
return difference < 0n ? -1 : difference > 0n ? 1 : 0;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The whole quotient of a by b and its remainder, from 0 to |b| − 1 whatever the signs: a place on
|
|
83
|
+
* a ring of b, counted on from any whole number. b of 0 throws.
|
|
84
|
+
*/
|
|
85
|
+
export function divideWithRemainder(a, b) {
|
|
86
|
+
safe(a);
|
|
87
|
+
safe(b);
|
|
88
|
+
if (b === 0) {
|
|
89
|
+
throw new RangeError("a division by 0");
|
|
90
|
+
}
|
|
91
|
+
// % keeps a's sign; a less its remainder is a multiple of b no larger than a, so it divides exactly.
|
|
92
|
+
const remainder = a % b;
|
|
93
|
+
const quotient = (a - remainder) / b;
|
|
94
|
+
if (remainder >= 0) {
|
|
95
|
+
return { quotient: quotient + 0, remainder: remainder + 0 };
|
|
96
|
+
}
|
|
97
|
+
return b > 0
|
|
98
|
+
? { quotient: quotient - 1, remainder: remainder + b }
|
|
99
|
+
: { quotient: quotient + 1, remainder: remainder - b };
|
|
100
|
+
}
|
|
101
|
+
/** The greatest common divisor of a and b, never negative; of 0 and 0, 0. */
|
|
102
|
+
export function gcd(a, b) {
|
|
103
|
+
safe(a);
|
|
104
|
+
safe(b);
|
|
105
|
+
let x = Math.abs(a);
|
|
106
|
+
let y = Math.abs(b);
|
|
107
|
+
while (y !== 0) {
|
|
108
|
+
const rest = x % y;
|
|
109
|
+
x = y;
|
|
110
|
+
y = rest;
|
|
111
|
+
}
|
|
112
|
+
return x;
|
|
113
|
+
}
|
|
114
|
+
/** The least common multiple of a and b, never negative; 0 where either is 0. */
|
|
115
|
+
export function lcm(a, b) {
|
|
116
|
+
if (a === 0 || b === 0) {
|
|
117
|
+
safe(a);
|
|
118
|
+
safe(b);
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
const multiple = product(Math.abs(a) / gcd(a, b), Math.abs(b));
|
|
122
|
+
if (multiple === undefined) {
|
|
123
|
+
throw new RangeError(`not a safe integer: the least common multiple of ${a} and ${b}`);
|
|
124
|
+
}
|
|
125
|
+
return multiple;
|
|
126
|
+
}
|
|
127
|
+
/** A fraction in the notation of fractionPattern, for a scene or a state. */
|
|
128
|
+
export function fractionText(value) {
|
|
129
|
+
return value.den === 1 ? String(value.num) : `${value.num}/${value.den}`;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The fraction a text writes in the notation of fractionPattern, or undefined where it writes
|
|
133
|
+
* none: a fraction not in lowest terms, with the denominator 1, or a part past a safe integer, the
|
|
134
|
+
* pattern's matches among them. One value has one text, so a state that holds it keys as one.
|
|
135
|
+
*/
|
|
136
|
+
export function parseFraction(value) {
|
|
137
|
+
if (value === "0") {
|
|
138
|
+
return made(0, 1);
|
|
139
|
+
}
|
|
140
|
+
const match = NOTATION.exec(value);
|
|
141
|
+
if (match === null) {
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
const num = Number(match[1]);
|
|
145
|
+
const den = match[2] === undefined ? 1 : Number(match[2]);
|
|
146
|
+
if (!Number.isSafeInteger(num) || !Number.isSafeInteger(den)) {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
if (match[2] !== undefined && (den === 1 || gcd(num, den) !== 1)) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
return made(num, den);
|
|
153
|
+
}
|
|
154
|
+
/** A fraction of a scene by fractionSchema, read as the other readers of a scene's JSON are. */
|
|
155
|
+
export function readFraction(value, path, errors) {
|
|
156
|
+
const read = typeof value === "string" ? parseFraction(value) : undefined;
|
|
157
|
+
if (read === undefined) {
|
|
158
|
+
errors.push({
|
|
159
|
+
path,
|
|
160
|
+
message: 'a number as "0", "-3" or "7/2": an integer, or a fraction in lowest terms',
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return read;
|
|
164
|
+
}
|
|
165
|
+
function safe(value) {
|
|
166
|
+
if (!Number.isSafeInteger(value)) {
|
|
167
|
+
throw new RangeError(`not a safe integer: ${String(value)}`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
// The product of two safe integers, or undefined where it is not one. Past 2^53 the floating-point
|
|
171
|
+
// product rounds to 2^53 or more, never back below it, so the check of the result is exact.
|
|
172
|
+
function product(a, b) {
|
|
173
|
+
const result = a * b;
|
|
174
|
+
return Number.isSafeInteger(result) ? result : undefined;
|
|
175
|
+
}
|
|
176
|
+
// The same fraction, reduced in bigint where its parts on the way are past a safe integer.
|
|
177
|
+
function exact(num, den) {
|
|
178
|
+
let x = num < 0n ? -num : num;
|
|
179
|
+
let y = den;
|
|
180
|
+
while (y !== 0n) {
|
|
181
|
+
const rest = x % y;
|
|
182
|
+
x = y;
|
|
183
|
+
y = rest;
|
|
184
|
+
}
|
|
185
|
+
const reducedNum = num / x;
|
|
186
|
+
const reducedDen = den / x;
|
|
187
|
+
const top = BigInt(Number.MAX_SAFE_INTEGER);
|
|
188
|
+
if (reducedNum > top || reducedNum < -top || reducedDen > top) {
|
|
189
|
+
throw new RangeError(`not a safe integer in a fraction: ${reducedNum}/${reducedDen}`);
|
|
190
|
+
}
|
|
191
|
+
return made(Number(reducedNum), Number(reducedDen));
|
|
192
|
+
}
|
|
193
|
+
// -0, which a product or a quotient of 0 by a negative number gives, is 0: JSON writes the two
|
|
194
|
+
// alike, and Object.is would part them.
|
|
195
|
+
function made(num, den) {
|
|
196
|
+
return { num: num + 0, den };
|
|
197
|
+
}
|
package/lib/cores/enumerate.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// The core of enumeration: what a key that is a count, a maximum or a minimum reads off a scene's
|
|
2
|
-
// whole state graph, which the search core builds within the kind's
|
|
3
|
-
// graph and walks it no deeper, so the kind's
|
|
2
|
+
// whole state graph, which the search core builds within the kind's limits. It adds no state to the
|
|
3
|
+
// graph and walks it no deeper, so the kind's limits bound it as they bound the search.
|
|
4
4
|
import { canonicalKey } from "./key.js";
|
|
5
5
|
/**
|
|
6
6
|
* The states of the graph that meet the goal, in the graph's order; where images is given, the
|
package/lib/cores/kind.d.ts
CHANGED
|
@@ -32,6 +32,15 @@ export type Played<State> = {
|
|
|
32
32
|
export interface Kind<Scene, State extends Json, Move extends Json> {
|
|
33
33
|
/** The most states a scene's whole state graph may hold, the same at a build and on a device. */
|
|
34
34
|
readonly limit: number;
|
|
35
|
+
/**
|
|
36
|
+
* The most work a scene's whole state graph may take, the same at a build and on a device: each
|
|
37
|
+
* state counts every move the kind lists from it, each move the length of the state's canonical
|
|
38
|
+
* key, what implied adds to it, and moveWork of cores/search.ts more — a move reads the state and
|
|
39
|
+
* writes the one it leads to, keyed again, and trying it costs something whatever the state. It
|
|
40
|
+
* bounds the time of a graph whose states carry many marks or places or have many moves to try,
|
|
41
|
+
* which a count of states does not.
|
|
42
|
+
*/
|
|
43
|
+
readonly workLimit: number;
|
|
35
44
|
read(input: unknown): Read<Scene>;
|
|
36
45
|
/** The state of the scene as given, at version 0. */
|
|
37
46
|
start(scene: Scene): State;
|
|
@@ -46,6 +55,22 @@ export interface Kind<Scene, State extends Json, Move extends Json> {
|
|
|
46
55
|
* path follows. A move listed here that play refuses is skipped.
|
|
47
56
|
*/
|
|
48
57
|
moves(scene: Scene, state: State): readonly Move[];
|
|
58
|
+
/**
|
|
59
|
+
* At least how many moves `moves` lists from the state, counted without listing them, so that a
|
|
60
|
+
* state whose moves alone pass the limit of work stops the search before they are made: a kind
|
|
61
|
+
* one of whose states may have tens of thousands of moves gives it. It is a whole number and never
|
|
62
|
+
* more than moves lists: the search throws on a count that is no whole number, and on one above
|
|
63
|
+
* the list where the count alone keeps within the limit, while a count above the list that passes
|
|
64
|
+
* it would stop a scene the list does not. A kind that leaves it out, or gives 0, has its moves
|
|
65
|
+
* counted once listed, and the work and the limit are the same either way.
|
|
66
|
+
*/
|
|
67
|
+
leastMoves?(scene: Scene, state: State): number;
|
|
68
|
+
/**
|
|
69
|
+
* What a move from the state reads and writes that its canonical key leaves out, in characters as
|
|
70
|
+
* the key would write it: a place the state keeps by leaving it out, which every move still
|
|
71
|
+
* works on. A kind that leaves it out, or gives 0, keeps nothing so.
|
|
72
|
+
*/
|
|
73
|
+
implied?(scene: Scene, state: State): number;
|
|
49
74
|
hasGoal(scene: Scene): boolean;
|
|
50
75
|
/** Whether the state meets the scene's goal; never true for a scene with no goal. */
|
|
51
76
|
meets(scene: Scene, state: State): boolean;
|
|
@@ -79,9 +104,9 @@ export type Ask<State> = {
|
|
|
79
104
|
measure(state: State): number;
|
|
80
105
|
};
|
|
81
106
|
/**
|
|
82
|
-
* Why a scene gives no proof: limit, its state graph passes the kind's limit
|
|
83
|
-
* passes the safe integers, beyond what the solver takes either way;
|
|
84
|
-
* no-goal, it has no goal to prove.
|
|
107
|
+
* Why a scene gives no proof: limit, its state graph passes the kind's limit of states or of
|
|
108
|
+
* work, or a count of ways passes the safe integers, beyond what the solver takes either way;
|
|
109
|
+
* invalid, the kind refuses it; no-goal, it has no goal to prove.
|
|
85
110
|
*/
|
|
86
111
|
export type Undecided = {
|
|
87
112
|
readonly undecided: "limit";
|
package/lib/cores/kind.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// The interface every widget kind gives the cores of solvers: its scene read by its schema, its
|
|
2
|
-
// rules, its goal and its
|
|
3
|
-
// them; its scene may be any form the kind reads its JSON into.
|
|
2
|
+
// rules, its goal and its limits of states and of work. A kind's state and moves are JSON, as the
|
|
3
|
+
// widget reports them; its scene may be any form the kind reads its JSON into.
|
|
4
4
|
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Json, Played, SceneError } from "./kind.js";
|
|
2
|
+
/** A part of a registry: an operation or a goal, by the name a scene and a move give it. */
|
|
3
|
+
export interface Named {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Throws where the registry's version is not "major.minor" or a name of an operation or a goal is
|
|
8
|
+
* given twice or is not in snake_case.
|
|
9
|
+
*/
|
|
10
|
+
export declare function checkRegistry(kind: string, registryVersion: string, parts: readonly Named[]): void;
|
|
11
|
+
/** Adds a name to those taken; throws, naming what it is, where it is taken or not in snake_case. */
|
|
12
|
+
export declare function takeName(taken: Set<string>, named: string, what: string): void;
|
|
13
|
+
/**
|
|
14
|
+
* One part's parameters read at the path, or undefined where they do not read: then an error is
|
|
15
|
+
* there, the part's own or, where it named none, one that the kind cannot read them.
|
|
16
|
+
*/
|
|
17
|
+
export declare function readPart<T>(read: (value: unknown, path: string, errors: SceneError[]) => T | undefined, value: unknown, path: string, errors: SceneError[]): {
|
|
18
|
+
readonly options: T;
|
|
19
|
+
} | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* The scene's goal, a map of one member named by the goal, with its parameters read; null where
|
|
22
|
+
* the scene has none, where it does not read, or where `read` is undefined because what the goal
|
|
23
|
+
* reads against — the kind's sheet or line — did not read itself.
|
|
24
|
+
*/
|
|
25
|
+
export declare function readGoal<G>(input: unknown, goals: ReadonlyMap<string, G>, errors: SceneError[], read: ((goal: G, value: unknown, path: string, errors: SceneError[]) => unknown) | undefined): {
|
|
26
|
+
readonly goal: G;
|
|
27
|
+
readonly options: unknown;
|
|
28
|
+
} | null;
|
|
29
|
+
/** The chosen operations with their parameters, in the registry's order. */
|
|
30
|
+
export declare function inRegistryOrder<O extends Named>(operations: readonly O[], chosen: ReadonlyMap<string, unknown>): {
|
|
31
|
+
readonly op: O;
|
|
32
|
+
readonly options: unknown;
|
|
33
|
+
}[];
|
|
34
|
+
/**
|
|
35
|
+
* A move dispatched to the scene's operation it names, by its one member: refused as not a move,
|
|
36
|
+
* as an operation the kind has but the scene does not allow, or as one the kind does not have.
|
|
37
|
+
*/
|
|
38
|
+
export declare function dispatch<O extends Named, S>(allowed: readonly {
|
|
39
|
+
readonly op: O;
|
|
40
|
+
readonly options: unknown;
|
|
41
|
+
}[], known: ReadonlyMap<string, O>, move: {
|
|
42
|
+
readonly [operation: string]: Json;
|
|
43
|
+
}, play: (op: O, options: unknown, held: Json) => Played<S>): Played<S>;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// What every kind assembled from a registry of operations and goals shares: the check of the
|
|
2
|
+
// registry's names, the reading of one part's parameters and of the scene's goal, and the dispatch
|
|
3
|
+
// of a move to the operation it names. Each kind keeps what its own scene and state add.
|
|
4
|
+
import { isObject, map, pointer } from "./json.js";
|
|
5
|
+
const version = /^[1-9][0-9]*\.(0|[1-9][0-9]*)$/;
|
|
6
|
+
const snakeCase = /^[a-z][a-z0-9_]*$/;
|
|
7
|
+
/**
|
|
8
|
+
* Throws where the registry's version is not "major.minor" or a name of an operation or a goal is
|
|
9
|
+
* given twice or is not in snake_case.
|
|
10
|
+
*/
|
|
11
|
+
export function checkRegistry(kind, registryVersion, parts) {
|
|
12
|
+
if (!version.test(registryVersion)) {
|
|
13
|
+
throw new Error(`the version of ${kind} is "major.minor": ${registryVersion}`);
|
|
14
|
+
}
|
|
15
|
+
const names = new Set();
|
|
16
|
+
for (const part of parts) {
|
|
17
|
+
takeName(names, part.name, "an operation's or a goal's name");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Adds a name to those taken; throws, naming what it is, where it is taken or not in snake_case. */
|
|
21
|
+
export function takeName(taken, named, what) {
|
|
22
|
+
if (!snakeCase.test(named) || taken.has(named)) {
|
|
23
|
+
throw new Error(`${what} is new and in snake_case: ${named}`);
|
|
24
|
+
}
|
|
25
|
+
taken.add(named);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* One part's parameters read at the path, or undefined where they do not read: then an error is
|
|
29
|
+
* there, the part's own or, where it named none, one that the kind cannot read them.
|
|
30
|
+
*/
|
|
31
|
+
export function readPart(read, value, path, errors) {
|
|
32
|
+
const before = errors.length;
|
|
33
|
+
const options = read(value, path, errors);
|
|
34
|
+
if (options === undefined && errors.length === before) {
|
|
35
|
+
errors.push({ path, message: "parameters the kind cannot read" });
|
|
36
|
+
}
|
|
37
|
+
return options === undefined ? undefined : { options };
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The scene's goal, a map of one member named by the goal, with its parameters read; null where
|
|
41
|
+
* the scene has none, where it does not read, or where `read` is undefined because what the goal
|
|
42
|
+
* reads against — the kind's sheet or line — did not read itself.
|
|
43
|
+
*/
|
|
44
|
+
export function readGoal(input, goals, errors, read) {
|
|
45
|
+
const goalMap = input === undefined ? undefined : map(input, "/goal", errors);
|
|
46
|
+
if (goalMap === undefined) {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const entries = Object.entries(goalMap);
|
|
50
|
+
const [named, value] = entries[0] ?? ["", undefined];
|
|
51
|
+
const found = goals.get(named);
|
|
52
|
+
if (entries.length !== 1) {
|
|
53
|
+
errors.push({ path: "/goal", message: "one goal, by its name" });
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
if (found === undefined) {
|
|
57
|
+
errors.push({ path: pointer("/goal", named), message: "no goal of the kind has this name" });
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
if (read === undefined) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
const part = readPart((v, path, errs) => read(found, v, path, errs), value, pointer("/goal", named), errors);
|
|
64
|
+
return part === undefined ? null : { goal: found, options: part.options };
|
|
65
|
+
}
|
|
66
|
+
/** The chosen operations with their parameters, in the registry's order. */
|
|
67
|
+
export function inRegistryOrder(operations, chosen) {
|
|
68
|
+
return operations
|
|
69
|
+
.filter((op) => chosen.has(op.name))
|
|
70
|
+
.map((op) => ({ op, options: chosen.get(op.name) }));
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A move dispatched to the scene's operation it names, by its one member: refused as not a move,
|
|
74
|
+
* as an operation the kind has but the scene does not allow, or as one the kind does not have.
|
|
75
|
+
*/
|
|
76
|
+
export function dispatch(allowed, known, move, play) {
|
|
77
|
+
const named = isObject(move) ? Object.keys(move) : [];
|
|
78
|
+
if (named.length !== 1) {
|
|
79
|
+
return { ok: false, reason: "not-a-move" };
|
|
80
|
+
}
|
|
81
|
+
const opName = named[0];
|
|
82
|
+
const chosen = allowed.find(({ op }) => op.name === opName);
|
|
83
|
+
if (chosen === undefined) {
|
|
84
|
+
return { ok: false, reason: known.has(opName) ? "op-not-in-scene" : "unknown-op" };
|
|
85
|
+
}
|
|
86
|
+
return play(chosen.op, chosen.options, move[opName]);
|
|
87
|
+
}
|
package/lib/cores/search.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import type { Json, Kind, ProofJson, SceneError, Undecided } from "./kind.js";
|
|
1
|
+
import type { Ask, Json, Kind, ProofJson, SceneError, Undecided } from "./kind.js";
|
|
2
|
+
/**
|
|
3
|
+
* The work of trying one move beyond the length of its state's canonical key, by which a kind's
|
|
4
|
+
* limit of work counts: about what listing, playing and keying a move costs whatever its state.
|
|
5
|
+
*/
|
|
6
|
+
export declare const moveWork = 64;
|
|
2
7
|
/** A move from a state and the index of the state it leads to. */
|
|
3
8
|
export interface Edge<Move> {
|
|
4
9
|
readonly move: Move;
|
|
@@ -17,6 +22,13 @@ export interface StateNode<State, Move> {
|
|
|
17
22
|
* the goal asks a maximum or a minimum, one of that measure —; null where none can be reached.
|
|
18
23
|
*/
|
|
19
24
|
readonly distance: number | null;
|
|
25
|
+
/**
|
|
26
|
+
* The fewest moves from the state to any state that meets the goal, whatever its measure; null
|
|
27
|
+
* where none can be reached. It is the distance but where the goal asks a maximum or a minimum,
|
|
28
|
+
* so that a state met short of the extreme measure, or one that can still meet the goal only
|
|
29
|
+
* short of it, is told from one where the goal cannot be met at all.
|
|
30
|
+
*/
|
|
31
|
+
readonly nearest: number | null;
|
|
20
32
|
/** Every move the rules take from the state, in the kind's order, a move that changes nothing among them. */
|
|
21
33
|
readonly next: readonly Edge<Move>[];
|
|
22
34
|
/** The state and move it was first reached by; null for the scene's own state. */
|
|
@@ -26,9 +38,13 @@ export interface StateNode<State, Move> {
|
|
|
26
38
|
} | null;
|
|
27
39
|
}
|
|
28
40
|
export interface StateGraph<State, Move> {
|
|
41
|
+
/** What the key asks, the fewest moves where the kind does not say: what the distances serve. */
|
|
42
|
+
readonly asks: Ask<State>["key"];
|
|
29
43
|
readonly nodes: readonly StateNode<State, Move>[];
|
|
30
44
|
/** The indices of the states of each layer, the scene's own state alone in layer 0. */
|
|
31
45
|
readonly layers: readonly (readonly number[])[];
|
|
46
|
+
/** The graph's work, as the kind's limit of work counts it. */
|
|
47
|
+
readonly work: number;
|
|
32
48
|
/** The node of a state, or undefined where the scene cannot reach it or it is no state. */
|
|
33
49
|
at(state: State): StateNode<State, Move> | undefined;
|
|
34
50
|
}
|
|
@@ -39,6 +55,7 @@ export type GraphResult<State, Move> = {
|
|
|
39
55
|
readonly ok: false;
|
|
40
56
|
} & ({
|
|
41
57
|
readonly undecided: "limit";
|
|
58
|
+
readonly passes: "states" | "work";
|
|
42
59
|
} | {
|
|
43
60
|
readonly undecided: "invalid";
|
|
44
61
|
readonly errors: readonly SceneError[];
|
|
@@ -67,8 +84,9 @@ export type Proof<State, Move> = {
|
|
|
67
84
|
/**
|
|
68
85
|
* The whole state graph of a scene, read from its JSON by the kind's schema: every state the
|
|
69
86
|
* scene's moves reach, layer by layer, states that are equal by their canonical key merged. A
|
|
70
|
-
* graph with more states than the kind's limit
|
|
71
|
-
*
|
|
87
|
+
* graph with more states than the kind's limit, or more work than its limit of work, is
|
|
88
|
+
* undecided, wherever its goal lies: the limits hold every state a learner can reach, not only the
|
|
89
|
+
* search from the scene to the goal. A
|
|
72
90
|
* measure the goal asks that is no safe integer throws, as a state that is no JSON of integers
|
|
73
91
|
* does: either is a fault of the kind, not of the scene.
|
|
74
92
|
*/
|
package/lib/cores/search.js
CHANGED
|
@@ -5,11 +5,17 @@
|
|
|
5
5
|
// or an adversary answers a move needs the core of games, whose witness is a strategy.
|
|
6
6
|
import { counted, extreme, ways } from "./enumerate.js";
|
|
7
7
|
import { canonicalKey } from "./key.js";
|
|
8
|
+
/**
|
|
9
|
+
* The work of trying one move beyond the length of its state's canonical key, by which a kind's
|
|
10
|
+
* limit of work counts: about what listing, playing and keying a move costs whatever its state.
|
|
11
|
+
*/
|
|
12
|
+
export const moveWork = 64;
|
|
8
13
|
/**
|
|
9
14
|
* The whole state graph of a scene, read from its JSON by the kind's schema: every state the
|
|
10
15
|
* scene's moves reach, layer by layer, states that are equal by their canonical key merged. A
|
|
11
|
-
* graph with more states than the kind's limit
|
|
12
|
-
*
|
|
16
|
+
* graph with more states than the kind's limit, or more work than its limit of work, is
|
|
17
|
+
* undecided, wherever its goal lies: the limits hold every state a learner can reach, not only the
|
|
18
|
+
* search from the scene to the goal. A
|
|
13
19
|
* measure the goal asks that is no safe integer throws, as a state that is no JSON of integers
|
|
14
20
|
* does: either is a fault of the kind, not of the scene.
|
|
15
21
|
*/
|
|
@@ -24,6 +30,15 @@ function search(kind, scene, seed) {
|
|
|
24
30
|
if (!Number.isSafeInteger(kind.limit) || kind.limit < 1) {
|
|
25
31
|
throw new RangeError(`a kind's limit of states is a whole number from 1: ${String(kind.limit)}`);
|
|
26
32
|
}
|
|
33
|
+
if (!Number.isSafeInteger(kind.workLimit) || kind.workLimit < 1) {
|
|
34
|
+
throw new RangeError(`a kind's limit of work is a whole number from 1: ${String(kind.workLimit)}`);
|
|
35
|
+
}
|
|
36
|
+
// The work is a sum over the states, whatever order they are found in, so a graph passes the
|
|
37
|
+
// limit at a build exactly where it does on a device. A state's work is counted before its moves
|
|
38
|
+
// are played, so one with too many moves to try stops the search before they cost anything more;
|
|
39
|
+
// where the kind counts them before it lists them, before they are listed too, and since that
|
|
40
|
+
// count is never more than the list, the search stops at the same state either way.
|
|
41
|
+
let work = 0;
|
|
27
42
|
const nodes = [];
|
|
28
43
|
const byKey = new Map();
|
|
29
44
|
const add = (state, key, layer, parent) => {
|
|
@@ -35,6 +50,7 @@ function search(kind, scene, seed) {
|
|
|
35
50
|
layer,
|
|
36
51
|
goal: kind.meets(scene, state),
|
|
37
52
|
distance: null,
|
|
53
|
+
nearest: null,
|
|
38
54
|
next: [],
|
|
39
55
|
parent,
|
|
40
56
|
});
|
|
@@ -51,7 +67,12 @@ function search(kind, scene, seed) {
|
|
|
51
67
|
// The version a state is played at is its layer, though a widget may reach the state at a
|
|
52
68
|
// later version, by an undo or a longer way: a kind on this core reads nothing of `at`.
|
|
53
69
|
const at = { version: node.layer, seed };
|
|
54
|
-
|
|
70
|
+
const listed = listMoves(kind, scene, node.state, node.key, work);
|
|
71
|
+
if (listed === null) {
|
|
72
|
+
return { ok: false, undecided: "limit", passes: "work" };
|
|
73
|
+
}
|
|
74
|
+
work = listed.work;
|
|
75
|
+
for (const move of listed.moves) {
|
|
55
76
|
const played = kind.play(scene, node.state, move, at);
|
|
56
77
|
if (!played.ok) {
|
|
57
78
|
continue;
|
|
@@ -65,7 +86,7 @@ function search(kind, scene, seed) {
|
|
|
65
86
|
let to = byKey.get(key);
|
|
66
87
|
if (to === undefined) {
|
|
67
88
|
if (nodes.length >= kind.limit) {
|
|
68
|
-
return { ok: false, undecided: "limit" };
|
|
89
|
+
return { ok: false, undecided: "limit", passes: "states" };
|
|
69
90
|
}
|
|
70
91
|
to = add(played.state, key, layer, { from, move });
|
|
71
92
|
found.push(to);
|
|
@@ -79,13 +100,20 @@ function search(kind, scene, seed) {
|
|
|
79
100
|
current = found;
|
|
80
101
|
}
|
|
81
102
|
const ask = asked(kind, scene);
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
103
|
+
const before = movesInto(nodes);
|
|
104
|
+
const nearest = distances(before, nodes.filter((node) => node.goal).map((node) => node.index));
|
|
105
|
+
const distance = ask.key === "max" || ask.key === "min"
|
|
106
|
+
? distances(before, extreme(nodes, ask.key, ask.measure)?.at ?? [])
|
|
107
|
+
: nearest;
|
|
108
|
+
for (const node of nodes) {
|
|
109
|
+
node.distance = distance[node.index] ?? null;
|
|
110
|
+
node.nearest = nearest[node.index] ?? null;
|
|
111
|
+
}
|
|
86
112
|
const graph = {
|
|
113
|
+
asks: ask.key,
|
|
87
114
|
nodes,
|
|
88
115
|
layers,
|
|
116
|
+
work,
|
|
89
117
|
at: (state) => {
|
|
90
118
|
let key;
|
|
91
119
|
try {
|
|
@@ -101,35 +129,70 @@ function search(kind, scene, seed) {
|
|
|
101
129
|
};
|
|
102
130
|
return { ok: true, graph };
|
|
103
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* The moves the kind lists from a state and the work so far with theirs, or null where the work
|
|
134
|
+
* passes the kind's limit: before the moves are listed, where the kind counts them first. It stays
|
|
135
|
+
* out of the search's loop: a closure over the loop's state there slowed every graph by half on the
|
|
136
|
+
* stand-in for a phone (measurements 2.5).
|
|
137
|
+
*/
|
|
138
|
+
function listMoves(kind, scene, state, key, before) {
|
|
139
|
+
const least = kind.leastMoves?.(scene, state) ?? 0;
|
|
140
|
+
if (!Number.isSafeInteger(least) || least < 0) {
|
|
141
|
+
throw new RangeError(`a kind's count of a state's moves is a whole number: ${least}`);
|
|
142
|
+
}
|
|
143
|
+
let weight = 0;
|
|
144
|
+
if (least > 0) {
|
|
145
|
+
weight = key.length + (kind.implied?.(scene, state) ?? 0) + moveWork;
|
|
146
|
+
if (before + least * weight > kind.workLimit) {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const moves = kind.moves(scene, state);
|
|
151
|
+
if (moves.length < least) {
|
|
152
|
+
throw new RangeError(`the kind listed ${moves.length} moves from a state it counted at least ${least} from`);
|
|
153
|
+
}
|
|
154
|
+
let work = before;
|
|
155
|
+
if (moves.length > 0) {
|
|
156
|
+
weight ||= key.length + (kind.implied?.(scene, state) ?? 0) + moveWork;
|
|
157
|
+
work += moves.length * weight;
|
|
158
|
+
}
|
|
159
|
+
return work > kind.workLimit ? null : { moves, work };
|
|
160
|
+
}
|
|
104
161
|
/** What the scene's goal asks: the fewest moves where the kind does not say, or the scene has no goal. */
|
|
105
162
|
function asked(kind, scene) {
|
|
106
163
|
return (kind.hasGoal(scene) ? kind.asks?.(scene) : undefined) ?? { key: "fewest" };
|
|
107
164
|
}
|
|
108
|
-
//
|
|
109
|
-
function
|
|
165
|
+
// The states each state is reached from by one move, by its index.
|
|
166
|
+
function movesInto(nodes) {
|
|
110
167
|
const before = nodes.map(() => []);
|
|
111
168
|
for (const node of nodes) {
|
|
112
169
|
for (const edge of node.next) {
|
|
113
170
|
before[edge.to].push(node.index);
|
|
114
171
|
}
|
|
115
172
|
}
|
|
173
|
+
return before;
|
|
174
|
+
}
|
|
175
|
+
// Each state's fewest moves to the targets, by its index, null where none can be reached: a search
|
|
176
|
+
// by layers backwards from them.
|
|
177
|
+
function distances(before, targets) {
|
|
178
|
+
const found = before.map(() => null);
|
|
116
179
|
let current = [...targets];
|
|
117
180
|
for (const index of current) {
|
|
118
|
-
|
|
181
|
+
found[index] = 0;
|
|
119
182
|
}
|
|
120
183
|
for (let distance = 1; current.length > 0; distance++) {
|
|
121
|
-
const
|
|
184
|
+
const next = [];
|
|
122
185
|
for (const to of current) {
|
|
123
186
|
for (const from of before[to]) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
found.push(from);
|
|
187
|
+
if (found[from] === null) {
|
|
188
|
+
found[from] = distance;
|
|
189
|
+
next.push(from);
|
|
128
190
|
}
|
|
129
191
|
}
|
|
130
192
|
}
|
|
131
|
-
current =
|
|
193
|
+
current = next;
|
|
132
194
|
}
|
|
195
|
+
return found;
|
|
133
196
|
}
|
|
134
197
|
// The moves by which the search first reached a state, from the scene: a path of the fewest moves.
|
|
135
198
|
function pathTo(graph, end) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const bishop: import("./operation.js").Operation<
|
|
1
|
+
export declare const bishop: import("./operation.js").Operation<import("./piece.js").Start>, bishopOn: import("./operation.js").Goal<import("./piece.js").On>, bishopReach: import("./operation.js").Goal<import("./piece.js").Reach>;
|
|
@@ -5,4 +5,12 @@
|
|
|
5
5
|
* every scene of the kind is proved again.
|
|
6
6
|
*/
|
|
7
7
|
export declare const limit = 3000;
|
|
8
|
+
/**
|
|
9
|
+
* The most work a scene's whole state graph may take, as the search core counts it, at a build and
|
|
10
|
+
* on a device alike: as much as a phone's stand-in does within a second on the operation whose
|
|
11
|
+
* work is slowest, measured by just graphs. It refuses a scene whose states carry hundreds of
|
|
12
|
+
* marks or places, or have a hundred thousand moves to try, though their count is within the
|
|
13
|
+
* limit of states; a change of it, or of how the search counts work, proves every scene again.
|
|
14
|
+
*/
|
|
15
|
+
export declare const workLimit = 6000000;
|
|
8
16
|
export declare const cells: import("../../cores/kind.js").Kind<import("./kind.js").Scene, import("./sheet.js").State, import("./kind.js").Move>;
|