@metronforge/concept-map 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -2
- package/dist/layout/index.js +4 -11
- package/dist/layout/index.js.map +1 -1
- package/dist/layout/place-sketch.d.ts +74 -0
- package/dist/layout/place-sketch.d.ts.map +1 -0
- package/dist/layout/place-sketch.js +16 -0
- package/dist/layout/place-sketch.js.map +1 -0
- package/dist/layout/place-with-hints.d.ts +7 -0
- package/dist/layout/place-with-hints.d.ts.map +1 -0
- package/dist/layout/place-with-hints.js +17 -0
- package/dist/layout/place-with-hints.js.map +1 -0
- package/dist/layout/relations/align-layout.d.ts.map +1 -1
- package/dist/layout/relations/align-layout.js +6 -2
- package/dist/layout/relations/align-layout.js.map +1 -1
- package/dist/layout/relations/apply.js +1 -1
- package/dist/layout/relations/apply.js.map +1 -1
- package/dist/layout/relations/bounds.d.ts +24 -0
- package/dist/layout/relations/bounds.d.ts.map +1 -0
- package/dist/layout/relations/bounds.js +126 -0
- package/dist/layout/relations/bounds.js.map +1 -0
- package/dist/layout/relations/check-linear.d.ts +17 -0
- package/dist/layout/relations/check-linear.d.ts.map +1 -0
- package/dist/layout/relations/check-linear.js +88 -0
- package/dist/layout/relations/check-linear.js.map +1 -0
- package/dist/layout/relations/check-objective.d.ts +10 -0
- package/dist/layout/relations/check-objective.d.ts.map +1 -0
- package/dist/layout/relations/check-objective.js +41 -0
- package/dist/layout/relations/check-objective.js.map +1 -0
- package/dist/layout/relations/check-witness.d.ts +4 -0
- package/dist/layout/relations/check-witness.d.ts.map +1 -1
- package/dist/layout/relations/check-witness.js +86 -5
- package/dist/layout/relations/check-witness.js.map +1 -1
- package/dist/layout/relations/cross.d.ts +1 -1
- package/dist/layout/relations/cross.d.ts.map +1 -1
- package/dist/layout/relations/cross.js +2 -2
- package/dist/layout/relations/cross.js.map +1 -1
- package/dist/layout/relations/derive.d.ts.map +1 -1
- package/dist/layout/relations/derive.js +18 -9
- package/dist/layout/relations/derive.js.map +1 -1
- package/dist/layout/relations/derived-input.d.ts +3 -0
- package/dist/layout/relations/derived-input.d.ts.map +1 -1
- package/dist/layout/relations/derived-input.js +9 -0
- package/dist/layout/relations/derived-input.js.map +1 -1
- package/dist/layout/relations/explain.d.ts +5 -0
- package/dist/layout/relations/explain.d.ts.map +1 -1
- package/dist/layout/relations/explain.js +53 -28
- package/dist/layout/relations/explain.js.map +1 -1
- package/dist/layout/relations/free-witness.d.ts +8 -0
- package/dist/layout/relations/free-witness.d.ts.map +1 -0
- package/dist/layout/relations/free-witness.js +38 -0
- package/dist/layout/relations/free-witness.js.map +1 -0
- package/dist/layout/relations/implication.d.ts +4 -0
- package/dist/layout/relations/implication.d.ts.map +1 -0
- package/dist/layout/relations/implication.js +23 -0
- package/dist/layout/relations/implication.js.map +1 -0
- package/dist/layout/relations/linear-differences.d.ts +16 -0
- package/dist/layout/relations/linear-differences.d.ts.map +1 -0
- package/dist/layout/relations/linear-differences.js +57 -0
- package/dist/layout/relations/linear-differences.js.map +1 -0
- package/dist/layout/relations/linear.d.ts +25 -0
- package/dist/layout/relations/linear.d.ts.map +1 -0
- package/dist/layout/relations/linear.js +75 -0
- package/dist/layout/relations/linear.js.map +1 -0
- package/dist/layout/relations/min-cost-flow.d.ts +7 -0
- package/dist/layout/relations/min-cost-flow.d.ts.map +1 -0
- package/dist/layout/relations/min-cost-flow.js +79 -0
- package/dist/layout/relations/min-cost-flow.js.map +1 -0
- package/dist/layout/relations/objective.d.ts +14 -0
- package/dist/layout/relations/objective.d.ts.map +1 -0
- package/dist/layout/relations/objective.js +33 -0
- package/dist/layout/relations/objective.js.map +1 -0
- package/dist/layout/relations/optimise.d.ts +20 -0
- package/dist/layout/relations/optimise.d.ts.map +1 -0
- package/dist/layout/relations/optimise.js +54 -0
- package/dist/layout/relations/optimise.js.map +1 -0
- package/dist/layout/relations/paths.d.ts +6 -0
- package/dist/layout/relations/paths.d.ts.map +1 -1
- package/dist/layout/relations/paths.js +35 -0
- package/dist/layout/relations/paths.js.map +1 -1
- package/dist/layout/relations/rank.d.ts +3 -2
- package/dist/layout/relations/rank.d.ts.map +1 -1
- package/dist/layout/relations/rank.js +5 -5
- package/dist/layout/relations/rank.js.map +1 -1
- package/dist/layout/relations/rational.d.ts +13 -0
- package/dist/layout/relations/rational.d.ts.map +1 -0
- package/dist/layout/relations/rational.js +28 -0
- package/dist/layout/relations/rational.js.map +1 -0
- package/dist/layout/relations/reduce-objective.d.ts +12 -0
- package/dist/layout/relations/reduce-objective.d.ts.map +1 -0
- package/dist/layout/relations/reduce-objective.js +37 -0
- package/dist/layout/relations/reduce-objective.js.map +1 -0
- package/dist/layout/relations/sketch-order.d.ts +6 -0
- package/dist/layout/relations/sketch-order.d.ts.map +1 -0
- package/dist/layout/relations/sketch-order.js +66 -0
- package/dist/layout/relations/sketch-order.js.map +1 -0
- package/dist/layout/relations/sketch-placement.d.ts +13 -0
- package/dist/layout/relations/sketch-placement.d.ts.map +1 -0
- package/dist/layout/relations/sketch-placement.js +123 -0
- package/dist/layout/relations/sketch-placement.js.map +1 -0
- package/dist/layout/relations/sketch-ranks.d.ts +5 -0
- package/dist/layout/relations/sketch-ranks.d.ts.map +1 -0
- package/dist/layout/relations/sketch-ranks.js +26 -0
- package/dist/layout/relations/sketch-ranks.js.map +1 -0
- package/dist/layout/relations/solve.d.ts +4 -1
- package/dist/layout/relations/solve.d.ts.map +1 -1
- package/dist/layout/relations/solve.js +10 -6
- package/dist/layout/relations/solve.js.map +1 -1
- package/dist/layout/route/channel-layout.d.ts +1 -0
- package/dist/layout/route/channel-layout.d.ts.map +1 -1
- package/dist/layout/route/channel-layout.js +4 -0
- package/dist/layout/route/channel-layout.js.map +1 -1
- package/dist/layout/route/channel-moved.d.ts +1 -1
- package/dist/layout/route/channel-moved.d.ts.map +1 -1
- package/dist/layout/route/channel-moved.js +8 -5
- package/dist/layout/route/channel-moved.js.map +1 -1
- package/dist/layout/sketch-agreement.d.ts +7 -0
- package/dist/layout/sketch-agreement.d.ts.map +1 -0
- package/dist/layout/sketch-agreement.js +48 -0
- package/dist/layout/sketch-agreement.js.map +1 -0
- package/dist/layout/sketch-bends.d.ts +5 -0
- package/dist/layout/sketch-bends.d.ts.map +1 -0
- package/dist/layout/sketch-bends.js +21 -0
- package/dist/layout/sketch-bends.js.map +1 -0
- package/dist/layout/sketch-returns.d.ts +4 -0
- package/dist/layout/sketch-returns.d.ts.map +1 -0
- package/dist/layout/sketch-returns.js +67 -0
- package/dist/layout/sketch-returns.js.map +1 -0
- package/dist/layout/sketch-route.d.ts +11 -0
- package/dist/layout/sketch-route.d.ts.map +1 -0
- package/dist/layout/sketch-route.js +109 -0
- package/dist/layout/sketch-route.js.map +1 -0
- package/dist/layout/sketch-safety.d.ts +6 -0
- package/dist/layout/sketch-safety.d.ts.map +1 -0
- package/dist/layout/sketch-safety.js +29 -0
- package/dist/layout/sketch-safety.js.map +1 -0
- package/dist/layout/sketch-shortcuts.d.ts +4 -0
- package/dist/layout/sketch-shortcuts.d.ts.map +1 -0
- package/dist/layout/sketch-shortcuts.js +51 -0
- package/dist/layout/sketch-shortcuts.js.map +1 -0
- package/dist/layout/width.d.ts +1 -0
- package/dist/layout/width.d.ts.map +1 -1
- package/dist/layout/width.js +3 -0
- package/dist/layout/width.js.map +1 -1
- package/package.json +2 -2
- package/src/layout/index.ts +10 -17
- package/src/layout/place-sketch.ts +34 -0
- package/src/layout/place-with-hints.ts +32 -0
- package/src/layout/relations/align-layout.ts +11 -6
- package/src/layout/relations/apply.ts +1 -1
- package/src/layout/relations/bounds.ts +163 -0
- package/src/layout/relations/check-linear.ts +153 -0
- package/src/layout/relations/check-objective.ts +60 -0
- package/src/layout/relations/check-witness.ts +118 -5
- package/src/layout/relations/cross.ts +2 -1
- package/src/layout/relations/derive.ts +24 -9
- package/src/layout/relations/derived-input.ts +13 -0
- package/src/layout/relations/explain.ts +69 -32
- package/src/layout/relations/free-witness.ts +41 -0
- package/src/layout/relations/implication.ts +30 -0
- package/src/layout/relations/linear-differences.ts +77 -0
- package/src/layout/relations/linear.ts +107 -0
- package/src/layout/relations/min-cost-flow.ts +96 -0
- package/src/layout/relations/objective.ts +53 -0
- package/src/layout/relations/optimise.ts +97 -0
- package/src/layout/relations/paths.ts +35 -0
- package/src/layout/relations/rank.ts +7 -4
- package/src/layout/relations/rational.ts +39 -0
- package/src/layout/relations/reduce-objective.ts +60 -0
- package/src/layout/relations/sketch-order.ts +79 -0
- package/src/layout/relations/sketch-placement.ts +168 -0
- package/src/layout/relations/sketch-ranks.ts +29 -0
- package/src/layout/relations/solve.ts +14 -6
- package/src/layout/route/channel-layout.ts +7 -0
- package/src/layout/route/channel-moved.ts +8 -6
- package/src/layout/sketch-agreement.ts +69 -0
- package/src/layout/sketch-bends.ts +29 -0
- package/src/layout/sketch-returns.ts +81 -0
- package/src/layout/sketch-route.ts +196 -0
- package/src/layout/sketch-safety.ts +64 -0
- package/src/layout/sketch-shortcuts.ts +65 -0
- package/src/layout/width.ts +4 -0
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
import { relationBounds } from "./bounds.js";
|
|
2
|
+
import { freeWitness } from "./free-witness.js";
|
|
1
3
|
import { normalizeRelations } from "./normalize.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
+
import { trackImplication } from "./implication.js";
|
|
5
|
+
import { extendLongestPaths, longestPaths } from "./paths.js";
|
|
6
|
+
import { solutionFromPaths } from "./solve.js";
|
|
4
7
|
import type { RelationSolution } from "./result.js";
|
|
8
|
+
import type { RelationBound } from "./bounds.js";
|
|
5
9
|
import type { RelationEdge, RelationGraph, RelationSystem } from "./types.js";
|
|
6
10
|
|
|
7
11
|
export interface RelationVerdict {
|
|
@@ -10,22 +14,56 @@ export interface RelationVerdict {
|
|
|
10
14
|
}
|
|
11
15
|
|
|
12
16
|
export interface RelationExplanation {
|
|
17
|
+
/** Uniqueness from both interval ends, independent of compact placement. */
|
|
18
|
+
status: RelationSolution["status"];
|
|
13
19
|
/** A status witness using only anchors and the judged relation prefix. */
|
|
14
20
|
solution: RelationSolution;
|
|
15
21
|
/** Source order; empty if anchors contradict, ends at the first conflict. */
|
|
16
22
|
verdicts: RelationVerdict[];
|
|
23
|
+
/** Exact feasible intervals; absent when the judged prefix contradicts. */
|
|
24
|
+
bounds: RelationBound[] | null;
|
|
17
25
|
}
|
|
18
26
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
function addRelation(
|
|
28
|
+
prior: RelationGraph,
|
|
29
|
+
paths: ReturnType<typeof longestPaths>,
|
|
30
|
+
bounds: RelationEdge[],
|
|
31
|
+
implies: ReturnType<typeof trackImplication>,
|
|
32
|
+
) {
|
|
33
|
+
const redundant = bounds.every(implies);
|
|
34
|
+
// An implied bound cannot improve any path, so later prefixes need not
|
|
35
|
+
// revisit it. The full graph still supplies source-backed interval proofs.
|
|
36
|
+
if (!redundant) prior.edges.push(...bounds);
|
|
37
|
+
const next = redundant ? paths : extendLongestPaths(prior, paths, bounds);
|
|
38
|
+
const status: RelationVerdict["status"] =
|
|
39
|
+
next.changed !== undefined
|
|
40
|
+
? "contradicts"
|
|
41
|
+
: redundant
|
|
42
|
+
? "redundant"
|
|
43
|
+
: "adds-information";
|
|
44
|
+
return { paths: next, status };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function explanationResult(
|
|
48
|
+
graph: RelationGraph,
|
|
49
|
+
prior: RelationGraph,
|
|
50
|
+
paths: ReturnType<typeof longestPaths>,
|
|
51
|
+
) {
|
|
52
|
+
const compact = solutionFromPaths(prior, paths);
|
|
53
|
+
const bounds =
|
|
54
|
+
compact.status === "contradictory"
|
|
55
|
+
? null
|
|
56
|
+
: relationBounds(graph, paths.distances);
|
|
57
|
+
const status = bounds
|
|
58
|
+
? bounds.every((bound) => bound.determined)
|
|
59
|
+
? "determined"
|
|
60
|
+
: "underdetermined"
|
|
61
|
+
: "contradictory";
|
|
62
|
+
const solution =
|
|
63
|
+
status === "underdetermined" && bounds
|
|
64
|
+
? freeWitness(graph, bounds)
|
|
65
|
+
: compact;
|
|
66
|
+
return { status, solution, bounds } as const;
|
|
29
67
|
}
|
|
30
68
|
|
|
31
69
|
/** Internal ordered diagnostics. checkRelations independently checks solution;
|
|
@@ -38,26 +76,25 @@ export function explainRelations(system: RelationSystem): RelationExplanation {
|
|
|
38
76
|
...graph,
|
|
39
77
|
edges: graph.edges.filter((edge) => edge.source.kind === "anchor"),
|
|
40
78
|
};
|
|
41
|
-
let
|
|
79
|
+
let paths = longestPaths(prior, Array(graph.variables.length + 1).fill(0));
|
|
80
|
+
const implies = trackImplication(prior);
|
|
81
|
+
const relationEdges = new Map<string, RelationEdge[]>();
|
|
82
|
+
for (const edge of graph.edges) {
|
|
83
|
+
if (edge.source.kind !== "relation") continue;
|
|
84
|
+
const edges = relationEdges.get(edge.source.id) ?? [];
|
|
85
|
+
edges.push(edge);
|
|
86
|
+
relationEdges.set(edge.source.id, edges);
|
|
87
|
+
}
|
|
42
88
|
const verdicts: RelationVerdict[] = [];
|
|
43
|
-
for (const
|
|
44
|
-
if (
|
|
45
|
-
const bounds =
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
);
|
|
49
|
-
solution = solveRelations({
|
|
50
|
-
...system,
|
|
51
|
-
relations: system.relations.slice(0, i + 1),
|
|
52
|
-
});
|
|
53
|
-
const status =
|
|
54
|
-
solution.status === "contradictory"
|
|
55
|
-
? "contradicts"
|
|
56
|
-
: implies(prior, bounds)
|
|
57
|
-
? "redundant"
|
|
58
|
-
: "adds-information";
|
|
59
|
-
verdicts.push({ id: relation.id, status });
|
|
60
|
-
prior.edges.push(...bounds);
|
|
89
|
+
for (const relation of system.relations) {
|
|
90
|
+
if (paths.changed !== undefined) break;
|
|
91
|
+
const bounds = relationEdges.get(relation.id)!;
|
|
92
|
+
const next = addRelation(prior, paths, bounds, implies);
|
|
93
|
+
paths = next.paths;
|
|
94
|
+
verdicts.push({ id: relation.id, status: next.status });
|
|
61
95
|
}
|
|
62
|
-
return {
|
|
96
|
+
return {
|
|
97
|
+
...explanationResult(graph, prior, paths),
|
|
98
|
+
verdicts,
|
|
99
|
+
};
|
|
63
100
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { RelationBound } from "./bounds.js";
|
|
2
|
+
import type { RelationSolution } from "./result.js";
|
|
3
|
+
import type { RelationGraph } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/** A feasible one-tick move from an endpoint or an unbounded ray. */
|
|
6
|
+
export function freeWitness(
|
|
7
|
+
graph: RelationGraph,
|
|
8
|
+
bounds: RelationBound[],
|
|
9
|
+
): Extract<RelationSolution, { status: "underdetermined" }> {
|
|
10
|
+
const bound = bounds.find((item) => !item.determined)!;
|
|
11
|
+
const target = graph.variables.indexOf(bound.variable) + 1;
|
|
12
|
+
const ray = bound.lowerRay ?? bound.upperRay;
|
|
13
|
+
const placement = ray?.placement ?? bound.lowerAttainment!;
|
|
14
|
+
const upward = bound.lowerRay === null;
|
|
15
|
+
const moving = new Set<number>();
|
|
16
|
+
if (ray) {
|
|
17
|
+
for (const variable of ray.variables)
|
|
18
|
+
moving.add(graph.variables.indexOf(variable) + 1);
|
|
19
|
+
} else {
|
|
20
|
+
moving.add(target);
|
|
21
|
+
const queue = [target];
|
|
22
|
+
for (let i = 0; i < queue.length; i++)
|
|
23
|
+
for (const edge of graph.edges) {
|
|
24
|
+
if (edge.from !== queue[i] || moving.has(edge.to)) continue;
|
|
25
|
+
if (placement[edge.to] - placement[edge.from] !== edge.weight) continue;
|
|
26
|
+
moving.add(edge.to);
|
|
27
|
+
queue.push(edge.to);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
status: "underdetermined",
|
|
32
|
+
placements: graph.variables.map((variable, i) => ({
|
|
33
|
+
variable,
|
|
34
|
+
position: placement[i + 1] / 64,
|
|
35
|
+
})),
|
|
36
|
+
shift: {
|
|
37
|
+
variables: graph.variables.filter((_, i) => moving.has(i + 1)),
|
|
38
|
+
delta: (upward ? 1 : -1) / 64,
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { extendLongestPaths, longestPaths } from "./paths.js";
|
|
2
|
+
import type { RelationEdge, RelationGraph } from "./types.js";
|
|
3
|
+
|
|
4
|
+
/** Keep source-specific longest paths current as the relation prefix grows. */
|
|
5
|
+
export function trackImplication(graph: RelationGraph) {
|
|
6
|
+
const cached = new Map<
|
|
7
|
+
number,
|
|
8
|
+
{ paths: ReturnType<typeof longestPaths>; edgeCount: number }
|
|
9
|
+
>();
|
|
10
|
+
return (edge: Pick<RelationEdge, "from" | "to" | "weight">): boolean => {
|
|
11
|
+
let entry = cached.get(edge.from);
|
|
12
|
+
if (!entry) {
|
|
13
|
+
const initial = Array<number>(graph.variables.length + 1).fill(-Infinity);
|
|
14
|
+
initial[edge.from] = 0;
|
|
15
|
+
entry = {
|
|
16
|
+
paths: longestPaths(graph, initial),
|
|
17
|
+
edgeCount: graph.edges.length,
|
|
18
|
+
};
|
|
19
|
+
cached.set(edge.from, entry);
|
|
20
|
+
} else if (entry.edgeCount < graph.edges.length) {
|
|
21
|
+
entry.paths = extendLongestPaths(
|
|
22
|
+
graph,
|
|
23
|
+
entry.paths,
|
|
24
|
+
graph.edges.slice(entry.edgeCount),
|
|
25
|
+
);
|
|
26
|
+
entry.edgeCount = graph.edges.length;
|
|
27
|
+
}
|
|
28
|
+
return entry.paths.distances[edge.to] >= edge.weight;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { relationBounds } from "./bounds.js";
|
|
2
|
+
import { normalizeRelations } from "./normalize.js";
|
|
3
|
+
import { solveRelations } from "./solve.js";
|
|
4
|
+
import { solveLinearEqualities } from "./linear.js";
|
|
5
|
+
import { checkLinearEqualities } from "./check-linear.js";
|
|
6
|
+
import type { LinearRelation } from "./linear.js";
|
|
7
|
+
import type { RelationSystem } from "./types.js";
|
|
8
|
+
|
|
9
|
+
export interface SubstitutedLinear {
|
|
10
|
+
fixed: Record<string, number>;
|
|
11
|
+
variables: string[];
|
|
12
|
+
relations: LinearRelation[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function substitute(
|
|
16
|
+
relation: LinearRelation,
|
|
17
|
+
fixed: Record<string, number>,
|
|
18
|
+
): LinearRelation {
|
|
19
|
+
if (
|
|
20
|
+
typeof relation.constant !== "bigint" &&
|
|
21
|
+
!Number.isSafeInteger(relation.constant)
|
|
22
|
+
)
|
|
23
|
+
throw new RangeError("Unsafe linear constant");
|
|
24
|
+
let constant = BigInt(relation.constant);
|
|
25
|
+
const terms = relation.terms.filter(({ variable, coefficient }) => {
|
|
26
|
+
if (typeof coefficient !== "bigint" && !Number.isSafeInteger(coefficient))
|
|
27
|
+
throw new RangeError("Unsafe linear coefficient");
|
|
28
|
+
if (!Object.hasOwn(fixed, variable)) return true;
|
|
29
|
+
constant -= BigInt(coefficient) * BigInt(fixed[variable]);
|
|
30
|
+
return false;
|
|
31
|
+
});
|
|
32
|
+
const value = Number(constant);
|
|
33
|
+
if (!Number.isSafeInteger(value)) return { kind: "linear", terms, constant };
|
|
34
|
+
return { kind: "linear", terms, constant: value };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Substitute only two-sided fixed coordinates, in exact 1/64 ticks. */
|
|
38
|
+
export function substituteDeterminedLinear(
|
|
39
|
+
system: RelationSystem,
|
|
40
|
+
relations: LinearRelation[],
|
|
41
|
+
): SubstitutedLinear | null {
|
|
42
|
+
if (solveRelations(system).status === "contradictory") return null;
|
|
43
|
+
const bounds = relationBounds(normalizeRelations(system));
|
|
44
|
+
const fixed: Record<string, number> = {};
|
|
45
|
+
for (const bound of bounds)
|
|
46
|
+
if (bound.determined) fixed[bound.variable] = bound.lower!;
|
|
47
|
+
return {
|
|
48
|
+
fixed,
|
|
49
|
+
variables: bounds
|
|
50
|
+
.filter((bound) => !bound.determined)
|
|
51
|
+
.map((bound) => bound.variable),
|
|
52
|
+
relations: relations.map((relation) => substitute(relation, fixed)),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Solve the equality remainder without letting it determine free difference coordinates. */
|
|
57
|
+
export function solveWithDifferences(
|
|
58
|
+
system: RelationSystem,
|
|
59
|
+
relations: LinearRelation[],
|
|
60
|
+
): {
|
|
61
|
+
status: "determined" | "underdetermined" | "contradictory";
|
|
62
|
+
certificate: ReturnType<typeof solveLinearEqualities>;
|
|
63
|
+
} | null {
|
|
64
|
+
const remainder = substituteDeterminedLinear(system, relations);
|
|
65
|
+
if (!remainder) return null;
|
|
66
|
+
const { variables, relations: equations } = remainder;
|
|
67
|
+
const certificate = solveLinearEqualities(variables, equations);
|
|
68
|
+
if (!checkLinearEqualities(variables, equations, certificate))
|
|
69
|
+
throw new Error("Invalid linear equality certificate");
|
|
70
|
+
const status =
|
|
71
|
+
certificate.status === "contradictory"
|
|
72
|
+
? "contradictory"
|
|
73
|
+
: variables.length
|
|
74
|
+
? "underdetermined"
|
|
75
|
+
: "determined";
|
|
76
|
+
return { status, certificate };
|
|
77
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { add, divide, multiply, negate, one, ratio, zero } from "./rational.js";
|
|
2
|
+
import type { Rational } from "./rational.js";
|
|
3
|
+
|
|
4
|
+
/** An internal integer equality; constants use the caller's coordinate unit. */
|
|
5
|
+
export interface LinearRelation {
|
|
6
|
+
kind: "linear";
|
|
7
|
+
terms: { variable: string; coefficient: number | bigint }[];
|
|
8
|
+
constant: number | bigint;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type LinearResult =
|
|
12
|
+
| { status: "contradictory"; witness: Rational[] }
|
|
13
|
+
| { status: "determined"; values: Rational[] }
|
|
14
|
+
| { status: "underdetermined"; particular: Rational[]; basis: Rational[][] };
|
|
15
|
+
|
|
16
|
+
type Row = { cells: Rational[]; right: Rational; origin: Rational[] };
|
|
17
|
+
|
|
18
|
+
function integer(value: number | bigint): Rational {
|
|
19
|
+
if (typeof value === "bigint") return ratio(value);
|
|
20
|
+
if (!Number.isSafeInteger(value))
|
|
21
|
+
throw new RangeError(
|
|
22
|
+
"Linear coefficients and constants must be safe integers",
|
|
23
|
+
);
|
|
24
|
+
return ratio(BigInt(value));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function rowsFor(variables: string[], relations: LinearRelation[]): Row[] {
|
|
28
|
+
const indices = new Map(variables.map((id, index) => [id, index]));
|
|
29
|
+
if (indices.size !== variables.length)
|
|
30
|
+
throw new Error("Duplicate linear variable");
|
|
31
|
+
return relations.map((relation, row) => {
|
|
32
|
+
const cells = variables.map(() => zero);
|
|
33
|
+
for (const { variable, coefficient } of relation.terms) {
|
|
34
|
+
const column = indices.get(variable);
|
|
35
|
+
if (column === undefined)
|
|
36
|
+
throw new Error(`Unknown linear variable: ${variable}`);
|
|
37
|
+
cells[column] = add(cells[column], integer(coefficient));
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
cells,
|
|
41
|
+
right: integer(relation.constant),
|
|
42
|
+
origin: relations.map((_, index) => (index === row ? one : zero)),
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function subtractRows(target: Row, source: Row, factor: Rational): void {
|
|
48
|
+
target.cells = target.cells.map((value, index) =>
|
|
49
|
+
add(value, negate(multiply(factor, source.cells[index]))),
|
|
50
|
+
);
|
|
51
|
+
target.right = add(target.right, negate(multiply(factor, source.right)));
|
|
52
|
+
target.origin = target.origin.map((value, index) =>
|
|
53
|
+
add(value, negate(multiply(factor, source.origin[index]))),
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function reduce(rows: Row[], columns: number): number[] {
|
|
58
|
+
const pivots: number[] = [];
|
|
59
|
+
for (let column = 0; column < columns; column++) {
|
|
60
|
+
const candidate = rows.findIndex(
|
|
61
|
+
(row, index) =>
|
|
62
|
+
index >= pivots.length && row.cells[column].numerator !== 0n,
|
|
63
|
+
);
|
|
64
|
+
if (candidate < 0) continue;
|
|
65
|
+
const pivotRow = pivots.length;
|
|
66
|
+
[rows[pivotRow], rows[candidate]] = [rows[candidate], rows[pivotRow]];
|
|
67
|
+
const row = rows[pivotRow];
|
|
68
|
+
const divisor = row.cells[column];
|
|
69
|
+
row.cells = row.cells.map((value) => divide(value, divisor));
|
|
70
|
+
row.right = divide(row.right, divisor);
|
|
71
|
+
row.origin = row.origin.map((value) => divide(value, divisor));
|
|
72
|
+
for (let index = 0; index < rows.length; index++) {
|
|
73
|
+
if (index !== pivotRow && rows[index].cells[column].numerator !== 0n)
|
|
74
|
+
subtractRows(rows[index], row, rows[index].cells[column]);
|
|
75
|
+
}
|
|
76
|
+
pivots.push(column);
|
|
77
|
+
}
|
|
78
|
+
return pivots;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Gaussian elimination over Q. The row transform carries a contradiction witness. */
|
|
82
|
+
export function solveLinearEqualities(
|
|
83
|
+
variables: string[],
|
|
84
|
+
relations: LinearRelation[],
|
|
85
|
+
): LinearResult {
|
|
86
|
+
const rows = rowsFor(variables, relations);
|
|
87
|
+
const pivots = reduce(rows, variables.length);
|
|
88
|
+
const conflict = rows.find(
|
|
89
|
+
(row) =>
|
|
90
|
+
row.cells.every((value) => value.numerator === 0n) &&
|
|
91
|
+
row.right.numerator !== 0n,
|
|
92
|
+
);
|
|
93
|
+
if (conflict) return { status: "contradictory", witness: conflict.origin };
|
|
94
|
+
const particular = variables.map(() => zero);
|
|
95
|
+
pivots.forEach((column, index) => (particular[column] = rows[index].right));
|
|
96
|
+
if (pivots.length === variables.length)
|
|
97
|
+
return { status: "determined", values: particular };
|
|
98
|
+
const basis = variables.flatMap((_, free) => {
|
|
99
|
+
if (pivots.includes(free)) return [];
|
|
100
|
+
const vector = variables.map((_, column) => (column === free ? one : zero));
|
|
101
|
+
pivots.forEach(
|
|
102
|
+
(column, index) => (vector[column] = negate(rows[index].cells[free])),
|
|
103
|
+
);
|
|
104
|
+
return [vector];
|
|
105
|
+
});
|
|
106
|
+
return { status: "underdetermined", particular, basis };
|
|
107
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { ReducedObjective } from "./reduce-objective.js";
|
|
2
|
+
|
|
3
|
+
interface Arc {
|
|
4
|
+
from: number;
|
|
5
|
+
to: number;
|
|
6
|
+
cost: number;
|
|
7
|
+
edge: number;
|
|
8
|
+
direction: 1 | -1;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function residual(reduced: ReducedObjective, flow: number[]): Arc[] {
|
|
12
|
+
return reduced.graph.edges.flatMap((edge, i) => [
|
|
13
|
+
{
|
|
14
|
+
from: edge.from,
|
|
15
|
+
to: edge.to,
|
|
16
|
+
cost: -edge.weight,
|
|
17
|
+
edge: i,
|
|
18
|
+
direction: 1 as const,
|
|
19
|
+
},
|
|
20
|
+
...(flow[i] > 0
|
|
21
|
+
? [
|
|
22
|
+
{
|
|
23
|
+
from: edge.to,
|
|
24
|
+
to: edge.from,
|
|
25
|
+
cost: edge.weight,
|
|
26
|
+
edge: i,
|
|
27
|
+
direction: -1 as const,
|
|
28
|
+
},
|
|
29
|
+
]
|
|
30
|
+
: []),
|
|
31
|
+
]);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function relax(arcs: Arc[], count: number) {
|
|
35
|
+
const distance = Array<number>(count).fill(0);
|
|
36
|
+
const predecessor = Array<Arc | undefined>(count);
|
|
37
|
+
let changed = -1;
|
|
38
|
+
for (let pass = 0; pass < count; pass++) {
|
|
39
|
+
changed = -1;
|
|
40
|
+
for (const arc of arcs) {
|
|
41
|
+
const next = distance[arc.from] + arc.cost;
|
|
42
|
+
if (next >= distance[arc.to]) continue;
|
|
43
|
+
distance[arc.to] = next;
|
|
44
|
+
predecessor[arc.to] = arc;
|
|
45
|
+
changed = arc.to;
|
|
46
|
+
}
|
|
47
|
+
if (changed < 0) break;
|
|
48
|
+
}
|
|
49
|
+
return { distance, predecessor, changed };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function negativeCycle(
|
|
53
|
+
predecessor: (Arc | undefined)[],
|
|
54
|
+
changed: number,
|
|
55
|
+
): Arc[] {
|
|
56
|
+
let vertex = changed;
|
|
57
|
+
for (let i = 0; i < predecessor.length; i++)
|
|
58
|
+
vertex = predecessor[vertex]!.from;
|
|
59
|
+
const cycle: Arc[] = [];
|
|
60
|
+
const start = vertex;
|
|
61
|
+
do {
|
|
62
|
+
const arc = predecessor[vertex]!;
|
|
63
|
+
cycle.push(arc);
|
|
64
|
+
vertex = arc.from;
|
|
65
|
+
} while (vertex !== start);
|
|
66
|
+
return cycle;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Integral cycle cancellation yields a dual flow and tight primal potentials. */
|
|
70
|
+
export function minimumCostFlow(reduced: ReducedObjective): {
|
|
71
|
+
flow: number[];
|
|
72
|
+
positions: number[];
|
|
73
|
+
} {
|
|
74
|
+
const flow = [...reduced.costs];
|
|
75
|
+
const count = reduced.graph.variables.length + 1;
|
|
76
|
+
for (let pivot = 0; pivot < 10000; pivot++) {
|
|
77
|
+
const result = relax(residual(reduced, flow), count);
|
|
78
|
+
if (result.changed < 0) {
|
|
79
|
+
const origin = result.distance[0];
|
|
80
|
+
return {
|
|
81
|
+
flow,
|
|
82
|
+
positions: result.distance.map((value) => origin - value),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const cycle = negativeCycle(result.predecessor, result.changed);
|
|
86
|
+
const amount = Math.min(
|
|
87
|
+
...cycle
|
|
88
|
+
.filter((arc) => arc.direction === -1)
|
|
89
|
+
.map((arc) => flow[arc.edge]),
|
|
90
|
+
);
|
|
91
|
+
if (!Number.isSafeInteger(amount) || amount <= 0)
|
|
92
|
+
throw new Error("Internal alignment error: unbounded objective");
|
|
93
|
+
for (const arc of cycle) flow[arc.edge] += arc.direction * amount;
|
|
94
|
+
}
|
|
95
|
+
throw new Error("Internal alignment error: objective pivot limit");
|
|
96
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { LayoutProblem } from "../problem.js";
|
|
2
|
+
import type { RelationGraph } from "./types.js";
|
|
3
|
+
|
|
4
|
+
/** Center difference is x_from - x_to + offset, in grid ticks. */
|
|
5
|
+
export interface ArrowTerm {
|
|
6
|
+
from: number;
|
|
7
|
+
to: number;
|
|
8
|
+
weight: number;
|
|
9
|
+
offset: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Main-path arrows matter most; free return arrows matter least. */
|
|
13
|
+
export function arrowObjective(
|
|
14
|
+
problem: LayoutProblem,
|
|
15
|
+
graph: RelationGraph,
|
|
16
|
+
): ArrowTerm[] {
|
|
17
|
+
const vertices = new Map(graph.variables.map((id, i) => [id, i + 1]));
|
|
18
|
+
const main = new Set(
|
|
19
|
+
problem.constraints.flatMap((c) => (c.kind === "mainPath" ? c.edges : [])),
|
|
20
|
+
);
|
|
21
|
+
const free = new Set(
|
|
22
|
+
problem.constraints.flatMap((c) => (c.kind === "free" ? [c.edge] : [])),
|
|
23
|
+
);
|
|
24
|
+
return problem.edges.flatMap((edge) => {
|
|
25
|
+
const from = vertices.get(edge.from);
|
|
26
|
+
const to = vertices.get(edge.to);
|
|
27
|
+
if (from === undefined || to === undefined) return [];
|
|
28
|
+
return [
|
|
29
|
+
{
|
|
30
|
+
from,
|
|
31
|
+
to,
|
|
32
|
+
weight: free.has(edge.id) ? 1 : main.has(edge.id) ? 4 : 2,
|
|
33
|
+
// An odd width difference puts the true center offset halfway
|
|
34
|
+
// between ticks. Ties round toward minus infinity; the error is at
|
|
35
|
+
// most half a tick (1/128 drawing unit), invisible in the drawing.
|
|
36
|
+
offset: Math.floor((graph.sizes[from - 1] - graph.sizes[to - 1]) / 2),
|
|
37
|
+
},
|
|
38
|
+
];
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Exact L1 objective; integer coordinates are relation-grid ticks. */
|
|
43
|
+
export function objectiveCost(terms: ArrowTerm[], positions: number[]): bigint {
|
|
44
|
+
return terms.reduce((cost, term) => {
|
|
45
|
+
const difference =
|
|
46
|
+
BigInt(positions[term.from]) -
|
|
47
|
+
BigInt(positions[term.to]) +
|
|
48
|
+
BigInt(term.offset);
|
|
49
|
+
return (
|
|
50
|
+
cost + BigInt(term.weight) * (difference < 0n ? -difference : difference)
|
|
51
|
+
);
|
|
52
|
+
}, 0n);
|
|
53
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {
|
|
2
|
+
checkObjectiveCertificate,
|
|
3
|
+
type ObjectiveCertificate,
|
|
4
|
+
} from "./check-objective.js";
|
|
5
|
+
import type { ArrowTerm } from "./objective.js";
|
|
6
|
+
import { arrowObjective } from "./objective.js";
|
|
7
|
+
import { normalizeRelations } from "./normalize.js";
|
|
8
|
+
import { minimumCostFlow } from "./min-cost-flow.js";
|
|
9
|
+
import { reduceArrowObjective } from "./reduce-objective.js";
|
|
10
|
+
import type { LayoutProblem } from "../problem.js";
|
|
11
|
+
import type { LayoutResult } from "../types.js";
|
|
12
|
+
import type { RelationSolution } from "./result.js";
|
|
13
|
+
import type { RelationSystem } from "./types.js";
|
|
14
|
+
import type { RelationGraph } from "./types.js";
|
|
15
|
+
|
|
16
|
+
/** A feasible zero-cost arrow is globally optimal with the zero dual flow. */
|
|
17
|
+
export function optimiseSingleArrow(
|
|
18
|
+
graph: RelationGraph,
|
|
19
|
+
term: ArrowTerm,
|
|
20
|
+
positions: number[],
|
|
21
|
+
): { positions: number[]; certificate: ObjectiveCertificate } | null {
|
|
22
|
+
return optimiseArrows(graph, [term], positions);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Find and independently certify a minimum-cost placement on the grid. */
|
|
26
|
+
export function optimiseArrows(
|
|
27
|
+
graph: RelationGraph,
|
|
28
|
+
terms: ArrowTerm[],
|
|
29
|
+
positions: number[],
|
|
30
|
+
): { positions: number[]; certificate: ObjectiveCertificate } | null {
|
|
31
|
+
const reduced = reduceArrowObjective(graph, terms);
|
|
32
|
+
const optimum = minimumCostFlow(reduced);
|
|
33
|
+
const next = optimum.positions.slice(0, graph.variables.length + 1);
|
|
34
|
+
const certificate = {
|
|
35
|
+
slopes: terms.map(
|
|
36
|
+
(term, i) => optimum.flow[graph.edges.length + 2 * i] - term.weight,
|
|
37
|
+
),
|
|
38
|
+
flows: optimum.flow.slice(0, graph.edges.length),
|
|
39
|
+
};
|
|
40
|
+
return checkObjectiveCertificate(graph, terms, next, certificate)
|
|
41
|
+
? { positions: next, certificate }
|
|
42
|
+
: null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function placedNodes(
|
|
46
|
+
graph: RelationGraph,
|
|
47
|
+
nodes: LayoutResult["nodes"],
|
|
48
|
+
before: number[],
|
|
49
|
+
after: number[],
|
|
50
|
+
axis: 0 | 1,
|
|
51
|
+
): LayoutResult["nodes"] {
|
|
52
|
+
const changed = graph.variables.flatMap((id, i) =>
|
|
53
|
+
after[i + 1] === before[i + 1]
|
|
54
|
+
? []
|
|
55
|
+
: [
|
|
56
|
+
[
|
|
57
|
+
id,
|
|
58
|
+
nodes[id].map((value, j) =>
|
|
59
|
+
j === axis ? after[i + 1] / 64 : value,
|
|
60
|
+
) as (typeof nodes)[string],
|
|
61
|
+
] as const,
|
|
62
|
+
],
|
|
63
|
+
);
|
|
64
|
+
return changed.length ? { ...nodes, ...Object.fromEntries(changed) } : nodes;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Apply a certified zero-cost improvement after the checked placement. */
|
|
68
|
+
export function optimiseAligned(
|
|
69
|
+
problem: LayoutProblem,
|
|
70
|
+
system: RelationSystem,
|
|
71
|
+
solution: RelationSolution,
|
|
72
|
+
nodes: LayoutResult["nodes"],
|
|
73
|
+
axis: 0 | 1,
|
|
74
|
+
): LayoutResult["nodes"] {
|
|
75
|
+
if (solution.status !== "determined") return nodes;
|
|
76
|
+
if (
|
|
77
|
+
!problem.constraints.some(
|
|
78
|
+
(constraint) =>
|
|
79
|
+
constraint.kind === "pin" && constraint.strength === "required",
|
|
80
|
+
)
|
|
81
|
+
)
|
|
82
|
+
return nodes;
|
|
83
|
+
const graph = normalizeRelations(system);
|
|
84
|
+
const terms = arrowObjective(problem, graph);
|
|
85
|
+
if (terms.length === 0) return nodes;
|
|
86
|
+
const positions = [
|
|
87
|
+
0,
|
|
88
|
+
...graph.variables.map(
|
|
89
|
+
(id) =>
|
|
90
|
+
(nodes[id]?.[axis] ??
|
|
91
|
+
solution.placements.find((p) => p.variable === id)!.position) * 64,
|
|
92
|
+
),
|
|
93
|
+
];
|
|
94
|
+
const improved = optimiseArrows(graph, terms, positions);
|
|
95
|
+
if (!improved) return nodes;
|
|
96
|
+
return placedNodes(graph, nodes, positions, improved.positions, axis);
|
|
97
|
+
}
|
|
@@ -19,6 +19,41 @@ export function longestPaths(graph: RelationGraph, initial: number[]) {
|
|
|
19
19
|
return { distances, predecessors, changed };
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
/** Relax only vertices reached by newly added bounds from a feasible prefix. */
|
|
23
|
+
export function extendLongestPaths(
|
|
24
|
+
graph: RelationGraph,
|
|
25
|
+
prior: ReturnType<typeof longestPaths>,
|
|
26
|
+
added: RelationEdge[],
|
|
27
|
+
) {
|
|
28
|
+
const distances = [...prior.distances];
|
|
29
|
+
const predecessors = [...prior.predecessors];
|
|
30
|
+
let frontier = new Set<number>();
|
|
31
|
+
for (const edge of added) {
|
|
32
|
+
const candidate = distances[edge.from] + edge.weight;
|
|
33
|
+
if (candidate <= distances[edge.to]) continue;
|
|
34
|
+
distances[edge.to] = candidate;
|
|
35
|
+
predecessors[edge.to] = edge;
|
|
36
|
+
frontier.add(edge.to);
|
|
37
|
+
}
|
|
38
|
+
if (!frontier.size) return { distances, predecessors, changed: undefined };
|
|
39
|
+
for (let pass = 1; pass < distances.length; pass++) {
|
|
40
|
+
const next = new Set<number>();
|
|
41
|
+
for (const edge of graph.edges) {
|
|
42
|
+
if (!frontier.has(edge.from)) continue;
|
|
43
|
+
const candidate = distances[edge.from] + edge.weight;
|
|
44
|
+
if (candidate <= distances[edge.to]) continue;
|
|
45
|
+
distances[edge.to] = candidate;
|
|
46
|
+
predecessors[edge.to] = edge;
|
|
47
|
+
next.add(edge.to);
|
|
48
|
+
}
|
|
49
|
+
if (!next.size) return { distances, predecessors, changed: undefined };
|
|
50
|
+
if (pass === distances.length - 1)
|
|
51
|
+
return { distances, predecessors, changed: next.values().next().value };
|
|
52
|
+
frontier = next;
|
|
53
|
+
}
|
|
54
|
+
return { distances, predecessors, changed: undefined };
|
|
55
|
+
}
|
|
56
|
+
|
|
22
57
|
/** A vertex changed on sweep V has a positive cycle in its predecessor walk. */
|
|
23
58
|
export function positiveCycle(
|
|
24
59
|
predecessors: (RelationEdge | undefined)[],
|