allocate¶
p-median depots, coverage, capacitated assignment and the capacity balance.
quakecascade.allocate
¶
Facility location and capacity allocation on cost matrices.
Kernels ported from the PlanX engine (engine/optimize.py, same author).
Inputs are cost matrices D of shape (sites, demand points) - typically
network travel times, i.e. the transpose of
quakecascade.network.RoadNetwork.travel_time_matrix with demand as
origins - and demand weights w. inf marks an unreachable pair.
- Greedy maximal coverage (Church & ReVelle 1974).
- p-median (Hakimi 1964; ReVelle & Swain 1970) by greedy construction and Teitz & Bart (1968) vertex substitution.
- Capacitated nearest-with-room assignment and capacitated siting.
capacity_balance: where demand overloads the sites it is nearest to, and how much a capacity-respecting allocation can serve.
fixed indices are existing facilities: part of the solution from the
start and not counted against p.
CapacityBalance
dataclass
¶
Result of capacity_balance.
coverage_weights
¶
Weighted demand within radius of each site (site screening score).
greedy_max_coverage
¶
Pick up to p sites maximising covered weighted demand (greedy).
Returns a dict with selected (pick order), gains (marginal covered
weight per pick), covered (bool per demand point), covered_weight,
total_weight and budget_used. Stops early when no site adds
coverage.
p_median
¶
Greedy + Teitz-Bart vertex-substitution p-median heuristic.
Minimises \(\sum_i w_i \min_{j \in S} d_{ji}\) over sets S of p
sites (Hakimi 1964). Greedy construction adds the site that lowers the
objective most; Teitz & Bart (1968) then swaps each chosen site for any
unchosen one while that improves the objective, so the result is never
worse than the greedy start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
D
|
(sites, demand) array
|
Costs; |
required |
w
|
(demand,) array
|
Demand weights. |
required |
p
|
int
|
Number of new sites. |
required |
fixed
|
iterable of int
|
Existing sites, always open. |
()
|
penalty
|
float
|
Cost charged for an unreachable pair. Required when |
None
|
max_iter
|
int
|
Substitution rounds. |
100
|
candidate_costs
|
optional
|
Site costs and a budget the free picks must fit in. |
None
|
budget
|
optional
|
Site costs and a budget the free picks must fit in. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
|
assign_to_nearest
¶
Assign every demand point to its nearest site of solution.
Returns (assign, cost): position in solution (-1 if unreachable)
and the cost to it (inf if unreachable).
exact_location_audit
¶
exact_location_audit(
D,
w,
p,
method="pmedian",
radius=None,
fixed=(),
max_combinations=50000,
candidate_costs=None,
budget=None,
penalty=None,
)
Exact optimum by enumeration when the case is small enough.
Returns audited=False instead of an unsafe combinatorial run when the
number of site sets exceeds max_combinations. penalty follows
p_median.
capacitated_assign
¶
Greedy nearest-with-room allocation of whole demand points.
The cheapest eligible pairs are filled first; a point whose nearest site is full spills to the next one with room for its whole demand; a point that fits nowhere within reach stays uncovered. A fast, explainable heuristic, not a min-cost-flow optimum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
D
|
(F, N) array
|
Cost from each of |
required |
w
|
(N,) array
|
Demand per point; assigned in full to one site. |
required |
cap
|
(F,) array
|
Site capacities in the units of |
required |
max_cost
|
float
|
Catchment limit; dearer pairs are not eligible. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
|
capacitated_siting
¶
Capacitated siting: greedy construction + Teitz-Bart swaps.
Lexicographic objective: serve the most demand under
capacitated_assign, then the least total weighted cost.
Returns:
| Type | Description |
|---|---|
dict
|
|
capacity_from_area
¶
People a site can hold from its usable area.
\(C = \lfloor A / a \rfloor\) with a the area per person. a is
required: published planning values differ by purpose and country
(for example the Sphere Handbook 2018, Shelter and settlement standard 3,
sets a minimum of 3.5 m2 covered living space per person for shelter,
while open assembly areas are planned with far smaller values set by
national guidance), so the caller states and cites the value used.
Returns float capacities; NaN where the area is missing or negative.
capacity_balance
¶
Load on sites when everyone goes to the nearest one, and what fits.
Two readings of the same supply and demand:
- Nearest-site load (no capacity limit): each demand point goes to
its nearest reachable site;
overload_ratio = nearest_demand / capacityshows where people would crowd. - Capacity-respecting allocation: the cheapest demand-site pairs are
filled first up to each site's capacity, so demand spills from full
sites to the next-nearest site with room (within
max_cost).split=Truelets a demand point's people go to several sites (divisible flow);split=Falseusescapacitated_assign(whole points only).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
demand
|
(N,) array
|
People per demand point; NaN = unknown (left out, |
required |
capacity
|
(F,) array
|
People each site holds (e.g. from |
required |
cost
|
(F, N) array
|
Cost from each site to each demand point ( |
required |
max_cost
|
float
|
Farthest acceptable pair for both readings. |
None
|
split
|
bool
|
Divisible (True) or whole-point (False) allocation. |
True
|
Returns:
| Type | Description |
|---|---|
CapacityBalance
|
|