pytsc.common package
Submodules
pytsc.common module
pytsc.common module
pytsc.common module
pytsc.common module
pytsc.common module
pytsc.common module
pytsc.common module
pytsc.common module
pytsc.common module
pytsc.common module
pytsc.common module
pytsc.common module
pytsc.common.actions module
- class pytsc.common.actions.BaseActionSpace(config, traffic_signals)[source]
Bases:
abc.ABC
Base class for action spaces in traffic signal control. This class defines the interface for different action spaces and provides common functionality for action space management.
- Parameters
config (Config) – Configuration object containing simulation parameters.
traffic_signals (dict) – Dictionary of traffic signals in the network.
- abstract apply(actions)[source]
Apply the given actions to the traffic signals.
- Parameters
actions (list) – List of actions to be applied to the traffic signals.
- abstract get_mask()[source]
Get the action mask for each traffic signal.
- Returns
List of action masks for each traffic signal.
- Return type
list
- class pytsc.common.actions.CentralizedActionSpace(individual_action_space)[source]
Bases:
pytsc.common.actions.BaseActionSpace
In this child class, actions are the phase index to switch to for each traffic signal.
- Parameters
individual_action_space (BaseActionSpace) – The action space for each traffic signal.
- apply(action)[source]
Apply the given action to the traffic signals.
- Parameters
action (int) – The action to be applied to the traffic signals.
- decode_action(action)[source]
Decode the action into individual actions for each traffic signal.
- Parameters
action (int) – The action to be decoded.
- Returns
List of individual actions for each traffic signal.
- Return type
list
- class pytsc.common.actions.PhaseSelectionActionSpace(config, traffic_signals)[source]
Bases:
pytsc.common.actions.BaseActionSpace
In this child class, actions are the phase index to switch to for each traffic signal.
- Parameters
config (Config) – Configuration object containing simulation parameters.
traffic_signals (dict) – Dictionary of traffic signals in the network.
- apply(actions)[source]
Apply the given actions to the traffic signals.
- Parameters
actions (list) – List of actions to be applied to the traffic signals.
- class pytsc.common.actions.PhaseSwitchActionSpace(config, traffic_signals)[source]
Bases:
pytsc.common.actions.BaseActionSpace
In this child class, actions are binary: 0 for no switch (remain in the current phase) 1 for switch (move to the next phase in a round-robin manner).
- apply(actions)[source]
Apply the given actions to the traffic signals.
- Parameters
actions (list) – List of actions to be applied to the traffic signals.
- get_mask()[source]
Get the action mask for each traffic signal.
- Returns
List of action masks for each traffic signal.
- Return type
list
pytsc.common.config module
- class pytsc.common.config.BaseConfig(scenario, debug=False, **kwargs)[source]
Bases:
abc.ABC
Base class for configuration management in traffic signal control. This class handles loading and merging configuration files for different scenarios and simulator backends.
- Parameters
scenario (str) – Name of the scenario for which the configuration is being loaded.
debug (bool) – Flag to enable debug mode. If True, detailed logs will be printed.
**kwargs – Additional configuration parameters to override default values.
pytsc.common.metrics module
- class pytsc.common.metrics.BaseMetricsParser[source]
Bases:
abc.ABC
Base class for metrics parsers in traffic signal control. This class defines the interface for different metrics parsers and provides common functionality for metrics management.
- Parameters
parsed_network (ParsedNetwork) – Parsed network object containing network information.
pytsc.common.network_parser module
- class pytsc.common.network_parser.BaseNetworkParser(config)[source]
Bases:
abc.ABC
Base class for network parsers in traffic signal control. This class defines the interface for different network parsers and provides common functionality for network management.
- Parameters
config (Config) – Configuration object containing simulation parameters.
traffic_signal_ids (list) – List of traffic signal IDs in the network.
pytsc.common.observations module
- class pytsc.common.observations.BaseObservationSpace(config, parsed_network, traffic_signals, simulator_backend)[source]
Bases:
abc.ABC
Base class for observation spaces in traffic signal control. This class defines the interface for different observation spaces and provides common functionality for observation space management.
- Parameters
config (Config) – Configuration object containing simulation parameters.
parsed_network (ParsedNetwork) – Parsed network object containing network information.
traffic_signals (dict) – Dictionary of traffic signals in the network.
simulator_backend (str) – The simulator backend to be used (e.g., “cityflow”, “sumo”).
- class pytsc.common.observations.LaneFeatures(config, parsed_network, traffic_signals, simulator_backend)[source]
Bases:
pytsc.common.observations.BaseObservationSpace
Each incoming lane is divided into bins of v_size + min_gap (7.5) meters. Visibility of each traffic signal is in terms of # of bins, not meters. -1: no vehicle in bin 0: vehicle in bin, not moving >0: vehicle in bin, moving Observation:
position matrix of each incoming lane
one hot encoding of current phase
- get_observation_info()[source]
Get information about the observation space. :returns: Dictionary containing information about the observation space. :rtype: dict
- get_observations()[source]
Each observation includes the lane features of each incoming lane and the one-hot encoding of the current phase. :returns: List of observations for each traffic signal. :rtype: list
- get_per_agent_lane_features()[source]
Get lane features for each agent (traffic signal). Each agent’s lane features are padded to the maximum number of controlled lanes. :returns: List of lane features for each agent. :rtype: list
- get_size()[source]
Get the size of the observation space. :returns: Size of the observation space. :rtype: int
- get_state()[source]
Get the state for each traffic signal. Each state includes the lane features of each incoming lane and the one-hot encoding of the current phase. :returns: List of states for each traffic signal. :rtype: list
- get_state_size()[source]
Get the size of the state space. :returns: Size of the state space. :rtype: int
- max_lane_length = 500
- max_lane_speed = 15.0
- max_lanes_per_direction = 6
- max_n_controlled_lanes = 16
- max_phases = 20
- class pytsc.common.observations.PositionMatrix(config, parsed_network, traffic_signals, simulator_backend)[source]
Bases:
pytsc.common.observations.BaseObservationSpace
Each incoming lane is divided into bins of v_size + min_gap (7.5) meters. Visibility of each traffic signal is in terms of # of bins, not meters. -1: no vehicle in bin 0: vehicle in bin, not moving >0: vehicle in bin, moving Observation:
position matrix of each incoming lane
one hot encoding of current phase
- get_observation_info()[source]
Get information about the observation space. :returns: Dictionary containing information about the observation space. :rtype: dict
- get_observations()[source]
Get observations for each traffic signal. Each observation includes the position matrix of each incoming lane and the one-hot encoding of the current phase. :returns: List of observations for each traffic signal. :rtype: list
- get_per_agent_lane_features()[source]
Get lane features for each agent (traffic signal). Each agent’s lane features are padded to the maximum number of controlled lanes. :returns: List of lane features for each agent. :rtype: list
- get_size()[source]
Get the size of the observation space. :returns: Size of the observation space. :rtype: int
- get_state()[source]
Get the state for each traffic signal. Each state includes the position matrix of each incoming lane and the one-hot encoding of the current phase. :returns: List of states for each traffic signal. :rtype: list
- get_state_size()[source]
Get the size of the state space. :returns: Size of the state space. :rtype: int
- max_lane_length = 500
- max_lane_speed = 15.0
- max_lanes_per_direction = 6
- max_n_controlled_lanes = 16
- max_phases = 20
pytsc.common.retriever module
- class pytsc.common.retriever.BaseRetriever(simulator)[source]
Bases:
abc.ABC
Base class for data retrieval in traffic signal control. This class defines the interface for different data retrieval methods and provides common functionality for data management.
- Parameters
simulator (Simulator) – Simulator object containing simulation parameters and network information.
- abstract retrieve_lane_measurements()[source]
Retrieve lane measurements from the simulator.
- Returns
Dictionary containing lane measurements.
- Return type
dict
pytsc.common.reward module
- class pytsc.common.reward.BaseRewardFunction(metrics, traffic_signals)[source]
Bases:
abc.ABC
Base class for reward functions in traffic signal control. This class defines the interface for different reward functions and provides common functionality for reward management.
- Parameters
metrics (Metrics) – Metrics object containing simulation parameters.
traffic_signals (dict) – Dictionary of traffic signals in the network.
- class pytsc.common.reward.MaxPressure(metrics, traffic_signals)[source]
Bases:
pytsc.common.reward.BaseRewardFunction
Max Pressure reward function.
- Parameters
metrics (Metrics) – Metrics object containing simulation parameters.
traffic_signals (dict) – Dictionary of traffic signals in the network.
- class pytsc.common.reward.QueueLength(metrics, traffic_signals)[source]
Bases:
pytsc.common.reward.BaseRewardFunction
Queue Length reward function.
- Parameters
metrics (Metrics) – Metrics object containing simulation parameters.
traffic_signals (dict) – Dictionary of traffic signals in the network.
pytsc.common.simulator module
- class pytsc.common.simulator.BaseSimulator(parsed_network)[source]
Bases:
abc.ABC
Base class for traffic signal control simulators. This class defines the interface for different simulators and provides common functionality for simulation management.
- Parameters
parsed_network (ParsedNetwork) – Parsed network object containing network information.
- abstract close_simulator()[source]
Close the simulator.
- Returns
True if the simulator closed successfully, False otherwise.
- Return type
bool
- abstract is_terminated()[source]
Check if the simulation has terminated.
- Returns
True if the simulation has terminated, False otherwise.
- Return type
bool
- abstract retrieve_step_measurements()[source]
Retrieve measurements for the current simulation step.
- Returns
Dictionary containing measurements for the current step.
- Return type
dict
pytsc.common.traffic_signal module
- class pytsc.common.traffic_signal.BaseTSController(id, config, simulator)[source]
Bases:
abc.ABC
Base class for traffic signal controllers. :param id: Unique identifier for the traffic signal controller. :type id: str :param config: Configuration dictionary containing traffic signal parameters. :type config: dict :param simulator: Simulator object containing simulation parameters and network information. :type simulator: Simulator
- property current_phase
Get the current phase of the traffic signal controller.
- Returns
Current phase of the traffic signal controller.
- Return type
str
- property current_phase_index
Get the index of the current phase in the traffic signal controller.
- Returns
Index of the current phase in the traffic signal controller.
- Return type
int
- get_allowable_phase_switches()[source]
Get the allowable phase switches based on the current phase and time spent on it.
- Returns
List of allowable phase switches for the traffic signal controller.
- Return type
list
- property green_phase_indices
Get the green phase indices of the traffic signal controller.
- Returns
List of green phase indices in the traffic signal controller.
- Return type
list
- property n_phases
Get the number of phases in the traffic signal controller.
- Returns
Number of phases in the traffic signal controller.
- Return type
int
- property next_green_phase_index
Get the index of the next green phase in the traffic signal controller.
- Returns
Index of the next green phase in the traffic signal controller.
- Return type
int
- property next_phase_index
Get the index of the next phase in the traffic signal controller.
- Returns
Index of the next phase in the traffic signal controller.
- Return type
int
- property norm_time_on_phase
Get the normalized time spent on the current phase of the traffic signal controller.
- Returns
Normalized time spent on the current phase of the traffic signal controller.
- Return type
float
- property phase_indices
Get the phase indices of the traffic signal controller.
- Returns
List of phase indices in the traffic signal controller.
- Return type
list
- property phase_one_hot
Get the one-hot encoding of the current phase of the traffic signal controller.
- Returns
One-hot encoding of the current phase of the traffic signal controller.
- Return type
list
- property phases_min_max_times
Get the minimum and maximum times for each phase in the traffic signal controller.
- Returns
Dictionary containing minimum and maximum times for each phase.
- Return type
dict
- property time_on_phase
Get the time spent on the current phase of the traffic signal controller.
- Returns
Time spent on the current phase of the traffic signal controller.
- Return type
int
- property yellow_phase_indices
Get the yellow phase indices of the traffic signal controller.
- Returns
List of yellow phase indices in the traffic signal controller.
- Return type
list
- property yellow_time
Get the yellow time of the traffic signal controller.
- Returns
Yellow time of the traffic signal controller.
- Return type
int
- class pytsc.common.traffic_signal.BaseTSProgram(id, config, simulator)[source]
Bases:
abc.ABC
Base class for traffic signal programs. :param id: Unique identifier for the traffic signal program. :type id: str :param config: Configuration dictionary containing traffic signal parameters. :type config: dict :param simulator: Simulator object containing simulation parameters and network information. :type simulator: Simulator
- class pytsc.common.traffic_signal.BaseTrafficSignal(id, config, simulator)[source]
Bases:
abc.ABC
Base class for traffic signals in the network. :param id: Unique identifier for the traffic signal. :type id: str :param config: Configuration dictionary containing traffic signal parameters. :type config: dict :param simulator: Simulator object containing simulation parameters and network information. :type simulator: Simulator
- class pytsc.common.traffic_signal.TLSFreePhaseSelectLogic(controller)[source]
Bases:
object
phase_dict: {phase_idx: {“min_time”: int, “max_time”: int}}} NOTES: - The phase indices must be ordered such that the phase list starts with a green phase and every green phase must be followed by a yellow phase. - Step size is fixed to be 5 seconds.
- class pytsc.common.traffic_signal.TLSRoundRobinPhaseSelectLogic(controller)[source]
Bases:
pytsc.common.traffic_signal.TLSFreePhaseSelectLogic
Round robin phase selection logic for traffic signal controllers.
- Parameters
controller (BaseTSController) – Traffic signal controller object.
pytsc.common.trip_generator module
pytsc.common.utils module
- class pytsc.common.utils.EnvLogger[source]
Bases:
object
A simple logger class to log messages to the console. This class ensures that the logger is initialized only once and provides methods to log messages at different levels (info, warning, etc.).
- logger = None
- pytsc.common.utils.calculate_vehicle_bin_index(n_bins, lane_length, vehicle_position)[source]
Calculate the bin index for a vehicle based on its position in the lane. :param n_bins: Number of bins. :type n_bins: int :param lane_length: Length of the lane. :type lane_length: float :param vehicle_position: Position of the vehicle in the lane. :type vehicle_position: float
- Returns
Bin index for the vehicle.
- Return type
int
- pytsc.common.utils.flatten_list(list_of_lists)[source]
Flattens a list of lists into a single list.
- pytsc.common.utils.generate_weibull_flow_rates(shape, scale, max_rate, num_segments)[source]
Generate flow rates based on a Weibull distribution. :param shape: Shape parameter for the Weibull distribution. :type shape: float :param scale: Scale parameter for the Weibull distribution. :type scale: float :param max_rate: Maximum flow rate. :type max_rate: float :param num_segments: Number of segments to divide the flow rates into. :type num_segments: int
- Returns
Flow rates for each segment.
- Return type
np.ndarray
- pytsc.common.utils.pad_array(array, size, pad_value=0)[source]
Pads the given array with the specified padding value so that it has the specified size. If the array is already larger than the specified size, it is returned unmodified.
- pytsc.common.utils.pad_list(inp_list, size, pad_value=0)[source]
Pads the given list with the specified padding value so that it has the specified size. If the list is already larger than the specified size, it is returned unmodified.