-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathobservations.py
50 lines (37 loc) · 1.65 KB
/
observations.py
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
"""Observation functions for traffic signals."""
from abc import abstractmethod
import numpy as np
from gymnasium import spaces
from .traffic_signal import TrafficSignal
class ObservationFunction:
"""Abstract base class for observation functions."""
def __init__(self, ts: TrafficSignal):
"""Initialize observation function."""
self.ts = ts
@abstractmethod
def __call__(self):
"""Subclasses must override this method."""
pass
@abstractmethod
def observation_space(self):
"""Subclasses must override this method."""
pass
class DefaultObservationFunction(ObservationFunction):
"""Default observation function for traffic signals."""
def __init__(self, ts: TrafficSignal):
"""Initialize default observation function."""
super().__init__(ts)
def __call__(self) -> np.ndarray:
"""Return the default observation."""
phase_id = [1 if self.ts.green_phase == i else 0 for i in range(self.ts.num_green_phases)] # one-hot encoding
min_green = [0 if self.ts.time_since_last_phase_change < self.ts.min_green + self.ts.yellow_time else 1]
density = self.ts.get_lanes_density()
queue = self.ts.get_lanes_queue()
observation = np.array(phase_id + min_green + density + queue, dtype=np.float32)
return observation
def observation_space(self) -> spaces.Box:
"""Return the observation space."""
return spaces.Box(
low=np.zeros(self.ts.num_green_phases + 1 + 2 * len(self.ts.lanes), dtype=np.float32),
high=np.ones(self.ts.num_green_phases + 1 + 2 * len(self.ts.lanes), dtype=np.float32),
)