def run_task(*_): """Implement the run_task method needed to run experiments with rllab.""" V_ENTER = 30 INNER_LENGTH = 300 LONG_LENGTH = 100 SHORT_LENGTH = 300 N_ROWS = 3 N_COLUMNS = 3 NUM_CARS_LEFT = 1 NUM_CARS_RIGHT = 1 NUM_CARS_TOP = 1 NUM_CARS_BOT = 1 tot_cars = (NUM_CARS_LEFT + NUM_CARS_RIGHT) * N_COLUMNS \ + (NUM_CARS_BOT + NUM_CARS_TOP) * N_ROWS grid_array = { "short_length": SHORT_LENGTH, "inner_length": INNER_LENGTH, "long_length": LONG_LENGTH, "row_num": N_ROWS, "col_num": N_COLUMNS, "cars_left": NUM_CARS_LEFT, "cars_right": NUM_CARS_RIGHT, "cars_top": NUM_CARS_TOP, "cars_bot": NUM_CARS_BOT } sim_params = SumoParams(sim_step=1, render=True) vehicles = VehicleParams() vehicles.add(veh_id="idm", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( min_gap=2.5, tau=1.1, max_speed=V_ENTER, speed_mode="all_checks"), routing_controller=(GridRouter, {}), num_vehicles=tot_cars) tl_logic = TrafficLightParams(baseline=False) additional_env_params = { "target_velocity": 50, "switch_time": 3.0, "num_observed": 2, "discrete": False, "tl_type": "controlled" } env_params = EnvParams(additional_params=additional_env_params) additional_net_params = { "speed_limit": 35, "grid_array": grid_array, "horizontal_lanes": 1, "vertical_lanes": 1 } if USE_INFLOWS: initial_config, net_params = get_flow_params( v_enter=V_ENTER, vehs_per_hour=EDGE_INFLOW, col_num=N_COLUMNS, row_num=N_ROWS, add_net_params=additional_net_params) else: initial_config, net_params = get_non_flow_params( V_ENTER, additional_net_params) scenario = SimpleGridScenario(name="grid-intersection", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env_name = "PO_TrafficLightGridEnv" pass_params = (env_name, sim_params, vehicles, env_params, net_params, initial_config, scenario) env = GymEnv(env_name, record_video=False, register_params=pass_params) horizon = env.horizon env = normalize(env) policy = GaussianMLPPolicy(env_spec=env.spec, hidden_sizes=(32, 32)) baseline = LinearFeatureBaseline(env_spec=env.spec) algo = TRPO( env=env, policy=policy, baseline=baseline, batch_size=40000, max_path_length=horizon, # whole_paths=True, n_itr=800, discount=0.999, # step_size=0.01, ) algo.train()
def grid_example(render=None): """ Perform a simulation of vehicles on a grid. Parameters ---------- render: bool, optional specifies whether to use sumo's gui during execution Returns ------- exp: flow.core.SumoExperiment type A non-rl experiment demonstrating the performance of human-driven vehicles and balanced traffic lights on a grid. """ inner_length = 300 long_length = 500 short_length = 300 n = 2 m = 3 num_cars_left = 20 num_cars_right = 20 num_cars_top = 20 num_cars_bot = 20 tot_cars = (num_cars_left + num_cars_right) * m \ + (num_cars_top + num_cars_bot) * n grid_array = { "short_length": short_length, "inner_length": inner_length, "long_length": long_length, "row_num": n, "col_num": m, "cars_left": num_cars_left, "cars_right": num_cars_right, "cars_top": num_cars_top, "cars_bot": num_cars_bot } sumo_params = SumoParams(sim_step=0.1, render=True) if render is not None: sumo_params.render = render vehicles = Vehicles() vehicles.add(veh_id="human", routing_controller=(GridRouter, {}), num_vehicles=tot_cars) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) tl_logic = TrafficLights(baseline=False) phases = [{ "duration": "31", "minDur": "8", "maxDur": "45", "state": "GGGrrrGGGrrr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yyyrrryyyrrr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rrrGGGrrrGGG" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "rrryyyrrryyy" }] tl_logic.add("center0", phases=phases, programID=1) tl_logic.add("center1", phases=phases, programID=1) tl_logic.add("center2", tls_type="actuated", phases=phases, programID=1) additional_net_params = { "grid_array": grid_array, "speed_limit": 35, "horizontal_lanes": 1, "vertical_lanes": 1 } net_params = NetParams(no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig() scenario = SimpleGridScenario(name="grid-intersection", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env = AccelEnv(env_params, sumo_params, scenario) return SumoExperiment(env, scenario)
def grid_mxn_exp_setup(row_num=1, col_num=1, sim_params=None, vehicles=None, env_params=None, net_params=None, initial_config=None, tl_logic=None): """ Create an environment and scenario pair for grid 1x1 test experiments. Parameters ---------- row_num: int, optional number of horizontal rows of edges in the grid network col_num: int, optional number of vertical columns of edges in the grid network sim_params : flow.core.params.SumoParams sumo-related configuration parameters, defaults to a time step of 1s and no sumo-imposed failsafe on human or rl vehicles vehicles : Vehicles type vehicles to be placed in the network, default is 5 vehicles per edge for a total of 20 vehicles with an IDM acceleration controller and GridRouter routing controller. env_params : flow.core.params.EnvParams environment-specific parameters, defaults to a environment with failsafes, where other parameters do not matter for non-rl runs net_params : flow.core.params.NetParams network-specific configuration parameters, defaults to a 1x1 grid with traffic lights on initial_config : flow.core.params.InitialConfig specifies starting positions of vehicles, defaults to evenly distributed vehicles across the length of the network tl_logic: flow.core.params.TrafficLightParams specifies logic of any traffic lights added to the system """ logging.basicConfig(level=logging.WARNING) if tl_logic is None: tl_logic = TrafficLightParams(baseline=False) if sim_params is None: # set default sim_params configuration sim_params = SumoParams(sim_step=1, render=False) if vehicles is None: vehicles_per_edge = 5 num_edges = 2 * (row_num + col_num) total_vehicles = num_edges * vehicles_per_edge vehicles = VehicleParams() vehicles.add(veh_id="idm", acceleration_controller=(IDMController, {}), car_following_params=SumoCarFollowingParams(min_gap=2.5, tau=1.1, max_speed=30), routing_controller=(GridRouter, {}), num_vehicles=total_vehicles) if env_params is None: # set default env_params configuration additional_env_params = { "target_velocity": 50, "switch_time": 3.0, "tl_type": "controlled", "discrete": False } env_params = EnvParams(additional_params=additional_env_params, horizon=100) if net_params is None: # set default net_params configuration total_vehicles = vehicles.num_vehicles num_entries = 2 * row_num + 2 * col_num assert total_vehicles % num_entries == 0, "{} total vehicles should " \ "be divisible by {" \ "}".format(total_vehicles, num_entries) grid_array = { "short_length": 100, "inner_length": 300, "long_length": 3000, "row_num": row_num, "col_num": col_num, "cars_left": int(total_vehicles / num_entries), "cars_right": int(total_vehicles / num_entries), "cars_top": int(total_vehicles / num_entries), "cars_bot": int(total_vehicles / num_entries) } additional_net_params = { "length": 200, "lanes": 2, "speed_limit": 35, "resolution": 40, "grid_array": grid_array, "horizontal_lanes": 1, "vertical_lanes": 1 } net_params = NetParams(additional_params=additional_net_params) if initial_config is None: # set default initial_config configuration initial_config = InitialConfig(spacing="custom", additional_params={"enter_speed": 30}) # create the scenario scenario = SimpleGridScenario(name="Grid1x1Test", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) # create the environment env = GreenWaveTestEnv(env_params=env_params, sim_params=sim_params, scenario=scenario) # reset the environment env.reset() return env, scenario
def __init__(self): self.colors = [(255,0,0), (0,255,0), (0,0,255), (255, 255,0), (255, 0, 255), (0, 255,255), (55,0,0), (0,55,0), (0,0,55), (55, 55,0), (55, 0, 55), (0, 55,55),] env_params = EnvParams( additional_params={"switch_time": CONFIG.SWITCH_TIME,"tl_type": CONFIG.TL_TYPE, "discrete": CONFIG.DISCRETE, "observation_distance": CONFIG.OBSERVATION_DISTANCE}, horizon=CONFIG.HORIZON) sim_params = SumoParams(sim_step=CONFIG.SIM_STEP, render=CONFIG.RENDER,) """ for scenario, we need to do a lot of work! It needs vehicles. Vehicles need router etc: scenario vehicles SimCarFollowingController SumoCarFollowingParams GridRouter num_vehicles net_params grid_array additional_net_params initial_config traffic_lights """ TOTAL_CARS = (CONFIG.NUM_CARS_LEFT + CONFIG.NUM_CARS_RIGHT) * CONFIG.N_COLUMNS + (CONFIG.NUM_CARS_BOT + CONFIG.NUM_CARS_TOP) * CONFIG.N_ROWS vehicles = VehicleParams() vehicles.add( veh_id='idm', acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( min_gap=CONFIG.MINGAP, max_speed=CONFIG.V_ENTER, speed_mode="all_checks", ), routing_controller=(GridRouter, {}), num_vehicles=TOTAL_CARS) grid_array = { "short_length": CONFIG.SHORT_LENGTH, "inner_length": CONFIG.INNER_LENGTH, "long_length": CONFIG.LONG_LENGTH, "row_num": CONFIG.N_ROWS, "col_num": CONFIG.N_COLUMNS, "cars_left": CONFIG.NUM_CARS_LEFT, "cars_right": CONFIG.NUM_CARS_RIGHT, "cars_top": CONFIG.NUM_CARS_TOP, "cars_bot": CONFIG.NUM_CARS_BOT } additional_net_params = { 'speed_limit': CONFIG.SPEED_LIMIT, 'grid_array': grid_array, 'horizontal_lanes': CONFIG.HORIZONTAL_LANES, 'vertical_lanes': CONFIG.VERTICAL_LANES } net_params= NetParams(no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig(spacing='custom', additional_params={'enter_speed': CONFIG.V_ENTER}) scenario = SimpleGridScenario( name="thisGetsDiscarded", vehicles=vehicles, net_params=net_params, initial_config=initial_config ) super().__init__(env_params, sim_params, scenario ) # since there is no reference to kernel_api.vehicle.getLength, I guestimate it. Should be used just for normalization. self.stop_penalty_weight=CONFIG.STOP_PENALTY_WEIGHT self.max_vehicle_density = 1.0/(2*CONFIG.MINGAP) self.edge_names = scenario.get_edge_names() self.traffic_lights = np.zeros( 4 * self.num_traffic_lights ) for i in range (self.num_traffic_lights): self.traffic_lights[4*i:4*i+4] = [1, 0, 0, 0] self.last_change = np.zeros( self.num_traffic_lights ) self.observation_mode = CONFIG.OBSERVATION_MODE if self.observation_mode == "Q_WEIGHT": self.observed_queue_max = 0.0 for i in np.linspace(0, self.observation_distance, np.floor(self.observation_distance*self.max_vehicle_density)): self.observed_queue_max += self.observation_distance - i self.observed_queue = np.zeros( len(self.edge_names) ) if self.observation_mode == "SEGMENT": self.segment_len = CONFIG.SEGMENT_LENGTH self.observed_segment_max = self.segment_len * self.max_vehicle_density self.total_nof_segments = 0 for edge_id in self.edge_names: self.total_nof_segments += int(np.ceil (self.k.scenario.edge_length(edge_id)/self.segment_len)) self.nof_segments = np.zeros(self.total_nof_segments, dtype=int) for edge_index , edge_id in enumerate(self.edge_names): self.nof_segments [edge_index] = int(np.ceil (self.k.scenario.edge_length(edge_id)/self.segment_len)) assert self.nof_segments [edge_index] < len(self.colors) self.observed_segments = np.zeros( self.total_nof_segments )
def grid_example(render=None, use_inflows=False): """ Perform a simulation of vehicles on a grid. Parameters ---------- render: bool, optional specifies whether to use the gui during execution use_inflows : bool, optional set to True if you would like to run the experiment with inflows of vehicles from the edges, and False otherwise Returns ------- exp: flow.core.experiment.Experiment A non-rl experiment demonstrating the performance of human-driven vehicles and balanced traffic lights on a grid. """ v_enter = 10 inner_length = 300 long_length = 500 short_length = 300 n_rows = 2 n_columns = 3 num_cars_left = 20 num_cars_right = 20 num_cars_top = 20 num_cars_bot = 20 tot_cars = (num_cars_left + num_cars_right) * n_columns \ + (num_cars_top + num_cars_bot) * n_rows grid_array = { "short_length": short_length, "inner_length": inner_length, "long_length": long_length, "row_num": n_rows, "col_num": n_columns, "cars_left": num_cars_left, "cars_right": num_cars_right, "cars_top": num_cars_top, "cars_bot": num_cars_bot } sim_params = SumoParams(sim_step=0.1, render=True) if render is not None: sim_params.render = render vehicles = VehicleParams() vehicles.add( veh_id="human", routing_controller=(GridRouter, {}), car_following_params=SumoCarFollowingParams( min_gap=2.5, decel=7.5, # avoid collisions at emergency stops ), num_vehicles=tot_cars) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) tl_logic = TrafficLightParams(baseline=False) phases = [{ "duration": "31", "minDur": "8", "maxDur": "45", "state": "GrGrGrGrGrGr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yryryryryryr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rGrGrGrGrGrG" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "ryryryryryry" }] tl_logic.add("center0", phases=phases, programID=1) tl_logic.add("center1", phases=phases, programID=1) tl_logic.add("center2", phases=phases, programID=1, tls_type="actuated") additional_net_params = { "grid_array": grid_array, "speed_limit": 35, "horizontal_lanes": 1, "vertical_lanes": 1 } if use_inflows: initial_config, net_params = get_flow_params( col_num=n_columns, row_num=n_rows, additional_net_params=additional_net_params) else: initial_config, net_params = get_non_flow_params( enter_speed=v_enter, add_net_params=additional_net_params) scenario = SimpleGridScenario(name="grid-intersection", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env = AccelEnv(env_params, sim_params, scenario) return Experiment(env)
def grid0_baseline(num_runs, render=True): """Run script for the grid0 baseline. Parameters ---------- num_runs : int number of rollouts the performance of the environment is evaluated over render : bool, optional specifies whether to use sumo's gui during execution Returns ------- SumoExperiment class needed to run simulations """ # we place a sufficient number of vehicles to ensure they confirm with the # total number specified above. We also use a "right_of_way" speed mode to # support traffic light compliance vehicles = Vehicles() vehicles.add(veh_id="human", acceleration_controller=(SumoCarFollowingController, {}), sumo_car_following_params=SumoCarFollowingParams( min_gap=2.5, max_speed=V_ENTER, ), routing_controller=(GridRouter, {}), num_vehicles=(N_LEFT + N_RIGHT) * N_COLUMNS + (N_BOTTOM + N_TOP) * N_ROWS, speed_mode="right_of_way") # inflows of vehicles are place on all outer edges (listed here) outer_edges = [] outer_edges += ["left{}_{}".format(N_ROWS, i) for i in range(N_COLUMNS)] outer_edges += ["right0_{}".format(i) for i in range(N_ROWS)] outer_edges += ["bot{}_0".format(i) for i in range(N_ROWS)] outer_edges += ["top{}_{}".format(i, N_COLUMNS) for i in range(N_ROWS)] # equal inflows for each edge (as dictate by the EDGE_INFLOW constant) inflow = InFlows() for edge in outer_edges: inflow.add(veh_type="human", edge=edge, vehs_per_hour=EDGE_INFLOW, departLane="free", departSpeed="max") # define the traffic light logic tl_logic = TrafficLights(baseline=False) phases = [{ "duration": "31", "minDur": "5", "maxDur": "45", "state": "GGGrrrGGGrrr" }, { "duration": "2", "minDur": "2", "maxDur": "2", "state": "yyyrrryyyrrr" }, { "duration": "31", "minDur": "5", "maxDur": "45", "state": "rrrGGGrrrGGG" }, { "duration": "2", "minDur": "2", "maxDur": "2", "state": "rrryyyrrryyy" }] for i in range(N_ROWS * N_COLUMNS): tl_logic.add("center" + str(i), tls_type="actuated", phases=phases, programID=1) net_params = NetParams( inflows=inflow, no_internal_links=False, additional_params={ "speed_limit": V_ENTER + 5, "grid_array": { "short_length": SHORT_LENGTH, "inner_length": INNER_LENGTH, "long_length": LONG_LENGTH, "row_num": N_ROWS, "col_num": N_COLUMNS, "cars_left": N_LEFT, "cars_right": N_RIGHT, "cars_top": N_TOP, "cars_bot": N_BOTTOM, }, "horizontal_lanes": 1, "vertical_lanes": 1, }, ) sumo_params = SumoParams( restart_instance=False, sim_step=1, render=render, ) env_params = EnvParams( evaluate=True, # Set to True to evaluate traffic metrics horizon=HORIZON, additional_params={ "target_velocity": 50, "switch_time": 2, "num_observed": 2, "discrete": False, "tl_type": "actuated" }, ) initial_config = InitialConfig(shuffle=True) scenario = SimpleGridScenario(name="grid", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env = PO_TrafficLightGridEnv(env_params, sumo_params, scenario) exp = SumoExperiment(env, scenario) results = exp.run(num_runs, HORIZON) total_delay = np.mean(results["returns"]) return total_delay
def run_task(*_): """Implement the run_task method needed to run experiments with rllab.""" v_enter = 10 inner_length = 300 long_length = 100 short_length = 300 n = 3 m = 3 num_cars_left = 1 num_cars_right = 1 num_cars_top = 1 num_cars_bot = 1 tot_cars = (num_cars_left + num_cars_right) * m \ + (num_cars_bot + num_cars_top) * n grid_array = { "short_length": short_length, "inner_length": inner_length, "long_length": long_length, "row_num": n, "col_num": m, "cars_left": num_cars_left, "cars_right": num_cars_right, "cars_top": num_cars_top, "cars_bot": num_cars_bot } sumo_params = SumoParams(sim_step=1, render=True) vehicles = Vehicles() vehicles.add(veh_id="idm", acceleration_controller=(SumoCarFollowingController, {}), sumo_car_following_params=SumoCarFollowingParams( min_gap=2.5, tau=1.1, max_speed=v_enter, speed_mode="all_checks"), routing_controller=(GridRouter, {}), num_vehicles=tot_cars) tl_logic = TrafficLightParams(baseline=False) additional_env_params = { "target_velocity": 50, "switch_time": 3.0, "num_observed": 2, "discrete": False, "tl_type": "controlled" } env_params = EnvParams(additional_params=additional_env_params) additional_net_params = { "speed_limit": 35, "grid_array": grid_array, "horizontal_lanes": 1, "vertical_lanes": 1 } initial_config, net_params = get_flow_params(10, 300, n, m, additional_net_params) scenario = SimpleGridScenario(name="grid-intersection", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env_name = "PO_TrafficLightGridEnv" pass_params = (env_name, sumo_params, vehicles, env_params, net_params, initial_config, scenario) env = GymEnv(env_name, record_video=False, register_params=pass_params) horizon = env.horizon env = normalize(env) policy = GaussianMLPPolicy(env_spec=env.spec, hidden_sizes=(32, 32)) baseline = LinearFeatureBaseline(env_spec=env.spec) algo = TRPO( env=env, policy=policy, baseline=baseline, batch_size=40000, max_path_length=horizon, # whole_paths=True, n_itr=800, discount=0.999, # step_size=0.01, ) algo.train()
def grid_example(render=None): """ Perform a simulation of vehicles on a grid. Parameters ---------- render: bool, optional specifies whether to use the gui during execution Returns ------- exp: flow.core.experiment.Experiment A non-rl experiment demonstrating the performance of human-driven vehicles and balanced traffic lights on a grid. """ inner_length = 300 long_length = 500 short_length = 300 N_ROWS = 2 N_COLUMNS = 3 num_cars_left = 20 num_cars_right = 20 num_cars_top = 20 num_cars_bot = 20 tot_cars = (num_cars_left + num_cars_right) * N_COLUMNS \ + (num_cars_top + num_cars_bot) * N_ROWS grid_array = { "short_length": short_length, "inner_length": inner_length, "long_length": long_length, "row_num": N_ROWS, "col_num": N_COLUMNS, "cars_left": num_cars_left, "cars_right": num_cars_right, "cars_top": num_cars_top, "cars_bot": num_cars_bot } sim_params = AimsunParams(sim_step=0.5, render=True) if render is not None: sim_params.render = render vehicles = VehicleParams() vehicles.add(veh_id="human", num_vehicles=tot_cars) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) tl_logic = TrafficLightParams(baseline=False) phases = [{ "duration": "31", "minDur": "8", "maxDur": "45", "yellow": "3", "state": "GGGrrrGGGrrr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "yellow": "3", "state": "yyyrrryyyrrr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "yellow": "3", "state": "rrrGGGrrrGGG" }, { "duration": "6", "minDur": "3", "maxDur": "6", "yellow": "3", "state": "rrryyyrrryyy" }] tl_logic.add("center0", phases=phases, programID=1) tl_logic.add("center1", phases=phases, programID=1) tl_logic.add("center2", tls_type="actuated", phases=phases, programID=1) tl_logic.add("center3", phases=phases, programID=1) tl_logic.add("center4", phases=phases, programID=1) tl_logic.add("center5", tls_type="actuated", phases=phases, programID=1) additional_net_params = { "grid_array": grid_array, "speed_limit": 35, "horizontal_lanes": 1, "vertical_lanes": 1 } net_params = NetParams(additional_params=additional_net_params) initial_config = InitialConfig(spacing='custom') scenario = SimpleGridScenario(name="grid-intersection", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env = AccelEnv(env_params, sim_params, scenario, simulator='aimsun') return Experiment(env)