mamut_routing_tools.campaign¶
Designed-diversity benchmark campaigns.
Generating a benchmark is not the same problem as generating an instance. An instance generator answers "make me one of these"; a campaign has to answer "which ones, so that the set as a whole is worth reporting results on".
The pieces, in the order a campaign uses them:
- :mod:
~.city_profilemeasures how far each city's road network departs from the Euclidean plane, and strata the cities by it. - :mod:
~.designenumerates a balanced candidate pool and evaluates each candidate cheaply -- selection and demands only, no distance matrices. - :mod:
~.descriptorsturns coordinates and demands into the features that say what an instance is like, and (after generation) how far its road metrics diverge from its Euclidean one. - :mod:
~.selectkeeps the subset that spans the feature space while honouring the coverage quotas in :mod:~.quotas. - :mod:
~.poi_capacitymeasures the ceiling on POI-only customers per city, which is what decides whether a large real-amenity instance is buildable there at all.
Generation itself stays where it was: generation.single for the artifacts
and family.build_base for the published collection.
CityProfile
dataclass
¶
CityProfile(
city: str,
osm_file: str,
num_vertices: int,
num_edges: int,
detour_mean: float,
detour_p90: float,
rank_tau_eucl_fast: float,
num_pairs: int,
distortion_stratum: str | None = None,
)
One city's road-network distortion, estimated over random node pairs.
InstanceDescriptors
dataclass
¶
InstanceDescriptors(
n: int,
extent_m: float,
clark_evans_r: float,
nnd_cv: float,
radial_dispersion: float,
depot_centrality: float,
depot_eccentricity: float,
demand_cv: float,
demand_gini: float,
demand_max_over_mean: float,
demand_moran_i: float,
lb_cap: int,
route_size: float,
capacity_slack: float,
)
What an instance looks like, from coordinates and demands alone.
MetricDivergence
dataclass
¶
MetricDivergence(
detour_mean: float,
detour_p90: float,
asymmetry_shortest: float,
asymmetry_fastest: float,
rank_tau_eucl_short: float,
rank_tau_eucl_fast: float,
rank_tau_short_fast: float,
)
How far an instance's road metrics depart from its Euclidean one.
CandidateSpec
dataclass
¶
CandidateSpec(
city: str,
osm_path: Path,
n: int,
method: str,
demand_type: int,
avg_route_size: int,
depot_mode: str,
customer_mode: str,
cluster_seeds: int,
cluster_decay_meters: float,
hybrid_poi_share: float,
poi_attach_mode: str = CAMPAIGN_POI_ATTACH_MODE,
categories: tuple[str, ...] = CAMPAIGN_POI_CATEGORIES,
distortion_stratum: str | None = None,
tier: str = "main",
rung_key: str = "",
)
A complete, deterministic recipe for one base instance.
EvaluatedCandidate
dataclass
¶
EvaluatedCandidate(
spec: CandidateSpec,
descriptors: InstanceDescriptors,
resolved_n: int,
effective_method: str = "",
poi_fraction: float = 0.0,
)
A candidate together with what it would look like if generated.
is_usable
¶
Servable at the requested size, a genuine VRP, and fully described.
The size check is not pedantry. A city whose amenities are sparse serves a POI request short -- Nairobi's extract has 27 amenities in total -- and the generator only warns. Accepting the short instance would put a candidate selected for the 701-1000 bucket into the tree at n=198, which is exactly the kind of silent drift a designed benchmark must not have.
Neither is the route-count check. This gate read lb_cap >= 2 in v2 --
"not a TSP" -- and 22 of the 110 published bases cleared it while still
being trivially partitioned, 19 of them into exactly two routes. Two is
not a fleet. The band admissibility rule in :mod:~.ladder is what makes
the guarantee hold by construction; this is the backstop that catches the
one case it cannot, where Q = ceil(r * mean demand) rounds up far
enough to cost a route at the very edge of a band.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/design.py
LadderPlan
dataclass
¶
LadderPlan(
assignments: list[RungAssignment] = list(),
unfilled: list[UnfilledRung] = list(),
method_weights: dict[str, float] = dict(),
headroom: float = DEFAULT_HEADROOM,
min_routes: int = CAMPAIGN_MIN_ROUTES,
)
The complete size/method/city assignment, plus what it could not fill.
band_size_corr
property
¶
corr(log n, band) over the plan -- the decorrelation guard's result.
Reported rather than asserted: admissibility puts a hard floor under it (band 6 cannot appear below n = 300), so the honest thing is to publish what the assignment achieved, not to pretend the axes are independent.
guaranteed_min_routes
property
¶
Fewest routes any assigned rung can produce, over every legal draw.
RungAssignment
dataclass
¶
RungAssignment(
index: int,
n: int,
method: str,
avg_route_size: int,
city: str,
osm_file: str,
distortion_stratum: str | None,
required: int,
available: int,
)
One ladder position, and the city that will supply it.
rung_key
property
¶
Stable identifier for quota'ing exactly one instance per rung.
The route-size band belongs in it. The key is what the candidate-pool cache is fingerprinted on, and a plan that differs only in its bands is a different design producing different demands and capacities -- but would hash identically without this, and silently reuse the other one's pool.
UnfilledRung
dataclass
¶
UnfilledRung(
index: int,
n: int,
method: str,
required: int,
best_available: int,
best_city: str | None,
)
A rung no remaining city could supply, and by how much it fell short.
PoiCapacity
dataclass
¶
PoiCapacity(
city: str,
osm_file: str,
num_vertices: int,
catalog_size: int,
attached_pois: int,
capacity: int,
collapse_ratio: float,
attach_mode: str,
attach_radius_m: float,
categories_digest: str = "",
)
One city's ceiling on POI-only customers.
supports
¶
Quota
dataclass
¶
Quota(
name: str,
key: Callable[[EvaluatedCandidate], Hashable],
minimum: Mapping[Hashable, int] = (),
maximum: Mapping[Hashable, int] | None = None,
per_level_maximum: int | None = None,
)
A coverage requirement over one design axis.
key maps a candidate to the level it occupies (its demand type, its
city, its size bucket). minimum is how many of each level the selection
must end up with; maximum caps a level -- {city: 1} style caps are
written with :meth:per_level_maximum.
QuotaInfeasibleError
¶
Bases: ValueError
The pool cannot satisfy the quotas within the requested budget.
assign_strata
¶
Label each profile low / mid / high by tercile of detour_mean.
Terciles of the observed set, not fixed thresholds: what counts as a high-distortion city is only meaningful relative to the cities on hand, and absolute cut-offs would silently empty a stratum on a different city list.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/city_profile.py
profile_cities
¶
profile_cities(
cities: Iterable[tuple[str, Path]],
*,
num_sources: int = DEFAULT_SOURCES,
num_targets: int = DEFAULT_TARGETS,
seed: int = 0,
jobs: int = 1,
on_progress: Callable[[str], None] | None = None,
) -> list[CityProfile]
Profile every city, then assign the distortion strata across the set.
Each city is an independent OSM parse plus a handful of Dijkstras, so this parallelizes perfectly; a worker holds one city's road graph at a time. The strata are assigned afterwards over the whole set, which is why the results are gathered before anything is labelled.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/city_profile.py
profile_city
¶
profile_city(
osm_path: str | Path,
city: str,
*,
num_sources: int = DEFAULT_SOURCES,
num_targets: int = DEFAULT_TARGETS,
seed: int = 0,
) -> CityProfile
Profile one city from its OSM extract.
Runs num_sources single-source Dijkstras per metric and samples
num_targets reachable targets from each, rather than an all-pairs pass:
a few thousand pairs already pin the mean detour to well within the spread
between cities, and an all-pairs pass on a 200k-vertex graph is not a
"cheap pre-pass" by any reading.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/city_profile.py
compute_descriptors
¶
compute_descriptors(
coordinates: Sequence[Sequence[float]],
demands: Sequence[int],
capacity: int,
) -> InstanceDescriptors
Descriptors of one instance. Index 0 is the depot, in both sequences.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/descriptors.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 | |
compute_divergence
¶
compute_divergence(
euclidean: Sequence[Sequence[float]],
shortest: Sequence[Sequence[float]],
fastest: Sequence[Sequence[float]],
*,
seed: int = 0,
) -> MetricDivergence
How much the road metrics of one instance disagree with its Euclidean one.
seed fixes the pair subsample used for the rank correlations, so the
reported values are reproducible for a given instance.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/descriptors.py
enumerate_candidates
¶
enumerate_candidates(
cities: Sequence[tuple[str, Path]],
profiles: Sequence[CityProfile] = (),
*,
per_city: int = 8,
size_grid: Sequence[int] = MAIN_SIZE_GRID,
methods: Sequence[str] = METHODS,
size_ceiling: Mapping[str, int] | None = None,
tier: str = "main",
base_seed: int = 0,
) -> list[CandidateSpec]
per_city candidates for every city, balanced on every design axis.
methods narrows the sourcing axis. The POI-only tier passes a single
method because "every customer is a real amenity" is the point of that tier,
not one option among three.
size_ceiling caps n per city. Sizes are drawn from a grid that knows
nothing about how many customers a given city can actually serve, so without
it a POI-only tier proposes n = 5000 in a city holding 1800 amenities and
discovers the shortfall only after paying for the evaluation. The cap
replaces an over-large draw with the largest grid size that still fits, and
drops the candidate when even the smallest does not.
The route-size band is drawn as a balanced column and then repaired against
the size, because which bands leave a real fleet depends on n: a target
of 50-200 customers per route is an ordinary instance at n = 4000 and a
two-route degenerate at n = 272. A draw the size cannot support falls back to
the largest admissible band, which keeps the column's ordering -- longer
draws still get longer routes -- without letting it off the end.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/design.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | |
enumerate_for_rungs
¶
enumerate_for_rungs(
assignments: Sequence[Any],
*,
osm_dir: str | Path = "osmdata",
per_rung: int = 6,
base_seed: int = 0,
tier: str = "main",
categories: Sequence[str] = CAMPAIGN_POI_CATEGORIES,
poi_attach_mode: str = CAMPAIGN_POI_ATTACH_MODE,
) -> list[CandidateSpec]
per_rung candidates for each ladder position.
The counterpart of :func:enumerate_candidates for a campaign that has
already decided what each instance is -- city, size and sourcing method
come from a :class:~.ladder.RungAssignment and are not up for negotiation
here. What is still free is what the instance is like: how demand is
distributed, how tightly the vehicles are loaded, where the depot sits and
whether the customers cluster. Those are drawn as balanced columns across
the whole pool exactly as before, so the selector still gets a spread to
choose from on every axis it can still move.
Splitting it this way is what stops the two decisions from fighting. Drawn together, a size the city could not serve was silently downgraded; drawn in order, the size is guaranteed feasible before any descriptor is computed.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/design.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
evaluate_candidate
¶
What this candidate would be, short of its distance matrices.
Rebuilds the customer selection and draws the demands through exactly the
calls generate_single_instance makes, from exactly the same seed, so the
descriptors describe the instance that generation will actually produce.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/design.py
evaluate_candidates
¶
evaluate_candidates(
specs: Sequence[CandidateSpec],
*,
jobs: int = 1,
drop_unusable: bool = True,
on_progress: Callable[[str, int, int], None]
| None = None,
) -> list[EvaluatedCandidate]
Evaluate a whole pool, one process per city.
drop_unusable=False returns everything that evaluated, letting the
caller apply :meth:EvaluatedCandidate.is_usable itself. That is what a
campaign wants for its saved pool: usability depends on the feature space,
and re-deciding it must not cost another pass over every OSM extract.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/design.py
resolve_cities
¶
(city_slug, osm_path) for the extracts on disk, slugged like the tree.
Aix-en-Provence.osm becomes aix_en_provence, matching the place slugs
the benchmark tree and the website already use.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/design.py
stable_seed
¶
A seed derived from what the instance is, never from a counter.
Mirrors family.family._stable_seed: re-running the campaign, or
regenerating one instance on its own, must land on the same draw.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/design.py
assign_rungs
¶
assign_rungs(
ladder: Sequence[int],
capacities: Iterable[PoiCapacity],
profiles: Iterable[CityProfile] = (),
*,
method_weights: Mapping[str, float] | None = None,
hybrid_share: float = 0.5,
headroom: float = DEFAULT_HEADROOM,
vertex_factor: float = DEFAULT_VERTEX_FACTOR,
min_routes: int = CAMPAIGN_MIN_ROUTES,
base_seed: int = 0,
) -> LadderPlan
Match every rung to a city that can demonstrably supply it.
Cheapest sufficient city, hardest rung first. Rungs are processed in decreasing order of what they demand, and each takes the unused city that satisfies it while costing the pool the least amenity capacity.
Amenity capacity is the scarce resource and road vertices are not: every city has plenty of road and only some have plenty of shops. So the cost of spending a city is its POI capacity, whatever the rung happens to need. For a POI or hybrid rung that is ordinary best fit -- the smallest capacity that still clears the bar, which keeps Tokyo available for the rungs that need Tokyo. For a parametric rung, which consumes no amenities at all, it means deliberately spending the amenity-poorest city that has enough road.
That second case is not a refinement, it is the difference between filling the ladder and not. Ranking parametric rungs by road vertices instead makes a city with 563 000 vertices and 57 amenities the last candidate for the only kind of rung it can serve, so it is never used -- and the ladder then runs out of cities while five amenity-dead extracts sit unspent.
Ties among equally sufficient cities break toward the distortion stratum that is currently most under-represented, so the road-network diversity the study depends on survives a matching that is otherwise driven entirely by amenity counts.
One instance per city: a city is removed from the pool once used.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/ladder.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | |
plan_summary
¶
Counts a human can scan to see whether the design came out as asked.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/ladder.py
size_ladder
¶
k distinct sizes rising smoothly from n_min to n_max.
Geometrically spaced, so the ladder spends its resolution where instances differ most: the step from 100 to 110 changes a CVRP far more than the step from 900 to 910. Rounding collides at the bottom, where consecutive rungs are less than a customer apart, so a repair pass walks the ladder and pushes each rung at least one past its predecessor. Both endpoints are exact.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/ladder.py
weighted_column
¶
length labels in the requested proportions, shuffled.
Exact rather than sampled: 50/25/25 of 100 is 50, 25 and 25, not "about that". Remainders from the largest-remainder apportionment go to the heaviest weights first, so a 50 % share is never the one left short.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/ladder.py
cities_supporting
¶
cities_supporting(
capacities: Iterable[PoiCapacity],
n: int,
*,
headroom: float = 1.0,
) -> list[PoiCapacity]
Cities that can serve n POI customers, largest capacity first.
headroom above 1.0 demands slack: at exactly capacity == n + 1 the
"selection" is the whole amenity pool, so the seed stops mattering and the
instance is no longer a draw from anything.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/poi_capacity.py
categories_digest
¶
A short fingerprint of a category list, order-independent.
A capacity is only meaningful for the categories it was measured over, and
curating the list changes every number in the file. Without a fingerprint a
stale poi-capacity.json is indistinguishable from a fresh one, and the
campaign would size instances against amenities it is no longer drawing
from.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/poi_capacity.py
load_capacities
¶
load_capacities(
path: str | Path,
*,
expect_categories: Sequence[str] | None = None,
) -> list[PoiCapacity]
Load a capacity file, refusing one measured over other categories.
expect_categories turns a silent mis-sizing into a loud failure: every
number in the file is relative to a category list, so reading it against a
different one would classify cities by amenities the campaign no longer
draws from.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/poi_capacity.py
measure_capacity
¶
measure_capacity(
osm_path: str | Path,
city: str,
*,
categories: Sequence[str] = POI_CATEGORIES,
attach_mode: str = POI_ATTACH_NEAREST_VERTEX,
attach_radius_m: float = DEFAULT_POI_ATTACH_RADIUS_M,
) -> PoiCapacity
Measure one city, reusing the same caches the generator uses.
Deliberately does not call select_customers_poi: that shuffles the pool
and builds customer lists to arrive at the same attached count. The
ceiling is a property of the attachment map alone, so the map is enough.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/poi_capacity.py
measure_cities
¶
measure_cities(
cities: Iterable[tuple[str, Path]],
*,
categories: Sequence[str] = POI_CATEGORIES,
attach_mode: str = POI_ATTACH_NEAREST_VERTEX,
attach_radius_m: float = DEFAULT_POI_ATTACH_RADIUS_M,
jobs: int = 1,
on_progress: Callable[[str], None] | None = None,
) -> list[PoiCapacity]
Measure every city; one worker holds one extract at a time.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/poi_capacity.py
ladder_tier_quotas
¶
Coverage for a campaign whose sizes and sourcing are already assigned.
Four of the old quotas are gone because the ladder assignment decided them
before selection began: method is fixed per rung, avg_route_size too,
size_bucket is covered by construction (one instance per distinct n),
and city is already one-per-instance. What replaces them is a single pinned quota --
exactly one instance per ladder position -- which is what turns "select 100
good instances" into "select the best instance for each of these 100 slots".
The axes that remain are the ones the assignment left free. They keep their
floors so the set stays analysable on each of them, and demand_type gains
a ceiling as well -- a floor alone does not stop a max-min selector from
parking its spare budget on one level.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/quotas.py
large_tier_quotas
¶
Coverage for the handful of large instances.
Far weaker, and deliberately so: at this size the budget is measured in hours per instance, so the only things worth insisting on are that no city appears twice and that the distortion strata are not all the same.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/quotas.py
main_tier_quotas
¶
Coverage the ~100-instance main tier must satisfy.
One instance per city, all three road-distortion strata represented, and every level of every design axis present in analysable numbers. The fractions are deliberately below an even share: they are a floor that makes the set analysable, not a straitjacket that turns selection back into a grid.
Superseded by :func:ladder_tier_quotas for the ladder campaign; kept for
the grid-drawn tiers, which still draw their sizes and methods at random.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/quotas.py
poi_tier_quotas
¶
Coverage for the POI-only large tier.
Three axes only. The sourcing axis is gone -- every instance in this tier is
poi_categories by construction, which is the tier's whole point -- and
the demand and depot axes are left to the max-min pass, because the pool is
already small: only a handful of cities on Earth hold enough attachable
amenities to serve thousands of customers, and quota'ing every axis over
~10 slots would fix the selection rather than guide it.
What is worth insisting on is that the tier spans its size range. An instance set that is nominally "1000 to 5000" but is really nine instances at 1100 and one at 5000 does not measure what happens as n grows.
Route size gets a ceiling and no floor. A floor here would be one of the
quotas that fixes rather than guides -- seven levels over ten slots -- but a
ceiling costs the selector almost nothing and stops the thing that actually
happened in v2, where five of these ten instances landed in the longest band
and mamut-athens-n4000-poi came out at 154 customers per route. This is
the tier where the longest band is legitimate, so it is capped rather than
excluded.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/quotas.py
diversity_report
¶
diversity_report(
selected: Sequence[EvaluatedCandidate],
pool: Sequence[EvaluatedCandidate],
quotas: Sequence[Quota] = (),
) -> dict[str, Any]
Coverage, spread and confounding of a selected set, as publishable data.
feature_correlation is the part worth reading first. If two descriptors
are strongly correlated across the selected set, the benchmark cannot
separate their effects on a solver, however wide its spread looks.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/select.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | |
select_maxmin
¶
select_maxmin(
candidates: Sequence[EvaluatedCandidate],
k: int,
quotas: Sequence[Quota] = (),
*,
swap_passes: int = 2,
) -> list[EvaluatedCandidate]
k candidates spread over feature space and honouring every quota.
Source code in MAMUT-routing-tools/src/mamut_routing_tools/campaign/select.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |