Spaces:
Running on Zero
Running on Zero
File size: 11,190 Bytes
eb1ca88 f234112 | 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 | try:
import traci
except ImportError:
traci = None
class SUMOPhaseAdapter:
"""
Converts SUMO traffic-light phases into a generic
representation that can be used by the decision engine.
SUMO-specific link indices are kept here instead of
being placed inside the generic topology classes.
"""
def __init__(self, traci_connection=None):
self.traci = traci_connection or traci
# ============================================================
# GET ALL SIGNAL PROGRAMS
# ============================================================
def get_programs(self, junction_id):
"""
Return all signal programs available at a junction.
"""
programs = (
self.traci.trafficlight.getAllProgramLogics(
junction_id
)
)
result = []
for program in programs:
program_data = {
"program_id": program.programID,
"type": program.type,
"current_phase": (
self.traci.trafficlight.getPhase(
junction_id
)
),
"phases": []
}
for phase_index, phase in enumerate(
program.phases
):
program_data["phases"].append({
"phase_index": phase_index,
"duration": phase.duration,
"min_duration": phase.minDur,
"max_duration": phase.maxDur,
"state": phase.state
})
result.append(program_data)
return result
# ============================================================
# GET CURRENT PROGRAM
# ============================================================
def get_current_program(self, junction_id):
"""
Get the currently active SUMO signal program.
"""
program_id = (
self.traci.trafficlight.getProgram(
junction_id
)
)
programs = self.get_programs(junction_id)
for program in programs:
if program["program_id"] == program_id:
return program
return None
# ============================================================
# GET CURRENT PHASE
# ============================================================
def get_current_phase(self, junction_id):
"""
Return the current phase index.
"""
return self.traci.trafficlight.getPhase(
junction_id
)
# ============================================================
# GET PHASE STATE
# ============================================================
def get_phase_state(
self,
junction_id,
phase_index
):
"""
Return the signal state string for a phase.
Example:
GGggrrrrGGggrrrr
"""
program = self.get_current_program(
junction_id
)
if program is None:
return None
for phase in program["phases"]:
if phase["phase_index"] == phase_index:
return phase["state"]
return None
# ============================================================
# GET GREEN LINKS
# ============================================================
def get_green_links(
self,
junction_id,
phase_index
):
"""
Return link indices that are green during
the specified phase.
SUMO signal characters:
G = green
g = permissive green
y = yellow
r = red
Both G and g are considered active green.
"""
state = self.get_phase_state(
junction_id,
phase_index
)
if state is None:
return []
green_links = []
for link_index, signal in enumerate(state):
if signal in ("G", "g"):
green_links.append(
link_index
)
return green_links
# ============================================================
# GET PHASE TYPE
# ============================================================
def get_phase_type(
self,
junction_id,
phase_index
):
"""
Classify a phase as:
green
yellow
red
mixed
"""
state = self.get_phase_state(
junction_id,
phase_index
)
if state is None:
return "unknown"
has_green = any(
signal in ("G", "g")
for signal in state
)
has_yellow = any(
signal == "y"
for signal in state
)
has_red = any(
signal == "r"
for signal in state
)
if has_green:
return "green"
if has_yellow:
return "yellow"
if has_red:
return "red"
return "mixed"
# ============================================================
# GET DECISION PHASES
# ============================================================
def get_decision_phases(self, junction_id):
"""
Return phases that can be considered normal
green decision phases.
Yellow phases are deliberately excluded.
"""
program = self.get_current_program(
junction_id
)
if program is None:
return []
decision_phases = []
for phase in program["phases"]:
phase_index = phase["phase_index"]
phase_type = self.get_phase_type(
junction_id,
phase_index
)
if phase_type == "green":
decision_phases.append({
"phase_index": phase_index,
"duration": phase["duration"],
"state": phase["state"],
"green_links": (
self.get_green_links(
junction_id,
phase_index
)
)
})
return decision_phases
# ============================================================
# CONVERT PHASE TO MOVEMENTS
# ============================================================
def phase_to_movements(
self,
junction_id,
phase_index,
movement_metadata
):
"""
Convert SUMO green link indices into movement IDs.
movement_metadata should be the metadata produced
by SUMOTopologyAdapter.
Example:
link 0
↓
A0_M0
link 1
↓
A0_M1
"""
green_links = self.get_green_links(
junction_id,
phase_index
)
movements = []
for movement_id, metadata in (
movement_metadata.items()
):
if (
metadata["junction_id"] == junction_id
and metadata["link_index"]
in green_links
):
movements.append(
movement_id
)
return movements
# ============================================================
# BUILD PHASE REPRESENTATION
# ============================================================
def build_phase_representation(
self,
junction_id,
movement_metadata
):
"""
Build a generic representation of all usable
signal phases at a junction.
"""
decision_phases = (
self.get_decision_phases(
junction_id
)
)
phases = []
for phase in decision_phases:
phase_index = phase[
"phase_index"
]
movement_ids = (
self.phase_to_movements(
junction_id,
phase_index,
movement_metadata
)
)
phases.append({
"phase_id": (
f"{junction_id}_P"
f"{phase_index}"
),
"junction_id": junction_id,
"phase_index": phase_index,
"duration": phase["duration"],
"state": phase["state"],
"green_links": phase[
"green_links"
],
"movements": movement_ids
})
return phases
# ============================================================
# PRINT DEBUG INFORMATION
# ============================================================
def print_junction_phases(
self,
junction_id,
movement_metadata
):
"""
Print a readable representation for debugging.
"""
print()
print("=" * 70)
print(
f"PHASES FOR JUNCTION: {junction_id}"
)
print("=" * 70)
phases = (
self.build_phase_representation(
junction_id,
movement_metadata
)
)
for phase in phases:
print()
print(
f"Phase: {phase['phase_id']}"
)
print(
f" SUMO index: "
f"{phase['phase_index']}"
)
print(
f" Duration: "
f"{phase['duration']} seconds"
)
print(
f" State: "
f"{phase['state']}"
)
print(
f" Green links: "
f"{phase['green_links']}"
)
print(
f" Movements: "
f"{phase['movements']}"
)
# ================================================================
# TEST
# ================================================================
if __name__ == "__main__":
SUMO_CMD = [
"sumo",
"-c",
"simulation/configs/corridor.sumocfg"
]
try:
traci.start(SUMO_CMD)
from integration.sumo_topology_adapter import (
SUMOTopologyAdapter
)
topology_adapter = (
SUMOTopologyAdapter()
)
phase_adapter = (
SUMOPhaseAdapter()
)
# Discover topology first.
topologies = (
topology_adapter
.discover_all_junctions()
)
# Display phases for the main
# corridor junctions.
for junction_id in [
"A0",
"B0",
"C0"
]:
phase_adapter.print_junction_phases(
junction_id,
topology_adapter.sumo_metadata
)
finally:
traci.close() |