metrics
agentevac.analysis.metrics
Run-level metrics collection and aggregation for evacuation simulations.
RunMetricsCollector accumulates agent-level events over the course of a simulation
run and computes five aggregate KPIs plus one destination-share summary used for
calibration and cross-scenario comparison:
1. **Departure-time variability** — Population variance of departure timestamps.
High variability suggests agents are making nuanced, heterogeneous decisions.
Low variability (everyone leaves at once) can indicate herding or over-reaction.
2. **Route-choice entropy** — Shannon entropy over the distribution of chosen
destinations/routes. High entropy means agents spread across many options;
low entropy means most converge on a single choice.
3. **Decision instability** — Average number of times agents changed their chosen
option across decision rounds. Frequent changes suggest the LLM or belief model
is producing erratic decisions; low instability suggests confident, stable routing.
4. **Average hazard exposure** — Mean edge-level risk score sampled across all
active vehicles each decision tick. Proxies the cumulative fire danger
experienced during the evacuation.
5. **Average travel time** — Mean time from departure to arrival for agents that
completed their evacuation during the simulation window.
6. **Destination choice share** — Final per-agent destination commitments
aggregated into counts and fractions for each designated evacuation point.
The collector writes a JSON summary to disk when export_run_metrics or close
is called. The file path is auto-timestamped to avoid overwrites across runs.
RunMetricsCollector
Stateful collector for run-level simulation metrics.
Designed to be instantiated once per simulation run. Event-recording methods
(record_departure, record_arrival, observe_active_vehicles, etc.) are
called by the main simulation loop during each step. Aggregation methods
(compute_*) are cheap and may be called at any time, including mid-run for
live monitoring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enabled
|
bool
|
If |
required |
base_path
|
str
|
Base file path for the output JSON (timestamp is appended). |
required |
run_mode
|
str
|
Run mode string ("record" or "replay") stored in the summary. |
required |
Source code in agentevac/analysis/metrics.py
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 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 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 | |
arrived_count()
Return the number of agents that have arrived at their destination.
Source code in agentevac/analysis/metrics.py
144 145 146 | |
close()
Flush and export metrics and agent profiles; typically called at simulation end.
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The path of the written metrics file, or |
Source code in agentevac/analysis/metrics.py
480 481 482 483 484 485 486 487 488 489 | |
compute_average_hazard_exposure()
Compute global and per-agent average hazard-exposure risk scores.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict with |
Source code in agentevac/analysis/metrics.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | |
compute_average_signal_conflict()
Compute global and per-agent average signal conflict.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict with |
Source code in agentevac/analysis/metrics.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | |
compute_average_travel_time()
Compute average travel time for agents that completed their evacuation.
Agents still en route at simulation end are excluded.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict with |
Dict[str, Any]
|
|
Source code in agentevac/analysis/metrics.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
compute_decision_instability()
Compute per-agent and aggregate decision instability (number of choice changes).
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict with |
Source code in agentevac/analysis/metrics.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
compute_departure_time_variability()
Compute the population variance of agent departure times (seconds²).
Returns:
| Type | Description |
|---|---|
float
|
Variance of departure timestamps, or 0.0 if fewer than two agents departed. |
Source code in agentevac/analysis/metrics.py
322 323 324 325 326 327 328 329 330 331 332 333 | |
compute_destination_choice_share()
Compute counts and fractions of agents' latest destination commitments.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict with |
Dict[str, Any]
|
|
Source code in agentevac/analysis/metrics.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | |
compute_route_choice_entropy()
Compute Shannon entropy of the aggregated route/destination choice distribution.
Returns:
| Type | Description |
|---|---|
float
|
Entropy in nats (≥ 0); 0 if no choices have been recorded. |
Source code in agentevac/analysis/metrics.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
export_agent_profiles()
Write per-agent profiles to a JSON file alongside the metrics file.
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The path of the written file, or |
Source code in agentevac/analysis/metrics.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
export_run_metrics(path=None)
Write the metrics summary to a JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Optional[str]
|
Override output path; falls back to the auto-timestamped path. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The path of the written file, or |
Source code in agentevac/analysis/metrics.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | |
observe_active_vehicles(active_vehicle_ids, sim_t_s)
Update the active-vehicle set for live bookkeeping only.
Arrival timing is intentionally not inferred from disappearances because a
transient omission from traci.vehicle.getIDList() can otherwise
produce false travel-time completions. True arrivals should be recorded
through :meth:record_arrival using explicit SUMO arrival events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
active_vehicle_ids
|
List[str]
|
List of vehicle IDs currently in the simulation. |
required |
sim_t_s
|
float
|
Current simulation time in seconds. |
required |
Source code in agentevac/analysis/metrics.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
record_agent_profile(agent_id, profile)
Record the sampled profile parameters for an agent.
Only the first call per agent_id is stored (profiles are immutable).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Vehicle ID. |
required |
profile
|
Dict[str, float]
|
Dict of psychological parameter values (theta_trust, theta_r, etc.). |
required |
Source code in agentevac/analysis/metrics.py
219 220 221 222 223 224 225 226 227 228 229 230 231 | |
record_arrival(agent_id, sim_t_s)
Record the first explicit arrival event for an agent.
Arrival timestamps are only accepted for agents that have already departed. Subsequent arrival records for the same agent are ignored so the original completion timestamp is preserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Vehicle ID. |
required |
sim_t_s
|
float
|
Simulation time of arrival in seconds. |
required |
Source code in agentevac/analysis/metrics.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |
record_conflict_sample(agent_id, signal_conflict)
Record one signal-conflict sample for an active vehicle.
Called once per agent per decision round from the belief update. The conflict score (JSD between env and social beliefs, [0, 1]) enables post-hoc RQ1 analysis of the mediation pathway: σ_info → signal_conflict → behavioral DVs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Vehicle ID. |
required |
signal_conflict
|
float
|
JSD-based conflict score ∈ [0, 1]. |
required |
Source code in agentevac/analysis/metrics.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
record_decision_snapshot(agent_id, sim_t_s, decision_round, state, choice_idx, action_status)
Record a decision-round snapshot for entropy, instability, and final destination tracking.
Detects decision-state changes by comparing control_mode::choice_idx
against the previous round's string for the same agent. In destination
mode, the latest selected destination name is also retained as the
agent's current final destination commitment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Vehicle ID. |
required |
sim_t_s
|
float
|
Simulation time of the decision in seconds. |
required |
decision_round
|
int
|
Global decision-round counter. |
required |
state
|
Dict[str, Any]
|
Full decision-state dict (supplies choice name and control mode). |
required |
choice_idx
|
Optional[int]
|
Index of the chosen option, or |
required |
action_status
|
str
|
Action status string (e.g., "depart_now", "wait_predeparture"). |
required |
Source code in agentevac/analysis/metrics.py
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 | |
record_departure(agent_id, sim_t_s, reason=None)
Record the first departure event for an agent.
Subsequent calls for the same agent_id are ignored so the original
departure timestamp is preserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Vehicle ID. |
required |
sim_t_s
|
float
|
Simulation time of departure in seconds. |
required |
reason
|
Optional[str]
|
Optional departure trigger label (e.g., "risk_threshold"). |
None
|
Source code in agentevac/analysis/metrics.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
record_exposure_sample(agent_id, sim_t_s, current_edge, current_margin_m, risk_score=None)
Record one hazard-exposure sample for an active vehicle.
Called once per active vehicle per decision tick.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_id
|
str
|
Vehicle ID. |
required |
sim_t_s
|
float
|
Current simulation time in seconds. |
required |
current_edge
|
str
|
SUMO edge ID where the vehicle is located. |
required |
current_margin_m
|
Optional[float]
|
Fire margin (metres) on the current edge (retained for future per-edge analysis; not aggregated currently). |
required |
risk_score
|
Optional[float]
|
Edge-level fire risk score ∈ [0, 1]; treated as 0 if |
None
|
Source code in agentevac/analysis/metrics.py
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 | |
summary()
Assemble the full run-metrics summary dict.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A JSON-serializable dict containing all KPIs, destination-share |
Dict[str, Any]
|
summary, and bookkeeping fields. |
Source code in agentevac/analysis/metrics.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |