Skip to main content

Google OR-Tools in Vaazhvu

Vaazhvu uses Google OR-Tools (ortools>=9.11, a core dependency in pyproject.toml; 9.15 installed) for its optimisation problems. Every model is an integer program solved with CP-SAT, OR-Tools' constraint-programming / SAT-based integer solver: a C++ engine called through the Python bindings (from ortools.sat.python import cp_model). Nothing uses the LP solvers (GLOP, pywraplp, MathOpt): every decision here is discrete (which unit, which base), so CP-SAT is the right tool.

There are three models. Each has a fallback, so the app still runs without OR-Tools (the CLAUDE.md rule that integrations are optional):

ModelCodeWhen it runsDecidesTime limitFallback
Joint dispatch (live)dispatch/joint.py solve_cpsatTwo or more calls reach dispatch inside the batch windowWhich response bundle each call gets1 s, 4 workersGreedy by P(critical)
MEXCLP fleet plan (offline)ops/fleet.py solve_mexclpmake fleet-plan, or at startup when data/fleet_plan.json is missingWhere each ambulance and police unit is based10 s per solve, 8 workers, seed 7None: the plan is cached in fleet_plan.json
Batch assignment (legacy)optimizer/cpsat.py solve_assignmentLegacy scenario path only (VAAZHVU_LEGACY=1)Which unit each incident gets1 sGreedy in dispatch/batch.py

To check it is active: GET /api/integrations reports ortools: available, the plan file says "solver": "MEXCLP / OR-Tools CP-SAT", and live joint rounds emit dispatch.joint with "engine": "ortools-cpsat".


1. Joint dispatch: dispatch/joint.py​

Why. The risk engine (dispatch/risk.py) scores every feasible bundle (a set of units: an ambulance plus optional ALS backup, police, fire) for one incident. If incidents are decided one at a time, the first call can take the unit that the second, sicker call needed more. When several calls reach dispatch within config/dispatch.yaml batch.window_sec (1.0 s), OpsController.dispatch_batch (ops/controller.py) solves them together.

Model.

min Σ_i Σ_k J_ik · y_ik + Σ_i P_i · u_i
s.t. Σ_k y_ik + u_i = 1 every incident gets exactly one bundle, or is left unassigned
Σ_{(i,k) : unit ∈ bundle_ik} y_ik ≤ 1 no unit is in two chosen bundles
y_ik = 0 for infeasible bundles (not created at all)
y_ik, u_i ∈ {0, 1}
  • J_ik: the risk engine's expected-adverse-outcome objective for bundle k on incident i. It is the same per-pair score the Go scorer (services/dispatch-scorer/risk.go) cross-checks, so the joint solve does not change how a single pair is scored.
  • P_i (unassigned_penalty): the severity-weighted hazard of incident i waiting for the next free unit (batch.unassigned_wait_sec, default coverage.no_unit_sec = 1800 s). A call is left without a unit only when the fleet really cannot cover everyone.
  • CP-SAT needs integer coefficients, so J and P are multiplied by SCALE = 1_000_000 and rounded.
  • Constraints use AddExactlyOne (each incident) and AddAtMostOne (each unit shared by more than one candidate bundle).

Limits and fallback. solver_time_limit_s: 1.0 (config/dispatch.yaml batch), num_workers = 4. If OR-Tools is missing, or the solver returns neither OPTIMAL nor FEASIBLE, solve() falls back to solve_greedy: the call with the highest P(critical) goes first and takes its cheapest bundle whose units are still free, and nothing is assigned when J > P.

Output. dispatch.joint event and audit row: engine (ortools-cpsat | greedy), the incidents, the joint objective, and sequential_greedy_objective (what one-at-a-time would have cost), so the gain is visible in the log. Each affected decision gets a JOINT (<engine>, <n> calls) line in why_selected.

Tests. tests/test_joint_dispatch.py: joint beats sequential greedy, no unit in two bundles, unassigned when short, greedy fallback without OR-Tools, calls arriving together are dispatched in one round, a window of 0 keeps one-at-a-time.


2. MEXCLP fleet plan: ops/fleet.py​

Why. Base locations are solved, not chosen by hand. The model is MEXCLP (Daskin 1983, maximum expected covering location): it maximises demand covered within the response target, counting a second and third covering unit because the first may be busy.

Model.

max Σ_i w_i · Σ_{k=1..K} (1 − q) · q^(k−1) · y_ik
s.t. Σ_k y_ik ≤ Σ_{j ∈ N_i} (x_j + fixed_j) for every demand point i
y_ik → y_i(k−1) k-th coverage only counts if the (k−1)-th does
Σ_j x_j = p
x_j ∈ {0 .. per_base_cap}, y_ik ∈ {0, 1}
  • i: demand points (OSM localities); w_i is the zone's demand prior spread evenly over its localities.
  • j: candidate bases (hospitals with an emergency-department profile, where TN 108 units park; every OSM hospital if none are profiled). The current plan has 4,189 candidates and 656 demand points.
  • N_i: bases whose OSRM drive time to i is ≤ response_target_sec (480 s, config/fleet.yaml).
  • q: busy_fraction (0.35); K: mexclp_max_k (3); p: units to place.
  • The objective is scaled by 10,000 and rounded to integers for CP-SAT.

Solve order. ALS units are placed first, since they define critical-call coverage. BLS units follow, with the ALS positions passed in as fixed. Police first responders are placed with the same model over OSM police stations (per_base_cap=1). Every OSM fire station gets one tender (no optimisation needed).

Limits. time_limit_s = 10.0 per solve, num_workers = 8, random_seed = 7 (reproducible plans). An infeasible model raises RuntimeError("MEXCLP infeasible"). There is no runtime fallback, because the plan is computed offline and cached in backend/app/data/fleet_plan.json. The current plan uses coverage_matrix: osrm and covers 94.05% of demand with 37 units (solve time about 21 s in total). Regenerate with make fleet-plan, check with make fleet-check.

Note. The per-base cap (x_j + fixed_j ≤ per_base_cap + fixed_j) reduces to x_j ≤ per_base_cap. It limits each pass separately, so a base can end up with up to per_base_cap ALS plus per_base_cap BLS units. Change it to x_j + fixed_j ≤ per_base_cap if a cap on the total per base is wanted (this changes the plan; re-run make fleet-plan).


3. Legacy batch assignment: optimizer/cpsat.py​

Used only by the legacy scenario path (dispatch/batch.py, mounted with VAAZHVU_LEGACY=1). It is kept for the regression tests in tests/legacy/.

min Σ_i Σ_j ⌊ETA_ji · sev_i / 10⌋ · x_ij
s.t. Σ_j x_ij = 1 every incident gets exactly one unit
Σ_i x_ij ≤ 1 every unit serves at most one incident
x_ij = 0 when the unit lacks a required capability (BLS-only needs always fit)

1 s limit; returns None on failure or without OR-Tools, and dispatch/batch.py keeps its greedy result.


Changing a model safely​

  • Weights and limits live in config, not code: config/dispatch.yaml (batch.*, risk parameters behind J) and config/fleet.yaml (response_target_sec, busy_fraction, mexclp_max_k, unit counts).
  • Joint dispatch keeps per-pair scoring in dispatch/risk.py. If you change how J is computed, change the Go twin services/dispatch-scorer/risk.go too (CLAUDE.md).
  • Run uv run pytest tests/test_joint_dispatch.py -q after touching joint.py, and make fleet-plan && make fleet-check after touching fleet.py.
  • No MCP tool may run or change these solvers (docs/MCP.md): they make dispatch decisions.