Spaces:
Running on Zero
Running on Zero
File size: 8,592 Bytes
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 | import traci
from state.state_engine import TrafficStateEngine
from state.state_history import TrafficStateHistory
from state.junction_config import JUNCTIONS
from state.corridor_state import CorridorStateEngine
from state.decision_interface import DecisionInterface
from control.adaptive_signal import AdaptiveSignalController
def main():
print("=" * 60)
print("TRAFFIC AI - CORRIDOR DECISION PIPELINE")
print("=" * 60)
engines = {}
histories = {}
# --------------------------------------------------
# Create state engines for J1, J2 and J3
# --------------------------------------------------
for junction_name, config in JUNCTIONS.items():
engines[junction_name] = TrafficStateEngine(
junction_id=config["junction_id"],
approaches=config["approaches"],
traffic_lights=config["traffic_lights"],
approach_length=200.0
)
histories[junction_name] = TrafficStateHistory(
max_length=60
)
# --------------------------------------------------
# Corridor state engine
# --------------------------------------------------
corridor_engine = CorridorStateEngine(
engines
)
# --------------------------------------------------
# Member 5 decision interface
# --------------------------------------------------
decision_interface = DecisionInterface()
# --------------------------------------------------
# Adaptive signal controller
# --------------------------------------------------
signal_controller = AdaptiveSignalController(
min_green=20,
max_green=60,
congestion_threshold=0.50,
extension_seconds=10
)
print()
print("Configured junctions:")
for junction_name, config in JUNCTIONS.items():
print(
f" {junction_name} -> "
f"{config['junction_id']}"
)
print()
print("Decision interface: READY")
print()
print("Phase-aware adaptive signal controller: READY")
print()
print("Starting SUMO...")
print()
# --------------------------------------------------
# Start SUMO
# --------------------------------------------------
traci.start([
"sumo",
"-c",
"simulation/configs/corridor.sumocfg"
])
try:
# --------------------------------------------------
# Main simulation loop
# --------------------------------------------------
for step in range(900):
traci.simulationStep()
# --------------------------------------------------
# Calculate corridor state
# --------------------------------------------------
corridor_state = (
corridor_engine.calculate_corridor_state(
traci
)
)
# --------------------------------------------------
# Store state history
# --------------------------------------------------
for junction_name, state in (
corridor_state["junctions"].items()
):
histories[junction_name].add_state(
state
)
# --------------------------------------------------
# Build Member 5 decision interface
# --------------------------------------------------
decision_state = (
decision_interface.build_corridor_decision(
corridor_state
)
)
# --------------------------------------------------
# Adaptive signal decisions
# --------------------------------------------------
signal_decisions = {}
for junction_name, state in (
corridor_state["junctions"].items()
):
# IMPORTANT:
# TraCI is passed to the controller so it can
# inspect the actual SUMO signal phase and
# controlled links.
decision = signal_controller.decide(
state,
traci
)
applied = (
signal_controller.apply_decision(
traci,
decision
)
)
decision["applied_to_sumo"] = applied
signal_decisions[junction_name] = (
decision
)
# --------------------------------------------------
# Print every 10 simulation steps
# --------------------------------------------------
if step % 10 == 0:
print()
print("-" * 60)
print(
f"SIMULATION STEP: {step}"
)
print("-" * 60)
print(
f"Corridor vehicles: "
f"{decision_state['total_vehicles']}"
)
print(
f"Worst junction: "
f"{decision_state['worst_junction']}"
)
print(
f"Highest congestion: "
f"{decision_state['highest_congestion_score']:.3f}"
)
print()
print("MEMBER 5 DECISION INTERFACE")
print(
f" Corridor ID: "
f"{decision_state['corridor_id']}"
)
print(
f" Junction count: "
f"{decision_state['junction_count']}"
)
for junction_name, state in (
decision_state["junctions"].items()
):
print()
print(
f" {junction_name} "
f"({state['junction_id']})"
)
print(
f" Vehicles: "
f"{state['vehicle_count']}"
)
print(
f" Congestion: "
f"{state['congestion_score']:.3f}"
)
print(
f" Worst approach: "
f"{state['most_congested_approach']}"
)
# --------------------------------------------------
# Signal decisions
# --------------------------------------------------
print()
print("PHASE-AWARE SIGNAL DECISIONS")
for junction_name, decision in (
signal_decisions.items()
):
print(
f" {junction_name}: "
f"{decision['action']} "
f"(applied="
f"{decision['applied_to_sumo']})"
)
print(
f" Current phase: "
f"{decision['current_phase']}"
)
print(
f" Target phase: "
f"{decision['target_phase']}"
)
print(
f" Congested approach: "
f"{decision['most_congested_approach']}"
)
print(
f" Serves congested approach: "
f"{decision['serves_congested_approach']}"
)
print(
f" Reason: "
f"{decision['reason']}"
)
# --------------------------------------------------
# State history
# --------------------------------------------------
print()
print("STATE HISTORY")
for junction_name, history in (
histories.items()
):
print(
f" {junction_name}: "
f"{history.size()} states stored"
)
finally:
traci.close()
print()
print("=" * 60)
print("SIMULATION FINISHED")
print("=" * 60)
if __name__ == "__main__":
main() |