Route with A* over numeric cell keys, so a route can reach past the Sun's crowd
The route search widened evenly from the departure, Dijkstra-style, with a budget of 20 000 stars. On the Gaia catalogue those are all within about 40 pc of the Sun, so it found no route to anything farther at any range: Sol to Mirfak (155 pc) failed at 3, 8, 15 and 30 pc alike. Every failure then asked minimumRangeBetween what range would work. That search widened the same way with a 30 pc ceiling, and it ran for up to a minute on the main thread before giving up with nothing. routeBetween is now an A* search. Each star is queued by the distance travelled to it plus the straight line on to the destination, on a binary heap rather than a linear scan of the frontier. It heads for the destination instead of flooding the core around the departure. minimumRangeBetween bisects the range, one routeBetween per step, because whether a chain exists can only become truer as the range grows. Its answer is always the longest hop of a route actually found, so a range it names always opens one. Its ceiling is now the Routes panel's own maximum, MAX_JUMP_RANGE_PC: a range the control cannot be set to is no answer, and raiseTo already clamped any figure above it. The spatial index keys its cells by one number packed from their three indices instead of an "ix,iy,iz" string. A search visits up to 125 cells for every star it expands, and building those strings was half of what a route cost. forEachWithin hands neighbours over unsorted and uncollected, which was most of the other half; within is now that, gathered and sorted. The no-route line said nothing in the catalogue bridged the gap; it now says no chain of jumps up to the panel's maximum reaches the star, which is what was searched. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016jxMkwA2rbicdGxHosecYi
This commit is contained in:
@@ -37,30 +37,72 @@ export interface JumpLink {
|
||||
}
|
||||
|
||||
/**
|
||||
* A cap on how much of the catalogue one search may walk. Reached only where a route does not
|
||||
* exist and the range is wide enough to make most of the catalogue one component; a search that
|
||||
* hits it has already visited more stars than any real chain passes through.
|
||||
* A cap on how much of the catalogue one search may walk. A search that hits it has already
|
||||
* visited more stars than any real chain passes through: the longest measured, Sol to HD 2626 at
|
||||
* 236 pc in jumps of 8 pc, settles about 7 000.
|
||||
*/
|
||||
const MAX_VISITED = 20000;
|
||||
|
||||
/** Pops the smallest-cost entry. A linear scan: the frontier is small next to the work per node. */
|
||||
function takeCheapest<T>(frontier: Map<number, T>, costOf: (value: T) => number): [number, T] | undefined {
|
||||
let bestId: number | undefined;
|
||||
let bestValue: T | undefined;
|
||||
let bestCost = Number.POSITIVE_INFINITY;
|
||||
for (const [id, value] of frontier) {
|
||||
const cost = costOf(value);
|
||||
if (cost < bestCost) {
|
||||
bestCost = cost;
|
||||
bestId = id;
|
||||
bestValue = value;
|
||||
/**
|
||||
* How close to the true minimum `minimumRangeBetween` works a range out: half the Routes panel's
|
||||
* own step, which it rounds up to. Never at the cost of an answer that fails to open a route,
|
||||
* since the figure it reports is always the longest hop of a route actually found.
|
||||
*/
|
||||
const RANGE_RESOLUTION_PC = 0.05;
|
||||
|
||||
/** A binary min-heap of star ids by priority. Duplicates are allowed; stale ones are skipped on the way out. */
|
||||
class Frontier {
|
||||
private readonly ids: number[] = [];
|
||||
private readonly priorities: number[] = [];
|
||||
|
||||
get size(): number {
|
||||
return this.ids.length;
|
||||
}
|
||||
|
||||
push(id: number, priority: number): void {
|
||||
let at = this.ids.length;
|
||||
this.ids.push(id);
|
||||
this.priorities.push(priority);
|
||||
while (at > 0) {
|
||||
const parent = (at - 1) >> 1;
|
||||
if (this.priorities[parent] <= priority) {
|
||||
break;
|
||||
}
|
||||
this.ids[at] = this.ids[parent];
|
||||
this.priorities[at] = this.priorities[parent];
|
||||
at = parent;
|
||||
}
|
||||
this.ids[at] = id;
|
||||
this.priorities[at] = priority;
|
||||
}
|
||||
if (bestId === undefined || bestValue === undefined) {
|
||||
return undefined;
|
||||
|
||||
/** The id with the lowest priority, taken out. Only called while `size` is not zero. */
|
||||
pop(): number {
|
||||
const top = this.ids[0];
|
||||
const lastId = this.ids.pop()!;
|
||||
const lastPriority = this.priorities.pop()!;
|
||||
const count = this.ids.length;
|
||||
if (count > 0) {
|
||||
let at = 0;
|
||||
for (;;) {
|
||||
const left = 2 * at + 1;
|
||||
if (left >= count) {
|
||||
break;
|
||||
}
|
||||
const right = left + 1;
|
||||
const child = right < count && this.priorities[right] < this.priorities[left] ? right : left;
|
||||
if (this.priorities[child] >= lastPriority) {
|
||||
break;
|
||||
}
|
||||
this.ids[at] = this.ids[child];
|
||||
this.priorities[at] = this.priorities[child];
|
||||
at = child;
|
||||
}
|
||||
this.ids[at] = lastId;
|
||||
this.priorities[at] = lastPriority;
|
||||
}
|
||||
return top;
|
||||
}
|
||||
frontier.delete(bestId);
|
||||
return [bestId, bestValue];
|
||||
}
|
||||
|
||||
function rebuild(cameFrom: Map<number, number>, fromId: number, toId: number): number[] {
|
||||
@@ -85,108 +127,97 @@ function rebuild(cameFrom: Map<number, number>, fromId: number, toId: number): n
|
||||
* length are not equally good, and the one that covers less ground is the one a reader means by
|
||||
* "the way there". Neighbours are asked for as the search reaches each star rather than built
|
||||
* into a graph first, so finding one route never costs a pass over the whole catalogue.
|
||||
*
|
||||
* An A* search: each star waits its turn by the distance travelled to it plus the straight line
|
||||
* on to the destination, which no chain can beat, so the search heads for the destination rather
|
||||
* than widening evenly in every direction. Widening evenly is what the Gaia catalogue broke. From
|
||||
* the Sun it spent its whole budget on the 20 000 stars nearest, all inside about 40 pc, and so
|
||||
* found no route to anything farther at any range; Mirfak, 155 pc out, is 27 jumps at 8 pc.
|
||||
*/
|
||||
export function routeBetween(index: StarNeighbourhood, fromId: number, toId: number, rangePc: number): Route | null {
|
||||
if (fromId === toId || rangePc <= 0 || !index.point(fromId) || !index.point(toId)) {
|
||||
const origin = index.point(fromId);
|
||||
const destination = index.point(toId);
|
||||
if (fromId === toId || rangePc <= 0 || !origin || !destination) {
|
||||
return null;
|
||||
}
|
||||
const straightLineOn = (x: number, y: number, z: number) => Math.hypot(destination.x - x, destination.y - y, destination.z - z);
|
||||
|
||||
const best = new Map<number, number>([[fromId, 0]]);
|
||||
const travelled = new Map<number, number>([[fromId, 0]]);
|
||||
const cameFrom = new Map<number, number>();
|
||||
// Each hop's length as the range test measured it. The route's longest hop is read from these
|
||||
// rather than measured again, so a range set to it is sure to admit the route a second time,
|
||||
// which is what `minimumRangeBetween` relies on.
|
||||
const hopTo = new Map<number, number>();
|
||||
const settled = new Set<number>();
|
||||
const frontier = new Map<number, number>([[fromId, 0]]);
|
||||
const frontier = new Frontier();
|
||||
frontier.push(fromId, straightLineOn(origin.x, origin.y, origin.z));
|
||||
|
||||
while (frontier.size > 0 && settled.size < MAX_VISITED) {
|
||||
const cheapest = takeCheapest(frontier, (cost) => cost);
|
||||
if (!cheapest) {
|
||||
break;
|
||||
}
|
||||
const [starId, costHere] = cheapest;
|
||||
const starId = frontier.pop();
|
||||
if (settled.has(starId)) {
|
||||
continue;
|
||||
}
|
||||
settled.add(starId);
|
||||
const costHere = travelled.get(starId)!;
|
||||
|
||||
if (starId === toId) {
|
||||
const stars = rebuild(cameFrom, fromId, toId);
|
||||
return stars.length === 0 ? null : { stars, totalPc: costHere, longestHopPc: longestHop(index, stars) };
|
||||
if (stars.length === 0) {
|
||||
return null;
|
||||
}
|
||||
let longestHopPc = 0;
|
||||
for (let i = 1; i < stars.length; i++) {
|
||||
longestHopPc = Math.max(longestHopPc, hopTo.get(stars[i])!);
|
||||
}
|
||||
return { stars, totalPc: costHere, longestHopPc };
|
||||
}
|
||||
|
||||
for (const neighbour of index.within(starId, rangePc)) {
|
||||
index.forEachWithin(starId, rangePc, (neighbour, distancePc) => {
|
||||
if (settled.has(neighbour.id)) {
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
const cost = costHere + neighbour.distancePc;
|
||||
if (cost < (best.get(neighbour.id) ?? Number.POSITIVE_INFINITY)) {
|
||||
best.set(neighbour.id, cost);
|
||||
const cost = costHere + distancePc;
|
||||
if (cost < (travelled.get(neighbour.id) ?? Number.POSITIVE_INFINITY)) {
|
||||
travelled.set(neighbour.id, cost);
|
||||
cameFrom.set(neighbour.id, starId);
|
||||
frontier.set(neighbour.id, cost);
|
||||
hopTo.set(neighbour.id, distancePc);
|
||||
frontier.push(neighbour.id, cost + straightLineOn(neighbour.x, neighbour.y, neighbour.z));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function longestHop(index: StarNeighbourhood, stars: readonly number[]): number {
|
||||
let longest = 0;
|
||||
for (let i = 1; i < stars.length; i++) {
|
||||
const a = index.point(stars[i - 1]);
|
||||
const b = index.point(stars[i]);
|
||||
if (a && b) {
|
||||
longest = Math.max(longest, Math.hypot(b.x - a.x, b.y - a.y, b.z - a.z));
|
||||
}
|
||||
}
|
||||
return longest;
|
||||
}
|
||||
|
||||
/**
|
||||
* The shortest range at which any chain at all exists between two stars, or `null` if none does
|
||||
* within `ceilingPc`.
|
||||
* The shortest range at which any chain at all exists between two stars, to within
|
||||
* `RANGE_RESOLUTION_PC`, or `null` if none does within `ceilingPc`.
|
||||
*
|
||||
* This is what turns "no route" from a dead end into an answer: the range control can be told
|
||||
* what it would have to be raised to. It is the minimax path — the chain whose longest hop is as
|
||||
* short as possible — found by the same search as above, with the cost of reaching a star being
|
||||
* the longest hop taken to get there rather than the sum of them.
|
||||
* what it would have to be raised to. The exact figure is the minimax path, the chain whose
|
||||
* longest hop is as short as possible. It used to be searched for directly, widening from the
|
||||
* departure in order of the worst hop needed, which from the Sun meant exhausting the whole dense
|
||||
* core before anything farther could be reached: it gave up with nothing after up to a minute.
|
||||
* Whether a chain exists can only become truer as the range grows, so the range is bisected
|
||||
* instead, each step one directed `routeBetween`.
|
||||
*/
|
||||
export function minimumRangeBetween(index: StarNeighbourhood, fromId: number, toId: number, ceilingPc: number): number | null {
|
||||
if (fromId === toId || ceilingPc <= 0 || !index.point(fromId) || !index.point(toId)) {
|
||||
const widest = routeBetween(index, fromId, toId, ceilingPc);
|
||||
if (!widest) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const best = new Map<number, number>([[fromId, 0]]);
|
||||
const settled = new Set<number>();
|
||||
const frontier = new Map<number, number>([[fromId, 0]]);
|
||||
|
||||
while (frontier.size > 0 && settled.size < MAX_VISITED) {
|
||||
const cheapest = takeCheapest(frontier, (cost) => cost);
|
||||
if (!cheapest) {
|
||||
break;
|
||||
}
|
||||
const [starId, worstHopHere] = cheapest;
|
||||
if (settled.has(starId)) {
|
||||
continue;
|
||||
}
|
||||
settled.add(starId);
|
||||
|
||||
if (starId === toId) {
|
||||
return worstHopHere;
|
||||
}
|
||||
|
||||
for (const neighbour of index.within(starId, ceilingPc)) {
|
||||
if (settled.has(neighbour.id)) {
|
||||
continue;
|
||||
}
|
||||
// What this chain would need: the longest hop on it, not the distance covered by it.
|
||||
const needed = Math.max(worstHopHere, neighbour.distancePc);
|
||||
if (needed < (best.get(neighbour.id) ?? Number.POSITIVE_INFINITY)) {
|
||||
best.set(neighbour.id, needed);
|
||||
frontier.set(neighbour.id, needed);
|
||||
}
|
||||
let unreachable = 0;
|
||||
let reachable = widest.longestHopPc;
|
||||
while (reachable - unreachable > RANGE_RESOLUTION_PC) {
|
||||
const range = (unreachable + reachable) / 2;
|
||||
const route = routeBetween(index, fromId, toId, range);
|
||||
if (route) {
|
||||
reachable = route.longestHopPc;
|
||||
} else {
|
||||
unreachable = range;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return reachable;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user