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, 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
from flow.core.params import TrafficLightParams from flow.controllers import IDMController from flow.core.params import SumoCarFollowingParams from ray import tune HORIZON = 3000 env_params = EnvParams(horizon=HORIZON) initial_config = InitialConfig() le_dir = "/Schreibtisch/wrapper/Schreibtisch/sumo_files/personal_sumo_files" from flow.core.params import SumoParams sim_params = SumoParams(render=False, sim_step=1, restart_instance=True) vehicles = VehicleParams() from flow.core.params import InFlows inflow = InFlows() inflow.add(veh_type="human", edge="right_east", probability=0.04) inflow.add(veh_type="human", edge="right_south", probability=0.04) inflow.add(veh_type="human", edge="right_north", probability=0.04) inflow.add(veh_type="human", edge="left_north", probability=0.04) inflow.add(veh_type="human", edge="left_south", probability=0.04) inflow.add(veh_type="human", edge="left_west", probability=0.04) net_params = NetParams( inflows=inflow, template={
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 = 512 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', 3, True), ('3', 3, False), ('4', 2, False), ('5', 1, False)] num_observed_segments = [('1', 1 * args.state_space_scaling), ('2', 3 * args.state_space_scaling), ('3', 3 * args.state_space_scaling), ('4', 3 * args.state_space_scaling), ('5', 1 * args.state_space_scaling)] 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, "life_penalty": args.life_penalty, "av_frac": args.av_frac, "lc_mode": lc_mode, "congest_penalty_start": args.congest_penalty_start, "num_sample_seconds": args.num_sample_seconds, "speed_reward": args.speed_reward, "fair_reward": args.fair_reward, "exit_history_seconds": args.exit_history_seconds, } # 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=10.0) inflow.add(veh_type='av', edge='1', vehs_per_hour=flow_rate * args.av_frac, departLane='random', departSpeed=10.0) else: inflow.add(veh_type='av', edge='1', vehs_per_hour=flow_rate, departLane='random', departSpeed=10.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} flow_params = dict( # name of the experiment exp_tag=args.exp_title, # name of the flow environment the experiment is running on env_name='DesiredVelocityEnv', # 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(args.warmup_steps / (args.sims_per_step * args.sim_step)), sims_per_step=args.sims_per_step, horizon=args.horizon, 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
def grid_example(render=None, use_inflows=False): """ Perform a simulation of vehicles on a grid. Parameters ---------- render: bool, optional specifies whether to use the gui during execution use_inflows : bool, optional set to True if you would like to run the experiment with inflows of vehicles from the edges, and False otherwise Returns ------- exp: flow.core.experiment.Experiment A non-rl experiment demonstrating the performance of human-driven vehicles and balanced traffic lights on a grid. """ v_enter = 10 inner_length = 300 long_length = 500 short_length = 300 n_rows = 2 n_columns = 3 num_cars_left = 20 num_cars_right = 20 num_cars_top = 20 num_cars_bot = 20 tot_cars = (num_cars_left + num_cars_right) * n_columns \ + (num_cars_top + num_cars_bot) * n_rows grid_array = { "short_length": short_length, "inner_length": inner_length, "long_length": long_length, "row_num": n_rows, "col_num": n_columns, "cars_left": num_cars_left, "cars_right": num_cars_right, "cars_top": num_cars_top, "cars_bot": num_cars_bot } sim_params = SumoParams(sim_step=0.1, render=True) if render is not None: sim_params.render = render vehicles = VehicleParams() vehicles.add( veh_id="human", routing_controller=(GridRouter, {}), car_following_params=SumoCarFollowingParams( min_gap=2.5, decel=7.5, # avoid collisions at emergency stops ), num_vehicles=tot_cars) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) tl_logic = TrafficLightParams(baseline=False) phases = [{ "duration": "31", "minDur": "8", "maxDur": "45", "state": "GrGrGrGrGrGr" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "yryryryryryr" }, { "duration": "31", "minDur": "8", "maxDur": "45", "state": "rGrGrGrGrGrG" }, { "duration": "6", "minDur": "3", "maxDur": "6", "state": "ryryryryryry" }] tl_logic.add("center0", phases=phases, programID=1) tl_logic.add("center1", phases=phases, programID=1) tl_logic.add("center2", phases=phases, programID=1, tls_type="actuated") additional_net_params = { "grid_array": grid_array, "speed_limit": 35, "horizontal_lanes": 1, "vertical_lanes": 1 } if use_inflows: initial_config, net_params = get_flow_params( col_num=n_columns, row_num=n_rows, additional_net_params=additional_net_params) else: initial_config, net_params = get_non_flow_params( enter_speed=v_enter, add_net_params=additional_net_params) scenario = SimpleGridScenario(name="grid-intersection", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=tl_logic) env = AccelEnv(env_params, sim_params, scenario) return Experiment(env)
def __init__(self): self.colors = [(255,0,0), (0,255,0), (0,0,255), (255, 255,0), (255, 0, 255), (0, 255,255), (55,0,0), (0,55,0), (0,0,55), (55, 55,0), (55, 0, 55), (0, 55,55),] env_params = EnvParams( additional_params={"switch_time": CONFIG.SWITCH_TIME,"tl_type": CONFIG.TL_TYPE, "discrete": CONFIG.DISCRETE, "observation_distance": CONFIG.OBSERVATION_DISTANCE}, horizon=CONFIG.HORIZON) sim_params = SumoParams(sim_step=CONFIG.SIM_STEP, render=CONFIG.RENDER,) """ for scenario, we need to do a lot of work! It needs vehicles. Vehicles need router etc: scenario vehicles SimCarFollowingController SumoCarFollowingParams GridRouter num_vehicles net_params grid_array additional_net_params initial_config traffic_lights """ TOTAL_CARS = (CONFIG.NUM_CARS_LEFT + CONFIG.NUM_CARS_RIGHT) * CONFIG.N_COLUMNS + (CONFIG.NUM_CARS_BOT + CONFIG.NUM_CARS_TOP) * CONFIG.N_ROWS vehicles = VehicleParams() vehicles.add( veh_id='idm', acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( min_gap=CONFIG.MINGAP, max_speed=CONFIG.V_ENTER, speed_mode="all_checks", ), routing_controller=(GridRouter, {}), num_vehicles=TOTAL_CARS) grid_array = { "short_length": CONFIG.SHORT_LENGTH, "inner_length": CONFIG.INNER_LENGTH, "long_length": CONFIG.LONG_LENGTH, "row_num": CONFIG.N_ROWS, "col_num": CONFIG.N_COLUMNS, "cars_left": CONFIG.NUM_CARS_LEFT, "cars_right": CONFIG.NUM_CARS_RIGHT, "cars_top": CONFIG.NUM_CARS_TOP, "cars_bot": CONFIG.NUM_CARS_BOT } additional_net_params = { 'speed_limit': CONFIG.SPEED_LIMIT, 'grid_array': grid_array, 'horizontal_lanes': CONFIG.HORIZONTAL_LANES, 'vertical_lanes': CONFIG.VERTICAL_LANES } net_params= NetParams(no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig(spacing='custom', additional_params={'enter_speed': CONFIG.V_ENTER}) scenario = SimpleGridScenario( name="thisGetsDiscarded", vehicles=vehicles, net_params=net_params, initial_config=initial_config ) super().__init__(env_params, sim_params, scenario ) # since there is no reference to kernel_api.vehicle.getLength, I guestimate it. Should be used just for normalization. self.stop_penalty_weight=CONFIG.STOP_PENALTY_WEIGHT self.max_vehicle_density = 1.0/(2*CONFIG.MINGAP) self.edge_names = scenario.get_edge_names() self.traffic_lights = np.zeros( 4 * self.num_traffic_lights ) for i in range (self.num_traffic_lights): self.traffic_lights[4*i:4*i+4] = [1, 0, 0, 0] self.last_change = np.zeros( self.num_traffic_lights ) self.observation_mode = CONFIG.OBSERVATION_MODE if self.observation_mode == "Q_WEIGHT": self.observed_queue_max = 0.0 for i in np.linspace(0, self.observation_distance, np.floor(self.observation_distance*self.max_vehicle_density)): self.observed_queue_max += self.observation_distance - i self.observed_queue = np.zeros( len(self.edge_names) ) if self.observation_mode == "SEGMENT": self.segment_len = CONFIG.SEGMENT_LENGTH self.observed_segment_max = self.segment_len * self.max_vehicle_density self.total_nof_segments = 0 for edge_id in self.edge_names: self.total_nof_segments += int(np.ceil (self.k.scenario.edge_length(edge_id)/self.segment_len)) self.nof_segments = np.zeros(self.total_nof_segments, dtype=int) for edge_index , edge_id in enumerate(self.edge_names): self.nof_segments [edge_index] = int(np.ceil (self.k.scenario.edge_length(edge_id)/self.segment_len)) assert self.nof_segments [edge_index] < len(self.colors) self.observed_segments = np.zeros( self.total_nof_segments )
def 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)
def run_task(_): """Implement the run_task method needed to run experiments with rllab.""" sim_params = SumoParams(render=True, sim_step=0.2, restart_instance=True) # 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, departLane="free", departSpeed=10) inflow.add(veh_type="rl", edge="inflow_highway", vehs_per_hour=RL_PENETRATION * FLOW_RATE, departLane="free", departSpeed=10) inflow.add(veh_type="human", edge="inflow_merge", vehs_per_hour=100, departLane="free", departSpeed=7.5) additional_env_params = { "target_velocity": 25, "num_rl": NUM_RL, "max_accel": 1.5, "max_decel": 1.5 } env_params = EnvParams(horizon=HORIZON, sims_per_step=5, warmup_steps=0, additional_params=additional_env_params) 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 net_params = NetParams(inflows=inflow, no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig(spacing="uniform", lanes_distribution=float("inf")) scenario = MergeScenario(name="merge-rl", vehicles=vehicles, net_params=net_params, initial_config=initial_config) env_name = "WaveAttenuationMergePOEnv" 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) env = normalize(env) policy = GaussianMLPPolicy( env_spec=env.spec, hidden_sizes=(32, 32, 32), ) baseline = LinearFeatureBaseline(env_spec=env.spec) algo = TRPO( env=env, policy=policy, baseline=baseline, batch_size=HORIZON * N_ROLLOUTS, max_path_length=HORIZON, n_itr=1000, # whole_paths=True, discount=0.999, ) algo.train(),
def get_flow_params(evaluate=False, multiagent=False): """Return the flow-specific parameters of the single lane highway network. Parameters ---------- evaluate : bool whether to compute the evaluation reward 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) """ # SET UP PARAMETERS FOR THE SIMULATION # number of steps per rollout HORIZON = 1800 # inflow rate on the highway in vehicles per hour HIGHWAY_INFLOW_RATE = 2000 # percentage of autonomous vehicles compared to human vehicles on highway PENETRATION_RATE = 0.1 # SET UP PARAMETERS FOR THE NETWORK additional_net_params = ADDITIONAL_NET_PARAMS.copy() additional_net_params.update({ # length of the highway "length": 2500, # number of lanes "lanes": 1, # speed limit for all edges "speed_limit": 30, # number of edges to divide the highway into "num_edges": 2 }) # CREATE VEHICLE TYPES AND INFLOWS vehicles = VehicleParams() inflows = InFlows() # human vehicles vehicles.add( "human", num_vehicles=0, lane_change_params=SumoLaneChangeParams( lane_change_mode="strategic", ), car_following_params=SumoCarFollowingParams(min_gap=0, ), acceleration_controller=(IDMController, { "a": 0.3, "b": 2.0, "noise": 0.5, "ignore_noise": [(0, 500)], }), ) inflows.add(veh_type="human", edge="highway_0", vehs_per_hour=int(HIGHWAY_INFLOW_RATE * (1 - PENETRATION_RATE)), depart_lane="free", depart_speed=15, name="idm_highway_inflow") # automated vehicles if PENETRATION_RATE > 0.0: vehicles.add( "av", num_vehicles=0, acceleration_controller=(RLController, {}), ) inflows.add(veh_type="av", edge="highway_0", vehs_per_hour=int(HIGHWAY_INFLOW_RATE * PENETRATION_RATE), depart_lane="free", depart_speed=15, name="av_highway_inflow") # SET UP THE FLOW PARAMETERS return dict( # name of the experiment exp_tag='highway-single', # name of the flow environment the experiment is running on env_name=AVOpenMultiAgentEnv if multiagent else AVOpenEnv, # name of the network class the experiment is running on network=HighwayNetwork, # simulator that is used by the experiment simulator='traci', # environment related parameters (see flow.core.params.EnvParams) env=EnvParams(evaluate=evaluate, horizon=HORIZON, warmup_steps=50, sims_per_step=2, additional_params={ "max_accel": 1, "max_decel": 1, "target_velocity": 30, "penalty_type": "time_headway", "penalty": 1, "inflows": None, "rl_penetration": PENETRATION_RATE, "num_rl": 10, "ghost_length": 500, }), # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams(sim_step=0.5, render=False, restart_instance=True), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams(inflows=inflows, 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 get_flow_params(config): """Return Flow experiment parameters, given an experiment result folder. Parameters ---------- config : dict stored RLlib configuration dict Returns ------- dict Dict of flow parameters, like net_params, env_params, vehicle characteristics, etc """ # collect all data from the json file flow_params = json.loads(config['env_config']['flow_params']) # 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 get_flow_params(fixed_density, stopping_penalty, acceleration_penalty, evaluate=False, multiagent=False, imitation=False): """Return the flow-specific parameters of the ring road network. This scenario consists of 50 (if density is fixed) or 50-75 vehicles (5 of which are automated) are placed on a sing-lane circular track of length 1500m. In the absence of the automated vehicle, the human-driven vehicles exhibit stop-and-go instabilities brought about by the string-unstable characteristic of human car-following dynamics. Within this setting, the RL vehicles are tasked with dissipating the formation and propagation of stop-and-go waves via an objective function that rewards maximizing system-level speeds. Parameters ---------- fixed_density : bool specifies whether the number of human-driven vehicles updates in between resets stopping_penalty : bool whether to include a stopping penalty acceleration_penalty : bool whether to include a regularizing penalty for accelerations by the AVs evaluate : bool whether to compute the evaluation reward 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 imitation : bool whether to use the imitation environment 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) """ vehicles = VehicleParams() for i in range(NUM_AUTOMATED): vehicles.add( veh_id="human_{}".format(i), acceleration_controller=(IDMController, { "a": 1.3, "b": 2.0, "noise": 0.3 if INCLUDE_NOISE else 0.0 }), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams(min_gap=0.5, ), lane_change_params=SumoLaneChangeParams(lane_change_mode=1621, ), num_vehicles=NUM_VEHICLES[0] - NUM_AUTOMATED if i == 0 else 0) vehicles.add( veh_id="rl_{}".format(i), acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams(min_gap=0.5, ), lane_change_params=SumoLaneChangeParams( lane_change_mode=0, # no lane changes by automated vehicles ), num_vehicles=NUM_AUTOMATED if i == 0 else 0) additional_net_params = ADDITIONAL_NET_PARAMS.copy() additional_net_params["length"] = RING_LENGTH additional_net_params["lanes"] = NUM_LANES if multiagent: if imitation: env_name = None # to be added later else: env_name = AVClosedMultiAgentEnv else: if imitation: env_name = AVClosedEnv else: env_name = AVClosedImitationEnv return dict( # name of the experiment exp_tag='multilane-ring', # name of the flow environment the experiment is running on env_name=env_name, # name of the network class the experiment is running on network=RingNetwork, # simulator that is used by the experiment simulator="traci", # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( use_ballistic=True, render=False, sim_step=0.5, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=1800, warmup_steps=50, sims_per_step=2, evaluate=evaluate, additional_params={ "max_accel": 1, "max_decel": 1, "target_velocity": 30, "stopping_penalty": stopping_penalty, "acceleration_penalty": acceleration_penalty, "use_follower_stopper": False, "num_vehicles": None if fixed_density else NUM_VEHICLES, "even_distribution": False, "sort_vehicles": True, "expert_model": (IDMController, { "a": 1.3, "b": 2.0, }), }, ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams(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( spacing="random", min_gap=0.5, shuffle=True, ), )
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-- | | | ''' # bij deze code is er het probleem dat de de routes niet gekend zijn in de simulatie, de voertuigen kunnen gen gedefinieerde route volgen # params for grid env edge_inflow = 300 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 # outer_edges of the network (zie traffic_light_grid.py file) inflow = InFlows() 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 = { "grid_array": grid_array, "speed_limit": enterSpeed + 5, "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="test_grid_static_diffgain", # 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 merge_example(render=None): """ Perform a simulation of vehicles on a merge. 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 on a merge. """ sim_params = SumoParams(render=True, emission_path="./data/", sim_step=0.2, restart_instance=False) if render is not None: sim_params.render = render 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) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS, sims_per_step=5, warmup_steps=0) inflow = InFlows() inflow.add(veh_type="human", edge="inflow_highway", vehs_per_hour=FLOW_RATE, departLane="free", departSpeed=10) inflow.add(veh_type="human", edge="inflow_merge", vehs_per_hour=100, departLane="free", departSpeed=7.5) 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 net_params = NetParams(inflows=inflow, additional_params=additional_net_params) initial_config = InitialConfig(spacing="uniform", perturbation=5.0) scenario = MergeScenario(name="merge-baseline", vehicles=vehicles, net_params=net_params, initial_config=initial_config) env = WaveAttenuationMergePOEnv(env_params, sim_params, scenario) return Experiment(env)
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 the 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.experiment.Experiment A non-rl experiment demonstrating the performance of human-driven vehicles simulated by sumo on the Bay Bridge. """ sim_params = SumoParams(sim_step=0.6, overtake_right=True) if render is not None: sim_params.render = render car_following_params = SumoCarFollowingParams( speedDev=0.2, speed_mode="all_checks", ) lane_change_params = SumoLaneChangeParams( lc_assertive=20, lc_pushy=0.8, lc_speed_gain=4.0, model="LC2013", lane_change_mode="no_lat_collide", # lcKeepRight=0.8 ) vehicles = VehicleParams() vehicles.add(veh_id="human", acceleration_controller=(SimCarFollowingController, {}), routing_controller=(BayBridgeRouter, {}), car_following_params=car_following_params, lane_change_params=lane_change_params, num_vehicles=1400) additional_env_params = {} env_params = EnvParams(additional_params=additional_env_params) traffic_lights = TrafficLightParams() 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, edges_distribution=EDGES_DISTRIBUTION.copy()) scenario = BayBridgeScenario(name="bay_bridge", vehicles=vehicles, traffic_lights=traffic_lights, net_params=net_params, initial_config=initial_config) env = BayBridgeEnv(env_params, sim_params, scenario) return Experiment(env)
def visualizer_rllib(args): """Visualizer for RLlib experiments. This function takes args (see function create_parser below for more detailed information on what information can be fed to this visualizer), and renders the experiment associated with it. """ result_dir = args.result_dir if args.result_dir[-1] != '/' \ else args.result_dir[:-1] config = get_rllib_config(os.path.join(os.getcwd(), result_dir)) # TODO(ev) backwards compatibility hack try: pkl = get_rllib_pkl(result_dir) except Exception: pass # check if we have a multiagent scenario but in a # backwards compatible way if config.get('multiagent', {}).get('policies', {}): multiagent = True config['multiagent'] = pkl['multiagent'] else: multiagent = False # Run on only one cpu for rendering purposes config['num_workers'] = 0 flow_params = get_flow_params(config) sim_params = flow_params['sim'] # Determine agent and checkpoint config_run = config['env_config']['run'] if 'run' in config['env_config'] \ else None if args.run and config_run: if args.run != config_run: print('visualizer_rllib.py: error: run argument ' + '\'{}\' passed in '.format(args.run) + 'differs from the one stored in params.json ' + '\'{}\''.format(config_run)) sys.exit(1) if args.run: agent_cls = get_agent_class(args.run) elif config_run: agent_cls = get_agent_class(config_run) else: print('visualizer_rllib.py: error: could not find flow parameter ' '\'run\' in params.json, ' 'add argument --run to provide the algorithm or model used ' 'to train the results\n e.g. ' 'python ./visualizer_rllib.py /tmp/ray/result_dir 1 --run PPO') sys.exit(1) sim_params.restart_instance = True dir_path = os.path.dirname(os.path.realpath(__file__)) emission_path = '{0}/test_time_rollout/'.format(dir_path) sim_params.emission_path = emission_path if args.gen_emission else None # pick your rendering mode if args.render_mode == 'sumo_web3d': sim_params.num_clients = 2 sim_params.render = False elif args.render_mode == 'drgb': sim_params.render = 'drgb' sim_params.pxpm = 4 elif args.render_mode == 'sumo_gui': sim_params.render = True print('NOTE: With render mode {}, an extra instance of the SUMO GUI ' 'will display before the GUI for visualizing the result. Click ' 'the green Play arrow to continue.'.format(args.render_mode)) elif args.render_mode == 'no_render': sim_params.render = False if args.save_render: sim_params.render = 'drgb' sim_params.pxpm = 4 sim_params.save_render = True # Go through and switch out the IDM vehicles with Krauss if requested if args.test_transfer: from flow.core.params import VehicleParams temp_vehicles = VehicleParams() from flow.controllers.car_following_models import IDMController from flow.controllers.car_following_models import SimCarFollowingController for veh in flow_params['veh'].initial: # swap the car following params with Krauss if veh['acceleration_controller'][0] == IDMController: veh['acceleration_controller'] = ( SimCarFollowingController, veh['acceleration_controller'][1]) car_following_param = veh['car_following_params'] car_following_param.car_follow_model = "Krauss" veh['car_following_params'] = car_following_param temp_vehicles.add(**veh) # Create and register a gym+rllib env create_env, env_name = make_create_env(params=flow_params, version=0) register_env(env_name, create_env) # 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 flow_params['env_name'] in single_agent_envs: # env_loc = 'flow.envs' # else: # env_loc = 'flow.multiagent_envs' # Start the environment with the gui turned on and a path for the # emission file env_params = flow_params['env'] env_params.restart_instance = False # import ipdb; ipdb.set_trace() if args.evaluate: env_params.evaluate = True # lower the horizon if testing if args.horizon: config['horizon'] = args.horizon env_params.horizon = args.horizon # create the agent that will be used to compute the actions agent = agent_cls(env=env_name, config=config) checkpoint = result_dir + '/checkpoint_' + args.checkpoint_num checkpoint = checkpoint + '/checkpoint-' + args.checkpoint_num agent.restore(checkpoint) if hasattr(agent, "local_evaluator") and \ os.environ.get("TEST_FLAG") != 'True': env = agent.local_evaluator.env else: env = gym.make(env_name) if multiagent: rets = {} # map the agent id to its policy policy_map_fn = config['multiagent']['policy_mapping_fn'] for key in config['multiagent']['policies'].keys(): rets[key] = [] else: rets = [] if config['model']['use_lstm']: use_lstm = True if multiagent: state_init = {} # map the agent id to its policy policy_map_fn = config['multiagent']['policy_mapping_fn'].func size = config['model']['lstm_cell_size'] for key in config['multiagent']['policies'].keys(): state_init[key] = [ np.zeros(size, np.float32), np.zeros(size, np.float32) ] else: state_init = [ np.zeros(config['model']['lstm_cell_size'], np.float32), np.zeros(config['model']['lstm_cell_size'], np.float32) ] else: use_lstm = False env.restart_simulation(sim_params=sim_params, render=sim_params.render) # Simulate and collect metrics final_outflows = [] final_inflows = [] mean_speed = [] std_speed = [] def policy_map_fn(agent_id): if agent_id != 'av': return 'action_adversary' else: return agent_id if args.evaluate: env.env_params.evaluate = True controller = RllibController(args.result_dir, args.checkpoint_num, algo='PPO') for i in range(args.num_rollouts): vel = [] state = env.reset() if multiagent: ret = {key: [0] for key in rets.keys()} else: ret = 0 for _ in range(env_params.horizon): vehicles = env.unwrapped.k.vehicle vel.append(np.mean(vehicles.get_speed(vehicles.get_ids()))) # import ipdb; ipdb.set_trace() if multiagent: # import ipdb; ipdb.set_trace() action = {} for agent_id in state.keys(): if use_lstm: action[agent_id], state_init[agent_id], logits = \ agent.compute_action( state[agent_id], state=state_init[agent_id], policy_id=policy_map_fn(agent_id)) else: # import ipdb; ipdb.set_trace() if agent_id == 'av': action[agent_id] = controller.get_action( state['action_adversary']) else: action[agent_id] = agent.compute_action( state[agent_id], policy_id=policy_map_fn(agent_id)) else: # action = agent.compute_action(state) action = controller.get_action(state) state, reward, done, _ = env.step(action) if multiagent: for actor, rew in reward.items(): ret[policy_map_fn(actor)][0] += rew else: ret += reward if multiagent and done['__all__']: break if not multiagent and done: break if multiagent: for key in rets.keys(): rets[key].append(ret[key]) else: rets.append(ret) outflow = vehicles.get_outflow_rate(500) final_outflows.append(outflow) inflow = vehicles.get_inflow_rate(500) final_inflows.append(inflow) if np.all(np.array(final_inflows) > 1e-5): throughput_efficiency = [ x / y for x, y in zip(final_outflows, final_inflows) ] else: throughput_efficiency = [0] * len(final_inflows) mean_speed.append(np.mean(vel)) std_speed.append(np.std(vel)) if multiagent: for agent_id, rew in rets.items(): print('Round {}, Return: {} for agent {}'.format( i, ret, agent_id)) else: print('Round {}, Return: {}'.format(i, ret)) import ipdb ipdb.set_trace() print('==== Summary of results ====') print("Return:") print(mean_speed) if multiagent: for agent_id, rew in rets.items(): print('For agent', agent_id) print(rew) print('Average, std return: {}, {} for agent {}'.format( np.mean(rew), np.std(rew), agent_id)) else: print(rets) print('Average, std: {}, {}'.format(np.mean(rets), np.std(rets))) print("\nSpeed, mean (m/s):") print(mean_speed) print('Average, std: {}, {}'.format(np.mean(mean_speed), np.std(mean_speed))) print("\nSpeed, std (m/s):") print(std_speed) print('Average, std: {}, {}'.format(np.mean(std_speed), np.std(std_speed))) # Compute arrival rate of vehicles in the last 500 sec of the run print("\nOutflows (veh/hr):") print(final_outflows) print('Average, std: {}, {}'.format(np.mean(final_outflows), np.std(final_outflows))) # Compute departure rate of vehicles in the last 500 sec of the run print("Inflows (veh/hr):") print(final_inflows) print('Average, std: {}, {}'.format(np.mean(final_inflows), np.std(final_inflows))) # Compute throughput efficiency in the last 500 sec of the print("Throughput efficiency (veh/hr):") print(throughput_efficiency) print('Average, std: {}, {}'.format(np.mean(throughput_efficiency), np.std(throughput_efficiency))) # terminate the environment env.unwrapped.terminate() # if prompted, convert the emission file into a csv file if args.gen_emission: time.sleep(0.1) dir_path = os.path.dirname(os.path.realpath(__file__)) emission_filename = '{0}-emission.xml'.format(env.scenario.name) emission_path = \ '{0}/test_time_rollout/{1}'.format(dir_path, emission_filename) # convert the emission file into a csv file emission_to_csv(emission_path) # delete the .xml version of the emission file os.remove(emission_path) # if we wanted to save the render, here we create the movie if args.save_render: dirs = os.listdir(os.path.expanduser('~') + '/flow_rendering') # Ignore hidden files dirs = [d for d in dirs if d[0] != '.'] dirs.sort(key=lambda date: datetime.strptime(date, "%Y-%m-%d-%H%M%S")) recent_dir = dirs[-1] # create the movie movie_dir = os.path.expanduser('~') + '/flow_rendering/' + recent_dir save_dir = os.path.expanduser('~') + '/flow_movies' if not os.path.exists(save_dir): os.mkdir(save_dir) os_cmd = "cd " + movie_dir + " && ffmpeg -i frame_%06d.png" os_cmd += " -pix_fmt yuv420p " + dirs[-1] + ".mp4" os_cmd += "&& cp " + dirs[-1] + ".mp4 " + save_dir + "/" os.system(os_cmd)
def setUp(self): # create the environment and network classes for a figure eight vehicles = VehicleParams() vehicles.add(veh_id="test", num_vehicles=20) self.env, _ = ring_road_exp_setup(vehicles=vehicles)
def get_flow_params(evaluate=False, multiagent=False, imitation=False): """Return the flow-specific parameters of the single lane highway network. Parameters ---------- evaluate : bool whether to compute the evaluation reward 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 imitation : bool whether to use the imitation environment 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) """ additional_net_params = ADDITIONAL_NET_PARAMS.copy() additional_net_params.update({ # length of the highway "length": 2500, # number of lanes "lanes": 1, # speed limit for all edges "speed_limit": 30, # number of edges to divide the highway into "num_edges": 2, # whether to include a ghost edge of length 500m. This edge is provided # a different speed limit. "use_ghost_edge": True, # speed limit for the ghost edge "ghost_speed_limit": END_SPEED, # length of the cell imposing a boundary "boundary_cell_length": 300, }) vehicles = VehicleParams() inflows = InFlows() # human vehicles vehicles.add( "human", num_vehicles=0, acceleration_controller=(IDMController, { 'a': 1.3, 'b': 2.0, 'noise': 0.3 if INCLUDE_NOISE else 0.0 }), car_following_params=SumoCarFollowingParams( min_gap=0.5 ), lane_change_params=SumoLaneChangeParams( model="SL2015", lc_sublane=2.0, ), ) inflows.add( veh_type="human", edge="highway_0", vehs_per_hour=int(TRAFFIC_FLOW * (1 - PENETRATION_RATE)), depart_lane="free", depart_speed=TRAFFIC_SPEED, name="idm_highway_inflow" ) # automated vehicles if PENETRATION_RATE > 0.0: vehicles.add( "rl", num_vehicles=0, acceleration_controller=(RLController, {}), ) inflows.add( veh_type="rl", edge="highway_0", vehs_per_hour=int(TRAFFIC_FLOW * PENETRATION_RATE), depart_lane="free", depart_speed=TRAFFIC_SPEED, name="rl_highway_inflow" ) # SET UP THE FLOW PARAMETERS if multiagent: if imitation: env_name = None # FIXME else: env_name = AVOpenMultiAgentEnv else: if imitation: env_name = AVOpenImitationEnv else: env_name = AVOpenEnv return dict( # name of the experiment exp_tag='highway-single', # name of the flow environment the experiment is running on env_name=env_name, # name of the network class the experiment is running on network=HighwayNetwork, # simulator that is used by the experiment simulator='traci', # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( evaluate=evaluate, horizon=HORIZON, warmup_steps=500, sims_per_step=3, additional_params={ "max_accel": 0.5, "max_decel": 0.5, "target_velocity": 10, "penalty_type": "acceleration", "penalty": 1, "inflows": None, "rl_penetration": PENETRATION_RATE, "num_rl": 10, "control_range": [500, 2300], "expert_model": (IDMController, { 'a': 1.3, 'b': 2.0, }), } ), # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( sim_step=0.4, render=False, restart_instance=True, use_ballistic=True, ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflows, 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 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", acceleration_controller=(IDMController, { "noise": 0.2 }), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), 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(spacing="uniform", perturbation=5.0) scenario = HighwayRampsScenario(name="highway-ramp", vehicles=vehicles, net_params=net_params, initial_config=initial_config) env = AccelEnv(env_params, sim_params, scenario) return Experiment(env)
def run_task(*_): """Implement the run_task method needed to run experiments with rllab.""" sim_params = SumoParams(sim_step=0.1, render=True) vehicles = VehicleParams() vehicles.add( veh_id="rl", acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", decel=1.5, ), num_vehicles=1) vehicles.add( veh_id="idm", acceleration_controller=(IDMController, { "noise": 0.2 }), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", decel=1.5, ), num_vehicles=13) additional_env_params = { "target_velocity": 20, "max_accel": 3, "max_decel": 3, "sort_vehicles": False } env_params = EnvParams( horizon=HORIZON, additional_params=additional_env_params) additional_net_params = { "radius_ring": 30, "lanes": 1, "speed_limit": 30, "resolution": 40 } net_params = NetParams( no_internal_links=False, additional_params=additional_net_params) initial_config = InitialConfig(spacing="uniform") print("XXX name", exp_tag) scenario = Figure8Scenario( exp_tag, vehicles, net_params, initial_config=initial_config) env_name = "AccelEnv" 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=(16, 16)) baseline = LinearFeatureBaseline(env_spec=env.spec) algo = TRPO( env=env, policy=policy, baseline=baseline, batch_size=15000, max_path_length=horizon, n_itr=500, # whole_paths=True, discount=0.999, # step_size=v["step_size"], ) algo.train(),
def gen_env(render='drgb'): # time horizon of a single rollout HORIZON = 400 # inflow rate of vehicles at every edge EDGE_INFLOW = 300 # enter speed for departing vehicles V_ENTER = 30 # number of row of bidirectional lanes N_ROWS = 3 # number of columns of bidirectional lanes N_COLUMNS = 3 # length of inner edges in the grid network INNER_LENGTH = 300 # length of final edge in route LONG_LENGTH = 100 # length of edges that vehicles start on SHORT_LENGTH = 300 # number of vehicles originating in the left, right, top, and bottom edges N_LEFT, N_RIGHT, N_TOP, N_BOTTOM = 1, 1, 1, 1 # 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() 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=(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, depart_lane="free", depart_speed=V_ENTER) flow_params = dict( # name of the experiment exp_tag="grid_0", # name of the flow environment the experiment is running on env_name=TrafficLightGridBenchmarkEnv, # 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=SumoParams( restart_instance=True, sim_step=1, render=render, save_render=True, ), # 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" }, ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, 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, }, ), # 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='custom', shuffle=True, ), ) return flow_params
def reset(self, new_inflow_rate=None): """See class definition.""" # Skip if ring length is None. if self.env_params.additional_params["num_vehicles"] is None: return super(AVClosedMultiAgentEnv, self).reset() self.step_counter = 1 self.time_counter = 1 # Make sure restart instance is set to True when resetting. self.sim_params.restart_instance = True # Create a new VehicleParams object with a new number of human- # driven vehicles. n_vehicles = self.env_params.additional_params["num_vehicles"] n_rl = self._network_vehicles.num_rl_vehicles n_vehicles_low = n_vehicles[0] - n_rl n_vehicles_high = n_vehicles[1] - n_rl new_n_vehicles = random.randint(n_vehicles_low, n_vehicles_high) params = self._network_vehicles.type_parameters print("humans: {}, automated: {}".format(new_n_vehicles, n_rl)) if self.env_params.additional_params["even_distribution"]: num_human = new_n_vehicles - n_rl humans_remaining = num_human new_vehicles = VehicleParams() for i in range(n_rl): # Add one automated vehicle. new_vehicles.add( veh_id="rl_{}".format(i), acceleration_controller=params["rl_{}".format( i)]["acceleration_controller"], lane_change_controller=params["rl_{}".format( i)]["lane_change_controller"], routing_controller=params["rl_{}".format( i)]["routing_controller"], initial_speed=params["rl_{}".format(i)]["initial_speed"], car_following_params=params["rl_{}".format( i)]["car_following_params"], lane_change_params=params["rl_{}".format( i)]["lane_change_params"], num_vehicles=1) # Add a fraction of the remaining human vehicles. vehicles_to_add = round(humans_remaining / (n_rl - i)) humans_remaining -= vehicles_to_add new_vehicles.add( veh_id="human_{}".format(i), acceleration_controller=params["human_{}".format( i)]["acceleration_controller"], lane_change_controller=params["human_{}".format( i)]["lane_change_controller"], routing_controller=params["human_{}".format( i)]["routing_controller"], initial_speed=params["human_{}".format( i)]["initial_speed"], car_following_params=params["human_{}".format( i)]["car_following_params"], lane_change_params=params["human_{}".format( i)]["lane_change_params"], num_vehicles=vehicles_to_add) else: new_vehicles = VehicleParams() new_vehicles.add( "human_0", acceleration_controller=params["human_0"] ["acceleration_controller"], lane_change_controller=params["human_0"] ["lane_change_controller"], routing_controller=params["human_0"]["routing_controller"], initial_speed=params["human_0"]["initial_speed"], car_following_params=params["human_0"]["car_following_params"], lane_change_params=params["human_0"]["lane_change_params"], num_vehicles=new_n_vehicles) new_vehicles.add( "rl_0", acceleration_controller=params["rl_0"] ["acceleration_controller"], lane_change_controller=params["rl_0"] ["lane_change_controller"], routing_controller=params["rl_0"]["routing_controller"], initial_speed=params["rl_0"]["initial_speed"], car_following_params=params["rl_0"]["car_following_params"], lane_change_params=params["rl_0"]["lane_change_params"], num_vehicles=n_rl) # Update the network. self.network = self._network_cls( self._network_name, net_params=self._network_net_params, vehicles=new_vehicles, initial_config=self._network_initial_config, traffic_lights=self._network_traffic_lights, ) # Perform the reset operation. _ = super(AVClosedMultiAgentEnv, self).reset() # Get the initial positions of the RL vehicles to allow us to sort the # vehicles by this term. def init_pos(veh_id): return self.k.vehicle.get_x_by_id(veh_id) # Create a list of the RL IDs sorted by the above term. self._sorted_rl_ids = sorted(self.k.vehicle.get_rl_ids(), key=init_pos) # Perform the reset operation again because the vehicle IDs weren't # caught the first time. obs = super(AVClosedMultiAgentEnv, self).reset() return obs
def run_task(*_): """Implement the run_task method needed to run experiments with rllab.""" sim_params = SumoParams(sim_step=0.2, render=True) # note that the vehicles are added sequentially by the scenario, # so place the merging vehicles after the vehicles in the ring vehicles = VehicleParams() # Inner ring vehicles vehicles.add(veh_id="human", acceleration_controller=(IDMController, { "noise": 0.2 }), lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=6, car_following_params=SumoCarFollowingParams(minGap=0.0, tau=0.5), lane_change_params=SumoLaneChangeParams()) # A single learning agent in the inner ring vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=1, car_following_params=SumoCarFollowingParams( minGap=0.01, tau=0.5, speed_mode="obey_safe_speed"), lane_change_params=SumoLaneChangeParams()) # Outer ring vehicles vehicles.add(veh_id="merge-human", acceleration_controller=(IDMController, { "noise": 0.2 }), lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=10, car_following_params=SumoCarFollowingParams(minGap=0.0, tau=0.5), lane_change_params=SumoLaneChangeParams()) env_params = EnvParams(horizon=HORIZON, additional_params={ "target_velocity": 10, "max_accel": 3, "max_decel": 3, "sort_vehicles": False }) additional_net_params = ADDITIONAL_NET_PARAMS.copy() additional_net_params["ring_radius"] = 50 additional_net_params["inner_lanes"] = 1 additional_net_params["outer_lanes"] = 1 additional_net_params["lane_length"] = 75 net_params = NetParams(additional_params=additional_net_params) initial_config = InitialConfig(x0=50, spacing="uniform") scenario = TwoLoopsOneMergingScenario(name=exp_tag, vehicles=vehicles, net_params=net_params, initial_config=initial_config) env_name = "AccelEnv" 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=(100, 50, 25)) baseline = LinearFeatureBaseline(env_spec=env.spec) algo = TRPO( env=env, policy=policy, baseline=baseline, batch_size=64 * 3 * horizon, max_path_length=horizon, # whole_paths=True, n_itr=1000, discount=0.999, # step_size=0.01, ) algo.train()
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 get_flow_params(fixed_boundary, stopping_penalty, acceleration_penalty, use_follower_stopper, evaluate=False, multiagent=False, imitation=False): """Return the flow-specific parameters of the I-210 subnetwork. Parameters ---------- fixed_boundary : bool specifies whether the boundary conditions update in between resets stopping_penalty : bool whether to include a stopping penalty acceleration_penalty : bool whether to include a regularizing penalty for accelerations by the AVs use_follower_stopper : bool whether to use the follower-stopper controller for the AVs evaluate : bool whether to compute the evaluation reward 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 imitation : bool whether to use the imitation environment 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) """ # steps to run before the agent is allowed to take control (set to lower # value during testing) if WARMUP_PATH is not None: warmup_steps = 0 else: warmup_steps = 50 if os.environ.get("TEST_FLAG") else 500 # Create the base vehicle types that will be used for inflows. vehicles = VehicleParams() vehicles.add( "human", num_vehicles=0, acceleration_controller=(IDMController, { 'a': 1.3, 'b': 2.0, 'noise': 0.3, "display_warnings": False, "fail_safe": ['obey_speed_limit', 'safe_velocity', 'feasible_accel'], }), lane_change_controller=(SimLaneChangeController, {}), car_following_params=SumoCarFollowingParams( min_gap=0.5, # right of way at intersections + obey limits on deceleration speed_mode=12), lane_change_params=SumoLaneChangeParams(lane_change_mode=1621, ), ) vehicles.add( "rl", num_vehicles=0, acceleration_controller=(RLController, { "fail_safe": ['obey_speed_limit', 'safe_velocity', 'feasible_accel'], }), car_following_params=SumoCarFollowingParams( min_gap=0.5, # right of way at intersections + obey limits on deceleration speed_mode=12, ), lane_change_params=SumoLaneChangeParams( lane_change_mode=0, # no lane changes ), ) # Add the inflows from the main highway. inflow = InFlows() for lane in [0, 1, 2, 3, 4]: inflow.add(veh_type="human", edge="ghost0", vehs_per_hour=int(INFLOW_RATE * (1 - PENETRATION_RATE)), depart_lane=lane, depart_speed=INFLOW_SPEED) inflow.add(veh_type="rl", edge="ghost0", vehs_per_hour=int(INFLOW_RATE * PENETRATION_RATE), depart_lane=lane, depart_speed=INFLOW_SPEED) # Choose the appropriate environment. if multiagent: if imitation: env_name = None # to be added later else: env_name = LaneOpenMultiAgentEnv else: if imitation: env_name = AVOpenImitationEnv else: env_name = AVOpenEnv return dict( # name of the experiment exp_tag='I-210_subnetwork', # name of the flow environment the experiment is running on env_name=env_name, # name of the network class the experiment is running on network=I210SubNetwork, # simulator that is used by the experiment simulator='traci', # simulation-related parameters sim=SumoParams( sim_step=0.4, render=False, restart_instance=True, use_ballistic=True, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams(evaluate=evaluate, horizon=HORIZON, warmup_steps=warmup_steps, done_at_exit=False, sims_per_step=3, additional_params={ "max_accel": 0.5, "max_decel": 0.5, "target_velocity": 10, "stopping_penalty": stopping_penalty, "acceleration_penalty": acceleration_penalty, "use_follower_stopper": use_follower_stopper, "inflows": None if fixed_boundary else INFLOWS, "rl_penetration": PENETRATION_RATE, "num_rl": 10 if multiagent else 50, "control_range": [500, 2300], "expert_model": (IDMController, { "a": 1.3, "b": 2.0, }), "warmup_path": WARMUP_PATH, }), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, template=os.path.join( flow_config.PROJECT_PATH, "examples/exp_configs/templates/sumo/i210_with_ghost_cell_" "with_downstream.xml"), additional_params={ "on_ramp": False, "ghost_edge": True, }), # 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(edges_distribution=EDGES_DISTRIBUTION.copy(), ), )
def test_make_create_env(self): """Tests that the make_create_env methods generates an environment with the expected flow parameters.""" # use a flow_params dict derived from flow/benchmarks/figureeight0.py vehicles = VehicleParams() vehicles.add(veh_id="human", acceleration_controller=(IDMController, { "noise": 0.2 }), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), num_vehicles=13) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), num_vehicles=1) flow_params = dict( exp_tag="figure_eight_0", env_name=AccelEnv, network=FigureEightNetwork, simulator='traci', sim=SumoParams( sim_step=0.1, render=False, ), env=EnvParams( horizon=1500, additional_params={ "target_velocity": 20, "max_accel": 3, "max_decel": 3, "sort_vehicles": False }, ), net=NetParams(additional_params={ "radius_ring": 30, "lanes": 1, "speed_limit": 30, "resolution": 40, }, ), veh=vehicles, initial=InitialConfig(), tls=TrafficLightParams(), ) # some random version number for testing v = 23434 # call make_create_env create_env, env_name = make_create_env(params=flow_params, version=v) # check that the name is correct self.assertEqual(env_name, '{}-v{}'.format(flow_params["env_name"].__name__, v)) # create the gym environment env = create_env() # Note that we expect the port number in sim_params to change, and # that this feature is in fact needed to avoid race conditions flow_params["sim"].port = env.sim_params.port # check that each of the parameter match self.assertEqual(env.env_params.__dict__, flow_params["env"].__dict__) self.assertEqual(env.sim_params.__dict__, flow_params["sim"].__dict__) self.assertEqual(env.network.traffic_lights.__dict__, flow_params["tls"].__dict__) self.assertEqual(env.net_params.__dict__, flow_params["net"].__dict__) self.assertEqual(env.initial_config.__dict__, flow_params["initial"].__dict__) self.assertEqual(env.__class__.__name__, flow_params["env_name"].__name__) self.assertEqual(env.network.__class__.__name__, flow_params["network"].__name__)
def getOmgeving(HORIZON): sim_params = SumoParams(render=False, sim_step=1, restart_instance=True) # 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, ), 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="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 # => 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": 2, "tl_type": "actuated", # kan ook actuated/controlled zijn "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_obo_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 bottleneck0_baseline(num_runs, render=True): """Run script for the bottleneck0 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 network_class = 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 env_class = 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 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, additional_params={"scaling": 1, "speed_limit": 23}) network = BottleneckNetwork( name="bay_bridge_toll", vehicles=vehicles, net_params=net_params) env = BottleneckDesiredVelocityEnv(env_params, sim_params, network) # reset the environment and get a new inflow rate env.reset() expected_inflow = 1353.6 # just from checking the new inflow # check that the first inflow rate is approximately what the seeded # value expects it to be for _ in range(500): env.step(rl_actions=None) self.assertAlmostEqual( env.k.vehicle.get_inflow_rate(250)/expected_inflow, 1, 1)
def test_no_junctions_highway(self): additional_net_params = { "length": 100, "lanes": 3, "speed_limit": 30, "resolution": 40, "num_edges": 1 } net_params = NetParams(additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add( veh_id="test", acceleration_controller=(RLController, {}), num_vehicles=3, initial_speed=1.0) # Test Cases # 1. If there's only one vehicle in each lane, we should still # find one leader and one follower for the central vehicle initial_config = InitialConfig(lanes_distribution=float("inf")) initial_config.spacing = "custom" initial_pos = {"start_positions": [('highway_0', 20), ('highway_0', 30), ('highway_0', 10)], "start_lanes": [1, 2, 0]} initial_config.additional_params = initial_pos env, _ = highway_exp_setup( sim_params=SumoParams(sim_step=0.1, render=False), net_params=net_params, vehicles=vehicles, initial_config=initial_config) env.reset() # test the central car # test_0 is car to test in central lane # test_1 should be leading car in lane 2 # test_2 should be trailing car in lane 0 actual_lane_leaders = env.k.vehicle.get_lane_leaders("test_0") expected_lane_leaders = ["", "", "test_1"] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.k.vehicle.get_lane_headways("test_0") expected_lane_headways = [1000, 1000, 5.0] np.testing.assert_array_almost_equal(actual_lane_headways, expected_lane_headways) actual_lane_followers = env.k.vehicle.get_lane_followers("test_0") expected_lane_followers = ["test_2", "", ""] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.k.vehicle.get_lane_tailways("test_0") expected_lane_tailways = [5.0, 1000, 1000] np.testing.assert_array_almost_equal(actual_lane_tailways, expected_lane_tailways) # test the leader/follower speed methods expected_leader_speed = [0.0, 0.0, 1.0] actual_leader_speed = env.k.vehicle.get_lane_leaders_speed("test_0") np.testing.assert_array_almost_equal(actual_leader_speed, expected_leader_speed) expected_follower_speed = [1.0, 0.0, 0.0] actual_follower_speed = env.k.vehicle.get_lane_followers_speed( "test_0") np.testing.assert_array_almost_equal(actual_follower_speed, expected_follower_speed) # Next, test the case where all vehicles are on the same # edge and there's two vehicles in each lane # Cases to test # 1. For lane 0, should find a leader and follower for tested car # 2. For lane 1, both vehicles are behind the test car # 3. For lane 2, both vehicles are in front of the tested car # 4. For lane 3, one vehicle in front and one behind the tested car additional_net_params = { "length": 100, "lanes": 4, "speed_limit": 30, "resolution": 40, "num_edges": 1 } net_params = NetParams(additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add( veh_id="test", acceleration_controller=(RLController, {}), num_vehicles=9, initial_speed=1.0) initial_config = InitialConfig(lanes_distribution=float("inf")) initial_config.spacing = "custom" initial_pos = {"start_positions": [('highway_0', 50), ('highway_0', 60), ('highway_0', 40), ('highway_0', 40), ('highway_0', 30), ('highway_0', 60), ('highway_0', 70), ('highway_0', 60), ('highway_0', 40), ], "start_lanes": [0, 0, 0, 1, 1, 2, 2, 3, 3]} initial_config.additional_params = initial_pos env, _ = highway_exp_setup( sim_params=SumoParams(sim_step=0.1, render=False), net_params=net_params, vehicles=vehicles, initial_config=initial_config) env.reset() actual_lane_leaders = env.k.vehicle.get_lane_leaders("test_0") expected_lane_leaders = ["test_1", "", "test_5", "test_7"] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.k.vehicle.get_lane_headways("test_0") expected_lane_headways = [5.0, 1000, 5.0, 5.0] np.testing.assert_array_almost_equal(actual_lane_headways, expected_lane_headways) actual_lane_followers = env.k.vehicle.get_lane_followers("test_0") expected_lane_followers = ["test_2", "test_3", "", "test_8"] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.k.vehicle.get_lane_tailways("test_0") expected_lane_tailways = [5.0, 5.0, 1000, 5.0] np.testing.assert_array_almost_equal(actual_lane_tailways, expected_lane_tailways) # test the leader/follower speed methods expected_leader_speed = [1.0, 0.0, 1.0, 1.0] actual_leader_speed = env.k.vehicle.get_lane_leaders_speed("test_0") np.testing.assert_array_almost_equal(actual_leader_speed, expected_leader_speed) expected_follower_speed = [1.0, 1.0, 0.0, 1.0] actual_follower_speed = env.k.vehicle.get_lane_followers_speed( "test_0") np.testing.assert_array_almost_equal(actual_follower_speed, expected_follower_speed) # Now test if all the vehicles are on different edges and # different lanes additional_net_params = { "length": 100, "lanes": 3, "speed_limit": 30, "resolution": 40, "num_edges": 3 } net_params = NetParams(additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add( veh_id="test", acceleration_controller=(RLController, {}), num_vehicles=3, initial_speed=1.0) # Test Cases # 1. If there's only one vehicle in each lane, we should still # find one leader and one follower for the central vehicle initial_config = InitialConfig(lanes_distribution=float("inf")) initial_config.spacing = "custom" initial_pos = {"start_positions": [('highway_1', 50 - (100 / 3.0)), ('highway_2', 75 - (2 * 100 / 3.0)), ('highway_0', 25)], "start_lanes": [1, 2, 0]} initial_config.additional_params = initial_pos env, _ = highway_exp_setup( sim_params=SumoParams(sim_step=0.1, render=False), net_params=net_params, vehicles=vehicles, initial_config=initial_config) env.reset() # test the central car # test_0 is car to test in central lane # test_1 should be leading car in lane 2 # test_2 should be trailing car in lane 0 actual_lane_leaders = env.k.vehicle.get_lane_leaders("test_0") expected_lane_leaders = ["", "", "test_1"] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.k.vehicle.get_lane_headways("test_0") expected_lane_headways = [1000, 1000, 22.996667] np.testing.assert_array_almost_equal(actual_lane_headways, expected_lane_headways) actual_lane_followers = env.k.vehicle.get_lane_followers("test_0") expected_lane_followers = ["test_2", "", ""] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.k.vehicle.get_lane_tailways("test_0") expected_lane_tailways = [20.096667, 1000, 1000] np.testing.assert_array_almost_equal(actual_lane_tailways, expected_lane_tailways) # test the leader/follower speed methods expected_leader_speed = [0.0, 0.0, 1.0] actual_leader_speed = env.k.vehicle.get_lane_leaders_speed("test_0") np.testing.assert_array_almost_equal(actual_leader_speed, expected_leader_speed) expected_follower_speed = [1.0, 0.0, 0.0] actual_follower_speed = env.k.vehicle.get_lane_followers_speed( "test_0") np.testing.assert_array_almost_equal(actual_follower_speed, expected_follower_speed) # Now test if all the vehicles are on different edges and same # lanes additional_net_params = { "length": 100, "lanes": 3, "speed_limit": 30, "resolution": 40, "num_edges": 3 } net_params = NetParams(additional_params=additional_net_params) vehicles = VehicleParams() vehicles.add( veh_id="test", acceleration_controller=(RLController, {}), num_vehicles=3, initial_speed=1.0) # Test Cases # 1. If there's only one vehicle in each lane, we should still # find one leader and one follower for the central vehicle initial_config = InitialConfig(lanes_distribution=float("inf")) initial_config.spacing = "custom" initial_pos = {"start_positions": [('highway_1', 50 - (100 / 3.0)), ('highway_2', 75 - (2 * 100 / 3.0)), ('highway_0', 25)], "start_lanes": [0, 0, 0]} initial_config.additional_params = initial_pos env, _ = highway_exp_setup( sim_params=SumoParams(sim_step=0.1, render=False), net_params=net_params, vehicles=vehicles, initial_config=initial_config) env.reset() # test the central car # test_0 is car to test in lane 0 # test_1 should be leading car in lane 0 # test_2 should be trailing car in lane 0 actual_lane_leaders = env.k.vehicle.get_lane_leaders("test_0") expected_lane_leaders = ["test_1", "", ""] self.assertTrue(actual_lane_leaders == expected_lane_leaders) actual_lane_headways = env.k.vehicle.get_lane_headways("test_0") expected_lane_headways = [22.996667, 1000, 1000] np.testing.assert_array_almost_equal(actual_lane_headways, expected_lane_headways) actual_lane_followers = env.k.vehicle.get_lane_followers("test_0") expected_lane_followers = ["test_2", "", ""] self.assertTrue(actual_lane_followers == expected_lane_followers) actual_lane_tailways = env.k.vehicle.get_lane_tailways("test_0") expected_lane_tailways = [20.096667, 1000, 1000] np.testing.assert_array_almost_equal(actual_lane_tailways, expected_lane_tailways) # test the leader/follower speed methods expected_leader_speed = [1.0, 0.0, 0.0] actual_leader_speed = env.k.vehicle.get_lane_leaders_speed("test_0") np.testing.assert_array_almost_equal(actual_leader_speed, expected_leader_speed) expected_follower_speed = [1.0, 0.0, 0.0] actual_follower_speed = env.k.vehicle.get_lane_followers_speed( "test_0") np.testing.assert_array_almost_equal(actual_follower_speed, expected_follower_speed)
# - 1: 25% RL penetration, 13 max controllable vehicles # - 2: 33% RL penetration, 17 max controllable vehicles N_CPUS = 2 #1#8#2 # inflow rate at the highway FLOW_RATE = 1500 MERGE_RATE = 160 ## We consider a highway network with an upstream merging lane producing # shockwaves additional_net_params = ADDITIONAL_NET_PARAMS.copy() # RL vehicles constitute 5% of the total number of vehicles # Daniel: adding vehicles and flow from osm.passenger.trips.xml vehicles = VehicleParams() vehicles.add( veh_id="human", acceleration_controller=(IDMController, { "noise": 0.2, }), lane_change_controller=(SimLaneChangeController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( # Define speed mode that will minimize collisions: https://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#speed_mode_.280xb3.29 speed_mode= "right_of_way", #"right_of_way", #"right_of_way", #"all_checks", #no_collide", decel=7.5, # avoid collisions at emergency stops # desired time-gap from leader tau=2, #7, min_gap=0,
def run_task(*_): """Implement the run_task method needed to run experiments with rllab.""" sim_params = SumoParams(sim_step=0.1, render=False, seed=0) vehicles = VehicleParams() vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=1) vehicles.add(veh_id="idm", acceleration_controller=(IDMController, {}), car_following_params=SumoCarFollowingParams(min_gap=0, ), routing_controller=(ContinuousRouter, {}), num_vehicles=21) additional_env_params = { "ring_length": [220, 270], "max_accel": 1, "max_decel": 1 } env_params = EnvParams(horizon=HORIZON, additional_params=additional_env_params, warmup_steps=750, clip_actions=False) additional_net_params = { "length": 260, "lanes": 1, "speed_limit": 30, "resolution": 40 } net_params = NetParams(additional_params=additional_net_params) initial_config = InitialConfig(spacing="uniform", bunching=50) print("XXX name", exp_tag) scenario = LoopScenario(exp_tag, vehicles, net_params, initial_config=initial_config) env_name = "WaveAttenuationPOEnv" 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 = GaussianGRUPolicy( env_spec=env.spec, hidden_sizes=(5, ), ) baseline = LinearFeatureBaseline(env_spec=env.spec) algo = TRPO( env=env, policy=policy, baseline=baseline, batch_size=60000, max_path_length=horizon, n_itr=500, # whole_paths=True, discount=0.999, # step_size=v["step_size"], ) algo.train(),