Spaces:
Paused
Paused
feat(model): add huggingface-ready model artifacts and card metadata
Browse files- README.md +29 -1
- artifacts/dqn_state_dict.pt +3 -0
- artifacts/export_model.py +34 -0
- artifacts/model_config.json +27 -0
- artifacts/model_metrics.json +7 -0
README.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# RL-Based Adaptive Traffic Intelligence System
|
| 2 |
|
| 3 |
A modular, production-style Reinforcement Learning project for adaptive traffic signal control.
|
|
@@ -9,6 +21,11 @@ Traffic control is treated as a sequential decision-making problem. An agent sel
|
|
| 9 |
- maximize throughput
|
| 10 |
- prioritize emergency vehicles
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
## Highlights
|
| 13 |
- OpenEnv-style deterministic environment (`TrafficEnv`)
|
| 14 |
- Dense multi-component reward (no sparse-only objective)
|
|
@@ -61,6 +78,17 @@ Use the project-local virtual environment.
|
|
| 61 |
.\.venv\Scripts\python.exe run_demo.py --episodes 70 --eval-episodes 20 --output-dir outputs
|
| 62 |
```
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
## Demo Pipeline
|
| 65 |
The demo performs:
|
| 66 |
1. fixed baseline simulation
|
|
@@ -76,7 +104,7 @@ Artifacts are saved to `outputs/`:
|
|
| 76 |
- `fixed_trajectory.png`
|
| 77 |
- `rl_trajectory.png`
|
| 78 |
|
| 79 |
-
## Measured Results (
|
| 80 |
From `outputs/metrics_summary.json`:
|
| 81 |
|
| 82 |
### Baseline vs RL
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: en
|
| 3 |
+
tags:
|
| 4 |
+
- reinforcement-learning
|
| 5 |
+
- dqn
|
| 6 |
+
- traffic-control
|
| 7 |
+
- pytorch
|
| 8 |
+
license: mit
|
| 9 |
+
library_name: pytorch
|
| 10 |
+
pipeline_tag: reinforcement-learning
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
# RL-Based Adaptive Traffic Intelligence System
|
| 14 |
|
| 15 |
A modular, production-style Reinforcement Learning project for adaptive traffic signal control.
|
|
|
|
| 21 |
- maximize throughput
|
| 22 |
- prioritize emergency vehicles
|
| 23 |
|
| 24 |
+
## Hugging Face Model Artifacts
|
| 25 |
+
- `artifacts/dqn_state_dict.pt` (trained DQN weights)
|
| 26 |
+
- `artifacts/model_config.json` (environment/training config snapshot)
|
| 27 |
+
- `artifacts/model_metrics.json` (evaluation snapshot)
|
| 28 |
+
|
| 29 |
## Highlights
|
| 30 |
- OpenEnv-style deterministic environment (`TrafficEnv`)
|
| 31 |
- Dense multi-component reward (no sparse-only objective)
|
|
|
|
| 78 |
.\.venv\Scripts\python.exe run_demo.py --episodes 70 --eval-episodes 20 --output-dir outputs
|
| 79 |
```
|
| 80 |
|
| 81 |
+
## Load Saved Weights
|
| 82 |
+
```python
|
| 83 |
+
import torch
|
| 84 |
+
from traffic_rl.agent.dqn_agent import DQNAgent
|
| 85 |
+
|
| 86 |
+
agent = DQNAgent(state_dim=10, action_dim=3)
|
| 87 |
+
state_dict = torch.load("artifacts/dqn_state_dict.pt", map_location="cpu")
|
| 88 |
+
agent.q_network.load_state_dict(state_dict)
|
| 89 |
+
agent.sync_target()
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
## Demo Pipeline
|
| 93 |
The demo performs:
|
| 94 |
1. fixed baseline simulation
|
|
|
|
| 104 |
- `fixed_trajectory.png`
|
| 105 |
- `rl_trajectory.png`
|
| 106 |
|
| 107 |
+
## Measured Results (Demo Run)
|
| 108 |
From `outputs/metrics_summary.json`:
|
| 109 |
|
| 110 |
### Baseline vs RL
|
artifacts/dqn_state_dict.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f720b22d1790faa32f0a80cbc6da7a1f67898ae81b9dca0d0e4864a20386c2c6
|
| 3 |
+
size 23401
|
artifacts/export_model.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
from traffic_rl.env.traffic_env import TrafficEnv
|
| 7 |
+
from traffic_rl.training.trainer import TrainingConfig, train_dqn
|
| 8 |
+
from traffic_rl.evaluation.evaluator import evaluate_agent
|
| 9 |
+
|
| 10 |
+
env_config = {
|
| 11 |
+
"max_steps": 120,
|
| 12 |
+
"arrival_mode": "stochastic",
|
| 13 |
+
"lane_bias": (1.6, 0.8, 1.4, 0.6),
|
| 14 |
+
"peak_rates": (3.8, 2.2, 3.4, 1.5),
|
| 15 |
+
"offpeak_rates": (1.4, 0.9, 1.2, 0.7),
|
| 16 |
+
"peak_duration": 35,
|
| 17 |
+
"cycle_duration": 60,
|
| 18 |
+
"service_rate": 2,
|
| 19 |
+
"ambulance_spawn_prob": 0.08,
|
| 20 |
+
"seed": 42,
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
cfg = TrainingConfig(episodes=80, max_steps=120, batch_size=64, target_sync_interval=10, epsilon_decay=0.97)
|
| 24 |
+
env = TrafficEnv(config=env_config)
|
| 25 |
+
agent, history = train_dqn(env, cfg)
|
| 26 |
+
|
| 27 |
+
metrics = evaluate_agent(agent=agent, env_config=env_config, episodes=20)
|
| 28 |
+
|
| 29 |
+
artifacts = Path("artifacts")
|
| 30 |
+
artifacts.mkdir(exist_ok=True)
|
| 31 |
+
torch.save(agent.q_network.state_dict(), artifacts / "dqn_state_dict.pt")
|
| 32 |
+
(artifacts / "model_metrics.json").write_text(json.dumps(metrics, indent=2), encoding="utf-8")
|
| 33 |
+
(artifacts / "model_config.json").write_text(json.dumps(env_config, indent=2), encoding="utf-8")
|
| 34 |
+
print(json.dumps({"metrics": metrics, "artifact_dir": str(artifacts)}, indent=2))
|
artifacts/model_config.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"max_steps": 120,
|
| 3 |
+
"arrival_mode": "stochastic",
|
| 4 |
+
"lane_bias": [
|
| 5 |
+
1.6,
|
| 6 |
+
0.8,
|
| 7 |
+
1.4,
|
| 8 |
+
0.6
|
| 9 |
+
],
|
| 10 |
+
"peak_rates": [
|
| 11 |
+
3.8,
|
| 12 |
+
2.2,
|
| 13 |
+
3.4,
|
| 14 |
+
1.5
|
| 15 |
+
],
|
| 16 |
+
"offpeak_rates": [
|
| 17 |
+
1.4,
|
| 18 |
+
0.9,
|
| 19 |
+
1.2,
|
| 20 |
+
0.7
|
| 21 |
+
],
|
| 22 |
+
"peak_duration": 35,
|
| 23 |
+
"cycle_duration": 60,
|
| 24 |
+
"service_rate": 2,
|
| 25 |
+
"ambulance_spawn_prob": 0.08,
|
| 26 |
+
"seed": 42
|
| 27 |
+
}
|
artifacts/model_metrics.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"reward": -1061689.7,
|
| 3 |
+
"avg_queue_length": 534.1583333333333,
|
| 4 |
+
"avg_waiting_time": 22575.933333333334,
|
| 5 |
+
"throughput": 240.0,
|
| 6 |
+
"ambulance_clearances": 0.0
|
| 7 |
+
}
|