def __init__(self, simulator, sim_params): """Instantiate a Flow kernel object. Parameters ---------- simulator : str simulator type, must be one of {"traci"} sim_params : flow.core.params.SimParams simulation-specific parameters Raises ------ ValueError if the specified input simulator is not a valid type """ self.kernel_api = None if simulator == "traci": self.simulation = TraCISimulation(self) self.scenario = TraCIScenario(self) self.vehicle = KernelVehicle(self, sim_params) self.traffic_light = TraCITrafficLight(self) else: raise ValueError('Simulator type "{}" is not valid.'. format(simulator))
def __init__(self, master_kernel, sim_params): """See parent class.""" KernelVehicle.__init__(self, master_kernel, sim_params) self.__ids = [] # ids of all vehicles self.__human_ids = [] # ids of human-driven vehicles self.__controlled_ids = [] # ids of flow-controlled vehicles self.__controlled_lc_ids = [] # ids of flow lc-controlled vehicles self.__rl_ids = [] # ids of rl-controlled vehicles self.__observed_ids = [] # ids of the observed vehicles # vehicles: Key = Vehicle ID, Value = Dictionary describing the vehicle # Ordered dictionary used to keep neural net inputs in order self.__vehicles = collections.OrderedDict() # create a sumo_observations variable that will carry all information # on the state of the vehicles for a given time step self.__sumo_obs = {} # total number of vehicles in the network self.num_vehicles = 0 # number of rl vehicles in the network self.num_rl_vehicles = 0 # contains the parameters associated with each type of vehicle self.type_parameters = {} # contain the minGap attribute of each type of vehicle self.minGap = {} # list of vehicle ids located in each edge in the network self._ids_by_edge = dict() # list of vehicle ids located in each lane in the network self._ids_by_lane = dict() # number of vehicles that entered the network for every time-step self._num_departed = [] self._departed_ids = [] # number of vehicles to exit the network for every time-step self._num_arrived = [] self._arrived_ids = [] self._arrived_rl_ids = [] # whether or not to automatically color vehicles try: self._color_by_speed = sim_params.color_by_speed self._force_color_update = sim_params.force_color_update except AttributeError: self._force_color_update = False # old speeds used to compute accelerations self.previous_speeds = {}
def __init__(self, master_kernel, sim_params): """See parent class. Copied from `flow.core.kernel.vehicle.traci`""" KernelVehicle.__init__(self, master_kernel, sim_params) self.__ids = [] # ids of all vehicles self.__human_ids = [] # ids of human-driven vehicles self.__controlled_ids = [] # ids of flow-controlled vehicles self.__controlled_lc_ids = [] # ids of flow lc-controlled vehicles self.__rl_ids = [] # ids of rl-controlled vehicles self.__observed_ids = [] # ids of the observed vehicles # vehicles: Key = Vehicle ID, Value = Dictionary describing the vehicle # Ordered dictionary used to keep neural net inputs in order self.__vehicles = collections.OrderedDict() # create a sumo_observations variable that will carry all information # on the state of the vehicles for a given time step self.__sumo_obs = {} # total number of vehicles in the network self.num_vehicles = 0 # number of rl vehicles in the network self.num_rl_vehicles = 0 # contains the parameters associated with each type of vehicle self.type_parameters = {} # list of vehicle ids located in each edge in the network self._ids_by_edge = dict() # number of vehicles that entered the network for every time-step self._num_departed = [] self._departed_ids = [] # number of vehicles to exit the network for every time-step self._num_arrived = [] self._arrived_ids = []
class Kernel(object): """Kernel for abstract function calling across traffic simulator APIs. The kernel contains four different subclasses for distinguishing between the various components of a traffic simulator. * simulation: controls starting, loading, saving, advancing, and resetting a simulation in Flow (see flow/core/kernel/simulation/base.py) * scenario: stores network-specific information (see flow/core/kernel/scenario/base.py) * vehicle: stores and regularly updates vehicle-specific information. At times, this class is optimized to efficiently collect information from the simulator (see flow/core/kernel/vehicle/base.py). * traffic_light: stores and regularly updates traffic light-specific information (see flow/core/kernel/traffic_light/base.py). The above kernel subclasses are designed specifically to support simulator-agnostic state information calling. For example, if you would like to collect the vehicle speed of a specific vehicle, then simply type: >>> k = Kernel(simulator="...") # a kernel for some simulator type >>> veh_id = "..." # some vehicle ID >>> speed = k.vehicle.get_speed(veh_id) In addition, these subclasses support sending commands to the simulator via its API. For example, in order to assign a specific vehicle a target acceleration, type: >>> k = Kernel(simulator="...") # a kernel for some simulator type >>> veh_id = "..." # some vehicle ID >>> k.vehicle.apply_acceleration(veh_id) These subclasses can be modified and recycled to support various different traffic simulators, e.g. SUMO, AIMSUN, TruckSim, etc... """ def __init__(self, simulator, sim_params): """Instantiate a Flow kernel object. Parameters ---------- simulator : str simulator type, must be one of {"traci"} sim_params : flow.core.params.SimParams simulation-specific parameters Raises ------ ValueError if the specified input simulator is not a valid type """ self.kernel_api = None if simulator == "traci": self.simulation = TraCISimulation(self) self.scenario = TraCIScenario(self) self.vehicle = KernelVehicle(self, sim_params) self.traffic_light = TraCITrafficLight(self) else: raise ValueError('Simulator type "{}" is not valid.'. format(simulator)) def pass_api(self, kernel_api): """Pass the kernel API to all kernel subclasses.""" self.kernel_api = kernel_api self.simulation.pass_api(kernel_api) self.scenario.pass_api(kernel_api) self.vehicle.pass_api(kernel_api) self.traffic_light.pass_api(kernel_api) def update(self, reset): """Update the kernel subclasses after a simulation step. This is meant to support optimizations in the performance of some simulators. For example, this step allows the vehicle subclass in the "traci" simulator uses the ``update`` method to collect and store subscription information. Parameters ---------- reset : bool specifies whether the simulator was reset in the last simulation step """ # self.scenario.update(reset) self.simulation.update(reset) # self.vehicle.update(reset) self.traffic_light.update(reset) def close(self): """Terminate all components within the simulation and scenario.""" self.scenario.close() self.simulation.close()