Spaces:
Sleeping
Sleeping
File size: 24,473 Bytes
f4f43f0 | 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 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 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 | """
run_simulation.py - Jaam Ctrl
=================================
Core simulation runner. Supports three modes:
"fixed" SUMO built-in fixed-time program (baseline)
"adaptive" Rule-based queue-aware controller (signal_controller.py)
"rl" Trained PPO agent (rl_agent.py)
Falls back to "adaptive" if no model is loaded.
Returns a SimResult with:
metrics – avg_delay, avg_stops, throughput, improvement, per_junction
gps_df – GPS probe DataFrame (for heatmap)
phase_log – per-step phase state for all 3 junctions (for timeline chart)
signal_events – list of every phase switch with timestamp and junction
raw_delays – per-vehicle accumulated delays
raw_stops – per-vehicle stop counts
"""
from __future__ import annotations
import os
import random
import sys
from dataclasses import dataclass, field
from typing import Callable, Optional
import numpy as np
import pandas as pd
# ── src/ on path ──────────────────────────────────────────────────────────────
_HERE = os.path.dirname(os.path.abspath(__file__))
if _HERE not in sys.path:
sys.path.insert(0, _HERE)
# ── TraCI ──────────────────────────────────────────────────────────────────────
try:
import traci
TRACI_OK = True
except ImportError:
TRACI_OK = False
from gps_generator import build_dataframe, collect_gps_frame, select_probe_vehicles
from signal_controller import FixedTimeController, RuleBasedController
# ── Paths & constants ──────────────────────────────────────────────────────────
_ROOT = os.path.dirname(_HERE)
SUMO_CFG = os.path.join(_ROOT, "sumo", "config.sumocfg")
SIM_DURATION = 1800 # seconds per run
TL_IDS = ["J0", "J1", "J2"]
JUNCTION_EDGES: dict[str, dict[str, list[str]]] = {
"J0": {"ew": ["W0J0", "J1J0"], "ns": ["N0J0", "S0J0"]},
"J1": {"ew": ["J0J1", "J2J1"], "ns": ["N1J1", "S1J1"]},
"J2": {"ew": ["J1J2"], "ns": ["N2J2", "S2J2"]},
}
# RL observation constants (must match rl_agent.py)
RL_MAX_QUEUE = 25.0
RL_MAX_THROUGHPUT = 10.0
RL_MAX_PHASE_DUR = 60.0
RL_CONTROL_STEP = 10
RL_MIN_PHASE_DUR = 15
RL_YELLOW_DUR = 5
PHASE_EW_GREEN = 0
PHASE_EW_YELLOW = 1
PHASE_NS_GREEN = 2
PHASE_NS_YELLOW = 3
PHASE_LABELS = {
PHASE_EW_GREEN: "EW Green",
PHASE_EW_YELLOW: "EW Yellow",
PHASE_NS_GREEN: "NS Green",
PHASE_NS_YELLOW: "NS Yellow",
}
# ══════════════════════════════════════════════════════════════════════════════
# SimResult
# ══════════════════════════════════════════════════════════════════════════════
@dataclass
class SimResult:
mode: str
metrics: dict = field(default_factory=dict)
gps_df: pd.DataFrame = field(default_factory=pd.DataFrame)
phase_log: list[dict] = field(default_factory=list)
signal_events: list[dict] = field(default_factory=list)
raw_delays: list[float] = field(default_factory=list)
raw_stops: list[int] = field(default_factory=list)
controller_log: list[dict] = field(default_factory=list)
# ══════════════════════════════════════════════════════════════════════════════
# Public entry point
# ══════════════════════════════════════════════════════════════════════════════
def run_simulation(
mode: str = "fixed",
traffic_scale: float = 1.0,
accident_step: int = -1,
seed: int = 42,
baseline_delay: Optional[float] = None,
ppo_model = None,
progress_cb: Optional[Callable] = None,
) -> SimResult:
"""
Run one full Jaam Ctrl simulation episode.
Parameters
----------
mode : "fixed" | "adaptive" | "rl"
traffic_scale : Vehicle flow multiplier (applied via TraCI scale command).
accident_step : Simulation second to inject a blocking vehicle (-1 = none).
seed : SUMO random seed for reproducibility.
baseline_delay : avg_delay_s from a prior "fixed" run (for % improvement).
ppo_model : Loaded SB3 PPO model. Required when mode="rl".
If None and mode="rl", falls back silently to "adaptive".
progress_cb : Optional callable(step: int, total: int) for progress bar.
Returns
-------
SimResult
"""
# ── SUMO not available → return mock data ─────────────────────────────────
if not TRACI_OK:
return _mock_result(mode, baseline_delay)
# ── Resolve mode / controller ──────────────────────────────────────────────
actual_mode = mode
if mode == "rl" and ppo_model is None:
actual_mode = "adaptive" # silent fallback
if actual_mode == "adaptive":
controller = RuleBasedController()
controller.reset()
elif actual_mode == "rl":
controller = None # PPO drives actions
else:
controller = FixedTimeController()
# ── RL per-junction phase state tracker ───────────────────────────────────
rl_phase_timer = {tl: 0 for tl in TL_IDS}
rl_cur_phase = {tl: PHASE_EW_GREEN for tl in TL_IDS}
# ── Launch SUMO ───────────────────────────────────────────────────────────
sumo_cmd = [
"sumo",
"-c", SUMO_CFG,
"--seed", str(seed),
"--no-warnings",
"--no-step-log",
"--quit-on-end",
]
traci.start(sumo_cmd)
# Apply traffic scale
if traffic_scale != 1.0:
try:
traci.simulation.setScale(traffic_scale)
except Exception:
pass
# ── Accumulators ──────────────────────────────────────────────────────────
gps_records: list[dict] = []
phase_log: list[dict] = []
signal_events: list[dict] = []
controller_log: list[dict] = []
delays_per_vehicle: dict[str, float] = {}
stops_per_vehicle: dict[str, int] = {}
arrived_count: int = 0
probe_vids: set = set()
# Track previous phase to detect switches
prev_phase = {tl: -1 for tl in TL_IDS}
# ── Main loop ─────────────────────────────────────────────────────────────
for step in range(SIM_DURATION):
# Update probe vehicle set every 60 s
if step % 60 == 0:
all_ids = traci.vehicle.getIDList()
probe_vids = select_probe_vehicles(list(all_ids))
# Collect GPS probes
gps_records.extend(collect_gps_frame(step, probe_vids))
# ── Signal control ────────────────────────────────────────────────────
if actual_mode == "rl" and step % RL_CONTROL_STEP == 0:
obs = _build_rl_obs(rl_phase_timer, rl_cur_phase)
action, _ = ppo_model.predict(obs, deterministic=True)
ctrl_state = _apply_rl_action(
int(action), rl_phase_timer, rl_cur_phase, step
)
elif controller is not None:
ctrl_state = controller.step(step)
else:
ctrl_state = {tl: {"phase": _safe_phase(tl),
"queue_ew": 0, "queue_ns": 0,
"action": "fixed"}
for tl in TL_IDS}
# ── Phase timeline snapshot (every 5 s) ───────────────────────────────
if step % 5 == 0:
snap: dict = {"step": step}
for tl in TL_IDS:
ph = _safe_phase(tl)
snap[f"{tl}_phase"] = ph
snap[f"{tl}_label"] = PHASE_LABELS.get(ph, "?")
snap[f"{tl}_queue_ew"] = ctrl_state.get(tl, {}).get("queue_ew", 0)
snap[f"{tl}_queue_ns"] = ctrl_state.get(tl, {}).get("queue_ns", 0)
snap[f"{tl}_action"] = ctrl_state.get(tl, {}).get("action", "")
phase_log.append(snap)
# ── Detect phase switches → signal_events ─────────────────────────────
for tl in TL_IDS:
ph = _safe_phase(tl)
if ph != prev_phase[tl] and prev_phase[tl] != -1:
signal_events.append({
"step": step,
"junction": tl,
"from_phase": PHASE_LABELS.get(prev_phase[tl], "?"),
"to_phase": PHASE_LABELS.get(ph, "?"),
"mode": actual_mode,
})
prev_phase[tl] = ph
# ── Per-vehicle metrics ───────────────────────────────────────────────
for vid in traci.vehicle.getIDList():
delays_per_vehicle[vid] = _accumulated_delay(vid)
stops_per_vehicle[vid] = stops_per_vehicle.get(vid, 0) + _is_stopped(vid)
arrived_count += traci.simulation.getArrivedNumber()
# ── Accident injection ────────────────────────────────────────────────
if step == accident_step:
_inject_accident()
# ── Advance ──────────────────────────────────────────────────────────
traci.simulationStep()
if progress_cb:
progress_cb(step + 1, SIM_DURATION)
traci.close()
# ── Build result ──────────────────────────────────────────────────────────
all_delays = list(delays_per_vehicle.values())
all_stops = list(stops_per_vehicle.values())
metrics = _build_metrics(
all_delays, all_stops, arrived_count,
actual_mode, baseline_delay, phase_log
)
return SimResult(
mode = actual_mode,
metrics = metrics,
gps_df = build_dataframe(gps_records),
phase_log = phase_log,
signal_events = signal_events,
raw_delays = all_delays,
raw_stops = all_stops,
)
# ══════════════════════════════════════════════════════════════════════════════
# RL helpers (18-dim obs matching rl_agent.py CorridorEnv._get_obs)
# ══════════════════════════════════════════════════════════════════════════════
def _build_rl_obs(
phase_timer: dict[str, int],
cur_phase: dict[str, int],
) -> np.ndarray:
"""
Build 18-dim observation vector for PPO inference.
Layout: [q_ew, q_ns, ph_ew, ph_ns, t_norm, thru] × 3 junctions
Must exactly match CorridorEnv._get_obs() in rl_agent.py.
"""
obs: list[float] = []
for tl in TL_IDS:
edges = JUNCTION_EDGES[tl]
q_ew = min(_sum_queue(edges["ew"]) / RL_MAX_QUEUE, 1.0)
q_ns = min(_sum_queue(edges["ns"]) / RL_MAX_QUEUE, 1.0)
phase = cur_phase[tl]
ph_ew = 1.0 if phase == PHASE_EW_GREEN else 0.0
ph_ns = 1.0 if phase == PHASE_NS_GREEN else 0.0
t_norm = min(phase_timer[tl] / RL_MAX_PHASE_DUR, 1.0)
thru = min(_edge_throughput(edges["ew"] + edges["ns"])
/ RL_MAX_THROUGHPUT, 1.0)
obs.extend([q_ew, q_ns, ph_ew, ph_ns, t_norm, thru])
return np.array(obs, dtype=np.float32)
def _apply_rl_action(
action: int,
phase_timer: dict[str, int],
cur_phase: dict[str, int],
sim_step: int,
) -> dict[str, dict]:
"""
Decode 3-bit action and apply phase switches.
Updates phase_timer and cur_phase in-place.
Returns per-junction state dict for phase_log.
"""
state = {}
for i, tl in enumerate(TL_IDS):
requested = bool(action & (1 << i))
phase_timer[tl] += RL_CONTROL_STEP
q_ew = _sum_queue(JUNCTION_EDGES[tl]["ew"])
q_ns = _sum_queue(JUNCTION_EDGES[tl]["ns"])
action_taken = "hold"
cur = cur_phase[tl]
# Don't interrupt yellow phases
if cur not in (PHASE_EW_YELLOW, PHASE_NS_YELLOW):
force = phase_timer[tl] >= RL_MAX_PHASE_DUR
allowed = requested and phase_timer[tl] >= RL_MIN_PHASE_DUR
if force or allowed:
_rl_switch_phase(tl, cur, cur_phase, phase_timer)
action_taken = "rl_switch"
state[tl] = {
"phase": cur_phase[tl],
"queue_ew": q_ew,
"queue_ns": q_ns,
"action": action_taken,
}
return state
def _rl_switch_phase(
tl: str,
cur: int,
cur_phase: dict[str, int],
phase_timer: dict[str, int],
):
"""Toggle EW-green ↔ NS-green through a yellow phase."""
try:
if cur == PHASE_EW_GREEN:
traci.trafficlight.setPhase(tl, PHASE_EW_YELLOW)
traci.trafficlight.setPhaseDuration(tl, RL_YELLOW_DUR)
cur_phase[tl] = PHASE_NS_GREEN
elif cur == PHASE_NS_GREEN:
traci.trafficlight.setPhase(tl, PHASE_NS_YELLOW)
traci.trafficlight.setPhaseDuration(tl, RL_YELLOW_DUR)
cur_phase[tl] = PHASE_EW_GREEN
phase_timer[tl] = 0
except Exception:
pass
# ══════════════════════════════════════════════════════════════════════════════
# TraCI helpers
# ══════════════════════════════════════════════════════════════════════════════
def _safe_phase(tl: str) -> int:
try:
return traci.trafficlight.getPhase(tl)
except Exception:
return 0
def _sum_queue(edges: list[str]) -> float:
total = 0.0
for e in edges:
try:
vids = traci.edge.getLastStepVehicleIDs(e)
total += sum(1 for v in vids if traci.vehicle.getSpeed(v) < 0.5)
except Exception:
pass
return total
def _edge_throughput(edges: list[str]) -> float:
total = 0.0
for e in edges:
try:
total += traci.edge.getLastStepVehicleNumber(e)
except Exception:
pass
return total
def _accumulated_delay(vid: str) -> float:
try:
return traci.vehicle.getAccumulatedWaitingTime(vid)
except Exception:
return 0.0
def _is_stopped(vid: str) -> int:
try:
return 1 if traci.vehicle.getWaitingTime(vid) > 0 else 0
except Exception:
return 0
def _inject_accident():
"""Stall a random vehicle to simulate an accident."""
try:
vids = list(traci.vehicle.getIDList())
if vids:
victim = random.choice(vids)
traci.vehicle.setSpeed(victim, 0.0)
traci.vehicle.setSpeedMode(victim, 0)
except Exception:
pass
# ══════════════════════════════════════════════════════════════════════════════
# Metrics builder
# ══════════════════════════════════════════════════════════════════════════════
def _build_metrics(
all_delays: list[float],
all_stops: list[int],
arrived: int,
mode: str,
baseline_delay: Optional[float],
phase_log: list[dict],
) -> dict:
avg_delay = float(np.mean(all_delays)) if all_delays else 0.0
avg_stops = float(np.mean(all_stops)) if all_stops else 0.0
improvement = 0.0
if baseline_delay and baseline_delay > 0:
improvement = round((baseline_delay - avg_delay) / baseline_delay * 100, 1)
# Per-junction average queue from phase_log
per_junction: dict[str, dict] = {}
if phase_log:
for tl in TL_IDS:
ew_vals = [s.get(f"{tl}_queue_ew", 0) for s in phase_log]
ns_vals = [s.get(f"{tl}_queue_ns", 0) for s in phase_log]
per_junction[tl] = {
"avg_queue_ew": round(float(np.mean(ew_vals)), 2),
"avg_queue_ns": round(float(np.mean(ns_vals)), 2),
"avg_queue": round(float(np.mean(ew_vals)) +
float(np.mean(ns_vals)), 2),
}
return {
"mode": mode,
"avg_delay_s": round(avg_delay, 2),
"avg_stops": round(avg_stops, 2),
"throughput": arrived,
"improvement": improvement,
"per_junction": per_junction,
}
# ══════════════════════════════════════════════════════════════════════════════
# Mock result (SUMO not installed)
# ══════════════════════════════════════════════════════════════════════════════
_MOCK_SEEDS = {"fixed": 1, "adaptive": 2, "rl": 3}
def _mock_result(mode: str, baseline_delay: Optional[float]) -> SimResult:
"""Realistic synthetic data so the Streamlit UI works without SUMO."""
rng = np.random.default_rng(_MOCK_SEEDS.get(mode, 99))
if mode == "fixed":
avg_delay, avg_stops, throughput = (
rng.uniform(45, 65), rng.uniform(3.5, 6.0), int(rng.integers(900, 1100))
)
improv = 0.0
elif mode == "adaptive":
avg_delay, avg_stops, throughput = (
rng.uniform(28, 42), rng.uniform(2.0, 3.5), int(rng.integers(1050, 1250))
)
bl = baseline_delay or 55.0
improv = round((bl - avg_delay) / bl * 100, 1)
else: # rl
avg_delay, avg_stops, throughput = (
rng.uniform(18, 30), rng.uniform(1.2, 2.4), int(rng.integers(1150, 1400))
)
bl = baseline_delay or 55.0
improv = round((bl - avg_delay) / bl * 100, 1)
# ── Synthetic GPS data ─────────────────────────────────────────────────────
n = 600
xs = rng.uniform(-200, 1400, n)
ys = rng.uniform(-300, 300, n)
junction_x = {"J0": 0, "J1": 500, "J2": 1000}
gps_df = pd.DataFrame({
"time": rng.integers(0, SIM_DURATION, n),
"vehicle_id": [f"v{i}" for i in range(n)],
"x": xs,
"y": ys,
"speed": rng.uniform(0, 14, n),
"vehicle_type": rng.choice(
["motorcycle", "car", "auto", "truck"],
size=n, p=[0.6, 0.2, 0.1, 0.1]
),
"junction_proximity": [
min(TL_IDS, key=lambda j: abs(x - junction_x[j]))
for x in xs
],
})
# ── Synthetic phase log ────────────────────────────────────────────────────
phase_log = []
phases = {tl: PHASE_EW_GREEN for tl in TL_IDS}
timers = {tl: 0 for tl in TL_IDS}
durations = {
PHASE_EW_GREEN: 35, PHASE_EW_YELLOW: 5,
PHASE_NS_GREEN: 30, PHASE_NS_YELLOW: 5,
}
next_phase_map = {
PHASE_EW_GREEN: PHASE_EW_YELLOW, PHASE_EW_YELLOW: PHASE_NS_GREEN,
PHASE_NS_GREEN: PHASE_NS_YELLOW, PHASE_NS_YELLOW: PHASE_EW_GREEN,
}
# Stagger for green-wave
timers["J1"] = 36
timers["J2"] = 72
for step in range(0, SIM_DURATION, 5):
snap: dict = {"step": step}
for tl in TL_IDS:
timers[tl] += 5
if timers[tl] >= durations[phases[tl]]:
phases[tl] = next_phase_map[phases[tl]]
timers[tl] = 0
# Adaptive / RL: vary queue lengths to look realistic
q_ew = max(0, int(rng.normal(5 if phases[tl] == PHASE_NS_GREEN else 2, 2)))
q_ns = max(0, int(rng.normal(5 if phases[tl] == PHASE_EW_GREEN else 2, 2)))
snap[f"{tl}_phase"] = phases[tl]
snap[f"{tl}_label"] = PHASE_LABELS[phases[tl]]
snap[f"{tl}_queue_ew"] = q_ew
snap[f"{tl}_queue_ns"] = q_ns
snap[f"{tl}_action"] = "hold"
phase_log.append(snap)
# ── Synthetic signal events ────────────────────────────────────────────────
signal_events = [
{"step": s["step"], "junction": tl,
"from_phase": "EW Green", "to_phase": "EW Yellow", "mode": mode}
for s in phase_log[::15]
for tl in TL_IDS
]
# ── Per-junction metrics ───────────────────────────────────────────────────
per_junction = {}
scale = {"fixed": 1.0, "adaptive": 0.65, "rl": 0.45}[mode]
for tl in TL_IDS:
per_junction[tl] = {
"avg_queue_ew": round(float(rng.uniform(3, 8) * scale), 2),
"avg_queue_ns": round(float(rng.uniform(2, 6) * scale), 2),
"avg_queue": round(float(rng.uniform(5, 14) * scale), 2),
}
metrics = {
"mode": mode,
"avg_delay_s": round(float(avg_delay), 2),
"avg_stops": round(float(avg_stops), 2),
"throughput": throughput,
"improvement": improv,
"per_junction": per_junction,
}
return SimResult(
mode = mode,
metrics = metrics,
gps_df = gps_df,
phase_log = phase_log,
signal_events = signal_events,
raw_delays = [float(rng.uniform(0, avg_delay * 2)) for _ in range(300)],
raw_stops = [int(rng.integers(0, int(avg_stops * 2) + 1)) for _ in range(300)],
)
|