lane_change_params=SumoLaneChangeParams( model="SL2015", # Define a lane changing mode that will allow lane changes # See: https://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#lane_change_mode_.280xb6.29 # and: ~/local/flow_2019_07/flow/core/params.py, see LC_MODES = {"aggressive": 0 /*bug, 0 is no lane-changes*/, "no_lat_collide": 512, "strategic": 1621}, where "strategic" is the default behavior lane_change_mode= 1621, #0b011000000001, # (like default 1621 mode, but no lane changes other than strategic to follow route, # 512, #(collision avoidance and safety gap enforcement) # "strategic", lc_speed_gain=1000000, lc_pushy=0, #0.5, #1, lc_assertive=5, #20, # the following two replace default values which are not read well by xml parser lc_impatience=1e-8, lc_time_to_impatience=1e12), num_vehicles=0) inflow = InFlows() inflow.add( veh_type="human", edge="404969345#0", # flow id sw2w1 from xml file begin=10, #0, end=90000, #probability=(1 - RL_PENETRATION), #* FLOW_RATE, vehs_per_hour=MERGE_RATE, #(1 - RL_PENETRATION)*FLOW_RATE, departSpeed="max", departLane="free", ) ''' inflow.add( veh_type="rl", edge="404969345#0", # flow id sw2w1 from xml file begin=10,#0,
def bottleneck_example(flow_rate, horizon, restart_instance=False, render=None): """ Perform a simulation of vehicles on a bottleneck. Parameters ---------- flow_rate : float total inflow rate of vehicles into the bottleneck horizon : int time horizon restart_instance: bool, optional whether to restart the instance upon reset 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 on a bottleneck. """ if render is None: render = False sim_params = SumoParams(sim_step=0.5, render=render, overtake_right=False, restart_instance=restart_instance) vehicles = VehicleParams() vehicles.add(veh_id="human", lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams(speed_mode=25, ), lane_change_params=SumoLaneChangeParams( lane_change_mode=1621, ), num_vehicles=1) additional_env_params = { "target_velocity": 40, "max_accel": 1, "max_decel": 1, "lane_change_duration": 5, "add_rl_if_exit": False, "disable_tb": DISABLE_TB, "disable_ramp_metering": DISABLE_RAMP_METER } env_params = EnvParams(horizon=horizon, additional_params=additional_env_params) inflow = InFlows() inflow.add(veh_type="human", edge="1", vehsPerHour=flow_rate, departLane="random", departSpeed=10) traffic_lights = TrafficLightParams() if not DISABLE_TB: traffic_lights.add(node_id="2") if not DISABLE_RAMP_METER: traffic_lights.add(node_id="3") additional_net_params = {"scaling": SCALING, "speed_limit": 23} net_params = NetParams(inflows=inflow, no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig(spacing="random", min_gap=5, lanes_distribution=float("inf"), edges_distribution=["2", "3", "4", "5"]) scenario = BottleneckScenario(name="bay_bridge_toll", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) env = BottleneckEnv(env_params, sim_params, scenario) return BottleneckDensityExperiment(env)
additional_net_params["post_merge_length"] = 1000 additional_net_params["INFLOW_EDGE_LEN"] = 1000 # RL vehicles constitute 5% of the total number of vehicles vehicles = VehicleParams() vehicles.add(veh_id="human", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams(speed_mode=9, ), num_vehicles=MAIN_HUMAN + MERGE_HUMAN) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), car_following_params=SumoCarFollowingParams(speed_mode=9, ), num_vehicles=MAIN_RL) # Vehicles are introduced from both sides of merge, with RL vehicles entering # from the highway portion as well inflow = InFlows() ''' inflow.add( veh_type="human", edge="inflow_highway", vehs_per_hour=(1 - RL_PENETRATION) * FLOW_RATE, number = 0,#MAIN_HUMAN,#round(FLOW_RATE/(FLOW_RATE+MERGE_RATE)*(1-RL_PENETRATION) * VEHICLE_NUMBER), depart_lane="free", depart_speed=10) inflow.add( veh_type="rl", edge="inflow_highway", vehs_per_hour=RL_PENETRATION * FLOW_RATE, number = 0,#MAIN_RL, #round(FLOW_RATE/(FLOW_RATE+MERGE_RATE)*RL_PENETRATION * VEHICLE_NUMBER), depart_lane="free", depart_speed=10)
additional_net_params["next_off_ramp_proba"] = 0.25 # inflow rates in vehs/hour HIGHWAY_INFLOW_RATE = 4000 ON_RAMPS_INFLOW_RATE = 350 vehicles = VehicleParams() vehicles.add( veh_id="human", car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", # for safer behavior at the merges tau=1.5 # larger distance between cars ), lane_change_params=SumoLaneChangeParams(lane_change_mode=1621)) inflows = InFlows() inflows.add(veh_type="human", edge="highway_0", vehs_per_hour=HIGHWAY_INFLOW_RATE, depart_lane="free", depart_speed="max", name="highway_flow") for i in range(len(additional_net_params["on_ramps_pos"])): inflows.add(veh_type="human", edge="on_ramp_{}".format(i), vehs_per_hour=ON_RAMPS_INFLOW_RATE, depart_lane="first", depart_speed="max", name="on_ramp_flow") flow_params = dict(
"disable_ramp_metering": True, "controlled_segments": controlled_segments, "symmetric": False, "observed_segments": num_observed_segments, "reset_inflow": False, "lane_change_duration": 5, "max_accel": 3, "max_decel": 3, "inflow_range": [1000, 2000] } # flow rate flow_rate = 1900 * SCALING # percentage of flow coming out of each lane inflow = InFlows() inflow.add( veh_type="rl", edge="1", vehs_per_hour=flow_rate * AV_FRAC, departLane="random", departSpeed=10) inflow.add( veh_type="human", edge="1", vehs_per_hour=flow_rate * (1 - AV_FRAC), departLane="random", departSpeed=10) traffic_lights = TrafficLightParams() if not DISABLE_TB:
def make_flow_params(n_rows, n_columns, edge_inflow): """ Generate the flow params for the experiment. :param n_rows: :param n_columns: :param edge_inflow: :return: """ # 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 = VehicleParams() num_vehicles = (N_LEFT + N_RIGHT) * n_columns + (N_BOTTOM + N_TOP) * n_rows vehicles.add( veh_id="human", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( min_gap=2.5, max_speed=V_ENTER, decel=7.5, # avoid collisions at emergency stops speed_mode="right_of_way", ), routing_controller=(GridRouter, {}), num_vehicles=num_vehicles) # 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=V_ENTER) flow_params = dict( # name of the experiment exp_tag="grid_0_{}x{}_i{}_multiagent".format(n_rows, n_columns, edge_inflow), # name of the flow environment the experiment is running on env_name='MultiTrafficLightGridPOEnv', # name of the scenario class the experiment is running on scenario="SimpleGridScenario", # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( restart_instance=True, sim_step=1, render=False, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, additional_params={ "target_velocity": 50, "switch_time": 3, "num_observed": 2, "discrete": False, "tl_type": "actuated", "num_local_edges": 4, "num_local_lights": 4, }, ), # network-related parameters (see flow.core.params.NetParams and the # scenario's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, additional_params={ "speed_limit": V_ENTER + 5, # inherited from grid0 benchmark "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, }, ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization # or reset (see flow.core.params.InitialConfig) initial=InitialConfig( spacing='custom', shuffle=True, ), ) return flow_params
def bottleneck0_baseline(num_runs, sumo_binary="sumo-gui"): """Run script for the bottleneck0 baseline. Parameters ---------- num_runs : int number of rollouts the performance of the environment is evaluated over sumo_binary: str, optional specifies whether to use sumo's gui during execution Returns ------- SumoExperiment class needed to run simulations """ vehicles = Vehicles() vehicles.add(veh_id="human", speed_mode=9, routing_controller=(ContinuousRouter, {}), lane_change_mode=0, num_vehicles=1 * SCALING) controlled_segments = [("1", 1, False), ("2", 2, True), ("3", 2, True), ("4", 2, True), ("5", 1, False)] num_observed_segments = [("1", 1), ("2", 3), ("3", 3), ("4", 3), ("5", 1)] additional_env_params = { "target_velocity": 40, "disable_tb": True, "disable_ramp_metering": True, "controlled_segments": controlled_segments, "symmetric": False, "observed_segments": num_observed_segments, "reset_inflow": False, "lane_change_duration": 5, "max_accel": 3, "max_decel": 3, "inflow_range": [1000, 2000] } # flow rate flow_rate = 1900 * SCALING # percentage of flow coming out of each lane inflow = InFlows() inflow.add(veh_type="human", edge="1", vehs_per_hour=flow_rate, departLane="random", departSpeed=10) traffic_lights = TrafficLights() if not DISABLE_TB: traffic_lights.add(node_id="2") if not DISABLE_RAMP_METER: traffic_lights.add(node_id="3") additional_net_params = {"scaling": SCALING} net_params = NetParams(in_flows=inflow, no_internal_links=False, additional_params=additional_net_params) sumo_params = SumoParams( sim_step=0.5, sumo_binary=sumo_binary, print_warnings=False, restart_instance=False, ) env_params = EnvParams( evaluate=True, # Set to True to evaluate traffic metrics warmup_steps=40, sims_per_step=1, horizon=HORIZON, additional_params=additional_env_params, ) initial_config = InitialConfig( spacing="uniform", min_gap=5, lanes_distribution=float("inf"), edges_distribution=["2", "3", "4", "5"], ) scenario = BottleneckScenario(name="bay_bridge_toll", generator_class=BottleneckGenerator, vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) env = DesiredVelocityEnv(env_params, sumo_params, scenario) exp = SumoExperiment(env, scenario) results = exp.run(num_runs, HORIZON) avg_outflow = np.mean([outflow[-1] for outflow in results["per_step_returns"]]) return avg_outflow
def reset(self): add_params = self.env_params.additional_params if add_params.get("reset_inflow"): inflow_range = add_params.get("inflow_range") flow_rate = np.random.uniform(min(inflow_range), max(inflow_range)) * self.scaling for _ in range(100): try: inflow = InFlows() inflow.add(veh_type="followerstopper", edge="1", vehs_per_hour=flow_rate * .1, departLane="random", departSpeed=10) inflow.add(veh_type="human", edge="1", vehs_per_hour=flow_rate * .9, departLane="random", departSpeed=10) additional_net_params = { "scaling": self.scaling, "speed_limit": self.scenario.net_params. additional_params['speed_limit'] } net_params = NetParams( inflows=inflow, no_internal_links=False, additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add( veh_id="human", car_following_params=SumoCarFollowingParams( speed_mode=9, ), lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), lane_change_params=SumoLaneChangeParams( lane_change_mode=0, # 1621,#0b100000101, ), num_vehicles=1 * self.scaling) vehicles.add( veh_id="followerstopper", acceleration_controller=(RLController, {}), lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode=9, ), lane_change_params=SumoLaneChangeParams( lane_change_mode=0, ), num_vehicles=1 * self.scaling) self.scenario = self.scenario.__class__( name=self.scenario.orig_name, vehicles=vehicles, net_params=net_params, initial_config=self.scenario.initial_config, traffic_lights=self.scenario.traffic_lights) observation = super().reset() # reset the timer to zero self.time_counter = 0 return observation except Exception as e: print('error on reset ', e) # perform the generic reset function observation = super().reset() # reset the timer to zero self.time_counter = 0 return observation
# Define a lane changing mode that will allow lane changes # See: https://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#lane_change_mode_.280xb6.29 # and: ~/local/flow_2019_07/flow/core/params.py, see LC_MODES = {"aggressive": 0 /*bug, 0 is no lane-changes*/, "no_lat_collide": 512, "strategic": 1621}, where "strategic" is the default behavior lane_change_mode= 1621, #0b011000000001, # (like default 1621 mode, but no lane changes other than strategic to follow route, # 512, #(collision avoidance and safety gap enforcement) # "strategic", #lc_speed_gain=1000000, lc_pushy=0, #0.5, #1, lc_assertive=5, #20, # the following two replace default values which are not read well by xml parser lc_impatience=1e-8, lc_time_to_impatience=1e12), num_vehicles=0) # Vehicles are introduced from both sides of merge, with RL vehicles entering # from the highway portion as well inflow = InFlows() ''' inflow.add( veh_type="human", edge="404969345#0", # flow id sw2w1 from xml file begin=10,#0, end=90000, #probability=(1 - RL_PENETRATION), #* FLOW_RATE, vehs_per_hour = MERGE_RATE,#(1 - RL_PENETRATION)*FLOW_RATE, departSpeed=7.5, departLane="free", ) ''' ''' inflow.add(
speed_mode=0b11111, lane_change_mode=512, lane_change_controller=(SumoLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=15 * SCALING) additional_env_params = { "target_velocity": 50, "num_steps": 150, "disable_tb": True, "disable_ramp_metering": True, "add_rl_if_exit": True } env_params = EnvParams(additional_params=additional_env_params) inflow = InFlows() inflow.add(veh_type="human", edge="1", vehsPerHour=FLOW_RATE, departLane="random", departSpeed=10) traffic_lights = TrafficLights() if not DISABLE_TB: traffic_lights.add(node_id="2") if not DISABLE_RAMP_METER: traffic_lights.add(node_id="3") additional_net_params = {"scaling": SCALING} net_params = NetParams(in_flows=inflow, no_internal_links=False,
vehicles.add(veh_id="human", acceleration_controller=(IDMController, { "noise": 0.2 }), car_following_params=SumoCarFollowingParams( speed_mode="all_checks", ), num_vehicles=0) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), car_following_params=SumoCarFollowingParams( speed_mode="all_checks", ), num_vehicles=0) # Vehicles are introduced from both sides of merge, with RL vehicles entering # from the highway portion as well inflow = InFlows() inflow.add(veh_type="human", edge="inflow_highway", vehs_per_hour=(1 - RL_PENETRATION) * FLOW_RATE, departLane="free", departSpeed=20) inflow.add(veh_type="rl", edge="inflow_highway", vehs_per_hour=RL_PENETRATION * FLOW_RATE, departLane="free", departSpeed=20) inflow.add(veh_type="human", edge="inflow_merge", vehs_per_hour=160 * (1 - RL_PENETRATION), departLane="free", departSpeed=20)
def getOmgeving(HORIZON): sim_params = SumoParams(render=True, sim_step=1, restart_instance=True, emission_path='result') # temp inflow edge_inflow = 300 # params for grid env inner_length = 300 long_length = 500 short_lengh = 500 rows = 1 columns = 1 num_cars_left = 1 num_cars_right = 1 num_cars_top = 1 num_cars_bottom = 1 enterSpeed = 30 tot_cars = (num_cars_left + num_cars_right) * columns + ( num_cars_top + num_cars_bottom) * rows grid_array = { "short_length": short_lengh, "inner_length": inner_length, "long_length": long_length, "row_num": rows, "col_num": columns, "cars_left": num_cars_left, "cars_right": num_cars_right, "cars_top": num_cars_top, "cars_bot": num_cars_bottom } # vehicles # add the starting vehicles vehicles = VehicleParams() vehicles.add("human", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( speed_mode="right_of_way", min_gap=2.5, max_speed=enterSpeed, decel=7.5, ), routing_controller=(GridRouter, {}), num_vehicles=tot_cars) # inflow # outer_edges of the network (zie traffic_light_grid.py file) outer_edges = [] outer_edges += ["left{}_{}".format(rows, i) for i in range(columns)] outer_edges += ["right0_{}".format(i) for i in range(rows)] outer_edges += ["bot{}_0".format(i) for i in range(columns)] outer_edges += ["top{}_{}".format(i, columns) for i in range(rows)] inflow = InFlows() for edge in outer_edges: if edge == "left1_0": prob = 0.10 elif edge == "right0_0": prob = 0.10 elif edge == "bot0_0": prob = 0.10 elif edge == "top0_1": prob = 0.10 inflow.add( veh_type="human", edge=edge, #vehs_per_hour=edge_inflow, probability=prob, depart_lane="free", depart_speed=enterSpeed) # Net Params additional_net_params = { "speed_limit": enterSpeed + 5, "grid_array": grid_array, "horizontal_lanes": 1, "vertical_lanes": 1, #"traffic_lights": True} } net_params = NetParams(inflows=inflow, additional_params=additional_net_params) # Env Params # => switch_time is de minimum tijd dat een licht in een bepaalde state zit # => num_observed aantal vehicles dat geobservered wordt vanuit elke richting van het kruispunt # => target_velocity is de snelheid dat elk voertuig moet proberen te behalen wanneer deze zich op het kruispunt bevindt additional_env_params = { "switch_time": 3.0, "tl_type": "controlled", # kan ook actuated/controlled zijn "discrete": True, "num_observed": 2, "target_velocity": 50 } env_params = EnvParams(horizon=HORIZON, additional_params=additional_env_params) # Initial config initial_config = InitialConfig(spacing='custom', shuffle=True) # Flow Params flow_param = dict( # name of the experiment exp_tag="RL_traffic_lights_one_by_one", # name of the flow environment the experiment is running on env_name=TrafficLightGridPOEnv, # name of the network class the experiment uses network=TrafficLightGridNetwork, # simulator that is used by the experiment simulator='traci', # simulation-related parameters sim=sim_params, # environment related parameters (see flow.core.params.EnvParams) env=env_params, # network-related parameters (see flow.core.params.NetParams and # the network's documentation or ADDITIONAL_NET_PARAMS component) net=net_params, # vehicles to be placed in the network at the start of a rollout # (see flow.core.vehicles.Vehicles) veh=vehicles, # (optional) parameters affecting the positioning of vehicles upon # initialization/reset (see flow.core.params.InitialConfig) initial=initial_config) return flow_param
max_speed=V_ENTER, decel=7.5, # avoid collisions at emergency stops speed_mode="right_of_way", ), routing_controller=(GridRouter, {}), num_vehicles=num_vehicles) # 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_COLUMNS)] 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=400, # probability=0.20, departLane="free", departSpeed=V_ENTER) myNetParams = NetParams( inflows=inflow, additional_params={ "speed_limit": V_ENTER + 5, # inherited from grid0 benchmark "grid_array": { "short_length": SHORT_LENGTH,
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 = 0 num_cars_right = 0 num_cars_top = 0 num_cars_bot = 0 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) """""" inflow = InFlows() inflow.add(veh_type="human", edge="bot1_0", probability=1, departLane="free", departSpeed=20) """ inflow.add( veh_type="human", edge="bot0_0", probability=0.25, departLane="free", departSpeed=20) inflow.add( veh_type="human", edge="top1_3", probability=1, departLane="free", departSpeed=20) inflow.add( veh_type="human", edge="top0_3", probability=0.25, departLane="free", departSpeed=20) inflow.add( veh_type="human", edge="left2_0", probability=1, departLane="free", departSpeed=20) inflow.add( veh_type="human", edge="left2_1", probability=0.25, departLane="free", departSpeed=20) inflow.add( veh_type="human", edge="left2_2", probability=1, departLane="free", departSpeed=20) inflow.add( veh_type="human", edge="right0_0", probability=1, departLane="free", departSpeed=20) inflow.add( veh_type="human", edge="right0_1", probability=0.25, departLane="free", departSpeed=20) """ inflow.add(veh_type="human", edge="right0_2", probability=1, departLane="free", departSpeed=20) additional_net_params = { "grid_array": grid_array, "speed_limit": 35, "horizontal_lanes": 1, "vertical_lanes": 1 } net_params = NetParams(inflows=inflow, 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)
col_sim = model.getColumn('GKExperiment::simStepAtt') # Set new simulation step value experiment.setDataValue(col_sim, sim_step) # collect the scenario-specific data data_file = 'flow/core/kernel/scenario/data.json' with open(os.path.join(config.PROJECT_PATH, data_file)) as f: data = json.load(f) # export the data from the dictionary veh_types = data['vehicle_types'] osm_path = data['osm_path'] if data['inflows'] is not None: inflows = InFlows() inflows.__dict__ = data['inflows'].copy() else: inflows = None if data['traffic_lights'] is not None: traffic_lights = TrafficLightParams() traffic_lights.__dict__ = data['traffic_lights'].copy() else: traffic_lights = None # generate the network if osm_path is not None: generate_net_osm(osm_path, inflows, veh_types) edge_osm = {}
def get_flow_params(config): """Return Flow experiment parameters, given an experiment result folder. Parameters ---------- config : dict < dict > or str May be one of two things: * If it is a dict, then it is the stored RLlib configuration dict. * If it is a string, then it is the path to a flow_params json file. Returns ------- dict flow-related parameters, consisting of the following keys: * exp_tag: name of the experiment * env_name: name of the flow environment the experiment is running on * network: name of the network class the experiment uses * simulator: simulator that is used by the experiment (e.g. aimsun) * sim: simulation-related parameters (see flow.core.params.SimParams) * env: environment related parameters (see flow.core.params.EnvParams) * net: network-related parameters (see flow.core.params.NetParams and the network's documentation or ADDITIONAL_NET_PARAMS component) * veh: vehicles to be placed in the network at the start of a rollout (see flow.core.params.VehicleParams) * initial: parameters affecting the positioning of vehicles upon initialization/reset (see flow.core.params.InitialConfig) * tls: traffic lights to be introduced to specific nodes (see flow.core.params.TrafficLightParams) """ # collect all data from the json file if type(config) == dict: flow_params = json.loads(config['env_config']['flow_params']) else: flow_params = json.load(open(config, 'r')) # reinitialize the vehicles class from stored data veh = VehicleParams() for veh_params in flow_params["veh"]: module = __import__( "flow.controllers", fromlist=[veh_params['acceleration_controller'][0]]) acc_class = getattr(module, veh_params['acceleration_controller'][0]) lc_class = getattr(module, veh_params['lane_change_controller'][0]) acc_controller = (acc_class, veh_params['acceleration_controller'][1]) lc_controller = (lc_class, veh_params['lane_change_controller'][1]) rt_controller = None if veh_params['routing_controller'] is not None: rt_class = getattr(module, veh_params['routing_controller'][0]) rt_controller = (rt_class, veh_params['routing_controller'][1]) # TODO: make ambiguous car_following_params = SumoCarFollowingParams() car_following_params.__dict__ = veh_params["car_following_params"] # TODO: make ambiguous lane_change_params = SumoLaneChangeParams() lane_change_params.__dict__ = veh_params["lane_change_params"] del veh_params["car_following_params"], \ veh_params["lane_change_params"], \ veh_params["acceleration_controller"], \ veh_params["lane_change_controller"], \ veh_params["routing_controller"] veh.add(acceleration_controller=acc_controller, lane_change_controller=lc_controller, routing_controller=rt_controller, car_following_params=car_following_params, lane_change_params=lane_change_params, **veh_params) # convert all parameters from dict to their object form sim = SumoParams() # TODO: add check for simulation type sim.__dict__ = flow_params["sim"].copy() net = NetParams() net.__dict__ = flow_params["net"].copy() net.inflows = InFlows() if flow_params["net"]["inflows"]: net.inflows.__dict__ = flow_params["net"]["inflows"].copy() env = EnvParams() env.__dict__ = flow_params["env"].copy() initial = InitialConfig() if "initial" in flow_params: initial.__dict__ = flow_params["initial"].copy() tls = TrafficLightParams() if "tls" in flow_params: tls.__dict__ = flow_params["tls"].copy() flow_params["sim"] = sim flow_params["env"] = env flow_params["initial"] = initial flow_params["net"] = net flow_params["veh"] = veh flow_params["tls"] = tls return flow_params
def getOmgeving(HORIZON): sim_params = SumoParams(render=False, sim_step=1,restart_instance=True) ''' Opstelling van het netwerk: | | | --6---7---8-- | | | --3---4---5-- | | | --0---1---2-- | | | ''' # temp inflow edge_inflow = 300 # params for grid env inner_length = 300 long_length = 500 short_lengh = 500 rows = 3 columns = 3 num_cars_left = 1 num_cars_right = 1 num_cars_top = 1 num_cars_bottom = 1 enterSpeed = 30 tot_cars = (num_cars_left + num_cars_right)*columns + (num_cars_top + num_cars_bottom)*rows grid_array = { "short_length": short_lengh, "inner_length": inner_length, "long_length": long_length, "row_num": rows, "col_num": columns, "cars_left": num_cars_left, "cars_right": num_cars_right, "cars_top": num_cars_top, "cars_bot": num_cars_bottom } # vehicles vehicles = VehicleParams() vehicles.add("human", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( speed_mode="right_of_way", min_gap=2.5, max_speed=enterSpeed, ), routing_controller=(GridRouter, {}), num_vehicles=tot_cars) # inflow inflow = InFlows() # outer_edges of the network (zie traffic_light_grid.py file) outer_edges = [] outer_edges += ["left{}_{}".format(rows, i) for i in range(columns)] outer_edges += ["right0_{}".format(i) for i in range (rows)] outer_edges += ["bot{}_0".format(i) for i in range(rows)] outer_edges += ["top{}_{}".format(i, columns) for i in range(rows)] # meerdere outeredges for edge in outer_edges: # collumns if edge == "bot0_0": prob = 0.10 if edge == "bot1_0": prob = 0.10 if edge == "bot2_0": prob = 0.10 if edge == "top0_3": prob = 0.10 if edge == "top1_3": prob = 0.10 if edge == "top2_3": prob = 0.10 # rows if edge == "right0_0": prob = 0.10 if edge == "right0_1": prob = 0.10 if edge == "right0_2": prob = 0.10 if edge == "left3_0": prob = 0.10 if edge == "left3_1": prob = 0.10 if edge == "left3_2": prob = 0.10 # inflow inflow.add( edge=edge, veh_type="human", #probability=prob, vehs_per_hour=edge_inflow, depart_lane="free", depart_speed="max") # net params additional_net_params = { "speed_limit": enterSpeed + 5, "grid_array": grid_array, "horizontal_lanes": 1, "vertical_lanes": 1, } net_params = NetParams(inflows=inflow, additional_params=additional_net_params) # env params additional_env_params = { "switch_time": 2, "tl_type": "actuated", "discrete": True, "num_observed":5, "target_velocity": 50 } env_params = EnvParams(horizon=HORIZON, additional_params=additional_env_params) # initial config initial_config = InitialConfig(spacing="custom", shuffle=True) # flow params flow_param = dict( # name of the experiment exp_tag="DQN_grid_static", # name of the flow environment the experiment is running on env_name=TrafficLightGridPOEnv, # name of the network class the experiment uses network=TrafficLightGridNetwork, # simulator that is used by the experiment simulator='traci', # simulation-related parameters sim=sim_params, # environment related parameters (see flow.core.params.EnvParams) env=env_params, # network-related parameters (see flow.core.params.NetParams and # the network's documentation or ADDITIONAL_NET_PARAMS component) net=net_params, # vehicles to be placed in the network at the start of a rollout # (see flow.core.vehicles.Vehicles) veh=vehicles, # (optional) parameters affecting the positioning of vehicles upon # initialization/reset (see flow.core.params.InitialConfig) initial=initial_config ) return flow_param
def stabilizing_triangle(highway_inflow): # experiment number # - 0: 10% RL penetration, 5 max controllable vehicles # - 1: 25% RL penetration, 13 max controllable vehicles # - 2: 33% RL penetration, 17 max controllable vehicles EXP_NUM = 0 # time horizon of a single rollout HORIZON = 600 # number of rollouts per training iteration N_ROLLOUTS = 20 # number of parallel workers N_CPUS = 2 # inflow rate at the highway FLOW_RATE = highway_inflow # percent of autonomous vehicles RL_PENETRATION = [0.1, 0.25, 0.33][EXP_NUM] # num_rl term (see ADDITIONAL_ENV_PARAMs) NUM_RL = [5, 13, 17][EXP_NUM] # We consider a highway network with an upstream merging lane producing # shockwaves # RL vehicles constitute 5% of the total number of vehicles vehicles = VehicleParams() vehicles.add(veh_id="human", acceleration_controller=(IDMController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), lane_change_params=SumoLaneChangeParams( lane_change_mode="strategic", ), num_vehicles=0) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), num_vehicles=0) # Vehicles are introduced from both sides of merge, with RL vehicles entering # from the highway portion as well inflow = InFlows() inflow.add(veh_type="human", edge="inflow_highway_2", vehs_per_hour=(1 - RL_PENETRATION) * FLOW_RATE, departLane="random", departSpeed=10) inflow.add(veh_type="rl", edge="inflow_highway_2", vehs_per_hour=RL_PENETRATION * FLOW_RATE, departLane="random", departSpeed=10) inflow.add(veh_type="human", edge="inflow_merge_2", vehs_per_hour=500, departLane="random", departSpeed=7.5) flow_params = dict( # name of the experiment exp_tag="stabilizing_triangle_merge", # name of the flow environment the experiment is running on env_name="AccelEnv", # name of the scenario class the experiment is running on scenario="TriangleMergeScenario", # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( render=False, sim_step=0.2, emission_path= '/Users/apple/Desktop/Berkeley/Repo/Flow/triange-data/'), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams(additional_params=ADDITIONAL_ENV_PARAMS), # network-related parameters (see flow.core.params.NetParams and the # scenario's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, # our inflows no_internal_links=False, additional_params=additional_net_params), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=InitialConfig(spacing="random", perturbation=1), )
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 triangle_scenario_example(highway_inflow, middle_length, emission_dir, render_=False): # create an empty vehicles object vehicles = VehicleParams() # add some vehicles to this object of type "human" vehicles.add(veh_id="human", acceleration_controller=(IDMController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), lane_change_params=SumoLaneChangeParams( lane_change_mode="strategic", ), num_vehicles=0) inflow = InFlows() inflow.add(veh_type="human", edge="inflow_highway_2", vehs_per_hour=highway_inflow, departSpeed=10, departLane="random") inflow.add(veh_type="human", edge="inflow_merge_2", vehs_per_hour=500, departSpeed=10, departLane="random") additional_net_params = { # length of the merge edge "merge_length": 100, # length of the highway leading to the merge "pre_merge_length": 200, # length of the highway past the merge "post_merge_length": 100, # number of lanes in the merge "merge_lanes": 2, # number of lanes in the highway "highway_lanes": 5, # max speed limit of the network "speed_limit": 30, } # we choose to make the main highway slightly longer additional_net_params["pre_merge_length"] = middle_length net_params = NetParams( inflows=inflow, # our inflows no_internal_links=False, additional_params=additional_net_params) sumo_params = SumoParams(render=render_, sim_step=0.2, emission_path=emission_dir) # '/Users/apple/Desktop/Berkeley/Repo/Flow/triange-data/' env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) initial_config = InitialConfig(spacing="random", perturbation=1) scenario = TriangleMergeScenario(name="custom-triangle-merge-example", vehicles=vehicles, net_params=net_params, initial_config=initial_config, inflow_edge_len=middle_length) env = AccelEnv(env_params, sumo_params, scenario) return Experiment(env)
def test_encoder_and_get_flow_params(self): """Tests both FlowParamsEncoder and get_flow_params. FlowParamsEncoder is used to serialize the data from a flow_params dict for replay by the visualizer later. Then, the get_flow_params method is used to try and read the parameters from the config file, and is checked to match expected results. """ # use a flow_params dict derived from flow/benchmarks/merge0.py vehicles = VehicleParams() vehicles.add( veh_id="human", acceleration_controller=(IDMController, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), # for testing coverage purposes, we add a routing controller routing_controller=(ContinuousRouter, {}), num_vehicles=5) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), num_vehicles=0) inflow = InFlows() inflow.add(veh_type="human", edge="inflow_highway", vehs_per_hour=1800, departLane="free", departSpeed=10) inflow.add(veh_type="rl", edge="inflow_highway", vehs_per_hour=200, departLane="free", departSpeed=10) inflow.add(veh_type="human", edge="inflow_merge", vehs_per_hour=100, departLane="free", departSpeed=7.5) flow_params = dict( exp_tag="merge_0", env_name="MergePOEnv", network="MergeNetwork", sim=SumoParams( restart_instance=True, sim_step=0.5, render=False, ), env=EnvParams( horizon=750, sims_per_step=2, warmup_steps=0, additional_params={ "max_accel": 1.5, "max_decel": 1.5, "target_velocity": 20, "num_rl": 5, }, ), net=NetParams( inflows=inflow, additional_params={ "merge_length": 100, "pre_merge_length": 500, "post_merge_length": 100, "merge_lanes": 1, "highway_lanes": 1, "speed_limit": 30, }, ), veh=vehicles, initial=InitialConfig(), tls=TrafficLightParams(), ) # create an config dict with space for the flow_params dict config = {"env_config": {}} # save the flow params for replay flow_json = json.dumps(flow_params, cls=FlowParamsEncoder, sort_keys=True, indent=4) config['env_config']['flow_params'] = flow_json # dump the config so we can fetch it json_out_file = 'params.json' with open(os.path.expanduser(json_out_file), 'w+') as outfile: json.dump(config, outfile, cls=FlowParamsEncoder, sort_keys=True, indent=4) # fetch values using utility function `get_flow_params` imported_flow_params = get_flow_params(config) # delete the created file os.remove(os.path.expanduser('params.json')) # test that this inflows are correct self.assertTrue(imported_flow_params["net"].inflows.__dict__ == flow_params["net"].inflows.__dict__) imported_flow_params["net"].inflows = None flow_params["net"].inflows = None # make sure the rest of the imported flow_params match the originals self.assertTrue(imported_flow_params["env"].__dict__ == flow_params["env"].__dict__) self.assertTrue(imported_flow_params["initial"].__dict__ == flow_params["initial"].__dict__) self.assertTrue(imported_flow_params["tls"].__dict__ == flow_params["tls"].__dict__) self.assertTrue(imported_flow_params["sim"].__dict__ == flow_params["sim"].__dict__) self.assertTrue(imported_flow_params["net"].__dict__ == flow_params["net"].__dict__) self.assertTrue( imported_flow_params["exp_tag"] == flow_params["exp_tag"]) self.assertTrue( imported_flow_params["env_name"] == flow_params["env_name"]) self.assertTrue( imported_flow_params["network"] == flow_params["network"]) def search_dicts(obj1, obj2): """Searches through dictionaries as well as lists of dictionaries recursively to determine if any two components are mismatched.""" for key in obj1.keys(): # if an next element is a list, either compare the two lists, # or if the lists contain dictionaries themselves, look at each # dictionary component recursively to check for mismatches if isinstance(obj1[key], list): if len(obj1[key]) > 0: if isinstance(obj1[key][0], dict): for i in range(len(obj1[key])): if not search_dicts(obj1[key][i], obj2[key][i]): return False elif obj1[key] != obj2[key]: return False # if the next element is a dict, run through it recursively to # determine if the separate elements of the dict match if isinstance(obj1[key], (dict, collections.OrderedDict)): if not search_dicts(obj1[key], obj2[key]): return False # if it is neither a list or a dictionary, compare to determine # if the two elements match elif obj1[key] != obj2[key]: # if the two elements that are being compared are objects, # make sure that they are the same type if not isinstance(obj1[key], type(obj2[key])): return False return True # make sure that the Vehicles class that was imported matches the # original one self.assertTrue( search_dicts(imported_flow_params["veh"].__dict__, flow_params["veh"].__dict__))
def highway_ramps_example(render=None): """ Perform a simulation of vehicles on a highway section with ramps. Parameters ---------- render: bool, optional Specifies whether or not to use the GUI during the simulation. Returns ------- exp: flow.core.experiment.Experiment A non-RL experiment demonstrating the performance of human-driven vehicles on a highway section with on and off ramps. """ sim_params = SumoParams(render=True, emission_path="./data/", sim_step=0.2, restart_instance=True) if render is not None: sim_params.render = render vehicles = VehicleParams() vehicles.add( veh_id="human", car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", # for safer behavior at the merges tau=1.5 # larger distance between cars ), lane_change_params=SumoLaneChangeParams(lane_change_mode=1621)) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS, sims_per_step=5, warmup_steps=0) inflows = InFlows() inflows.add(veh_type="human", edge="highway_0", vehs_per_hour=HIGHWAY_INFLOW_RATE, depart_lane="free", depart_speed="max", name="highway_flow") for i in range(len(additional_net_params["on_ramps_pos"])): inflows.add(veh_type="human", edge="on_ramp_{}".format(i), vehs_per_hour=ON_RAMPS_INFLOW_RATE, depart_lane="first", depart_speed="max", name="on_ramp_flow") net_params = NetParams(inflows=inflows, additional_params=additional_net_params) initial_config = InitialConfig() # no vehicles initially network = HighwayRampsNetwork(name="highway-ramp", vehicles=vehicles, net_params=net_params, initial_config=initial_config) env = AccelEnv(env_params, sim_params, network) return Experiment(env)
def test_reset_inflows(self): """Tests that the inflow change within the expected range when calling reset.""" # set a random seed for inflows to be the same every time np.random.seed(seed=123) sim_params = SumoParams(sim_step=0.5, restart_instance=True) vehicles = VehicleParams() vehicles.add(veh_id="human") vehicles.add(veh_id="followerstopper") # edge name, how many segments to observe/control, whether the segment # is controlled controlled_segments = [("1", 1, False), ("2", 2, True), ("3", 2, True), ("4", 2, True), ("5", 1, False)] num_observed_segments = [("1", 1), ("2", 3), ("3", 3), ("4", 3), ("5", 1)] env_params = EnvParams( additional_params={ "target_velocity": 40, "disable_tb": True, "disable_ramp_metering": True, "controlled_segments": controlled_segments, "symmetric": False, "observed_segments": num_observed_segments, "reset_inflow": True, # this must be set to True for the test "lane_change_duration": 5, "max_accel": 3, "max_decel": 3, "inflow_range": [1000, 2000] # this is what we're testing } ) inflow = InFlows() inflow.add(veh_type="human", edge="1", vehs_per_hour=1500, # the initial inflow we're checking for departLane="random", departSpeed=10) net_params = NetParams( inflows=inflow, no_internal_links=False, additional_params={"scaling": 1, "speed_limit": 23}) scenario = BottleneckScenario( name="bay_bridge_toll", vehicles=vehicles, net_params=net_params) env = DesiredVelocityEnv(env_params, sim_params, scenario) # reset the environment and get a new inflow rate env.reset() expected_inflow = 1343.178 # just from checking the new inflow # check that the first inflow rate is approximately 1500 for _ in range(500): env.step(rl_actions=None) self.assertAlmostEqual( env.k.vehicle.get_inflow_rate(250)/expected_inflow, 1, 1) # reset the environment and get a new inflow rate env.reset() expected_inflow = 1729.050 # just from checking the new inflow # check that the new inflow rate is approximately as expected for _ in range(500): env.step(rl_actions=None) self.assertAlmostEqual( env.k.vehicle.get_inflow_rate(250)/expected_inflow, 1, 1)
def get_flow_params(exp_num=1, horizon=6000, simulator="traci", multiagent=False): """Return the flow-specific parameters of the merge network. This scenario consists of a single-lane highway network with an on-ramp used to generate periodic perturbations to sustain congested behavior. In order to model the effect of p% CAV penetration on the network, every 100/pth vehicle is replaced with an automated vehicle whose actions are sampled from an RL policy. This benchmark is adapted from the following article: Kreidieh, Abdul Rahman, Cathy Wu, and Alexandre M. Bayen. "Dissipating stop-and-go waves in closed and open networks via deep reinforcement learning." 2018 21st International Conference on Intelligent Transportation Systems (ITSC). IEEE, 2018. Parameters ---------- exp_num : int experiment number * 0: 10% RL penetration, 5 max controllable vehicles * 1: 25% RL penetration, 13 max controllable vehicles * 2: 33% RL penetration, 17 max controllable vehicles horizon : int time horizon of a single rollout simulator : str the simulator used, one of {'traci', 'aimsun'} multiagent : bool whether the automated vehicles are via a single-agent policy or a shared multi-agent policy with the actions of individual vehicles assigned by a separate policy call Returns ------- dict flow-related parameters, consisting of the following keys: * exp_tag: name of the experiment * env_name: environment class of the flow environment the experiment is running on. (note: must be in an importable module.) * network: network class the experiment uses. * simulator: simulator that is used by the experiment (e.g. aimsun) * sim: simulation-related parameters (see flow.core.params.SimParams) * env: environment related parameters (see flow.core.params.EnvParams) * net: network-related parameters (see flow.core.params.NetParams and the network's documentation or ADDITIONAL_NET_PARAMS component) * veh: vehicles to be placed in the network at the start of a rollout (see flow.core.params.VehicleParams) * initial (optional): parameters affecting the positioning of vehicles upon initialization/reset (see flow.core.params.InitialConfig) * tls (optional): traffic lights to be introduced to specific nodes (see flow.core.params.TrafficLightParams) Raises ------ AssertionError if the `exp_num` parameter is a value other than 0, 1, or 2 """ assert exp_num in [0, 1, 2], "exp_num must be 0, 1, or 2" # inflow rate at the highway flow_rate = 2000 # percent of autonomous vehicles rl_penetration = [0.1, 0.25, 0.33][exp_num] # num_rl term (see ADDITIONAL_ENV_PARAMs) num_rl = [5, 13, 17][exp_num] # We consider a highway network with an upstream merging lane producing # shockwaves additional_net_params = ADDITIONAL_NET_PARAMS.copy() additional_net_params["merge_lanes"] = 1 additional_net_params["highway_lanes"] = 1 additional_net_params["pre_merge_length"] = 500 # RL vehicles constitute 5% of the total number of vehicles vehicles = VehicleParams() vehicles.add(veh_id="human", acceleration_controller=(IDMController, { "noise": 0.2 }), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), num_vehicles=5) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), num_vehicles=0) # Vehicles are introduced from both sides of merge, with RL vehicles # entering from the highway portion as well inflow = InFlows() inflow.add(veh_type="human", edge="inflow_highway", vehs_per_hour=(1 - rl_penetration) * flow_rate, depart_lane="free", depart_speed=10) inflow.add(veh_type="rl", edge="inflow_highway", vehs_per_hour=rl_penetration * flow_rate, depart_lane="free", depart_speed=10) inflow.add(veh_type="human", edge="inflow_merge", vehs_per_hour=100, depart_lane="free", depart_speed=7.5) return dict( # name of the experiment exp_tag="merge", # name of the flow environment the experiment is running on env_name=MultiAgentMergePOEnv if multiagent else MergePOEnv, # name of the network class the experiment is running on network=MergeNetwork, # simulator that is used by the experiment simulator=simulator, # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( sim_step=0.2, render=False, restart_instance=True, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=horizon, sims_per_step=5, warmup_steps=0, additional_params={ "max_accel": 1.5, "max_decel": 1.5, "target_velocity": 20, "num_rl": num_rl, }, ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, additional_params=additional_net_params, ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon init/reset # (see flow.core.params.InitialConfig) initial=InitialConfig(), )
def dissipating_waves(render=None): sim_params = SumoParams(sim_step=0.1, render=True, restart_instance=True) if render is not None: sim_params.render = render # Setup vehicle types vehicles = VehicleParams() vehicles.add(veh_id="human", acceleration_controller=(IDMController, { "noise": 0.2 }), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), num_vehicles=5) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), num_vehicles=0) # Vehicles are introduced from both sides of merge, with RL vehicles entering # from the highway portion as well inflow = InFlows() inflow.add(veh_type="human", edge="inflow_highway", vehs_per_hour=(1 - RL_PENETRATION) * FLOW_RATE, depart_lane="free", depart_speed=10) inflow.add(veh_type="rl", edge="inflow_highway", vehs_per_hour=RL_PENETRATION * FLOW_RATE, depart_lane="free", depart_speed=10) inflow.add(veh_type="human", edge="inflow_merge", vehs_per_hour=100, depart_lane="free", depart_speed=7.5) # Set parameters for the network additional_net_params = ADDITIONAL_NET_PARAMS.copy() additional_net_params["pre_merge_length"] = 600 additional_net_params["post_merge_length"] = 100 additional_net_params["merge_lanes"] = 1 additional_net_params["highway_lanes"] = 1 net_params = NetParams(inflows=inflow, additional_params=additional_net_params) # Setup the scenario initial_config = InitialConfig() scenario = MergeScenario(name='testing', vehicles=vehicles, net_params=net_params, initial_config=initial_config) # Setup the environment env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) env = MergePOEnv(env_params, sim_params, scenario) return Experiment(env)
max_speed=V_ENTER, speed_mode="right_of_way", ), routing_controller=(GridRouter, {}), num_vehicles=(N_LEFT + N_RIGHT) * N_COLUMNS + (N_BOTTOM + N_TOP) * N_ROWS) # 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") flow_params = dict( # name of the experiment exp_tag="grid_1", # name of the flow environment the experiment is running on env_name="PO_TrafficLightGridEnv", # name of the scenario class the experiment is running on
def bottleneck2_baseline(num_runs, render=True): """Run script for the bottleneck2 baseline. Parameters ---------- num_runs : int number of rollouts the performance of the environment is evaluated over render : bool, optional specifies whether to use the gui during execution Returns ------- flow.core.experiment.Experiment class needed to run simulations """ exp_tag = flow_params['exp_tag'] sim_params = flow_params['sim'] env_params = flow_params['env'] net_params = flow_params['net'] initial_config = flow_params.get('initial', InitialConfig()) traffic_lights = flow_params.get('tls', TrafficLightParams()) # we want no autonomous vehicles in the simulation vehicles = VehicleParams() vehicles.add(veh_id='human', car_following_params=SumoCarFollowingParams(speed_mode=9, ), routing_controller=(ContinuousRouter, {}), lane_change_params=SumoLaneChangeParams(lane_change_mode=0, ), num_vehicles=1 * SCALING) # only include human vehicles in inflows flow_rate = 2300 * SCALING inflow = InFlows() inflow.add(veh_type='human', edge='1', vehs_per_hour=flow_rate, departLane='random', departSpeed=10) net_params.inflows = inflow # modify the rendering to match what is requested sim_params.render = render # set the evaluation flag to True env_params.evaluate = True # import the network class module = __import__('flow.networks', fromlist=[flow_params['network']]) network_class = getattr(module, flow_params['network']) # create the network object network = network_class(name=exp_tag, vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) # import the environment class module = __import__('flow.envs', fromlist=[flow_params['env_name']]) env_class = getattr(module, flow_params['env_name']) # create the environment object env = env_class(env_params, sim_params, network) exp = Experiment(env) results = exp.run(num_runs, env_params.horizon) return np.mean(results['returns']), np.std(results['returns'])
def reset(self): """Reset the environment with a new inflow rate. The diverse set of inflows are used to generate a policy that is more robust with respect to the inflow rate. The inflow rate is update by creating a new scenario similar to the previous one, but with a new Inflow object with a rate within the additional environment parameter "inflow_range", which is a list consisting of the smallest and largest allowable inflow rates. **WARNING**: The inflows assume there are vehicles of type "followerstopper" and "human" within the VehicleParams object. """ add_params = self.env_params.additional_params if add_params.get("reset_inflow"): inflow_range = add_params.get("inflow_range") flow_rate = np.random.uniform(min(inflow_range), max(inflow_range)) * self.scaling # We try this for 100 trials in case unexpected errors during # instantiation. for _ in range(100): try: # introduce new inflows within the pre-defined inflow range inflow = InFlows() inflow.add( veh_type="followerstopper", # FIXME: make generic edge="1", vehs_per_hour=flow_rate * .1, departLane="random", departSpeed=10) inflow.add(veh_type="human", edge="1", vehs_per_hour=flow_rate * .9, departLane="random", departSpeed=10) # all other network parameters should match the previous # environment (we only want to change the inflow) additional_net_params = { "scaling": self.scaling, "speed_limit": self.net_params.additional_params['speed_limit'] } net_params = NetParams( inflows=inflow, no_internal_links=False, additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add( veh_id="human", # FIXME: make generic car_following_params=SumoCarFollowingParams( speed_mode=9, ), lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), lane_change_params=SumoLaneChangeParams( lane_change_mode=0, # 1621,#0b100000101, ), num_vehicles=1 * self.scaling) vehicles.add( veh_id="followerstopper", acceleration_controller=(RLController, {}), lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode=9, ), lane_change_params=SumoLaneChangeParams( lane_change_mode=0, ), num_vehicles=1 * self.scaling) # recreate the scenario object self.scenario = self.scenario.__class__( name=self.scenario.orig_name, vehicles=vehicles, net_params=net_params, initial_config=self.initial_config, traffic_lights=self.scenario.traffic_lights) observation = super().reset() # reset the timer to zero self.time_counter = 0 return observation except Exception as e: print('error on reset ', e) # perform the generic reset function observation = super().reset() # reset the timer to zero self.time_counter = 0 return observation
additional_net_params["pre_merge_length"] = 500 # RL vehicles constitute 5% of the total number of vehicles vehicles = VehicleParams() vehicles.add(veh_id="human", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams(speed_mode=9, ), num_vehicles=5) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), car_following_params=SumoCarFollowingParams(speed_mode=9, ), num_vehicles=0) # Vehicles are introduced from both sides of merge, with RL vehicles entering # from the highway portion as well inflow = InFlows() inflow.add(veh_type="human", edge="inflow_highway", vehs_per_hour=(1 - RL_PENETRATION) * FLOW_RATE, depart_lane="free", depart_speed=10) inflow.add(veh_type="rl", edge="inflow_highway", vehs_per_hour=RL_PENETRATION * FLOW_RATE, depart_lane="free", depart_speed=10) inflow.add(veh_type="human", edge="inflow_merge", vehs_per_hour=200, depart_lane="free", depart_speed=7.5)
def bay_bridge_example(render=None, use_inflows=False, use_traffic_lights=False): """ Perform a simulation of vehicles on the Oakland-San Francisco Bay Bridge. Parameters ---------- render: bool, optional specifies whether to use sumo's gui during execution use_inflows: bool, optional whether to activate inflows from the peripheries of the network use_traffic_lights: bool, optional whether to activate the traffic lights in the scenario Returns ------- exp: flow.core.SumoExperiment type A non-rl experiment demonstrating the performance of human-driven vehicles simulated by sumo on the Bay Bridge. """ sumo_params = SumoParams(sim_step=0.6, overtake_right=True) if render is not None: sumo_params.render = render sumo_car_following_params = SumoCarFollowingParams(speedDev=0.2) sumo_lc_params = SumoLaneChangeParams( lc_assertive=20, lc_pushy=0.8, lc_speed_gain=4.0, model="LC2013", # lcKeepRight=0.8 ) vehicles = Vehicles() vehicles.add(veh_id="human", acceleration_controller=(SumoCarFollowingController, {}), routing_controller=(BayBridgeRouter, {}), speed_mode="all_checks", lane_change_mode="no_lat_collide", sumo_car_following_params=sumo_car_following_params, sumo_lc_params=sumo_lc_params, num_vehicles=1400) additional_env_params = {} env_params = EnvParams(additional_params=additional_env_params) traffic_lights = TrafficLights() inflow = InFlows() if use_inflows: # south inflow.add(veh_type="human", edge="183343422", vehsPerHour=528, departLane="0", departSpeed=20) inflow.add(veh_type="human", edge="183343422", vehsPerHour=864, departLane="1", departSpeed=20) inflow.add(veh_type="human", edge="183343422", vehsPerHour=600, departLane="2", departSpeed=20) inflow.add(veh_type="human", edge="393649534", probability=0.1, departLane="0", departSpeed=20) # no data for this # west inflow.add(veh_type="human", edge="11189946", vehsPerHour=1752, departLane="0", departSpeed=20) inflow.add(veh_type="human", edge="11189946", vehsPerHour=2136, departLane="1", departSpeed=20) inflow.add(veh_type="human", edge="11189946", vehsPerHour=576, departLane="2", departSpeed=20) # north inflow.add(veh_type="human", edge="28413687#0", vehsPerHour=2880, departLane="0", departSpeed=20) inflow.add(veh_type="human", edge="28413687#0", vehsPerHour=2328, departLane="1", departSpeed=20) inflow.add(veh_type="human", edge="28413687#0", vehsPerHour=3060, departLane="2", departSpeed=20) inflow.add(veh_type="human", edge="11198593", probability=0.1, departLane="0", departSpeed=20) # no data for this inflow.add(veh_type="human", edge="11197889", probability=0.1, departLane="0", departSpeed=20) # no data for this # midway through bridge inflow.add(veh_type="human", edge="35536683", probability=0.1, departLane="0", departSpeed=20) # no data for this net_params = NetParams(inflows=inflow, no_internal_links=False) net_params.netfile = NETFILE # download the netfile from AWS if use_traffic_lights: my_url = "https://s3-us-west-1.amazonaws.com/flow.netfiles/" \ "bay_bridge_TL_all_green.net.xml" else: my_url = "https://s3-us-west-1.amazonaws.com/flow.netfiles/" \ "bay_bridge_junction_fix.net.xml" my_file = urllib.request.urlopen(my_url) data_to_write = my_file.read() with open( os.path.join(os.path.dirname(os.path.abspath(__file__)), NETFILE), "wb+") as f: f.write(data_to_write) initial_config = InitialConfig(spacing="uniform", min_gap=15) scenario = BayBridgeScenario(name="bay_bridge", vehicles=vehicles, traffic_lights=traffic_lights, net_params=net_params, initial_config=initial_config) env = BayBridgeEnv(env_params, sumo_params, scenario) return SumoExperiment(env, scenario)