def test_init(self): # Initialize a pyglet renderer self.renderer = Renderer(self.network, mode=self.mode, save_render=self.save_render, sight_radius=self.sight_radius, pxpm=self.pxpm, show_radius=self.show_radius, alpha=self.alpha) # Ensure that the attributes match their correct values self.assertEqual(self.renderer.mode, self.mode) self.assertEqual(self.renderer.save_render, self.save_render) self.assertEqual(self.renderer.sight_radius, self.sight_radius) self.assertEqual(self.renderer.pxpm, self.pxpm) self.assertEqual(self.renderer.show_radius, self.show_radius) self.assertEqual(self.renderer.alpha, self.alpha)
def test_render_drgb(self): # Initialize a pyglet renderer self.renderer = Renderer(self.network, mode=self.mode, save_render=self.save_render, sight_radius=self.sight_radius, pxpm=self.pxpm, show_radius=self.show_radius, alpha=self.alpha) _human_orientations, _machine_orientations, \ _human_dynamics, _machine_dynamics, \ _human_logs, _machine_logs = self.data[100] frame = self.renderer.render(_human_orientations, _machine_orientations, _human_dynamics, _machine_dynamics, _human_logs, _machine_logs) self.assertEqual(self.renderer.mode, 'drgb') self.assertEqual(frame.shape, (378, 378, 3))
def test_close(self): # Initialize a pyglet renderer self.renderer = Renderer(self.network, mode=self.mode, save_render=self.save_render, sight_radius=self.sight_radius, pxpm=self.pxpm, show_radius=self.show_radius, alpha=self.alpha) self.renderer.close() _human_orientations, _machine_orientations, \ _human_dynamics, _machine_dynamics, \ _human_logs, _machine_logs = self.data[1] self.assertRaises(ctypes.ArgumentError, self.renderer.render, _human_orientations, _machine_orientations, _human_dynamics, _machine_dynamics, _human_logs, _machine_logs)
def test_get_sight(self): # Initialize a pyglet renderer self.renderer = Renderer(self.network, mode=self.mode, save_render=self.save_render, sight_radius=self.sight_radius, pxpm=self.pxpm, show_radius=self.show_radius, alpha=self.alpha) _human_orientations, _machine_orientations, \ _human_dynamics, _machine_dynamics, \ _human_logs, _machine_logs = self.data[101] self.renderer.render(_human_orientations, _machine_orientations, _human_dynamics, _machine_dynamics, _human_logs, _machine_logs) orientation = self.data[101][0][0] id = self.data[101][4][0][-1] sight = self.renderer.get_sight(orientation, id) self.assertEqual(sight.shape, (150, 150, 3))
def test_save_renderer(self): self.save_render = True # Initialize a pyglet renderer self.renderer = Renderer(self.network, mode=self.mode, save_render=self.save_render, path='/tmp', sight_radius=self.sight_radius, pxpm=self.pxpm, show_radius=self.show_radius, alpha=self.alpha) _human_orientations, _machine_orientations, \ _human_dynamics, _machine_dynamics, \ _human_logs, _machine_logs = self.data[101] self.renderer.render(_human_orientations, _machine_orientations, _human_dynamics, _machine_dynamics, _human_logs, _machine_logs) save_path = self.renderer.close() saved_data = np.load(save_path, allow_pickle=True) self.assertEqual(self.data[0], saved_data[0]) self.assertEqual(self.data[101], saved_data[1])
def __init__(self, env_params, sim_params, scenario, simulator='traci'): """Initialize the environment class. Parameters ---------- env_params : flow.core.params.EnvParams see flow/core/params.py sim_params : flow.core.params.SimParams see flow/core/params.py scenario : flow.scenarios.Scenario see flow/scenarios/base_scenario.py simulator : str the simulator used, one of {'traci', 'aimsun'}. Defaults to 'traci' Raises ------ flow.utils.exceptions.FatalFlowError if the render mode is not set to a valid value """ # Invoke serializable if using rllab if serializable_flag: Serializable.quick_init(self, locals()) self.env_params = env_params self.scenario = scenario self.net_params = scenario.net_params self.initial_config = scenario.initial_config self.sim_params = sim_params time_stamp = ''.join(str(time.time()).split('.')) if os.environ.get("TEST_FLAG", 0): # 1.0 works with stress_test_start 10k times time.sleep(1.0 * int(time_stamp[-6:]) / 1e6) # FIXME: this is sumo-specific self.sim_params.port = sumolib.miscutils.getFreeSocketPort() # time_counter: number of steps taken since the start of a rollout self.time_counter = 0 # step_counter: number of total steps taken self.step_counter = 0 # initial_state: self.initial_state = {} self.state = None self.obs_var_labels = [] # simulation step size self.sim_step = sim_params.sim_step # the simulator used by this environment self.simulator = simulator # create the Flow kernel self.k = Kernel(simulator=self.simulator, sim_params=sim_params) # use the scenario class's network parameters to generate the necessary # scenario components within the scenario kernel self.k.scenario.generate_network(scenario) # initial the vehicles kernel using the VehicleParams object self.k.vehicle.initialize(deepcopy(scenario.vehicles)) # initialize the simulation using the simulation kernel. This will use # the scenario kernel as an input in order to determine what network # needs to be simulated. kernel_api = self.k.simulation.start_simulation( scenario=self.k.scenario, sim_params=sim_params) # pass the kernel api to the kernel and it's subclasses self.k.pass_api(kernel_api) # the available_routes variable contains a dictionary of routes # vehicles can traverse; to be used when routes need to be chosen # dynamically self.available_routes = self.k.scenario.rts # store the initial vehicle ids self.initial_ids = deepcopy(scenario.vehicles.ids) # store the initial state of the vehicles kernel (needed for restarting # the simulation) self.k.vehicle.kernel_api = None self.k.vehicle.master_kernel = None self.initial_vehicles = deepcopy(self.k.vehicle) self.k.vehicle.kernel_api = self.k.kernel_api self.k.vehicle.master_kernel = self.k self.setup_initial_state() # use pyglet to render the simulation if self.sim_params.render in ['gray', 'dgray', 'rgb', 'drgb']: save_render = self.sim_params.save_render sight_radius = self.sim_params.sight_radius pxpm = self.sim_params.pxpm show_radius = self.sim_params.show_radius # get network polygons network = [] # FIXME: add to scenario kernel instead of hack for lane_id in self.k.kernel_api.lane.getIDList(): _lane_poly = self.k.kernel_api.lane.getShape(lane_id) lane_poly = [i for pt in _lane_poly for i in pt] network.append(lane_poly) # instantiate a pyglet renderer self.renderer = Renderer(network, self.sim_params.render, save_render, sight_radius=sight_radius, pxpm=pxpm, show_radius=show_radius) # render a frame self.render(reset=True) elif self.sim_params.render in [True, False]: pass # default to sumo-gui (if True) or sumo (if False) else: raise FatalFlowError('Mode %s is not supported!' % self.sim_params.render) atexit.register(self.terminate)
def __init__(self, env_params, sim_params, scenario): # Invoke serializable if using rllab if serializable_flag: Serializable.quick_init(self, locals()) self.env_params = env_params self.scenario = scenario self.sim_params = sim_params time_stamp = ''.join(str(time.time()).split('.')) if os.environ.get("TEST_FLAG", 0): # 1.0 works with stress_test_start 10k times time.sleep(1.0 * int(time_stamp[-6:]) / 1e6) # FIXME: this is sumo-specific self.sim_params.port = sumolib.miscutils.getFreeSocketPort() self.vehicles = scenario.vehicles self.traffic_lights = scenario.traffic_lights # time_counter: number of steps taken since the start of a rollout self.time_counter = 0 # step_counter: number of total steps taken self.step_counter = 0 # initial_state: # Key = Vehicle ID, # Entry = (type_id, route_id, lane_index, lane_pos, speed, pos) self.initial_state = {} self.state = None self.obs_var_labels = [] # simulation step size self.sim_step = sim_params.sim_step # TraCI connection used to communicate with sumo self.traci_connection = None # dictionary of initial observations used while resetting vehicles # after each rollout self.initial_observations = dict.fromkeys(self.vehicles.get_ids()) # store the initial vehicle ids self.initial_ids = deepcopy(self.vehicles.get_ids()) # store the initial state of the vehicles class (for restarting sumo) self.initial_vehicles = deepcopy(self.vehicles) # colors used to distinguish between types of vehicles in the network self.colors = {} # the simulator used by this environment self.simulator = 'traci' # create the Flow kernel self.k = Kernel(simulator=self.simulator, sim_params=sim_params) # use the scenario class's network parameters to generate the necessary # scenario components within the scenario kernel self.k.scenario.generate_network(scenario) # initialize the simulation using the simulation kernel. This will use # the scenario kernel as an input in order to determine what network # needs to be simulated. self.traci_connection = self.k.simulation.start_simulation( scenario=self.k.scenario, sim_params=sim_params) # pass the kernel api to the kernel and it's subclasses self.k.pass_api(self.traci_connection) # the available_routes variable contains a dictionary of routes # vehicles can traverse; to be used when routes need to be chosen # dynamically self.available_routes = self.k.scenario.rts self.setup_initial_state() # use pyglet to render the simulation if self.sim_params.render in ['gray', 'dgray', 'rgb', 'drgb']: save_render = self.sim_params.save_render sight_radius = self.sim_params.sight_radius pxpm = self.sim_params.pxpm show_radius = self.sim_params.show_radius # get network polygons network = [] for lane_id in self.traci_connection.lane.getIDList(): _lane_poly = self.traci_connection.lane.getShape(lane_id) lane_poly = [i for pt in _lane_poly for i in pt] network.append(lane_poly) # instantiate a pyglet renderer self.renderer = Renderer( network, self.sim_params.render, save_render, sight_radius=sight_radius, pxpm=pxpm, show_radius=show_radius) # render a frame self.render(reset=True) elif self.sim_params.render in [True, False]: pass # default to sumo-gui (if True) or sumo (if False) else: raise ValueError("Mode %s is not supported!" % self.sim_params.render) atexit.register(self.terminate)
def __init__(self, env_params, sim_params, network=None, simulator='traci', scenario=None): """Initialize the environment class. Parameters ---------- env_params : flow.core.params.EnvParams see flow/core/params.py sim_params : flow.core.params.SimParams see flow/core/params.py network : flow.networks.Network see flow/networks/base.py simulator : str the simulator used, one of {'traci', 'aimsun'}. Defaults to 'traci' Raises ------ flow.utils.exceptions.FatalFlowError if the render mode is not set to a valid valuefor _ in range(self.env_params.sims_per_step): self.time_counter += 1 self.step_counter += 1 # perform acceleration actions for controlled human-driven vehicles if len(self.k.vehicle.get_controlled_ids()) > 0: accel = [] for veh_id in self.k.vehicle.get_controlled_ids(): action = self.k.vehicle.get_acc_controller( veh_id).get_action(self) accel.append(action) if self.k.vehicle.get_edge(veh_id)[0] == ":": if self.k.vehicle.get_speed(veh_id) <= 0.00000001: print(self.time_counter,'veh_id:',veh_id,'its leader:',self.k.vehicle.get_leader(veh_id),'headway to leader:',self.k.vehicle.get_headway(veh_id),'action:',action,'speed:',self.k.vehicle.get_speed(veh_id)) #if self.k.vehicle.get_leader(self.k.vehicle.get_leader(veh_id))==veh_id: # break self.k.vehicle.apply_acceleration( self.k.vehicle.get_controlled_ids(), accel) # perform lane change actions for controlled human-driven vehicles if len(self.k.vehicle.get_controlled_lc_ids()) > 0: direction = [] for veh_id in self.k.vehicle.get_controlled_lc_ids(): target_lane = self.k.vehicle.get_lane_changing_controller( veh_id).get_action(self) direction.append(target_lane) self.k.vehicle.apply_lane_change( self.k.vehicle.get_controlled_lc_ids(), direction=direction) # perform (optionally) routing actions for all vehicles in the # network, including RL and SUMO-controlled vehicles routing_ids = [] routing_actions = [] for veh_id in self.k.vehicle.get_ids(): if self.k.vehicle.get_routing_controller(veh_id) \ is not None: routing_ids.append(veh_id) route_contr = self.k.vehicle.get_routing_controller( veh_id) routing_actions.append(route_contr.choose_route(self)) self.k.vehicle.choose_routes(routing_ids, routing_actions) self.apply_rl_actions(rl_actions) #self.additional_command() # advance the simulation in the simulator by one step """ self.process_seeds_file = None self.env_params = env_params self.time_with_no_vehicles = 0 if scenario is not None: deprecated_attribute(self, "scenario", "network") self.network = scenario if scenario is not None else network self.net_params = self.network.net_params self.initial_config = self.network.initial_config self.sim_params = sim_params time_stamp = ''.join(str(time.time()).split('.')) if os.environ.get("TEST_FLAG", 0): # 1.0 works with stress_test_start 10k times time.sleep(1.0 * int(time_stamp[-6:]) / 1e6) # FIXME: this is sumo-specific self.sim_params.port = sumolib.miscutils.getFreeSocketPort() # time_counter: number of steps taken since the start of a rollout self.time_counter = 0 # step_counter: number of total steps taken self.step_counter = 0 # initial_state: self.initial_state = {} self.state = None self.obs_var_labels = [] # simulation step size self.sim_step = sim_params.sim_step # the simulator used by this environment self.simulator = simulator # create the Flow kernel self.k = Kernel(simulator=self.simulator, sim_params=sim_params) # use the network class's network parameters to generate the necessary # network components within the network kernel self.k.network.generate_network(self.network) # initial the vehicles kernel using the VehicleParams object self.k.vehicle.initialize(deepcopy(self.network.vehicles)) # initialize the simulation using the simulation kernel. This will use # the network kernel as an input in order to determine what network # needs to be simulated. kernel_api = self.k.simulation.start_simulation(network=self.k.network, sim_params=sim_params) # pass the kernel api to the kernel and it's subclasses self.k.pass_api(kernel_api) # the available_routes variable contains a dictionary of routes # vehicles can traverse; to be used when routes need to be chosen # dynamically self.available_routes = self.k.network.rts # store the initial vehicle ids self.initial_ids = deepcopy(self.network.vehicles.ids) # store the initial state of the vehicles kernel (needed for restarting # the simulation) self.k.vehicle.kernel_api = None self.k.vehicle.master_kernel = None self.initial_vehicles = deepcopy(self.k.vehicle) self.k.vehicle.kernel_api = self.k.kernel_api self.k.vehicle.master_kernel = self.k self.setup_initial_state() # use pyglet to render the simulation if self.sim_params.render in ['gray', 'dgray', 'rgb', 'drgb']: save_render = self.sim_params.save_render sight_radius = self.sim_params.sight_radius pxpm = self.sim_params.pxpm show_radius = self.sim_params.show_radius # get network polygons network = [] # FIXME: add to network kernel instead of hack for lane_id in self.k.kernel_api.lane.getIDList(): _lane_poly = self.k.kernel_api.lane.getShape(lane_id) lane_poly = [i for pt in _lane_poly for i in pt] network.append(lane_poly) # instantiate a pyglet renderer self.renderer = Renderer(network, self.sim_params.render, save_render, sight_radius=sight_radius, pxpm=pxpm, show_radius=show_radius) # render a frame self.render(reset=True) elif self.sim_params.render in [True, False]: pass # default to sumo-gui (if True) or sumo (if False) else: raise FatalFlowError('Mode %s is not supported!' % self.sim_params.render) atexit.register(self.terminate)
def __init__(self, env_params, sumo_params, scenario): # Invoke serializable if using rllab if Serializable is not object: Serializable.quick_init(self, locals()) self.env_params = env_params self.scenario = scenario self.sumo_params = sumo_params time_stamp = ''.join(str(time.time()).split('.')) if os.environ.get("TEST_FLAG", 0): # 1.0 works with stress_test_start 10k times time.sleep(1.0 * int(time_stamp[-6:]) / 1e6) self.sumo_params.port = sumolib.miscutils.getFreeSocketPort() self.vehicles = scenario.vehicles self.traffic_lights = scenario.traffic_lights # time_counter: number of steps taken since the start of a rollout self.time_counter = 0 # step_counter: number of total steps taken self.step_counter = 0 # initial_state: # Key = Vehicle ID, # Entry = (type_id, route_id, lane_index, lane_pos, speed, pos) self.initial_state = {} self.state = None self.obs_var_labels = [] # simulation step size self.sim_step = sumo_params.sim_step self.vehicle_arrangement_shuffle = \ env_params.vehicle_arrangement_shuffle self.starting_position_shuffle = env_params.starting_position_shuffle # the available_routes variable contains a dictionary of routes # vehicles can traverse; to be used when routes need to be chosen # dynamically self.available_routes = self.scenario.rts # TraCI connection used to communicate with sumo self.traci_connection = None # dictionary of initial observations used while resetting vehicles # after each rollout self.initial_observations = dict.fromkeys(self.vehicles.get_ids()) # store the initial vehicle ids self.initial_ids = deepcopy(self.vehicles.get_ids()) # store the initial state of the vehicles class (for restarting sumo) self.initial_vehicles = deepcopy(self.vehicles) # colors used to distinguish between types of vehicles in the network self.colors = {} # contains the subprocess.Popen instance used to start traci self.sumo_proc = None self.start_sumo() self.setup_initial_state() # use pyglet to render the simulation if self.sumo_params.render in ['gray', 'dgray', 'rgb', 'drgb']: save_render = self.sumo_params.save_render sight_radius = self.sumo_params.sight_radius pxpm = self.sumo_params.pxpm show_radius = self.sumo_params.show_radius # get network polygons network = [] for lane_id in self.traci_connection.lane.getIDList(): _lane_poly = self.traci_connection.lane.getShape(lane_id) lane_poly = [i for pt in _lane_poly for i in pt] network.append(lane_poly) # instantiate a pyglet renderer self.renderer = Renderer(network, self.sumo_params.render, save_render, sight_radius=sight_radius, pxpm=pxpm, show_radius=show_radius) # render a frame self.render(reset=True) elif self.sumo_params.render in [True, False]: pass # default to sumo-gui (if True) or sumo (if False) else: raise ValueError("Mode %s is not supported!" % self.sumo_params.render)
def test_pyglet_renderer(self): # Ring road network polygons network = \ [[36.64, -1.6500000000000001, 38.15, -1.62, 39.69, -1.52, 41.22, -1.37, 42.74, -1.1500000000000001, 44.26, -0.88, 45.77, -0.53, 47.25, -0.13, 48.72, 0.32, 50.17, 0.84, 51.61, 1.41, 53.01, 2.05, 54.39, 2.74, 55.730000000000004, 3.48, 57.050000000000004, 4.2700000000000005, 58.34, 5.12, 59.59, 6.03, 60.800000000000004, 6.98, 61.97, 7.97, 63.11, 9.02, 64.2, 10.11, 65.25, 11.25, 66.24, 12.42, 67.19, 13.63, 68.1, 14.88, 68.95, 16.17, 69.74, 17.490000000000002, 70.48, 18.830000000000002, 71.17, 20.21, 71.81, 21.61, 72.38, 23.05, 72.9, 24.5, 73.35000000000001, 25.97, 73.75, 27.45, 74.10000000000001, 28.96, 74.37, 30.48, 74.59, 32.0, 74.74, 33.53, 74.84, 35.07, 74.87, 36.58], [-1.6500000000000001, 36.58, -1.62, 35.07, -1.52, 33.53, -1.37, 32.0, -1.1500000000000001, 30.48, -0.88, 28.96, -0.53, 27.45, -0.13, 25.97, 0.32, 24.5, 0.84, 23.05, 1.41, 21.61, 2.05, 20.21, 2.74, 18.830000000000002, 3.48, 17.490000000000002, 4.2700000000000005, 16.17, 5.12, 14.88, 6.03, 13.63, 6.98, 12.42, 7.97, 11.25, 9.02, 10.11, 10.11, 9.02, 11.25, 7.97, 12.42, 6.98, 13.63, 6.03, 14.88, 5.12, 16.17, 4.2700000000000005, 17.490000000000002, 3.48, 18.830000000000002, 2.74, 20.21, 2.05, 21.61, 1.41, 23.05, 0.84, 24.5, 0.32, 25.97, -0.13, 27.45, -0.53, 28.96, -0.88, 30.48, -1.1500000000000001, 32.0, -1.37, 33.53, -1.52, 35.07, -1.62, 36.58, -1.6500000000000001], [74.87, 36.64, 74.84, 38.15, 74.74, 39.69, 74.59, 41.22, 74.37, 42.74, 74.10000000000001, 44.26, 73.75, 45.77, 73.35000000000001, 47.25, 72.9, 48.72, 72.38, 50.17, 71.81, 51.61, 71.17, 53.01, 70.48, 54.39, 69.74, 55.730000000000004, 68.95, 57.050000000000004, 68.1, 58.34, 67.19, 59.59, 66.24, 60.800000000000004, 65.25, 61.97, 64.2, 63.11, 63.11, 64.2, 61.97, 65.25, 60.800000000000004, 66.24, 59.59, 67.19, 58.34, 68.1, 57.050000000000004, 68.95, 55.730000000000004, 69.74, 54.39, 70.48, 53.01, 71.17, 51.61, 71.81, 50.17, 72.38, 48.72, 72.9, 47.25, 73.35000000000001, 45.77, 73.75, 44.26, 74.10000000000001, 42.74, 74.37, 41.22, 74.59, 39.69, 74.74, 38.15, 74.84, 36.64, 74.87], [36.58, 74.87, 35.07, 74.84, 33.53, 74.74, 32.0, 74.59, 30.48, 74.37, 28.96, 74.10000000000001, 27.45, 73.75, 25.97, 73.35000000000001, 24.5, 72.9, 23.05, 72.38, 21.61, 71.81, 20.21, 71.17, 18.830000000000002, 70.48, 17.490000000000002, 69.74, 16.17, 68.95, 14.88, 68.1, 13.63, 67.19, 12.42, 66.24, 11.25, 65.25, 10.11, 64.2, 9.02, 63.11, 7.97, 61.97, 6.98, 60.800000000000004, 6.03, 59.59, 5.12, 58.34, 4.2700000000000005, 57.050000000000004, 3.48, 55.730000000000004, 2.74, 54.39, 2.05, 53.01, 1.41, 51.61, 0.84, 50.17, 0.32, 48.72, -0.13, 47.25, -0.53, 45.77, -0.88, 44.26, -1.1500000000000001, 42.74, -1.37, 41.22, -1.52, 39.69, -1.62, 38.15, -1.6500000000000001, 36.64]] # Renderer parameters mode = "drgb" save_render = False sight_radius = 25 pxpm = 3 show_radius = True # initialize a pyglet renderer renderer = Renderer(network, mode, save_render=save_render, sight_radius=sight_radius, pxpm=pxpm, show_radius=show_radius) # ensure that the attributes match their correct values self.assertEqual(renderer.mode, mode) self.assertEqual(renderer.save_render, save_render) self.assertEqual(renderer.sight_radius, sight_radius) self.assertEqual(renderer.pxpm, pxpm) self.assertEqual(renderer.show_radius, show_radius)