Spaces:
Sleeping
Sleeping
File size: 16,136 Bytes
07473e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 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 342 343 344 345 346 347 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 | """DispatchSimulation engine. Pure Python, deterministic, seedable."""
from __future__ import annotations
from typing import Dict, List, Optional, Tuple
import numpy as np
from models import (
EmergencyCall,
EmergencyType,
EmergencyUnit,
Hospital,
Position,
Severity,
UnitStatus,
UnitType,
WorldConfig,
)
from reward import calculate_call_outcome, get_effectiveness
from utils import (
calculate_distance,
calculate_eta,
generate_caller_text,
get_capable_units,
get_optimal_unit,
)
def _parse_severity(value) -> Severity:
if isinstance(value, Severity):
return value
return Severity(int(value))
def generate_call_schedule(scenario: dict, seed: int) -> List[EmergencyCall]:
"""Build a deterministic list of EmergencyCall objects from a scenario dict."""
rng = np.random.RandomState(seed)
calls: List[EmergencyCall] = []
grid_size = scenario.get("grid_size", 10.0)
inaccuracy = float(scenario.get("caller_inaccuracy", 0.0))
for idx, call_cfg in enumerate(scenario["calls"], start=1):
true_type = EmergencyType(call_cfg["type"])
true_severity = _parse_severity(call_cfg["severity"])
if inaccuracy > 0 and rng.random() < inaccuracy:
other_types = [t for t in EmergencyType if t != true_type]
reported_type = EmergencyType(str(rng.choice([t.value for t in other_types])))
shifted = max(1, min(5, true_severity.value + int(rng.randint(-1, 2))))
reported_severity = Severity(shifted)
else:
reported_type = true_type
reported_severity = true_severity
location = Position(
x=round(float(rng.uniform(0.5, grid_size - 0.5)), 1),
y=round(float(rng.uniform(0.5, grid_size - 0.5)), 1),
)
caller_text = generate_caller_text(true_type, reported_type, rng)
calls.append(
EmergencyCall(
call_id=f"CALL-{idx:03d}",
timestamp=int(call_cfg["arrival_minute"]),
caller_description=caller_text,
location=location,
true_type=true_type,
true_severity=true_severity,
reported_type=reported_type,
reported_severity=reported_severity,
requires_unit_types=get_capable_units(true_type),
optimal_unit_type=get_optimal_unit(true_type),
)
)
calls.sort(key=lambda c: c.timestamp)
return calls
# Scene-time table: how long a unit stays on scene treating a call
SCENE_TIME_MINUTES = {
EmergencyType.CARDIAC_ARREST: 20,
EmergencyType.TRAUMA: 25,
EmergencyType.STROKE: 15,
EmergencyType.FIRE: 30,
EmergencyType.BREATHING: 15,
EmergencyType.MINOR_INJURY: 10,
EmergencyType.MENTAL_HEALTH: 20,
}
class DispatchSimulation:
"""Discrete-time simulation of an emergency dispatch episode."""
def __init__(self, scenario: dict, seed: int = 42) -> None:
self.scenario_name: str = scenario.get("name", "unnamed")
self.scenario: dict = scenario
self.seed: int = seed
self.rng = np.random.RandomState(seed)
world_cfg = scenario.get("world_config", {})
self.config = WorldConfig(**world_cfg)
self.current_time: int = 0
self.episode_done: bool = False
self.all_calls: List[EmergencyCall] = generate_call_schedule(scenario, seed)
self.active_calls: List[EmergencyCall] = []
self.completed_calls: List[dict] = []
self.timed_out_calls: List[dict] = []
self.dispatches: List[dict] = []
self.units: Dict[str, EmergencyUnit] = {}
for unit_cfg in scenario["units"]:
unit = EmergencyUnit(**unit_cfg)
self.units[unit.unit_id] = unit
self.hospitals: Dict[str, Hospital] = {}
for hosp_cfg in scenario["hospitals"]:
hosp = Hospital(**hosp_cfg)
self.hospitals[hosp.hospital_id] = hosp
self.call_index: int = 0
# Release any calls scheduled for time 0
self._release_due_calls()
# ------------------------------------------------------------------
# Time advancement
# ------------------------------------------------------------------
def _release_due_calls(self) -> None:
"""Move calls whose arrival time has passed into the active queue."""
while (
self.call_index < len(self.all_calls)
and self.all_calls[self.call_index].timestamp <= self.current_time
):
call = self.all_calls[self.call_index]
call.active = True
self.active_calls.append(call)
self.call_index += 1
def advance_time(self, minutes: int = 1) -> None:
"""Step the simulation forward by ``minutes`` discrete minutes."""
if self.episode_done:
return
minutes = max(1, int(minutes))
for _ in range(minutes):
self.current_time += 1
self._tick_once()
if self.episode_done:
break
def _tick_once(self) -> None:
"""Advance simulation by exactly one minute, updating units & calls."""
# 1. Move units according to their status
for unit in self.units.values():
if unit.status == UnitStatus.EN_ROUTE:
self._move_unit_toward_call(unit)
elif unit.status == UnitStatus.ON_SCENE:
if unit.busy_until is not None and self.current_time >= unit.busy_until:
unit.status = UnitStatus.RETURNING
unit.assigned_call_id = None
unit.assigned_hospital_id = None
elif unit.status == UnitStatus.RETURNING:
self._move_unit_toward_base(unit)
# 2. Time-out any active call that has waited too long
for call in list(self.active_calls):
if call.dispatched_unit_id is None:
wait = self.current_time - call.timestamp
if wait >= self.config.call_timeout_minutes:
call.active = False
self.active_calls.remove(call)
self.timed_out_calls.append(
{
"call_id": call.call_id,
"true_type": call.true_type.value,
"true_severity": call.true_severity.value,
"outcome_score": 0.0,
"reason": "timed_out",
}
)
# 3. Release new calls
self._release_due_calls()
# 4. Episode end conditions
if self.current_time >= self.config.time_limit_minutes:
self._finalize_episode("time_limit")
return
no_more_incoming = self.call_index >= len(self.all_calls)
no_pending = all(c.dispatched_unit_id is not None for c in self.active_calls)
all_units_idle = all(u.status == UnitStatus.AVAILABLE for u in self.units.values())
if no_more_incoming and no_pending and all_units_idle and not self.active_calls:
self._finalize_episode("all_resolved")
def _finalize_episode(self, reason: str) -> None:
"""Mark episode done.
Any remaining call (whether un-dispatched OR dispatched but the unit
never actually arrived on scene) is recorded as a timeout — the agent
failed to deliver care in time, so the patient outcome is 0.0.
"""
self.episode_done = True
for call in list(self.active_calls):
self.timed_out_calls.append(
{
"call_id": call.call_id,
"true_type": call.true_type.value,
"true_severity": call.true_severity.value,
"outcome_score": 0.0,
"reason": reason
if call.dispatched_unit_id is None
else f"{reason}_in_transit",
}
)
self.active_calls.clear()
# ------------------------------------------------------------------
# Unit movement
# ------------------------------------------------------------------
def _move_unit_toward_call(self, unit: EmergencyUnit) -> None:
call = self._get_call_by_id(unit.assigned_call_id) if unit.assigned_call_id else None
if call is None:
unit.status = UnitStatus.AVAILABLE
unit.assigned_call_id = None
return
distance_per_step = (unit.speed_kmh / 60.0) * self.config.step_duration_minutes
dist = calculate_distance(unit.position, call.location)
if dist <= distance_per_step:
unit.position = Position(x=call.location.x, y=call.location.y)
unit.status = UnitStatus.ON_SCENE
response_time = float(self.current_time - call.timestamp)
call.response_time = response_time
hospital = (
self.hospitals.get(unit.assigned_hospital_id)
if unit.assigned_hospital_id
else None
)
outcome = calculate_call_outcome(call, unit, response_time, hospital)
call.outcome_score = outcome
call.active = False
if call in self.active_calls:
self.active_calls.remove(call)
if hospital is not None and not hospital.on_diversion and hospital.available_beds > 0:
hospital.available_beds = max(0, hospital.available_beds - 1)
call.delivered_hospital_id = hospital.hospital_id
self.completed_calls.append(
{
"call_id": call.call_id,
"true_type": call.true_type.value,
"true_severity": call.true_severity.value,
"response_time": response_time,
"outcome_score": outcome,
"unit_id": unit.unit_id,
"unit_type": unit.unit_type.value,
"effectiveness": get_effectiveness(unit.unit_type, call.true_type),
"hospital_id": call.delivered_hospital_id,
}
)
scene_time = SCENE_TIME_MINUTES.get(call.true_type, 15)
unit.busy_until = self.current_time + scene_time
else:
ratio = distance_per_step / dist
unit.position = Position(
x=round(unit.position.x + (call.location.x - unit.position.x) * ratio, 3),
y=round(unit.position.y + (call.location.y - unit.position.y) * ratio, 3),
)
def _move_unit_toward_base(self, unit: EmergencyUnit) -> None:
distance_per_step = (unit.speed_kmh / 60.0) * self.config.step_duration_minutes
dist = calculate_distance(unit.position, unit.base_position)
if dist <= distance_per_step:
unit.position = Position(x=unit.base_position.x, y=unit.base_position.y)
unit.status = UnitStatus.AVAILABLE
unit.busy_until = None
else:
ratio = distance_per_step / dist
unit.position = Position(
x=round(unit.position.x + (unit.base_position.x - unit.position.x) * ratio, 3),
y=round(unit.position.y + (unit.base_position.y - unit.position.y) * ratio, 3),
)
# ------------------------------------------------------------------
# Action handlers (called from the MCP environment)
# ------------------------------------------------------------------
def dispatch(
self, call_id: str, unit_id: str, hospital_id: Optional[str] = None
) -> Tuple[float, str]:
"""Dispatch a unit to a call (optionally pre-assigning a destination hospital)."""
call = self._get_active_undispatched_call(call_id)
if call is None:
return -0.05, f"Call {call_id} not found in pending queue."
unit = self.units.get(unit_id)
if unit is None:
return -0.05, f"Unit {unit_id} not found."
if unit.status != UnitStatus.AVAILABLE:
return -0.05, f"Unit {unit_id} is {unit.status.value}, cannot dispatch."
# Treat empty string / whitespace as "no hospital chosen"
if isinstance(hospital_id, str):
hospital_id = hospital_id.strip() or None
chosen_hospital = None
if hospital_id is not None:
chosen_hospital = self.hospitals.get(hospital_id)
if chosen_hospital is None:
return -0.02, f"Hospital '{hospital_id}' not found."
unit.status = UnitStatus.EN_ROUTE
unit.assigned_call_id = call.call_id
unit.assigned_hospital_id = hospital_id
call.dispatched_unit_id = unit.unit_id
eta = calculate_eta(unit, call.location)
effectiveness = get_effectiveness(unit.unit_type, call.true_type)
self.dispatches.append(
{
"call_id": call.call_id,
"unit_id": unit.unit_id,
"unit_type": unit.unit_type.value,
"true_type": call.true_type.value,
"true_severity": call.true_severity.value,
"arrival_time": call.timestamp,
"dispatch_time": self.current_time,
"timeout_window": self.config.call_timeout_minutes,
"eta": eta,
"effectiveness": effectiveness,
"hospital_id": hospital_id,
}
)
msg = (
f"Dispatched {unit.unit_id} to {call.call_id}. "
f"ETA {eta:.1f} min. Unit effectiveness for {call.true_type.value}: "
f"{effectiveness:.0%}."
)
if hospital_id is not None and chosen_hospital is not None:
msg += f" Destination hospital: {chosen_hospital.name}."
return 0.02 * effectiveness, msg
def classify(self, call_id: str, severity: int) -> Tuple[float, str]:
call = self._get_active_undispatched_call(call_id)
if call is None:
return -0.02, f"Call {call_id} not in pending queue."
try:
new_sev = Severity(int(severity))
except ValueError:
return -0.02, f"Invalid severity {severity}; must be 1-5."
old = call.reported_severity
call.reported_severity = new_sev
return 0.01, f"Reclassified {call_id} severity from {old} to {new_sev.value}."
def callback(self, call_id: str, question: str) -> Tuple[float, str]:
call = self._get_active_undispatched_call(call_id)
if call is None:
return -0.02, f"Call {call_id} not in pending queue."
# 70% chance the caller clarifies; 30% they're too distressed
if self.rng.random() < 0.70:
call.reported_type = call.true_type
call.reported_severity = call.true_severity
return (
0.02,
f"Caller for {call.call_id} confirms: this is a {call.true_type.value}, "
f"severity {call.true_severity.value}.",
)
return 0.0, f"Caller for {call.call_id} is too distressed to give clear info."
# ------------------------------------------------------------------
# Lookups
# ------------------------------------------------------------------
def _get_call_by_id(self, call_id: str) -> Optional[EmergencyCall]:
for c in self.all_calls:
if c.call_id == call_id:
return c
return None
def _get_active_undispatched_call(self, call_id: str) -> Optional[EmergencyCall]:
for c in self.active_calls:
if c.call_id == call_id and c.dispatched_unit_id is None:
return c
return None
def get_pending_calls(self) -> List[EmergencyCall]:
return [c for c in self.active_calls if c.dispatched_unit_id is None]
def get_available_units(self) -> List[EmergencyUnit]:
return [u for u in self.units.values() if u.status == UnitStatus.AVAILABLE]
def total_calls(self) -> int:
return len(self.all_calls)
|