def __init__(self, name, vehicles, net_params, initial_config=InitialConfig(), traffic_lights=TrafficLightParams(), detector_params=None): """Instantiate the base network class. Attributes ---------- name : str A tag associated with the network vehicles : flow.core.params.VehicleParams see flow/core/params.py net_params : flow.core.params.NetParams see flow/core/params.py initial_config : flow.core.params.InitialConfig see flow/core/params.py traffic_lights : flow.core.params.TrafficLightParams see flow/core/params.py """ self.orig_name = name # To avoid repeated concatenation upon reset self.name = name + time.strftime('_%Y%m%d-%H%M%S') + str(time.time()) self.vehicles = vehicles self.net_params = net_params self.initial_config = initial_config self.traffic_lights = traffic_lights self.detector_params = detector_params if detector_params else DetectorParams( ) # specify routes vehicles can take self.routes = self.specify_routes(net_params) if net_params.template is None and net_params.osm_path is None: # specify the attributes of the nodes self.nodes = self.specify_nodes(net_params) # collect the attributes of each edge self.edges = self.specify_edges(net_params) # specify the types attributes (default is None) self.types = self.specify_types(net_params) # specify the connection attributes (default is None) self.connections = self.specify_connections(net_params) # this is to be used if file paths other than the the network geometry # file is specified elif type(net_params.template) is dict: if 'rou' in net_params.template: veh, rou = self._vehicle_infos(net_params.template['rou']) vtypes = self._vehicle_type(net_params.template.get('vtype')) cf = self._get_cf_params(vtypes) lc = self._get_lc_params(vtypes) # add the vehicle types to the VehicleParams object for t in vtypes: vehicles.add(veh_id=t, length=vtypes[t]['length'], car_following_params=cf[t], lane_change_params=lc[t], num_vehicles=0) # add the routes of the vehicles that will be departed later # under the name of the vehicle. This will later be identified # by k.vehicles._add_departed self.routes = rou # vehicles to be added with different departure times self.template_vehicles = veh if 'add' in net_params.template: e1Detectors, e2Detectors = self._get_detector_params( net_params.template['add']) for e1Detector in e1Detectors: self.detector_params.add_induction_loop_detector( name=e1Detector['id'], lane_id=e1Detector['lane'], position=e1Detector['pos'], frequency=e1Detector['freq'], storage_file=e1Detector['file'], ) for e2Detector in e2Detectors: self.detector_params.add_lane_area_detector( name=e2Detector['id'], lane_id=e2Detector['lane'], position=e2Detector['pos'], frequency=e2Detector['freq'], storage_file=e2Detector['file'], ) self.types = None self.nodes = None self.edges = None self.connections = None # osm_path or template as type str else: self.nodes = None self.edges = None self.types = None self.connections = None # optional parameters, used to get positions from some global reference self.edge_starts = self.specify_edge_starts() self.internal_edge_starts = self.specify_internal_edge_starts() self.intersection_edge_starts = [] # this will be deprecated
def figure_eight_baseline(num_runs, render=True): """Run script for all figure eight baselines. 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 ------- 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()) # modify the rendering to match what is requested sim_params.render = render # set the evaluation flag to True env_params.evaluate = True # we want no autonomous vehicles in the simulation vehicles = VehicleParams() vehicles.add(veh_id='human', acceleration_controller=(IDMController, { 'noise': 0.2 }), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode='no_collide', ), num_vehicles=14) # import the scenario class module = __import__('flow.scenarios', fromlist=[flow_params['scenario']]) scenario_class = getattr(module, flow_params['scenario']) # create the scenario object scenario = scenario_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, scenario) exp = Experiment(env) results = exp.run(num_runs, env_params.horizon) avg_speed = np.mean(results['mean_returns']) return avg_speed
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 sumo's gui during execution Returns ------- flow.core.experiment.Experiment class needed to run simulations """ exp_tag = flow_params['exp_tag'] sumo_params = flow_params['sumo'] 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 = Vehicles() vehicles.add(veh_id='human', sumo_car_following_params=SumoCarFollowingParams( speed_mode=9, ), routing_controller=(ContinuousRouter, {}), sumo_lc_params=SumoLaneChangeParams( lane_change_mode=0, ), num_vehicles=1 * SCALING) # only include human vehicles in inflows flow_rate = 1900 * 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 sumo_params.render = render # set the evaluation flag to True env_params.evaluate = True # import the scenario class module = __import__('flow.scenarios', fromlist=[flow_params['scenario']]) scenario_class = getattr(module, flow_params['scenario']) # create the scenario object scenario = scenario_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, sumo_params, scenario) exp = Experiment(env) results = exp.run(num_runs, env_params.horizon) return np.mean(results['returns']), np.std(results['returns'])
def traffic_light_grid_example(render=None): """ Perform a simulation of vehicles on a traffic light 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 traffic light 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') network = TrafficLightGridNetwork( name="grid-intersection", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env = AccelEnv(env_params, sim_params, network, simulator='aimsun') return Experiment(env)
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 which traffic lights on and "no_internal_links" set to False 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: total_vehicles = 20 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 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(no_internal_links=False, 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 traffic_light_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 network pair for traffic light grid 1x1 test experiments. Parameters ---------- row_num: int, optional number of horizontal rows of edges in the traffic light grid network col_num: int, optional number of vertical columns of edges in the traffic light 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 traffic light 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}) flow_params = dict( # name of the experiment exp_tag="Grid1x1Test", # name of the flow environment the experiment is running on env_name=TrafficLightGridTestEnv, # name of the network class the experiment is running on network=TrafficLightGridNetwork, # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) 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.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=initial_config, # traffic lights to be introduced to specific nodes (see # flow.core.params.TrafficLightParams) tls=tl_logic) # create the network network = TrafficLightGridNetwork(name="Grid1x1Test", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) # create the environment env = TrafficLightGridTestEnv(env_params=env_params, sim_params=sim_params, network=network) # reset the environment env.reset() return env, network, flow_params
def __init__(self, name, vehicles, net_params, initial_config=InitialConfig(), traffic_lights=TrafficLightParams()): """Initialize an nxm grid scenario. The grid scenario consists of m vertical lanes and n horizontal lanes, with a total of nxm intersections where the vertical and horizontal edges meet. Requires from net_params: - grid_array: dictionary of grid array data, with the following keys - row_num: number of horizontal rows of edges - col_num: number of vertical columns of edges - inner_length: length of inner edges in the grid network - short_length: length of edges that vehicles start on - long_length: length of final edge in route - cars_top: number of cars starting at the edges heading to the top - cars_bot: number of cars starting at the edges heading to the bottom - cars_left: number of cars starting at the edges heading to the left - cars_right: number of cars starting at the edges heading to the right - horizontal_lanes: number of lanes in the horizontal edges - vertical_lanes: number of lanes in the vertical edges - speed_limit: speed limit for all edges. This may be represented as a float value, or a dictionary with separate values for vertical and horizontal lanes. In order for right-of-way dynamics to take place at the intersections, set "no_internal_links" in net_params to False. See flow/scenarios/base_scenario.py for description of params. """ optional = ["tl_logic"] for p in ADDITIONAL_NET_PARAMS.keys(): if p not in net_params.additional_params and p not in optional: raise KeyError('Network parameter "{}" not supplied'.format(p)) for p in ADDITIONAL_NET_PARAMS["grid_array"].keys(): if p not in net_params.additional_params["grid_array"]: raise KeyError( 'Grid array parameter "{}" not supplied'.format(p)) # this is a (mx1)x(nx1)x2 array # the third dimension is vertical length, horizontal length self.grid_array = net_params.additional_params["grid_array"] vertical_lanes = net_params.additional_params["vertical_lanes"] horizontal_lanes = net_params.additional_params["horizontal_lanes"] self.horizontal_junction_len = 2.9 + 3.3 * vertical_lanes self.vertical_junction_len = 2.9 + 3.3 * horizontal_lanes self.row_num = self.grid_array["row_num"] self.col_num = self.grid_array["col_num"] self.num_edges = (self.col_num+1) * self.row_num * 2 \ + (self.row_num+1) * self.col_num * 2 + self.row_num * self.col_num self.inner_length = self.grid_array["inner_length"] self.short_length = self.grid_array["short_length"] self.long_length = self.grid_array["long_length"] # this is a dictionary containing inner length, long outer length, # short outer length, and number of rows and columns self.grid_array = net_params.additional_params["grid_array"] self.node_mapping = defaultdict(list) self.name = "BobLoblawsLawBlog" # DO NOT CHANGE super().__init__(name, vehicles, net_params, initial_config, traffic_lights)
def setup_flow_params(args): DISABLE_TB = True DISABLE_RAMP_METER = True av_frac = args.av_frac if args.lc_on: lc_mode = 1621 else: lc_mode = 0 vehicles = VehicleParams() if not np.isclose(av_frac, 1): vehicles.add(veh_id="human", lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode=31, ), lane_change_params=SumoLaneChangeParams( lane_change_mode=lc_mode, ), num_vehicles=1) vehicles.add( veh_id="av", acceleration_controller=(RLController, {}), lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams(speed_mode=31, ), lane_change_params=SumoLaneChangeParams(lane_change_mode=0, ), num_vehicles=1) else: vehicles.add( veh_id="av", acceleration_controller=(RLController, {}), lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams(speed_mode=31, ), lane_change_params=SumoLaneChangeParams(lane_change_mode=0, ), num_vehicles=1) # flow rate flow_rate = 1900 * args.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)] if np.isclose(args.av_frac, 0.4): q_init = 1000 else: q_init = 600 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': True, 'lane_change_duration': 5, 'max_accel': 3, 'max_decel': 3, 'inflow_range': [args.low_inflow, args.high_inflow], 'start_inflow': flow_rate, 'congest_penalty': args.congest_penalty, 'communicate': args.communicate, "centralized_obs": args.central_obs, "aggregate_info": args.aggregate_info, "av_frac": args.av_frac, "congest_penalty_start": args.congest_penalty_start, "lc_mode": lc_mode, "life_penalty": args.life_penalty, 'keep_past_actions': args.keep_past_actions, "num_sample_seconds": args.num_sample_seconds, "speed_reward": args.speed_reward, 'fair_reward': False, # This doesn't do anything, remove 'exit_history_seconds': 0, # This doesn't do anything, remove # parameters for the staggering controller that we imitate "n_crit": 8, "q_max": 15000, "q_min": 200, "q_init": q_init, # "feedback_coeff": 1, # 'num_imitation_iters': args.num_imitation_iters, } # percentage of flow coming out of each lane inflow = InFlows() if not np.isclose(args.av_frac, 1.0): inflow.add(veh_type='human', edge='1', vehs_per_hour=flow_rate * (1 - args.av_frac), departLane='random', departSpeed=23.0) inflow.add(veh_type='av', edge='1', vehs_per_hour=flow_rate * args.av_frac, departLane='random', departSpeed=23.0) else: inflow.add(veh_type='av', edge='1', vehs_per_hour=flow_rate, departLane='random', departSpeed=23.0) 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': args.scaling, "speed_limit": 23.0} if args.imitate: env_name = 'MultiBottleneckImitationEnv' else: env_name = 'MultiBottleneckEnv' flow_params = dict( # name of the experiment exp_tag=args.exp_title, # name of the flow environment the experiment is running on env_name=env_name, # name of the scenario class the experiment is running on scenario='BottleneckScenario', # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( sim_step=args.sim_step, render=args.render, print_warnings=False, restart_instance=True, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( warmup_steps=int(0 / args.sim_step), sims_per_step=2, horizon=args.horizon, clip_actions=False, 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, 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.vehicles.Vehicles) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=InitialConfig( spacing='uniform', min_gap=5, lanes_distribution=float('inf'), edges_distribution=['2', '3', '4', '5'], ), # traffic lights to be introduced to specific nodes (see # flow.core.traffic_lights.TrafficLights) tls=traffic_lights, ) return flow_params
from flow.core.params import SumoParams # de parameters die we nog is hebben om sumo te runnen from flow.core.params import EnvParams from flow.core.experiment import Experiment #opzetten van het expirement # ? from flow.controllers.car_following_models import IDMController # reroute all vehicles to the initial set route from flow.controllers.routing_controllers import ContinuousRouter # toevoegen van extra parameters from flow.networks.ring import ADDITIONAL_NET_PARAMS from flow.envs.ring.accel import ADDITIONAL_ENV_PARAMS # naam van de simulatie name = "ring_example" #creating empty vehicleParamsobject/ trafficlightobject vehicles = VehicleParams() trafficL = TrafficLightParams() # simulation parameters sim_params = SumoParams(sim_step=0.1, render=True, emission_path='data') # 22 voertuigen toevoegen met bovenstaande acceleration and routing behavior vehicles.add("human", acceleration_controller=(IDMController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=22) net_params = NetParams(additional_params=ADDITIONAL_NET_PARAMS) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) # initial config voor de simulatie, initial_config = InitialConfig(spacing="uniform", perturbation=1) # vinden we de nodigeparameters mee terug print(ADDITIONAL_NET_PARAMS) print("runnen v/ h expirement")
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 grid1_baseline(num_runs, render=True): """Run script for the grid1 baseline. Parameters ---------- num_runs : int number of rollouts the performance of the environment is evaluated over render: bool, optional specifies whether to the gui during execution Returns ------- flow.core.experiment.Experiment class needed to run simulations """ sim_params = flow_params['sim'] env_params = flow_params['env'] # define the traffic light logic tl_logic = TrafficLightParams(baseline=False) phases = [{ 'duration': '31', 'minDur': '5', 'maxDur': '45', "state": "GrGr" }, { 'duration': '2', 'minDur': '2', 'maxDur': '2', "state": "yryr" }, { 'duration': '31', 'minDur': '5', 'maxDur': '45', "state": "rGrG" }, { 'duration': '2', 'minDur': '2', 'maxDur': '2', "state": "ryry" }] for i in range(N_ROWS * N_COLUMNS): tl_logic.add('center' + str(i), tls_type='actuated', phases=phases, programID=1) # modify the rendering to match what is requested sim_params.render = render # set the evaluation flag to True env_params.evaluate = True flow_params['env'].horizon = env_params.horizon exp = Experiment(flow_params) results = exp.run(num_runs) total_delay = np.mean(results['returns']) return total_delay
def traffic_light_grid_example(render=None, use_inflows=False): """ Perform a simulation of vehicles on a traffic light 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 traffic light 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 = OTMParams(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) network = TrafficLightGridNetwork(name="grid-intersection", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env = TestEnv(env_params, sim_params, network, simulator='otm') return Experiment(env)
# all other imports are standard from flow.core.params import VehicleParams from flow.core.params import NetParams from flow.core.params import InitialConfig from flow.core.params import EnvParams from flow.core.params import SumoParams from flow.core.params import TrafficLightParams # map data from Network.osm_network import OsmNetwork from flow.envs.base import Env import gym from abc import ABCMeta # if traffic is in osm, activate this tl_logic = TrafficLightParams(baseline=False) net_params = NetParams(osm_path='./Network/map.osm') env_params = EnvParams() sim_params = SumoParams(render=True) initial_config = InitialConfig() vehicles = VehicleParams() vehicles.add( 'human', num_vehicles=80, ) flow_params = dict( exp_tag='osm_test', env_name=TestEnv, network=OsmNetwork,
def __init__(self, name, vehicles, net_params, initial_config=InitialConfig(), traffic_lights=TrafficLightParams()): """Initialize a highway with on and off ramps network.""" for p in ADDITIONAL_NET_PARAMS.keys(): if p not in net_params.additional_params: raise KeyError('Network parameter "{}" not supplied'.format(p)) # load parameters into class params = net_params.additional_params self.highway_length = params['highway_length'] self.on_ramps_length = params['on_ramps_length'] self.off_ramps_length = params['off_ramps_length'] self.highway_lanes = params['highway_lanes'] self.on_ramps_lanes = params['on_ramps_lanes'] self.off_ramps_lanes = params['off_ramps_lanes'] self.highway_speed = params['highway_speed'] self.on_ramps_speed = params['on_ramps_speed'] self.off_ramps_speed = params['off_ramps_speed'] self.on_ramps_pos = params['on_ramps_pos'] self.off_ramps_pos = params['off_ramps_pos'] self.p = params['next_off_ramp_proba'] self.angle_on_ramps = params['angle_on_ramps'] self.angle_off_ramps = params['angle_off_ramps'] # generate position of all network nodes self.ramps_pos = sorted(self.on_ramps_pos + self.off_ramps_pos) self.nodes_pos = sorted( list(set([0] + self.ramps_pos + [self.highway_length]))) # highway_pos[x] = id of the highway node whose starting position is x self.highway_pos = {x: i for i, x in enumerate(self.nodes_pos)} # ramp_pos[x] = id of the ramp node whose intersection with the highway # is at position x self.ramp_pos = { x: "on_ramp_{}".format(i) for i, x in enumerate(self.on_ramps_pos) } self.ramp_pos.update({ x: "off_ramp_{}".format(i) for i, x in enumerate(self.off_ramps_pos) }) # make sure network is constructable if (len(self.ramps_pos) > 0 and (min(self.ramps_pos) <= 0 or max(self.ramps_pos) >= self.highway_length)): raise ValueError('All ramps positions should be positive and less ' 'than highway length. Current ramps positions: {}' '. Current highway length: {}.'.format( self.ramps_pos, self.highway_length)) if len(self.ramps_pos) != len(list(set(self.ramps_pos))): raise ValueError('Two ramps positions cannot be equal.') super().__init__(name, vehicles, net_params, initial_config, traffic_lights)
# features associated with the routes vehicles take "vtype": "/home/rong/Safe-RL-for-Driving/traci_pedestrian_crossing/pedcrossing.add.xml", # 和下方specify_routes一致 "rou": "/home/rong/Safe-RL-for-Driving/traci_pedestrian_crossing/data/pedcrossing.rou.xml", "trip": "/home/rong/Safe-RL-for-Driving/traci_pedestrian_crossing/pedestrians.trip.xml" }, inflows=inflow, ) # specify the edges vehicles can originate on initial_config = InitialConfig(edges_distribution=["WC"]) tl_logic = TrafficLightParams(baseline=False) phases = [{ "duration": "100000", "state": "GGGGr" }, { "duration": "4", "state": "yyyyr" }, { "duration": "10", "state": "rrrrG" }, { "duration": "10", "state": "rrrrr" }] tl_logic.add("C", phases=phases, programID="custom", offset="0")
def evaluate_policy(benchmark, _get_actions, _get_states=None): """Evaluate the performance of a controller on a predefined benchmark. Parameters ---------- benchmark : str name of the benchmark, must be printed as it is in the benchmarks folder; otherwise a ValueError will be raised _get_actions : method the mapping from states to actions for the RL agent(s) _get_states : method, optional a mapping from the environment object in Flow to some state, which overrides the _get_states method of the environment. Note that the same cannot be done for the actions. Returns ------- float mean of the evaluation return of the benchmark from NUM_RUNS number of simulations float standard deviation of the evaluation return of the benchmark from NUM_RUNS number of simulations Raises ------ ValueError If the specified benchmark is not available. """ if benchmark not in AVAILABLE_BENCHMARKS.keys(): raise ValueError( "benchmark {} is not available. Check spelling?".format(benchmark)) # get the flow params from the benchmark flow_params = AVAILABLE_BENCHMARKS[benchmark] exp_tag = flow_params["exp_tag"] sim_params = flow_params["sim"] vehicles = flow_params["veh"] env_params = flow_params["env"] env_params.evaluate = True # Set to true to get evaluation returns net_params = flow_params["net"] initial_config = flow_params.get("initial", InitialConfig()) traffic_lights = flow_params.get("tls", TrafficLightParams()) # import the environment and scenario classes module = __import__("flow.envs", fromlist=[flow_params["env_name"]]) env_class = getattr(module, flow_params["env_name"]) module = __import__("flow.scenarios", fromlist=[flow_params["scenario"]]) scenario_class = getattr(module, flow_params["scenario"]) # recreate the scenario and environment scenario = scenario_class( name=exp_tag, vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) # make sure the _get_states method of the environment is the one # specified by the user if _get_states is not None: class _env_class(env_class): def get_state(self): return _get_states(self) env_class = _env_class env = env_class( env_params=env_params, sim_params=sim_params, scenario=scenario) # create a Experiment object with the "rl_actions" method as # described in the inputs. Note that the state may not be that which is # specified by the environment. exp = Experiment(env=env) # run the experiment and return the reward res = exp.run( num_runs=NUM_RUNS, num_steps=env.env_params.horizon, rl_actions=_get_actions) return np.mean(res["returns"]), np.std(res["returns"])
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 = AimsunParams( sim_step=0.5, render=render, restart_instance=restart_instance) vehicles = VehicleParams() vehicles.add( veh_id="human", 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": 30/3.6} 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, simulator='aimsun') return Experiment(env)
from flow.core.params import NetParams net_params = NetParams(additional_params={ 'length': 500, 'lanes': 3, 'speed_limit': 30, 'resolution': 40 }) from flow.core.params import InitialConfig initial_config = InitialConfig(spacing="uniform", perturbation=1) from flow.core.params import TrafficLightParams traffic_lights = TrafficLightParams() from flow.envs.ring.accel import AccelEnv from flow.core.params import SumoParams sim_params = SumoParams(sim_step=0.1, render=True, emission_path='data') from flow.envs.ring.accel import ADDITIONAL_ENV_PARAMS print(ADDITIONAL_ENV_PARAMS) from flow.core.params import EnvParams env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)
def variable_lanes_exp_setup(sim_params=None, vehicles=None, env_params=None, net_params=None, initial_config=None, traffic_lights=None): """ Create an environment and network variable-lane ring road. Each edge in this network can have a different number of lanes. Used for test purposes. Parameters ---------- sim_params : flow.core.params.SumoParams sumo-related configuration parameters, defaults to a time step of 0.1s and no sumo-imposed failsafe on human or rl vehicles vehicles : Vehicles type vehicles to be placed in the network, default is one vehicles with an IDM acceleration controller and ContinuousRouter routing controller. env_params : flow.core.params.EnvParams environment-specific parameters, defaults to a environment with no failsafes, where other parameters do not matter for non-rl runs net_params : flow.core.params.NetParams network-specific configuration parameters, defaults to a figure eight with a 30 m radius initial_config : flow.core.params.InitialConfig specifies starting positions of vehicles, defaults to evenly distributed vehicles across the length of the network traffic_lights: flow.core.params.TrafficLightParams traffic light signals, defaults to no traffic lights in the network """ logging.basicConfig(level=logging.WARNING) if sim_params is None: # set default sim_params configuration sim_params = SumoParams(sim_step=0.1, render=False) if vehicles is None: # set default vehicles configuration vehicles = VehicleParams() vehicles.add(veh_id="idm", acceleration_controller=(IDMController, {}), car_following_params=SumoCarFollowingParams( speed_mode="aggressive", ), routing_controller=(ContinuousRouter, {}), num_vehicles=1) if env_params is None: # set default env_params configuration additional_env_params = { "target_velocity": 8, "max_accel": 1, "max_decel": 1, "sort_vehicles": False } env_params = EnvParams(additional_params=additional_env_params) if net_params is None: # set default net_params configuration additional_net_params = { "length": 230, "lanes": 1, "speed_limit": 30, "resolution": 40 } net_params = NetParams(additional_params=additional_net_params) if initial_config is None: # set default initial_config configuration initial_config = InitialConfig() if traffic_lights is None: # set default to no traffic lights traffic_lights = TrafficLightParams() flow_params = dict( # name of the experiment exp_tag="VariableLaneRingRoadTest", # name of the flow environment the experiment is running on env_name=AccelEnv, # name of the network class the experiment is running on network=VariableLanesNetwork, # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) 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.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization/ # reset (see flow.core.params.InitialConfig) initial=initial_config, # traffic lights to be introduced to specific nodes (see # flow.core.params.TrafficLightParams) tls=traffic_lights, ) # create the network network = VariableLanesNetwork(name="VariableLaneRingRoadTest", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) # create the environment env = AccelEnv(env_params=env_params, sim_params=sim_params, network=network) # reset the environment env.reset() return env, network, flow_params
def make_create_env(params, version=0, render=None): """Create a parametrized flow environment compatible with OpenAI gym. This environment creation method allows for the specification of several key parameters when creating any flow environment, including the requested environment and scenario classes, and the inputs needed to make these classes generalizable to networks of varying sizes and shapes, and well as varying forms of control (e.g. AVs, automated traffic lights, etc...). This method can also be used to recreate the environment a policy was trained on and assess it performance, or a modified form of the previous environment may be used to profile the performance of the policy on other types of networks. Parameters ---------- params : 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 - scenario: name of the scenario 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 scenario's documentation or ADDITIONAL_NET_PARAMS component) - veh: vehicles to be placed in the network at the start of a rollout (see flow.core.vehicles.Vehicles) - 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) version : int, optional environment version number render : bool, optional specifies whether to use the gui during execution. This overrides the render attribute in SumoParams Returns ------- function method that calls OpenAI gym's register method and make method str name of the created gym environment """ exp_tag = params["exp_tag"] env_name = params["env_name"] + '-v{}'.format(version) module = __import__("flow.scenarios", fromlist=[params["scenario"]]) scenario_class = getattr(module, params["scenario"]) env_params = params['env'] net_params = params['net'] initial_config = params.get('initial', InitialConfig()) traffic_lights = params.get("tls", TrafficLightParams()) def create_env(*_): sim_params = deepcopy(params['sim']) vehicles = deepcopy(params['veh']) scenario = scenario_class( name=exp_tag, vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights, ) # accept new render type if not set to None sim_params.render = render or sim_params.render # check if the environment is a single or multiagent environment, and # get the right address accordingly single_agent_envs = [env for env in dir(flow.envs) if not env.startswith('__')] if params['env_name'] in single_agent_envs: env_loc = 'flow.envs' else: env_loc = 'flow.multiagent_envs' try: register( id=env_name, entry_point=env_loc + ':{}'.format(params["env_name"]), kwargs={ "env_params": env_params, "sim_params": sim_params, "scenario": scenario, "simulator": params['simulator'] }) except Exception: pass return gym.envs.make(env_name) return create_env, env_name
return initial, net 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": "GrGr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yryr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rGrG"
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. """ 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, {}), 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)
flow_rate = 2000 * 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: 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) flow_params = dict( # name of the experiment exp_tag="bottleneck_2", # name of the flow environment the experiment is running on
# specify vehicle parameters to be added vehicles = VehicleParams() vehicles.add( veh_id="human", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( min_gap=2.5, decel=7.5, # avoid collisions at emergency stops speed_mode="right_of_way", ), routing_controller=(GridRouter, {}), num_vehicles=0) # Set up traffic light parameters tl_logic = TrafficLightParams(baseline=False) phases = [{ "duration": "31", "minDur": "8", "maxDur": "45", "state": "GrGr" }, { "duration": "4", "minDur": "3", "maxDur": "6", "state": "yryr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rGrG"
def variable_lanes_exp_setup(sim_params=None, vehicles=None, env_params=None, net_params=None, initial_config=None, traffic_lights=None): """ Create an environment and scenario variable-lane ring road. Each edge in this scenario can have a different number of lanes. Used for test purposes. Parameters ---------- sim_params : flow.core.params.SumoParams sumo-related configuration parameters, defaults to a time step of 0.1s and no sumo-imposed failsafe on human or rl vehicles vehicles : Vehicles type vehicles to be placed in the network, default is one vehicles with an IDM acceleration controller and ContinuousRouter routing controller. env_params : flow.core.params.EnvParams environment-specific parameters, defaults to a environment with no failsafes, where other parameters do not matter for non-rl runs net_params : flow.core.params.NetParams network-specific configuration parameters, defaults to a figure eight with a 30 m radius and "no_internal_links" set to False initial_config : flow.core.params.InitialConfig specifies starting positions of vehicles, defaults to evenly distributed vehicles across the length of the network traffic_lights: flow.core.params.TrafficLightParams traffic light signals, defaults to no traffic lights in the network """ logging.basicConfig(level=logging.WARNING) if sim_params is None: # set default sim_params configuration sim_params = SumoParams(sim_step=0.1, render=False) if vehicles is None: # set default vehicles configuration vehicles = VehicleParams() vehicles.add(veh_id="idm", acceleration_controller=(IDMController, {}), car_following_params=SumoCarFollowingParams( speed_mode="aggressive", ), routing_controller=(ContinuousRouter, {}), num_vehicles=1) if env_params is None: # set default env_params configuration additional_env_params = { "target_velocity": 8, "max_accel": 1, "max_decel": 1, "sort_vehicles": False } env_params = EnvParams(additional_params=additional_env_params) if net_params is None: # set default net_params configuration additional_net_params = { "length": 230, "lanes": 1, "speed_limit": 30, "resolution": 40 } net_params = NetParams(additional_params=additional_net_params) if initial_config is None: # set default initial_config configuration initial_config = InitialConfig() if traffic_lights is None: # set default to no traffic lights traffic_lights = TrafficLightParams() # create the scenario scenario = VariableLanesScenario(name="VariableLaneRingRoadTest", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) # create the environment env = AccelEnv(env_params=env_params, sim_params=sim_params, scenario=scenario) # reset the environment env.reset() return env, scenario
data_file = 'flow/core/kernel/network/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 = {} section_type = model.getType("GKSection") for types in model.getCatalog().getUsedSubTypesFromType(section_type): for s in types.itervalues(): s_id = s.getId() num_lanes = s.getNbFullLanes() length = s.length2D()
def grid1_baseline(num_runs, render=True): """Run script for the grid1 baseline. Parameters ---------- num_runs : int number of rollouts the performance of the environment is evaluated over render: bool, optional specifies whether to 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'] vehicles = flow_params['veh'] env_params = flow_params['env'] net_params = flow_params['net'] initial_config = flow_params.get('initial', InitialConfig()) # define the traffic light logic tl_logic = TrafficLightParams(baseline=False) phases = [{ 'duration': '31', 'minDur': '5', 'maxDur': '45', "state": "GrGr" }, { 'duration': '2', 'minDur': '2', 'maxDur': '2', "state": "yryr" }, { 'duration': '31', 'minDur': '5', 'maxDur': '45', "state": "rGrG" }, { 'duration': '2', 'minDur': '2', 'maxDur': '2', "state": "ryry" }] for i in range(N_ROWS * N_COLUMNS): tl_logic.add('center' + str(i), tls_type='actuated', phases=phases, programID=1) # 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=tl_logic) # 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) total_delay = np.mean(results['returns']) return total_delay
def para_produce_rl(HORIZON=3000): # Create default environment parameters env_params = EnvParams() # Vehicle definition vehicles = VehicleParams() num_vehicles = 1 vehicles.add( veh_id="human", routing_controller=(GridRouter, {}), lane_change_controller=(SimLaneChangeController, {}), car_following_params=SumoCarFollowingParams( min_gap=2.5, decel=7.5, # avoid collisions at emergency stops ), lane_change_params=SumoLaneChangeParams( lane_change_mode=1621, ), num_vehicles=num_vehicles) # whether to allow turns at intersections ALLOW_TURNS = False # initialize traffic lights, used when you want define your own traffic lights tl_logic = TrafficLightParams(baseline=False) # To see static traffic lights in action, the `TrafficLightParams` object should be instantiated with `baseline=False` # when use off_ramp_grid.net.xml file, you should use a phase state example as "GGGgrrrrGGGgrrrr" # when use off_ramp_grid_turn.net.xml file, you should use a phase state example as "GGGggrrrrrGGGggrrrrr" if ALLOW_TURNS: phases = [{ "duration": "31", "minDur": "8", "maxDur": "45", # for actuated traffic lights, you can add these optional values below # "maxGap": int, describes the maximum time gap between successive vehicle sthat will cause the current phase to be prolonged # "detectorGap": int, determines the time distance between the (automatically generated) detector and the stop line in seconds # "showDetectors": bool, toggles whether or not detectors are shown in sumo-gui "state": "GGGggrrrrrGGGggrrrrr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yyyyyrrrrryyyyyrrrrr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rrrrrGGGggrrrrrGGGgg" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "rrrrryyyyyrrrrryyyyy" }] tl_logic.add("center0", phases=phases, programID=1, detectorGap=1,tls_type="actuated") tl_logic.add("center1", phases=phases, programID=1, detectorGap=1,tls_type="actuated") tl_logic.add("center2", phases=phases, programID=1, detectorGap=1,tls_type="actuated") tl_logic.add("center3", phases=phases, programID=1, detectorGap=1,tls_type="actuated") else: phases = [{ "duration": "31", "minDur": "8", "maxDur": "45", # for actuated traffic lights, you can add these optional values below # "maxGap": int, describes the maximum time gap between successive vehicle sthat will cause the current phase to be prolonged # "detectorGap": int, determines the time distance between the (automatically generated) detector and the stop line in seconds # "showDetectors": bool, toggles whether or not detectors are shown in sumo-gui "state": "GGGgrrrrGGGgrrrr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yyyyrrrryyyyrrrr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rrrrGGGgrrrrGGGg" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "rrrryyyyrrrryyyy" }] # THIS IS A BUG THAT I DON'T KNOW WHY IT HAPPENS!!!!!! phase0 = [{ "duration": "31", "minDur": "8", "maxDur": "45", "state": "GGrrGGrrGGrrGGrr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yyrryyrryyrryyrr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rrGGrrGGrrGGrrGG" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "rryyrryyrryyrryy" }] tl_logic.add("center0", phases=phases, programID=1, detectorGap=1,tls_type="actuated") tl_logic.add("center1", phases=phases, programID=1, detectorGap=1,tls_type="actuated") tl_logic.add("center2", phases=phases, programID=1, detectorGap=1,tls_type="actuated") tl_logic.add("center3", phases=phases, programID=1, detectorGap=1,tls_type="actuated") flow_params = dict( exp_tag='offramp_multiagent_inflow_1.0_speed_20', env_name=MultiTrafficLightGridPOEnv, network=offRampGrid, simulator='traci', sim=SumoParams( sim_step=0.1, render=False, #emission_path='./data', restart_instance=True, ), env=EnvParams( horizon=3000, additional_params=ADDITIONAL_ENV_PARAMS.copy(), ), net=net_params, veh=vehicles, initial=initial_config, # used when you define your own traffic lights #tls=tl_logic, ) #flow_params['env'].horizon = HORIZON return flow_params
def __init__(self, name, vehicles, net_params, initial_config=InitialConfig(), traffic_lights=TrafficLightParams()): """Instantiate the base scenario class. Attributes ---------- name : str A tag associated with the scenario vehicles : flow.core.params.VehicleParams see flow/core/params.py net_params : flow.core.params.NetParams see flow/core/params.py initial_config : flow.core.params.InitialConfig see flow/core/params.py traffic_lights : flow.core.params.TrafficLightParams see flow/core/params.py """ # Invoke serializable if using rllab if Serializable is not object: Serializable.quick_init(self, locals()) self.orig_name = name # To avoid repeated concatenation upon reset self.name = name + time.strftime('_%Y%m%d-%H%M%S') + str(time.time()) self.vehicles = vehicles self.net_params = net_params self.initial_config = initial_config self.traffic_lights = traffic_lights # specify routes vehicles can take self.routes = self.specify_routes(net_params) if net_params.template is None and net_params.osm_path is None: # specify the attributes of the nodes self.nodes = self.specify_nodes(net_params) # collect the attributes of each edge self.edges = self.specify_edges(net_params) # specify the types attributes (default is None) self.types = self.specify_types(net_params) # specify the connection attributes (default is None) self.connections = self.specify_connections(net_params) # this is to be used if file paths other than the the network geometry # file is specified elif type(net_params.template) is dict: if 'rou' in net_params.template: veh, rou = self._vehicle_infos(net_params.template['rou']) vtypes = self._vehicle_type(net_params.template.get('vtype')) cf = self._get_cf_params(vtypes) lc = self._get_lc_params(vtypes) # add the vehicle types to the VehicleParams object for t in vtypes: vehicles.add(veh_id=t, car_following_params=cf[t], lane_change_params=lc[t], num_vehicles=0) # add the routes of the vehicles that will be departed later # under the name of the vehicle. This will later be identified # by k.vehicles._add_departed self.routes = rou # vehicles to be added with different departure times self.template_vehicles = veh self.types = None self.nodes = None self.edges = None self.connections = None # osm_path or template as type str else: self.nodes = None self.edges = None self.types = None self.connections = None # optional parameters, used to get positions from some global reference self.edge_starts = self.specify_edge_starts() self.internal_edge_starts = self.specify_internal_edge_starts() self.intersection_edge_starts = [] # this will be deprecated
def da_yuan_example(render=None, use_inflows=True): """ Perform a simulation of vehicles on a traffic light 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 traffic light grid. """ v_enter = 10 sim_params = SumoParams(sim_step=0.05, 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=6) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) tl_logic = TrafficLightParams(baseline=False) phases = [{ "duration": "31", "minDur": "8", "maxDur": "45", "state": "rrGGGG" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "rryyyy" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "GGrrrr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yyrrrr" }] tl_logic.add("inner0", phases=phases, programID=1) tl_logic.add("inner1", phases=phases, programID=1) tl_logic.add("inner2", phases=phases, programID=1, tls_type="actuated") tl_logic.add("inner3", phases=phases, programID=1, tls_type="actuated") additional_net_params = { "speed_limit": 35, "horizontal_lanes": 1, "vertical_lanes": 1 } if use_inflows: initial_config, net_params = get_flow_params( additional_net_params=additional_net_params) else: initial_config, net_params = get_non_flow_params(enter_speed=v_enter) network = DaYuanNetwork(name="grid-intersection", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env = AccelEnv(env_params, sim_params, network) return Experiment(env)