Skip to content

Commit 786cd33

Browse files
authored
Merge pull request #41 from meta-pytorch/Sumo-RL
Sumo rl: Traffic Simulation Environment
2 parents f183392 + fdbf865 commit 786cd33

File tree

16 files changed

+1425
-0
lines changed

16 files changed

+1425
-0
lines changed

.github/workflows/docker-build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ jobs:
7373
dockerfile: src/envs/chat_env/server/Dockerfile
7474
- name: coding-env
7575
dockerfile: src/envs/coding_env/server/Dockerfile
76+
- name: sumo-rl-env
77+
dockerfile: src/envs/sumo_rl_env/server/Dockerfile
7678
- name: atari-env
7779
dockerfile: src/envs/atari_env/server/Dockerfile
7880

examples/sumo_rl_simple.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Simple example demonstrating SUMO-RL Environment usage.
4+
5+
This example shows how to:
6+
1. Connect to a SUMO traffic signal control environment
7+
2. Reset the environment
8+
3. Take actions (select traffic light phases)
9+
4. Process observations and rewards
10+
11+
Usage:
12+
# Option 1: Start the server manually
13+
python -m envs.sumo_rl_env.server.app
14+
# Then run: python examples/sumo_rl_simple.py
15+
16+
# Option 2: Use Docker
17+
docker run -p 8000:8000 sumo-rl-env:latest
18+
# Then run: python examples/sumo_rl_simple.py
19+
"""
20+
21+
import numpy as np
22+
23+
from envs.sumo_rl_env import SumoAction, SumoRLEnv
24+
25+
26+
def main():
27+
"""Run a simple SUMO traffic control episode."""
28+
# Connect to the SUMO environment server
29+
print("Connecting to SUMO-RL environment...")
30+
env = SumoRLEnv(base_url="http://localhost:8000")
31+
32+
try:
33+
# Reset the environment
34+
print("\nResetting environment...")
35+
result = env.reset()
36+
print(f"Observation shape: {result.observation.observation_shape}")
37+
print(f"Available actions: {result.observation.action_mask}")
38+
print(f"Number of green phases: {len(result.observation.action_mask)}")
39+
40+
# Get initial state
41+
state = env.state()
42+
print(f"\nSimulation configuration:")
43+
print(f" Network: {state.net_file}")
44+
print(f" Duration: {state.num_seconds} seconds")
45+
print(f" Delta time: {state.delta_time} seconds")
46+
print(f" Reward function: {state.reward_fn}")
47+
48+
# Run a few steps with random policy
49+
print("\nRunning traffic control with random policy...")
50+
episode_reward = 0
51+
steps = 0
52+
max_steps = 100
53+
54+
for step in range(max_steps):
55+
# Random policy: select random green phase
56+
action_id = np.random.choice(result.observation.action_mask)
57+
58+
# Take action
59+
result = env.step(SumoAction(phase_id=action_id))
60+
61+
episode_reward += result.reward or 0
62+
steps += 1
63+
64+
# Print progress every 10 steps
65+
if step % 10 == 0:
66+
state = env.state()
67+
print(
68+
f"Step {step:3d}: "
69+
f"phase={action_id}, "
70+
f"reward={result.reward:6.2f}, "
71+
f"vehicles={state.total_vehicles:3d}, "
72+
f"waiting={state.mean_waiting_time:6.2f}s, "
73+
f"speed={state.mean_speed:5.2f}m/s"
74+
)
75+
76+
if result.done:
77+
print(f"\nEpisode finished after {steps} steps!")
78+
break
79+
80+
# Final statistics
81+
print(f"\n{'='*60}")
82+
print(f"Episode Summary:")
83+
print(f" Total steps: {steps}")
84+
print(f" Total reward: {episode_reward:.2f}")
85+
print(f" Average reward: {episode_reward/steps:.2f}")
86+
87+
# Get final state
88+
state = env.state()
89+
print(f"\nFinal State:")
90+
print(f" Simulation time: {state.sim_time:.0f} seconds")
91+
print(f" Total vehicles: {state.total_vehicles}")
92+
print(f" Total waiting time: {state.total_waiting_time:.2f} seconds")
93+
print(f" Mean waiting time: {state.mean_waiting_time:.2f} seconds")
94+
print(f" Mean speed: {state.mean_speed:.2f} m/s")
95+
print(f"{'='*60}")
96+
97+
finally:
98+
# Cleanup
99+
print("\nClosing environment...")
100+
env.close()
101+
print("Done!")
102+
103+
104+
if __name__ == "__main__":
105+
main()

0 commit comments

Comments
 (0)