Traffic Signal

class sumo_rl.environment.traffic_signal.TrafficSignal(env, ts_id: str, delta_time: int, yellow_time: int, min_green: int, max_green: int, begin_time: int, reward_fn: str | Callable, sumo)

This class represents a Traffic Signal controlling an intersection.

It is responsible for retrieving information and changing the traffic phase using the Traci API.

IMPORTANT: It assumes that the traffic phases defined in the .net file are of the form:

[green_phase, yellow_phase, green_phase, yellow_phase, …]

Currently it is not supporting all-red phases (but should be easy to implement it).

# Observation Space The default observation for each traffic signal agent is a vector:

obs = [phase_one_hot, min_green, lane_1_density,…,lane_n_density, lane_1_queue,…,lane_n_queue]

  • `phase_one_hot` is a one-hot encoded vector indicating the current active green phase

  • `min_green` is a binary variable indicating whether min_green seconds have already passed in the current phase

  • `lane_i_density` is the number of vehicles in incoming lane i dividided by the total capacity of the lane

  • `lane_i_queue` is the number of queued (speed below 0.1 m/s) vehicles in incoming lane i divided by the total capacity of the lane

You can change the observation space by implementing a custom observation class. See sumo_rl.environment.observations.ObservationFunction.

# Action Space Action space is discrete, corresponding to which green phase is going to be open for the next delta_time seconds.

# Reward Function The default reward function is ‘diff-waiting-time’. You can change the reward function by implementing a custom reward function and passing to the constructor of sumo_rl.environment.env.SumoEnvironment.

compute_observation()

Computes the observation of the traffic signal.

compute_reward()

Computes the reward of the traffic signal.

get_accumulated_waiting_time_per_lane() List[float]

Returns the accumulated waiting time per lane.

Returns:

List[float] – List of accumulated waiting time of each intersection lane.

get_average_speed() float

Returns the average speed normalized by the maximum allowed speed of the vehicles in the intersection.

Obs: If there are no vehicles in the intersection, it returns 1.0.

get_lanes_density() List[float]

Returns the density [0,1] of the vehicles in the incoming lanes of the intersection.

Obs: The density is computed as the number of vehicles divided by the number of vehicles that could fit in the lane.

get_lanes_queue() List[float]

Returns the queue [0,1] of the vehicles in the incoming lanes of the intersection.

Obs: The queue is computed as the number of vehicles halting divided by the number of vehicles that could fit in the lane.

get_out_lanes_density() List[float]

Returns the density of the vehicles in the outgoing lanes of the intersection.

get_pressure()

Returns the pressure (#veh leaving - #veh approaching) of the intersection.

get_total_queued() int

Returns the total number of vehicles halting in the intersection.

classmethod register_reward_fn(fn: Callable)

Registers a reward function.

Parameters:

fn (Callable) – The reward function to register.

set_next_phase(new_phase: int)

Sets what will be the next green phase and sets yellow phase if the next phase is different than the current.

Parameters:

new_phase (int) – Number between [0 … num_green_phases]

property time_to_act

Returns True if the traffic signal should act in the current step.

update()

Updates the traffic signal state.

If the traffic signal should act, it will set the next green phase and update the next action time.