Lanes: a few curves take the order that crosses least, of all orders

The unit test that failed about one run in six was the lanes property: moving
one curve at a time could stop at a local minimum (two crossings where an
order has none). Up to six curves in a gutter every order is now tried, a
strictly better one replacing the order there, so the bundle still keeps its
order while nothing crosses less. The property now asks for the least and
the counterexample it found is kept as a case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
thalladaandClaude Opus 5.5 committed 2026-11-16 02:02:41 -05:00
1 parent 1e0469477c
commit edc31aac9a
2 files changed
+27 -3

No files matched your search

+22 -2
View File
@@ -8,6 +8,8 @@ export const LANE_PITCH = 4;
export const LANE_FIRST = 5;
export const LANE_LAST = 29;
export const LANES = Math.floor((LANE_LAST - LANE_FIRST) / LANE_PITCH) + 1;
/** Up to this many curves in a gutter, every order is tried (720 at most). */
const EVERY = 6;
/** Up to this many curves in a gutter, every lane is tried for each; past it, only the neighbouring lanes. */
const MOVES = 8;
@@ -48,9 +50,27 @@ export function laneOrder<T extends { key: string; sy: number; ey: number }>(edg
const at = order.findIndex((o) => rule.indexOf(o) > r);
order.splice(at < 0 ? order.length : at, 0, e);
}
// Move one curve to another lane while that removes crossings: a few curves per gutter, every place
// tried; many (past MOVES), only neighbours swapped, which stays cheap enough for every frame.
let best = crossings(order);
// A few curves: the order that crosses least, of all of them. Moving one curve at a time can stop
// short of it (two moves that each add a crossing may together remove two). Only a strictly better
// order replaces the one there, so the bundle keeps its order while no order crosses less.
if (order.length <= EVERY) {
if (!best) return order;
let pick = order.slice();
const go = (left: T[], done: T[]) => {
if (!best) return;
if (!left.length) {
const n = crossings(done);
if (n < best) (best = n), (pick = done.slice());
return;
}
for (let i = 0; i < left.length; i++) go([...left.slice(0, i), ...left.slice(i + 1)], [...done, left[i]]);
};
go(order, []);
return pick;
}
// More: move one curve to another lane while that removes crossings, every place tried for up to
// MOVES curves and only neighbours swapped past that, which stays cheap enough for every frame.
const far = order.length <= MOVES;
for (let pass = 0, better = best > 0; better && pass < order.length; pass++) {
better = false;
+5 -1
View File
@@ -38,9 +38,13 @@ describe("lanes", () => {
const es = ys.map(([a, b], i) => e(String(i), a, b));
const o = laneOrder(es);
const again = laneOrder(es, keys(o));
return o.length === es.length && new Set(keys(o)).size === es.length && keys(again).join() === keys(o).join() && (es.length > 6 || crossings(o) <= least(es) + 1);
return o.length === es.length && new Set(keys(o)).size === es.length && keys(again).join() === keys(o).join() && (es.length > 6 || crossings(o) === least(es));
}),
{ numRuns: 300 },
);
// Found by the property above (it failed about one run in six): moving one curve at a time stopped at 2 crossings where an order has none.
const stuck = [e("0", 3, 1), e("1", 6, 2), e("2", 7, 4), e("3", 2, 0)];
expect(crossings(laneOrder(stuck))).toBe(0);
// A small scroll that changes no crossing keeps the lanes as they were.
const before = laneOrder([e("a", 300, 100), e("b", 335, 400), e("c", 370, 700)]);
const after = laneOrder([e("a", 290, 100), e("b", 325, 400), e("c", 360, 700)], keys(before));