Skip to content

feat(solver): bound Clarke-Wright savings to nearest neighbours - #259

Merged
blackopsrepl merged 2 commits into
mainfrom
feat/bounded-clarke-wright
Nov 13, 2026
Merged

blackopsrepl merged 2 commits into
mainfrom
feat/bounded-clarke-wright

Conversation

@blackopsrepl

Copy link
Copy Markdown
Collaborator

Closes #252 (with #258, which made the distance source pluggable).

Two commits.

1. perf(solver): resolve Clarke-Wright owner restrictions once per route

Clarke-Wright asked the element-owner hook for every element of a route once per candidate owner: in the singleton pass, in the owner-set and has-owner checks, and in completion. That made routes × owners × route length hook calls. Once savings were bounded, this took 87% of a 20k-stop, 500-vehicle construction. Each loop now resolves the restriction once through list_placement::OwnerRestriction (new from_hook_owner, and) and tests each owner against it. Feasibility is still asked in the same order, so every decision is unchanged. Raw owner-hook answers are now interpreted only in list_placement, as AGENTS.md requires.

2. feat(solver): bound Clarke-Wright savings to nearest neighbours

  • Config: savings_neighbors = K on a list_clarke_wright phase. It must be ≥ 1, and other heuristics reject it. A pair is scored only when one element is among the other's K nearest, so the list holds about K entries per element instead of n²/2. When the setting is absent, savings, their order and the merges are unchanged.
  • Hook: new optional savings_neighbors_fn, fn(&S, owner, values, k, &mut Vec<usize>), on ListVariableMetadata, ListVariableSlot and the savings_neighbors_fn macro attribute. For a bounded phase on a slot without the hook, the compiler returns MissingCapability { ListSavingsNeighbors }. Host-language slots do not declare it.
  • Ordering: each pair is scored once, left before right in unassigned order, so the bounded savings are an ordered subset of the full list. Any K ≥ n − 1 reproduces unbounded construction exactly.
  • CVRP profile: domain = "cvrp" supplies cvrp::savings_neighbors, which calls the vehicle's RouteMetric::nearest.
    • The provided method ranks every pair: quadratic time, linear memory. DenseMatrix uses it.
    • EuclideanMetric answers from a uniform grid, visiting rings of cells until no closer stop can remain. Its result is exactly the provided method's (same rounded-distance-then-index key).
  • Slot size: the compiled list slot stays 400 bytes. The route bindings now read the metric-class hook from the slot instead of holding a copy, which offsets the new field. This avoids the heap-layout shift that failed perf(solver): keep Clarke-Wright owner admission incremental #257's first gate run.
  • Docs: config, solver, macros, cvrp and facade wireframes; docs/configuration.md (new "Bounded Clarke-Wright savings" section), docs/modeling.md, docs/extend-domain.md; public-api.txt.

Verification

  • bounded_savings tests:

    • On 40 seeded instances, K = n − 1 and K = n + 3 give exactly the unbounded routes.
    • For K ∈ {1, 2, 4}, the scored pairs are exactly the neighbour pairs, once per metric class and oriented left to right.
    • A hook returning the wrong row length panics with a clear message.
    • Two planted bugs (dropping the dedup; scoring right-to-left) each fail a test.
  • Grid tests: grid neighbours equal the ranked scan on spread, clustered, duplicate, collinear and fine-lattice coordinates. A planted early stop of the ring search fails the fine-lattice test.

  • Other tests:

    • Compiler: the capability error, and the invalid-value errors for 0 or a non-Clarke-Wright heuristic.
    • Macros: the profile and attribute emission.
    • Facade integration test: through solver.toml and the cvrp profile, K ≥ n − 1 equals unbounded, and K = 3 assigns everyone.
  • Default config is unchanged: construction-only Clarke-Wright on all 100 X instances gives identical routes (2.84 s → 2.48 s total), and synthetic instances at 1k–8k stops give identical routes.

  • Scale, synthetic stock-profile instances on EuclideanMetric:

    Stops Unbounded K = 40
    10k 13.2 s, 1.58 GB 0.30 s, 25 MB
    20k 56.6 s, 6.29 GB 1.06 s, 52 MB
    50k — 6.0 s, 164 MB
    100k — 22.8 s, 389 MB

    Scores are equal at 20k and within 0.02% at 10k.

  • Quality on the 100 X instances (DenseMatrix), K = 40 vs unbounded:

    • same score on 90 instances;
    • better hard score on 2;
    • soft score −0.10% on average (median 0, range −6.0%..+0.4%);
    • total time 2.54 s → 0.53 s.
  • make pre-release and make examples pass.

  • Full qualifying make bench-zero-regression against main (73d1abac): posted below when it completes.

Clarke-Wright asked the element-owner hook for every element of a route
once per candidate owner: in the singleton pass, in owner-set and
has-owner checks, and in completion. That is routes x owners x route
length hook calls, which dominated the phase once savings stopped doing:
87% of a 20k-stop, 500-vehicle construction went to owner restriction
checks.

Each loop now resolves the route's (or element's) restriction once through
list_placement's OwnerRestriction, which gains from_hook_owner and and, and
tests each owner against it. The feasibility hook is still asked in the
same order, so every decision is unchanged. Raw owner-hook answers are now
interpreted only in list_placement, as for the other list phases.

Construction-only runs of all 100 X instances give identical routes
(2.84 s -> 2.48 s in total), as do synthetic stock-profile instances at
1k-8k stops.
list_clarke_wright scores every pair of unassigned elements, so its savings
list holds 32 bytes per pair: 1.6 GB at 10k stops, 40 GB at 50k. With the
pluggable route metric that was the last structure quadratic in the stops.

`savings_neighbors = K` on a list_clarke_wright phase scores a pair only
when one element is among the other's K nearest, about K entries per
element. The neighbours come from a new optional list hook,
savings_neighbors_fn (ListVariableMetadata, ListVariableSlot and the
savings_neighbors_fn macro attribute); the compiler rejects a bounded phase
on a slot without one. Each pair is scored once, left before right in the
unassigned order, so the bounded savings are an ordered subset of the full
list and a bound of at least n - 1 reproduces unbounded construction.
Without the setting nothing changes.

The domain = "cvrp" profile supplies cvrp::savings_neighbors, which asks
the vehicle's RouteMetric::nearest. Its provided method ranks every pair
(quadratic time, linear memory), which DenseMatrix uses; EuclideanMetric
answers from a uniform grid, visiting cell rings until no closer stop can
remain, with exactly the provided method's result. The compiled list slot
keeps its size: the route bindings read the metric-class hook from the slot
instead of holding a copy, and the setting is an Option<NonZeroU32>, which
fits the construction config's existing padding: a larger config shifted the
solver's heap layout and failed the cache-miss gate on unrelated cases.

Synthetic stock-profile instances on EuclideanMetric, K = 40:

  stops    unbounded             K = 40
  10k      13.2 s, 1.58 GB       0.30 s, 25 MB
  20k      56.6 s, 6.29 GB       1.06 s, 52 MB
  50k      -                     6.0 s, 164 MB
  100k     -                     22.8 s, 389 MB

Scores are equal at 20k and within 0.02% at 10k. On the 100 X instances
K = 40 keeps the score of 90, improves the hard score of 2 and changes the
soft score by -0.10% on average (median 0), in a fifth of the time.

Closes #252
@blackopsrepl
blackopsrepl force-pushed the feat/bounded-clarke-wright branch from 06fe6a7 to b7e14ad Compare November 13, 2026 06:43
@blackopsrepl

Copy link
Copy Markdown
Collaborator Author

Qualifying make bench-zero-regression, candidate b7e14ad0 vs main 73d1abac: passed. 14 cases, 5 symbol-hash layouts, 51 trials, semantic_match=true on every case.

Every case within budget: wall −0.85%..+0.48%, instructions −0.00%..+0.04%, allocations identical, simulated L1 misses −0.05%..+3.34%, LL misses −0.24%..+0.39%.

A first run on 06fe6a74 failed simulated cache misses on scalar_change (L1 +10.6%, LL +7.6%), list_swap (LL +9.6%) and nearby_swap (L1 +5.1%) with identical semantics. Cause: savings_neighbors: Option<usize> grew ConstructionHeuristicConfig by 16 bytes and shifted the heap layout (a dummy 16-byte field on unchanged main reproduced it exactly). The amended commit makes it Option<NonZeroU32>, which fits the config's existing padding; 0 now fails to parse.

@blackopsrepl
blackopsrepl merged commit f3fd5b2 into main Nov 13, 2026
7 checks passed
@blackopsrepl
blackopsrepl deleted the feat/bounded-clarke-wright branch November 13, 2026 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make the stock CVRP profile able to scale linearly in memory, with no regression

1 participant