Spaces:
Sleeping
Sleeping
File size: 16,766 Bytes
f4f43f0 b9460d5 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 | """
src/run_simulation.py
---------------------
Simulation runner for Jaam Ctrl.
Exports required by app.py:
run_simulation(mode, traffic_scale, accident_step, seed,
baseline_delay, ppo_model, progress_cb) -> SimResult
SimResult dataclass
SIM_DURATION int (seconds)
_mock_result(mode, baseline_delay) -> SimResult
SUMO-GUI
--------
When SUMO is installed, the simulation opens a SUMO-GUI desktop window
automatically. The GUI starts playing immediately — you can watch cars
moving on the real CP network while Streamlit shows live metrics.
To disable the GUI (headless / CI):
SUMO_NO_GUI=1 streamlit run app.py
"""
from __future__ import annotations
import os
import sys
import time
import math
import random
from dataclasses import dataclass, field
from typing import Callable, Any
import numpy as np
import pandas as pd
# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
SIM_DURATION = 1800
CONTROL_STEP = 10
MIN_PHASE = 15
MAX_PHASE = 60
MAX_SPEED = 50.0
TL_IDS = ["J0", "J1", "J2"]
JUNCTION_COORDS = {
"J0": (28.6315, 77.2167),
"J1": (28.6328, 77.2195),
"J2": (28.6287, 77.2140),
}
_ROAD_SEGMENTS = [
(28.6315, 77.2167, 28.6328, 77.2195, "J0"),
(28.6328, 77.2195, 28.6315, 77.2167, "J1"),
(28.6287, 77.2140, 28.6315, 77.2167, "J2"),
(28.6315, 77.2167, 28.6287, 77.2140, "J0"),
(28.6350, 77.2167, 28.6315, 77.2167, "J1"),
(28.6315, 77.2167, 28.6270, 77.2167, "J0"),
(28.6328, 77.2240, 28.6328, 77.2195, "J1"),
(28.6315, 77.2100, 28.6315, 77.2140, "J2"),
(28.6315, 77.2140, 28.6315, 77.2167, "J2"),
(28.6300, 77.2225, 28.6287, 77.2200, "J2"),
(28.6335, 77.2225, 28.6328, 77.2195, "J1"),
(28.6315, 77.2167, 28.6335, 77.2225, "J0"),
]
# ---------------------------------------------------------------------------
# SUMO-GUI flag
# Set SUMO_NO_GUI=1 in your environment to force headless mode.
# ---------------------------------------------------------------------------
_USE_GUI = os.environ.get("SUMO_NO_GUI", "0") != "1"
# ---------------------------------------------------------------------------
# SUMO availability
# ---------------------------------------------------------------------------
try:
import traci
import sumolib
SUMO_AVAILABLE = True
except ImportError:
SUMO_AVAILABLE = False
# ---------------------------------------------------------------------------
# SimResult
# ---------------------------------------------------------------------------
@dataclass
class SimResult:
mode: str
metrics: dict = field(default_factory=dict)
gps_df: pd.DataFrame = field(default_factory=pd.DataFrame)
phase_log: list = field(default_factory=list)
signal_events: list = field(default_factory=list)
# ---------------------------------------------------------------------------
# GPS probe generator
# ---------------------------------------------------------------------------
def _generate_gps_df(n_vehicles, congestion_factor, accident_junc, rng):
frames = []
per_seg = max(1, n_vehicles // len(_ROAD_SEGMENTS))
for lat1, lon1, lat2, lon2, junc in _ROAD_SEGMENTS:
# Minimal noise so differences between control strategies are visible
cf = float(np.clip(congestion_factor + rng.uniform(-0.03, 0.03), 0, 1))
if accident_junc and junc == accident_junc:
cf = min(cf + 0.4, 1.0)
t = rng.uniform(0, 1, per_seg)
lats = lat1 + t*(lat2-lat1) + rng.normal(0, 0.00003, per_seg)
lons = lon1 + t*(lon2-lon1) + rng.normal(0, 0.00003, per_seg)
base_s = MAX_SPEED * (1.0 - cf)
speeds = np.clip(rng.normal(base_s, base_s*0.25+0.5, per_seg), 1.0, MAX_SPEED)
weights= np.clip(1.0 - speeds/MAX_SPEED, 0.05, 1.0)
frames.append(pd.DataFrame({
"lat": lats, "lon": lons,
"speed_kmph": speeds, "weight": weights, "junction": junc,
}))
return pd.concat(frames, ignore_index=True)
# ---------------------------------------------------------------------------
# Mock simulation (no SUMO required)
# ---------------------------------------------------------------------------
_BASE_METRICS = {
"fixed": {"avg_delay_s": 62.0, "avg_stops": 3.8, "throughput": 950, "cf": 0.72},
"adaptive": {"avg_delay_s": 43.0, "avg_stops": 2.5, "throughput": 1120, "cf": 0.52},
"rl": {"avg_delay_s": 30.0, "avg_stops": 1.7, "throughput": 1310, "cf": 0.38},
}
_QUEUE_BASE = {
"fixed": {"J0": (9,7), "J1": (11,8), "J2": (8,6)},
"adaptive": {"J0": (6,4), "J1": (7,5), "J2": (5,4)},
"rl": {"J0": (4,3), "J1": (5,3), "J2": (3,2)},
}
_PHASE_LABELS = {0:"EW Green", 1:"EW Yellow", 2:"NS Green", 3:"NS Yellow"}
def _mock_phase_log(mode, traffic_scale, rng):
phase_log, signal_events = [], []
qb = _QUEUE_BASE[mode]
phases = {jid: 0 for jid in TL_IDS}
offsets = {"J0": 0, "J1": 36, "J2": 72}
for step in range(0, SIM_DURATION, CONTROL_STEP):
row = {"step": step}
for jid in TL_IDS:
qew_b, qns_b = qb[jid]
qew = max(0.0, qew_b * traffic_scale + rng.uniform(-1.5, 1.5))
qns = max(0.0, qns_b * traffic_scale + rng.uniform(-1.5, 1.5))
t_eff = (step + offsets[jid]) % 78
ph = 0 if t_eff < 40 else 1 if t_eff < 44 else 2 if t_eff < 74 else 3
if ph != phases[jid]:
signal_events.append({
"step": step, "junction": jid,
"from_phase": _PHASE_LABELS[phases[jid]],
"to_phase": _PHASE_LABELS[ph],
})
phases[jid] = ph
action = ("extend_ew" if mode != "fixed" and qew > qns
else "extend_ns" if mode != "fixed" else "fixed")
row[f"{jid}_label"] = _PHASE_LABELS[ph]
row[f"{jid}_queue_ew"] = round(qew, 1)
row[f"{jid}_queue_ns"] = round(qns, 1)
row[f"{jid}_action"] = action
phase_log.append(row)
return phase_log, signal_events
def _mock_result(mode, baseline_delay=None, traffic_scale=1.0,
accident_step=-1, seed=42):
rng = np.random.default_rng(seed + {"fixed":0,"adaptive":1,"rl":2}.get(mode,0))
bm = _BASE_METRICS[mode]
delay = bm["avg_delay_s"] * traffic_scale + rng.uniform(-2, 2)
stops = bm["avg_stops"] * traffic_scale + rng.uniform(-0.2, 0.2)
throughput = int(bm["throughput"] / max(traffic_scale, 0.5) + rng.integers(-30, 30))
improvement= 0.0
if baseline_delay and mode != "fixed":
improvement = max(0.0, (baseline_delay - delay) / baseline_delay * 100)
per_junction = {}
for jid in TL_IDS:
qew_b, qns_b = _QUEUE_BASE[mode][jid]
qew = max(0.0, qew_b * traffic_scale + rng.uniform(-1, 1))
qns = max(0.0, qns_b * traffic_scale + rng.uniform(-1, 1))
per_junction[jid] = {
"avg_queue": round((qew+qns)/2, 1),
"avg_queue_ew": round(qew, 1),
"avg_queue_ns": round(qns, 1),
}
acc_junc = "J1" if accident_step >= 0 else None
gps_df = _generate_gps_df(int(400*traffic_scale), bm["cf"], acc_junc, rng)
phase_log, signal_events = _mock_phase_log(mode, traffic_scale, rng)
return SimResult(
mode = mode,
metrics = {
"avg_delay_s": round(delay, 1),
"avg_stops": round(stops, 2),
"throughput": throughput,
"improvement": round(improvement, 1),
"per_junction": per_junction,
},
gps_df = gps_df,
phase_log = phase_log,
signal_events = signal_events,
)
# ---------------------------------------------------------------------------
# SUMO simulation — with GUI support
# ---------------------------------------------------------------------------
def _sumo_result(mode, traffic_scale, accident_step, seed,
baseline_delay, ppo_model, progress_cb):
src_dir = os.path.dirname(os.path.abspath(__file__))
root_dir = os.path.dirname(src_dir)
cfg_path = os.path.join(root_dir, "sumo", "corridor.sumocfg")
if not os.path.exists(cfg_path):
return _mock_result(mode, baseline_delay, traffic_scale, accident_step, seed)
# ── Use sumo-gui when _USE_GUI is True, sumo otherwise ──────────────────
binary = "sumo-gui" if _USE_GUI else "sumo"
sumo_cmd = [
binary,
"--configuration-file", cfg_path,
"--seed", str(seed),
"--scale", str(traffic_scale),
"--no-warnings", "true",
"--no-step-log", "true",
"--time-to-teleport", "300",
# Start playing automatically — no need to press Play in the GUI
"--start", "true",
# Close GUI when simulation ends
"--quit-on-end", "true",
# 50 ms per step so cars are visible but sim stays fast
"--delay", "50",
]
traci.start(sumo_cmd)
total_delay = 0.0
total_stops = 0
total_veh = 0
arrive_count = 0
phase_log = []
signal_events= []
gps_rows = []
prev_phases = {jid: None for jid in TL_IDS}
queue_hist = {jid: {"ew": [], "ns": []} for jid in TL_IDS}
rng = np.random.default_rng(seed)
for step in range(SIM_DURATION):
traci.simulationStep()
# Accident injection
if accident_step > 0 and step == accident_step:
vehs = traci.vehicle.getIDList()
if vehs:
v = random.choice(vehs)
traci.vehicle.setSpeed(v, 0)
traci.vehicle.setSpeedMode(v, 0)
veh_ids = traci.vehicle.getIDList()
for vid in veh_ids:
spd = traci.vehicle.getSpeed(vid) * 3.6
wait = traci.vehicle.getWaitingTime(vid)
pos = traci.vehicle.getPosition(vid)
lon = 77.2167 + pos[0] / 111320.0
lat = 28.6315 + pos[1] / 110540.0
weight = max(0.05, 1.0 - spd / MAX_SPEED)
junc = _nearest_junction(lat, lon)
gps_rows.append({"lat":lat,"lon":lon,"speed_kmph":spd,
"weight":weight,"junction":junc})
total_delay += wait
total_veh += 1
if traci.vehicle.getStopState(vid) & 1:
total_stops += 1
arrive_count += traci.simulation.getArrivedNumber()
if step % CONTROL_STEP == 0 and step > 0:
row = {"step": step}
queues = {}
for jid in TL_IDS:
ph = traci.trafficlight.getPhase(jid)
lbl = _PHASE_LABELS.get(ph, "Unknown")
if prev_phases[jid] is not None and ph != prev_phases[jid]:
signal_events.append({
"step": step, "junction": jid,
"from_phase": _PHASE_LABELS.get(prev_phases[jid], "?"),
"to_phase": lbl,
})
prev_phases[jid] = ph
jlat, jlon = JUNCTION_COORDS[jid]
qew = qns = 0
for vid in veh_ids:
pos = traci.vehicle.getPosition(vid)
vlat = 28.6315 + pos[1] / 110540.0
vlon = 77.2167 + pos[0] / 111320.0
if abs(vlat-jlat)<0.001 and abs(vlon-jlon)<0.001:
angle = traci.vehicle.getAngle(vid)
if 45 < angle < 135 or 225 < angle < 315:
qns += 1
else:
qew += 1
queue_hist[jid]["ew"].append(qew)
queue_hist[jid]["ns"].append(qns)
queues[jid] = (qew, qns)
action = "fixed"
if mode == "adaptive":
action = _adaptive_action(jid, ph, qew, qns, step)
if action.startswith("switch"):
traci.trafficlight.setPhase(jid, (ph+1)%4)
elif mode == "rl" and ppo_model is not None:
obs = _build_obs(jid, ph, qew, qns, step, queues)
ac, _ = ppo_model.predict(obs, deterministic=True)
if ac == 1:
traci.trafficlight.setPhase(jid, (ph+1)%4)
action = f"rl_action_{ac}"
row[f"{jid}_label"] = lbl
row[f"{jid}_queue_ew"] = qew
row[f"{jid}_queue_ns"] = qns
row[f"{jid}_action"] = action
phase_log.append(row)
if progress_cb:
progress_cb(step, SIM_DURATION)
traci.close()
n = max(total_veh, 1)
avg_delay = total_delay / n
avg_stops = total_stops / n
improvement = 0.0
if baseline_delay and mode != "fixed":
improvement = max(0.0, (baseline_delay - avg_delay) / baseline_delay * 100)
per_junction = {}
for jid in TL_IDS:
ew_h = queue_hist[jid]["ew"]
ns_h = queue_hist[jid]["ns"]
qew = float(np.mean(ew_h)) if ew_h else 0.0
qns = float(np.mean(ns_h)) if ns_h else 0.0
per_junction[jid] = {
"avg_queue": round((qew+qns)/2, 1),
"avg_queue_ew": round(qew, 1),
"avg_queue_ns": round(qns, 1),
}
gps_df = pd.DataFrame(gps_rows) if gps_rows else pd.DataFrame(
columns=["lat","lon","speed_kmph","weight","junction"])
if len(gps_df) > 2000:
gps_df = gps_df.sample(2000, random_state=seed)
return SimResult(
mode = mode,
metrics = {
"avg_delay_s": round(avg_delay, 1),
"avg_stops": round(avg_stops, 2),
"throughput": arrive_count,
"improvement": round(improvement, 1),
"per_junction": per_junction,
},
gps_df = gps_df,
phase_log = phase_log,
signal_events = signal_events,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _nearest_junction(lat, lon):
best, best_d = "J0", float("inf")
for jid, (jlat, jlon) in JUNCTION_COORDS.items():
d = (lat-jlat)**2 + (lon-jlon)**2
if d < best_d:
best, best_d = jid, d
return best
def _adaptive_action(jid, phase, qew, qns, step):
if phase in (1, 3):
return "hold_yellow"
if phase == 0 and qns > qew * 1.8 and qew < 3:
return "switch_to_ns"
if phase == 2 and qew > qns * 1.8 and qns < 3:
return "switch_to_ew"
return "hold"
def _build_obs(jid, phase, qew, qns, step, queues):
return np.array([
min(qew / 25.0, 1.0),
min(qns / 25.0, 1.0),
1.0 if phase == 0 else 0.0,
1.0 if phase == 2 else 0.0,
min((step % 60) / 60.0, 1.0),
0.5,
], dtype=np.float32)
# ---------------------------------------------------------------------------
# Public entry point
# ---------------------------------------------------------------------------
def run_simulation(
mode: str,
traffic_scale: float = 1.0,
accident_step: int = -1,
seed: int = 42,
baseline_delay: float | None = None,
ppo_model: Any | None = None,
progress_cb: Callable[[int, int], None] | None = None,
) -> SimResult:
"""
Run a simulation and return a SimResult.
If SUMO is installed: opens sumo-gui automatically, cars are visible
on your desktop while Streamlit shows metrics in the browser.
If SUMO is not installed: runs the mock path, full dashboard works.
Set env var SUMO_NO_GUI=1 to suppress the GUI window.
"""
if SUMO_AVAILABLE:
try:
return _sumo_result(
mode, traffic_scale, accident_step, seed,
baseline_delay, ppo_model, progress_cb,
)
except Exception:
pass # fall through to mock on any SUMO error
# Mock path — animate the progress bar so it feels live
if progress_cb:
for i in range(20):
progress_cb(i * (SIM_DURATION // 20), SIM_DURATION)
time.sleep(0.05)
progress_cb(SIM_DURATION, SIM_DURATION)
return _mock_result(mode, baseline_delay, traffic_scale, accident_step, seed) |