arrow072/open_env_meta
0
1---2title: Traffic Signal Optimization โ OpenEnv Elite3emoji: ๐ฆ4colorFrom: blue5colorTo: green6sdk: docker7app_port: 78608pinned: false9---10 11# ๐ฅ Traffic Signal Optimization โ OpenEnv Elite12 13> **Meta ร PyTorch OpenEnv Hackathon Submission**14>15> A world-class Reinforcement Learning environment for urban traffic control, featuring stochastic multi-lane dynamics, emergency vehicle prioritization, and sophisticated fairness-driven rewards.16 17---18 19## ๐๏ธ Problem Statement20 21Fixed-cycle traffic signals are a relic of the past. In modern urban environments, they create **needless congestion**, increase **CO2 emissions**, and โ most critically โ cause **life-threatening delays** for emergency vehicles.22 23This project provides a high-fidelity 4-way intersection simulation designed for OpenEnv. It challenges RL agents to move beyond simple throughput and master the art of **dynamic balancing**: serving high-demand lanes while maintaining fairness for low-traffic directions and clearing "Golden Windows" for emergency responders.24 25---26 27## ๐ Quick Start28 29```bash30# Run the complete suite: Simulation + Sanity Checks + Comparison31python test_env.py32 33# Run a specific high-intensity scenario34python test_env.py hard35```36 37```python38from env import TrafficEnv39from tasks import get_config40from baseline_agent import RuleBasedAgent41 42# 1. Load a structured difficulty profile43config = get_config("medium")44env = TrafficEnv(config)45 46# 2. Initialize our sophisticated Rule-Based Controller47agent = RuleBasedAgent()48 49state = env.reset()50done = False51 52while not done:53 action = agent.select_action(state)54 state, reward, done, info = env.step(action)55 56print(f"Total Cleared: {info['total_cleared']}")57print(f"Fairness Index: {info['fairness_score']:.2f}")58```59 60---61 62## ๐ง Environment Design Philosophy63 64### State Space65The environment exposes a **14-dimensional** continuous observation vector, providing the agent with full situational awareness:66- **Queues (4)**: Exact vehicle count per lane [N, S, E, W].67- **Wait Pressure (4)**: Cumulative "impatience" score per lane.68- **Emergency Flags (4)**: Binary detection of EVs per lane.69- **Signal State (2)**: Current phase [0=NS, 1=EW] and step count.70 71### Action Space72- `0`: **Maintain** โ keep the current green phase.73- `1`: **Switch** โ transition the signal (includes yellow-phase discharge friction).74 75---76 77## ๐ Reward Engineering (The "Judge's Choice")78 79Our reward function is the core of this submission. It isn't just a count; it's a **multi-objective ethical framework** clipped to `[-1, 1]`:80 81| Component | Logic | Purpose |82| :--- | :--- | :--- |83| **Throughput (+)** | `+0.20 * cars_cleared` | Incentivizes active vehicle flow. |84| **Density (-)** | `-0.40 * total_congestion` | Penalizes letting the intersection fill up. |85| **Bottleneck (-)** | `-0.15 * max_queue` | Discourages extreme build-up in any single lane. |86| **Stability (-)** | `-switch_penalty` | Prevents "flickering" and promotes signal stability. |87| **Fairness (+/-)** | `+0.10` bonus / `-penalty` | Rewards balanced service; penalizes starvation. |88| **Emergency (๐จ)** | `Golden Window` Bonus | Massive reward for clearing EVs within target steps. |89| **EV Delay (-)** | `Exponential Penalty` | Punishes agents for delaying life-saving vehicles. |90 91---92 93## ๐ Evaluation Metrics94 95We track **8 key performance indicators** per episode to ensure a winning submission can be quantified:96 971. **Total Cleared**: Raw efficiency metric.982. **Avg Waiting Time**: The "commuter frustration" index.993. **Max Queue Length**: Gauges system robustness against bottlenecks.1004. **Signal Switch Count**: Measures policy stability.1015. **Congestion Score**: Final system state snapshot.1026. **Avg EV Clear Time**: Critical safety metric (lower is better).1037. **Fairness Score**: [0, 1] index โ how equally did we serve all lanes?1048. **Total EV Penalty**: Measures total failure to prioritize safety.105 106---107 108## โก Task Difficulty Levels109 110| Parameter | Easy | Medium | Hard |111| :--- | :--- | :--- | :--- |112| **Arrival Rate** | 0โ1 | 1โ3 | 2โ5 |113| **Discharge Rate** | 4โ5 | 3โ5 | 2โ4 |114| **Burst Frequency** | 0% | 10% | 20% |115| **Emergency Prob** | 1% | 5% | 15% |116| **EV Golden Window** | 8 steps | 5 steps | 3 steps |117| **Fairness Limit** | 20 steps | 15 steps | 10 steps |118 119---120 121## ๐ Emergency & Fairness Logic122 123### The "Golden Window"124When an Emergency Vehicle (EV) appears, the agent is granted a bonus if it switches and clears the lane within the **Golden Window** (defined per difficulty). Failing to do so triggers an **exponential delay penalty**, simulating the real-world cost of stopping an ambulance or fire truck.125 126### Fairness Guard127To prevent "Starvation" (where the agent ignores a low-traffic lane to optimize throughput on a high-traffic lane), a **Fairness Score** is calculated. If a lane remains red beyond the **Starvation Limit**, the agent suffers a heavy penalty. This forces the agent to learn the complex trade-off between total throughput and social fairness.128 129---130 131## ๐ถ Step Walkthrough132 133```text134Step 12: ๐จ Ambulance detected in East lane (currently RED).135 - EW Queue: 4, EV Timer: 0136 - Agent receives p_emergency penalty.137 138Step 13: Agent Action: 1 (SWITCH to EW).139 - Switch penalty applied (-0.20).140 - NS lanes stop; EW lanes turn GREEN.141 142Step 14: EV Cleared!143 - EV Clear Time: 2 steps.144 - Agent receives r_ev_bonus (+0.25) for "Golden Window" clearance.145 - Total cleared (+0.60 reward).146```147 148---149 150## ๐ฎ Future Improvements151 152- **Multi-Intersection Coordination**: Extending to a grid of agents using MARL.153- **Pedestrian Logic**: Adding crosswalks and pedestrian priority.154- **V2X Communication**: Providing agents with ahead-of-time traffic predictions.155 156---157 158## ๐ License159 160MIT ยฉ 2026 Meta x PyTorch OpenEnv Hackathon161 