Spaces:
Sleeping
Sleeping
File size: 3,647 Bytes
ec5ee81 | 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 | from __future__ import annotations
from dataclasses import dataclass
from typing import Mapping, Protocol
@dataclass(frozen=True)
class BaselineObservation:
simulation_time: float
current_phase_id: str
phase_elapsed_seconds: float
halting_by_approach: Mapping[str, int]
class BaselineController(Protocol):
label: str
def propose_phase(self, observation: BaselineObservation) -> str | None: ...
_NEXT_CLEARANCE_PHASE = {
"NS_YELLOW": "ALL_RED_TO_EW",
"ALL_RED_TO_EW": "EW_GREEN",
"EW_YELLOW": "ALL_RED_TO_NS",
"ALL_RED_TO_NS": "NS_GREEN",
}
_CLEARANCE_SECONDS = {
"NS_YELLOW": 2.0,
"ALL_RED_TO_EW": 1.0,
"EW_YELLOW": 2.0,
"ALL_RED_TO_NS": 1.0,
}
@dataclass(frozen=True)
class FixedTimeController:
"""A deterministic two-stage fixed-time policy; A1 remains the signal owner."""
green_seconds: float = 15.0
label: str = "fixed-time"
def __post_init__(self) -> None:
if self.green_seconds < 5.0:
raise ValueError("fixed green must satisfy the five-second safety minimum")
def propose_phase(self, observation: BaselineObservation) -> str | None:
phase = observation.current_phase_id
elapsed = observation.phase_elapsed_seconds
if phase == "NS_GREEN" and elapsed >= self.green_seconds:
return "NS_YELLOW"
if phase == "EW_GREEN" and elapsed >= self.green_seconds:
return "EW_YELLOW"
threshold = _CLEARANCE_SECONDS.get(phase)
if threshold is not None and elapsed >= threshold:
return _NEXT_CLEARANCE_PHASE[phase]
return None
@dataclass(frozen=True)
class ActuatedController:
"""Queue-actuated policy with bounded greens and deterministic gap-out behavior."""
minimum_green_seconds: float = 5.0
maximum_green_seconds: float = 25.0
switch_queue_advantage: int = 1
label: str = "actuated"
def __post_init__(self) -> None:
if self.minimum_green_seconds < 5.0:
raise ValueError("actuated minimum green must satisfy the safety plan")
if self.maximum_green_seconds < self.minimum_green_seconds:
raise ValueError("maximum green cannot precede minimum green")
if self.switch_queue_advantage < 0:
raise ValueError("queue advantage cannot be negative")
def propose_phase(self, observation: BaselineObservation) -> str | None:
phase = observation.current_phase_id
elapsed = observation.phase_elapsed_seconds
threshold = _CLEARANCE_SECONDS.get(phase)
if threshold is not None:
return _NEXT_CLEARANCE_PHASE[phase] if elapsed >= threshold else None
if phase not in {"NS_GREEN", "EW_GREEN"}:
raise ValueError(f"unsupported baseline phase: {phase}")
if elapsed < self.minimum_green_seconds:
return None
ns_queue = int(observation.halting_by_approach.get("north", 0)) + int(
observation.halting_by_approach.get("south", 0)
)
ew_queue = int(observation.halting_by_approach.get("east", 0)) + int(
observation.halting_by_approach.get("west", 0)
)
current_queue, opposing_queue = (
(ns_queue, ew_queue) if phase == "NS_GREEN" else (ew_queue, ns_queue)
)
gap_out = opposing_queue >= current_queue + self.switch_queue_advantage
max_out = elapsed >= self.maximum_green_seconds
if gap_out or max_out:
return "NS_YELLOW" if phase == "NS_GREEN" else "EW_YELLOW"
return None
|