File size: 2,245 Bytes
a871dae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
HTTP/WebSocket client for the Autonomous Traffic Control OpenEnv environment.

Extends openenv-core's EnvClient so it works seamlessly with all openenv-core
compatible RL training frameworks (TRL, TorchForge, Unsloth, ART, Oumi, etc.).

Usage (synchronous):
    from traffic_control_env import TrafficControlEnv, TrafficAction

    with TrafficControlEnv(base_url="http://localhost:8000").sync() as client:
        obs = client.reset(task_id="emergency_priority", seed=42)
        while not obs.done:
            action = TrafficAction(light_phase=0)
            obs = client.step(action)
        state = client.state()
        print(state.total_vehicles_passed)

Usage (async):
    import asyncio
    from traffic_control_env import TrafficControlEnv, TrafficAction

    async def main():
        async with TrafficControlEnv(base_url="http://localhost:8000") as client:
            obs = await client.reset(task_id="basic_flow", seed=0)
            while not obs.done:
                obs = await client.step(TrafficAction(light_phase=0))

    asyncio.run(main())
"""

from typing import Any, Dict
from openenv.core.env_client import EnvClient, StepResult

from models import TrafficAction, TrafficObservation, TrafficState


class TrafficControlEnv(EnvClient[TrafficAction, TrafficObservation, TrafficState]):
    """
    Client for the Autonomous Traffic Control environment.

    Inherits all openenv-core EnvClient functionality:
    - async context manager (async with TrafficControlEnv(...) as env: ...)
    - .sync() wrapper for synchronous use
    - reset() / step() / state()
    - from_docker_image() class method for local Docker deployment
    - from_env() class method for HuggingFace Space deployment
    """

    def _step_payload(self, action: TrafficAction) -> Dict[str, Any]:
        return action.model_dump()

    def _parse_result(self, payload: Dict[str, Any]) -> StepResult[TrafficObservation]:
        obs = TrafficObservation(**payload["observation"])
        return StepResult(
            observation=obs,
            reward=payload.get("reward"),
            done=payload.get("done", False)
        )

    def _parse_state(self, payload: Dict[str, Any]) -> TrafficState:
        return TrafficState(**payload)