Downloads · 30 days
0
gourxv/trafficenv
trafficenv is a machine learning model from gourxv. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
OpenEnv-compatible reinforcement learning environment for traffic signal control in Indian-style unregulated intersections, including non-compliance, obstructions, emergency windows, and congestion propagation.
Downloads · 30 days
0
Access
Public
Updated Apr 10, 2026
Repo size
—
Likes
0
Public
Click a slice to open those files.
.lock576 KB · 91%
From the Hugging Face model README
OpenEnv-compatible reinforcement learning environment for traffic signal control in Indian-style unregulated intersections, including non-compliance, obstructions, emergency windows, and congestion propagation.
[0, 1] for stable RL training loopseasy, medium, hard) with increasing realism and difficultyinference.py) with strict [START]/[STEP]/[END] stdout contractopenenv-core[core]>=0.2.2pyproject.tomlopenenv-base)traffic_env/
├── __init__.py # Package exports (TrafficEnv, grading helpers, models)
├── client.py # EnvClient wrapper for remote/container interaction
├── models.py # Action/observation/reward/grading schemas
├── inference.py # Inference runner with logging contract
├── server/
│ ├── app.py # FastAPI app wiring
│ ├── traffic_env.py # Environment dynamics implementation
│ ├── __init__.py
│ └── requirements.txt
├── openenv.yaml # OpenEnv metadata (app entrypoint, port)
├── pyproject.toml # Package metadata and script entrypoint
├── Dockerfile
└── README.md
>=3.10uv (recommended) or pipinference.py:
HF_TOKEN or API_KEYAPI_BASE_URLMODEL_NAMEgit clone <your-repo-url>
cd traffic_env
Using uv:
uv sync
Using pip:
python -m venv .venv
# Linux/macOS
source .venv/bin/activate
# Windows PowerShell
# .venv\Scripts\Activate.ps1
pip install -U pip
pip install -e .
python -c "from traffic_env import TrafficEnv; print(TrafficEnv.__name__)"
Expected output:
TrafficEnv
from traffic_env import TrafficEnv
from traffic_env.server.traffic_env import heuristic_policy
env = TrafficEnv(task_id="medium", seed=42)
obs = env.reset(seed=42, task_id="medium")
for _ in range(100):
action = heuristic_policy(obs)
obs = env.step(action)
if obs.done:
break
print("Final score:", obs.metadata.get("final_score"))
cd traffic_env
uv sync
uv run uvicorn server.app:app --host 0.0.0.0 --port 8000
Open docs:
http://127.0.0.1:8000/docsAfter editable install:
server
This calls the entrypoint from pyproject.toml:
traffic_env.server.app:maininference.py supports four environment modes:
local (default): direct TrafficEnv(...)server: connects to TRAFFIC_SERVER_URLdocker: from_docker_image(IMAGE_NAME)openenv: from_env(OPENENV_REPO_ID, use_docker=...)# Linux/macOS
export TRAFFIC_ENV_MODE=local
export USE_LLM_POLICY=false
export TRAFFIC_TASK=medium
export MAX_STEPS=10
python inference.py
# Windows PowerShell
# $env:TRAFFIC_ENV_MODE='local'
# $env:USE_LLM_POLICY='false'
# $env:TRAFFIC_TASK='medium'
# $env:MAX_STEPS='10'
# python inference.py
# Linux/macOS
export TRAFFIC_ENV_MODE=local
export USE_LLM_POLICY=true
export HF_TOKEN=<token>
export API_BASE_URL=https://router.huggingface.co/v1
export MODEL_NAME=Qwen/Qwen2.5-72B-Instruct
python inference.py
inference.py)[START] task=<task_name> env=<benchmark> model=<model_name>
[STEP] step=<n> action=<action_json> reward=<0.00> done=<true|false> error=<msg|null>
[END] success=<true|false> steps=<n> score=<score> rewards=<r1,r2,...,rn>
reset(seed=None, task_id=None) -> initial observationstep(action) -> next observation (reward, done included)state property -> full serializable internal Markov stateEach observation includes:
density in [0, 1]queue_length (int)avg_wait_time (float)vehicle_mix (bikes, cars, trucks, sum = 1)obstruction (bool)current_phase: NS_GREEN | EW_GREEN | ALL_STOPphase_duration_remainingcompliance_ratetime_of_day (0-24)road_type: arterial | local | highwaystep_countepisode_rewardtask_idreward_breakdownIndiaUnregulatedIntersectionTrafficControllerAction supports:
actions: List[IntersectionSignalAction]
intersection_idphasegreen_duration in [10, 120]priority_override (bool)For single-intersection control, a single action entry is sufficient. For multi-intersection control, provide one action per intersection each step.
2.5x3x (time-windowed)1.4x1.0x0.6xraw_reward = (
-0.40 * normalized_wait
-0.30 * normalized_queue
-0.20 * conflict_risk
+0.10 * throughput
)
reward = clip(raw_reward + 1.0, 0.0, 1.0)
Reward is clipped to [0, 1] every step.
easy
N/Smedium
N/S/E/W~15%)hard
Episode length: 100 steps.
grade_task(policy, task_id, seed=7)grade_all_tasks(policy, seed=7)Pass thresholds:
>= 0.60>= 0.50>= 0.40When server is running (server.app:app):
GET / -> redirects to /docsGET /favicon.ico -> 204 (no content)GET /docs -> Swagger UIGET /openapi.jsonGET /healthGET /metadataGET /schemaGET /statePOST /resetPOST /stepWS /wsinference.py)| Variable | Required | Default | Description |
|---|---|---|---|
TRAFFIC_ENV_MODE | No | local | local, server, docker, or openenv |
TRAFFIC_TASK | No | medium | Task id: easy, medium, hard |
TRAFFIC_SEED | No | 42 | Random seed |
MAX_STEPS | No | 100 | Inference horizon |
USE_LLM_POLICY | No | true | If false, always heuristic |
API_BASE_URL | No | https://router.huggingface.co/v1 | OpenAI-compatible endpoint |
MODEL_NAME | No | Qwen/Qwen2.5-72B-Instruct | Model name for chat completions |
HF_TOKEN / API_KEY | If USE_LLM_POLICY=true | - | API credential |
TRAFFIC_SERVER_URL | For server mode | http://127.0.0.1:8000 | Remote env server URL |
IMAGE_NAME | For docker mode | - | Docker image for from_docker_image |
OPENENV_REPO_ID | For openenv mode | benchmark name | Repo id used by from_env |
OPENENV_USE_DOCKER | For openenv mode | true | Whether from_env uses docker |
TEMPERATURE | No | 0.1 | LLM temperature |
MAX_TOKENS | No | 260 | Max output tokens |
Defined in openenv.yaml:
app: server.app:appport: 8000runtime: fastapiBuild image:
docker build -t traffic_env:latest .
Run container:
docker run --rm -p 8000:8000 traffic_env:latest
Then open:
http://127.0.0.1:8000/docsopenenv validate
openenv build
GET / returns 404Current server behavior should redirect / to /docs. If you still see 404, ensure you are running the latest code.
GET /favicon.ico returns 404Current server returns 204 for /favicon.ico. If you still get 404, restart server and clear old process.
NameError: clea is not definedThis was caused by stale image/code. Rebuild and rerun:
docker build --no-cache -t traffic_env:latest .
docker run --rm -p 8000:8000 traffic_env:latest
Check:
USE_LLM_POLICY=trueHF_TOKEN or API_KEY is setAPI_BASE_URL and MODEL_NAME are validtraffic_envInstall editable package:
pip install -e .
or use uv sync from project root.