feat(route-editor): "Optimise order" action (2-opt/Or-opt, one undo step) #53

Closed
maximus wants to merge 0 commits from feat/45-optimise-order into deploy
Member

Closes #45.

What

An Optimise order button (shuffle icon, next to Reverse direction in the route editor). It reorders the anchors between the fixed start and end to shorten the route.

  • Solver (web/src/lib/util/route_order.ts, pure TS): works on haversine distances, and the first and last anchor never move. Up to 10 movable points it solves exactly (Held-Karp, well under 1 ms). Local search alone can stall in a local optimum, and the exact solve is cheap at that size. Above 10 it runs 2-opt + Or-opt (chains of up to 3, either way round) from the current order and from a nearest-neighbour order, keeping the shorter result. Starting from the current order means the result is never longer than the input.
  • Loops: a route whose last anchor is within 200 m of the first counts as a loop. The closing anchor stays last, and the loop keeps its direction (clockwise or counter-clockwise) unless only the mirror image is shorter. The issue lists loop direction as user intent.
  • Routing: one routing pass for the result. Legs that keep their direction are reused, and only the new legs are routed, four at a time. A single multi-waypoint call can't be split back into the editor's per-leg segments, because RouteResult has no leg boundaries and segments carry their own track/off-road mode. A new leg keeps the mode of the old leg between the same two anchors, else it gets the sticky default mode.
  • Apply only if shorter: if the solver finds nothing shorter, a toast says so and no routing happens. Otherwise the routed length is compared with the current route. If it's shorter, the reorder is applied and a toast shows before → after; if not, nothing changes and the toast shows both lengths.
  • Exactly one undo entry: applyReorderedRoute records the anchor order and the route geometry as one entry, so undo and redo restore both together.
  • The button is enabled from 4 anchors (2 movable points). New strings are in en.json only.

Not done

  • The optional "precise" mode (trail distances via about n² cached routing calls for ≤ 15 points). It isn't trivial: it needs a distance cache and progress UI, and the solver would also have to accept asymmetric distances (uphill ≠ downhill). The solver already takes a pluggable distance, so it can be added later.
  • POI distance_from_start values aren't recomputed after a reorder. This matches how moving an anchor in the list behaves today.

Tests

  • route_order.test.ts: brute-force optimality on 300 random cases (4–9 points) and 200 random loops; start and end stay fixed; loop direction is kept both ways; nothing is applied when the order is already optimal or only ties; the local search alone (forced) handles a zig-zag, a crossing and a misplaced point, reaching ≥ 90 % optimal and ≤ 15 % off on random small cases; results are never longer on 20–80 point cases; isLoop.
  • valhalla_reorder.test.ts: only new legs are routed; same-direction legs are reused; modes carry over; invalid orders are rejected; exactly one undo entry, with undo and redo restoring anchors and geometry.
  • Local results: npm run check gives 0 errors (2 warnings on an existing <tbody> I didn't touch), and npm run test:unit -- --run gives 860 passed, 13 skipped.

Not yet tried in the browser.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XMf4EGfBC9xbQ6rjD6HxgP

Closes #45. ## What An **Optimise order** button (shuffle icon, next to *Reverse direction* in the route editor). It reorders the anchors between the fixed start and end to shorten the route. - **Solver** (`web/src/lib/util/route_order.ts`, pure TS): works on haversine distances, and the first and last anchor never move. Up to 10 movable points it solves exactly (Held-Karp, well under 1 ms). Local search alone can stall in a local optimum, and the exact solve is cheap at that size. Above 10 it runs **2-opt + Or-opt** (chains of up to 3, either way round) from the current order and from a nearest-neighbour order, keeping the shorter result. Starting from the current order means the result is never longer than the input. - **Loops**: a route whose last anchor is within 200 m of the first counts as a loop. The closing anchor stays last, and the loop keeps its direction (clockwise or counter-clockwise) unless only the mirror image is shorter. The issue lists loop direction as user intent. - **Routing**: one routing pass for the result. Legs that keep their direction are reused, and only the new legs are routed, four at a time. A single multi-waypoint call can't be split back into the editor's per-leg segments, because `RouteResult` has no leg boundaries and segments carry their own track/off-road mode. A new leg keeps the mode of the old leg between the same two anchors, else it gets the sticky default mode. - **Apply only if shorter**: if the solver finds nothing shorter, a toast says so and no routing happens. Otherwise the routed length is compared with the current route. If it's shorter, the reorder is applied and a toast shows `before → after`; if not, nothing changes and the toast shows both lengths. - **Exactly one undo entry**: `applyReorderedRoute` records the anchor order and the route geometry as one entry, so undo and redo restore both together. - The button is enabled from 4 anchors (2 movable points). New strings are in `en.json` only. ## Not done - The optional "precise" mode (trail distances via about n² cached routing calls for ≤ 15 points). It isn't trivial: it needs a distance cache and progress UI, and the solver would also have to accept asymmetric distances (uphill ≠ downhill). The solver already takes a pluggable `distance`, so it can be added later. - POI `distance_from_start` values aren't recomputed after a reorder. This matches how moving an anchor in the list behaves today. ## Tests - `route_order.test.ts`: brute-force optimality on 300 random cases (4–9 points) and 200 random loops; start and end stay fixed; loop direction is kept both ways; nothing is applied when the order is already optimal or only ties; the local search alone (forced) handles a zig-zag, a crossing and a misplaced point, reaching ≥ 90 % optimal and ≤ 15 % off on random small cases; results are never longer on 20–80 point cases; `isLoop`. - `valhalla_reorder.test.ts`: only new legs are routed; same-direction legs are reused; modes carry over; invalid orders are rejected; exactly one undo entry, with undo and redo restoring anchors and geometry. - Local results: `npm run check` gives 0 errors (2 warnings on an existing `<tbody>` I didn't touch), and `npm run test:unit -- --run` gives 860 passed, 13 skipped. Not yet tried in the browser. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01XMf4EGfBC9xbQ6rjD6HxgP
Pure module for the "Optimise order" action (#45). Up to 10 movable
points it solves exactly (Held-Karp); above that it runs 2-opt + Or-opt
from the current and a nearest-neighbour order. Distances are haversine.
The first and last point never move, a loop keeps its direction, and an
order that is not strictly shorter is not returned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XMf4EGfBC9xbQ6rjD6HxgP
feat(route-editor): add an "Optimise order" action with one undo step
All checks were successful
CI / go (push) Successful in 14m3s
CI / web (push) Successful in 1m4s
1efcc3b4b4
A shuffle button next to "Reverse direction" reorders the anchors between
the fixed start and end (#45). The order comes from the straight-line
solver; legs that keep their direction are reused, and only the new legs
are routed, four at a time. The result is applied only if the routed
route is shorter, as exactly one undo entry. A toast shows the length
before and after, or says why nothing changed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XMf4EGfBC9xbQ6rjD6HxgP
Author
Member

Shipped via batch PR #64 (deploy 0d0d85a4), which merged this branch head unchanged; live since 2026-11-05 ~19:30 CET in wanderer-web:deploy-676a7c62 (homelab-flux #843). Forgejo does not mark stacked PRs as merged, so I am closing this by hand.

🤖 Generated with Claude Code

Shipped via batch PR #64 (`deploy` 0d0d85a4), which merged this branch head unchanged; live since 2026-11-05 ~19:30 CET in `wanderer-web:deploy-676a7c62` (homelab-flux #843). Forgejo does not mark stacked PRs as merged, so I am closing this by hand. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
maximus closed this pull request 2026-11-05 19:10:43 +00:00
All checks were successful
CI / go (push) Successful in 14m3s
Required
Details
CI / web (push) Successful in 1m4s
Required
Details

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
public/wanderer!53
No description provided.