Skip to content

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

CapacityBalance(sites, demand, summary=dict())

Result of capacity_balance.

coverage_weights

coverage_weights(D, w, radius)

Weighted demand within radius of each site (site screening score).

greedy_max_coverage

greedy_max_coverage(
    D,
    w,
    p,
    radius,
    fixed=(),
    candidate_costs=None,
    budget=None,
)

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

p_median(
    D,
    w,
    p,
    fixed=(),
    penalty=None,
    max_iter=100,
    candidate_costs=None,
    budget=None,
)

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; inf = unreachable.

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 D holds inf (no published default; PlanX used 1.5 x the largest finite cost).

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

selected (free picks), objective (weighted cost of the whole solution incl. fixed), swaps, penalty, budget_used, greedy_objective.

assign_to_nearest

assign_to_nearest(D, solution)

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

capacitated_assign(D, w, cap, max_cost=None)

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 F sites to each of N demand points (inf where unreachable).

required
w (N,) array

Demand per point; assigned in full to one site.

required
cap (F,) array

Site capacities in the units of w.

required
max_cost float

Catchment limit; dearer pairs are not eligible.

None

Returns:

Type Description
dict

assign (site per point, -1 uncovered), cost (inf uncovered), spilled, nearest (ignoring capacity, -1 none), load, remaining.

capacitated_siting

capacitated_siting(
    dist,
    demand_w,
    capacities,
    p,
    existing_idx=(),
    max_cost=None,
    max_iter=100,
)

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

selected, load and utilization per site (inf for a zero-capacity site with load, NaN for a site of unknown capacity, which receives nobody), assign (site per point, -1 uncovered), cost, uncovered, obj_history (list of (served, total_cost)).

capacity_from_area

capacity_from_area(area_m2, m2_per_person=None)

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

capacity_balance(
    demand, capacity, cost, max_cost=None, split=True
)

Load on sites when everyone goes to the nearest one, and what fits.

Two readings of the same supply and demand:

  1. Nearest-site load (no capacity limit): each demand point goes to its nearest reachable site; overload_ratio = nearest_demand / capacity shows where people would crowd.
  2. 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=True lets a demand point's people go to several sites (divisible flow); split=False uses capacitated_assign (whole points only).

Parameters:

Name Type Description Default
demand (N,) array

People per demand point; NaN = unknown (left out, no_demand).

required
capacity (F,) array

People each site holds (e.g. from capacity_from_area); NaN = unknown: the site receives nobody (no_capacity), it is never given an assumed capacity.

required
cost (F, N) array

Cost from each site to each demand point (inf unreachable). An unknown cost (NaN) is accepted only where it cannot change the balance silently: for a demand point with NaN demand, or for a site with NaN capacity, where it counts as possibly the nearest site (the demand is then partial or no_capacity). NaN elsewhere raises.

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

sites: capacity, nearest_demand, overload_ratio (inf when capacity is 0 and demand > 0), served (NaN for a site of unknown capacity), utilisation, site_status (partial when the site also receives demand whose nearest site has unknown capacity, so its load may be overstated). demand: demand, nearest_site, nearest_cost, served, unserved, demand_status: computed; partial for demand diverted from a nearer site of unknown capacity; no_capacity (NaN served and unserved) when every reachable site has unknown capacity; no_demand. summary: totals and counts, with total_demand = served + unserved + unknown_capacity_demand.