# download the template from AWS if USE_TRAFFIC_LIGHTS: my_url = "http://s3-us-west-1.amazonaws.com/flow.netfiles/" \ "bay_bridge_TL_all_green.net.xml" else: my_url = "http://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__)), TEMPLATE), "wb+") as f: f.write(data_to_write) vehicles = VehicleParams() vehicles.add( veh_id="human", acceleration_controller=(SimCarFollowingController, {}), routing_controller=(BayBridgeRouter, {}), 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
HORIZON = 400 # time horizon of a single rollout V_ENTER = 30 # enter speed for departing vehicles INNER_LENGTH = 300 # length of inner edges in the traffic light grid network LONG_LENGTH = 100 # length of final edge in route SHORT_LENGTH = 300 # length of edges that vehicles start on # number of vehicles originating in the left, right, top, and bottom edges N_LEFT, N_RIGHT, N_TOP, N_BOTTOM = 0, 0, 0, 0 EDGE_INFLOW = 300 # inflow rate of vehicles at every edge N_ROWS = 2 # number of row of bidirectional lanes N_COLUMNS = 2 # number of columns of bidirectional lanes # we place a sufficient number of vehicles to ensure they confirm with the # total number specified above. We also use a "right_of_way" speed mode to # support traffic light compliance vehicles = VehicleParams() num_vehicles = (N_LEFT + N_RIGHT) * N_COLUMNS + (N_BOTTOM + N_TOP) * N_ROWS vehicles.add( veh_id="human", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( min_gap=2.5, max_speed=V_ENTER, decel=7.5, # avoid collisions at emergency stops speed_mode="right_of_way", ), routing_controller=(GridRouter, {}), num_vehicles=num_vehicles) # inflows of vehicles are place on all outer edges (listed here) outer_edges = []
# SET UP PARAMETERS FOR THE ENVIRONMENT additional_env_params = ADDITIONAL_ENV_PARAMS.copy() additional_env_params.update({ 'max_accel': 1, 'max_decel': 1, 'target_velocity': 30 }) # CREATE VEHICLE TYPES AND INFLOWS vehicles = VehicleParams() inflows = InFlows() # human vehicles vehicles.add( veh_id="human", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( speed_mode=9, # for safer behavior at the merges #tau=1.5 # larger distance between cars ), #lane_change_params=SumoLaneChangeParams(lane_change_mode=1621) num_vehicles=5) # autonomous vehicles vehicles.add(
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)
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) network = MergeNetwork(name="merge-baseline", vehicles=vehicles, net_params=net_params, initial_config=initial_config) env = MergePOEnv(env_params, sim_params, network) return Experiment(env)
from flow.core.params import SumoParams, EnvParams, InitialConfig, NetParams, VehicleParams, SumoCarFollowingParams, InFlows, SumoLaneChangeParams from flow.controllers import RLController, IDMController, SimLaneChangeController from gym.spaces.box import Box from gym.spaces import Tuple from constructionRouter import ConstructionRouter from constructionEnv_simplified import myEnv # time horizon of a single rollout HORIZON = 5000 #5000 # number of rollouts per training iteration N_ROLLOUTS = 10 # number of parallel workers N_CPUS = 10 vehicles = VehicleParams() vehicles.add("rl", acceleration_controller=(RLController, {}), routing_controller=(ConstructionRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), num_vehicles=8) vehicles.add("human", acceleration_controller=(IDMController, {}), lane_change_controller=(SimLaneChangeController, {}), car_following_params=SumoCarFollowingParams(speed_mode=25), lane_change_params=SumoLaneChangeParams(lane_change_mode=1621), num_vehicles=0) # specify the edges vehicles can originate on initial_config = InitialConfig(edges_distribution=["edge4"])
from flow.networks.ring import RingNetwork from flow.core.params import VehicleParams from flow.controllers.car_following_models import IDMController from flow.controllers.routing_controllers import ContinuousRouter from flow.networks.ring import ADDITIONAL_NET_PARAMS from flow.core.params import NetParams from flow.core.params import InitialConfig from flow.core.params import TrafficLightParams from flow.envs.ring.accel import AccelEnv from flow.core.params import SumoParams from flow.envs.ring.accel import ADDITIONAL_ENV_PARAMS from flow.core.params import EnvParams from flow.core.experiment import Experiment vehicles = VehicleParams() name = "ring_example" vehicles.add("human", acceleration_controller = (IDMController, {}), routing_controller = (ContinuousRouter, {}), num_vehicles = 22) net_params = NetParams(additional_params = { 'length': 230, 'lanes': 3, 'speed_limit': 40, 'resolution': 50 }) initial_config = InitialConfig(spacing = "uniform", perturbation = 1)
# inflow rate at the highway FLOW_RATE = 2000 # percent of autonomous vehicles RL_PENETRATION = [0.1, 0.25, 0.33][EXP_NUM] # num_rl term (see ADDITIONAL_ENV_PARAMs) NUM_RL = [5, 13, 17][EXP_NUM] # We consider a highway network with an upstream merging lane producing # shockwaves additional_net_params = ADDITIONAL_NET_PARAMS.copy() additional_net_params["merge_lanes"] = 1 additional_net_params["highway_lanes"] = 1 additional_net_params["pre_merge_length"] = 500 # RL vehicles constitute 5% of the total number of vehicles vehicles = VehicleParams() vehicles.add( veh_id="human", acceleration_controller=(IDMController, { "noise": 0.2 }), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), num_vehicles=5) vehicles.add( veh_id="rl", acceleration_controller=(RLController, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ),
from flow.networks import MultiRingNetwork from flow.utils.registry import make_create_env from ray.tune.registry import register_env # make sure (sample_batch_size * num_workers ~= train_batch_size) # time horizon of a single rollout HORIZON = 500 # Number of rings NUM_RINGS = 3 # number of rollouts per training iteration N_ROLLOUTS = 20 # int(20/NUM_RINGS) # number of parallel workers N_CPUS = 3 # int(20/NUM_RINGS) # We place one autonomous vehicle and 21 human-driven vehicles in the network vehicles = VehicleParams() for i in range(NUM_RINGS): vehicles.add( veh_id='human_{}'.format(i), acceleration_controller=(IDMController, { 'noise': 0.2 }), routing_controller=(ContinuousRouter, {}), num_vehicles=21) vehicles.add( veh_id='rl_{}'.format(i), acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=1) flow_params = dict(
from flow.core.params import SumoParams, EnvParams, InitialConfig, NetParams from flow.core.params import VehicleParams from flow.controllers import RLController, IDMController, ContinuousRouter # that's the baseline scenario for this learning agent from examples.sumo import sugiyama_8 #import ADDITIONAL_ENV_PARAMS, ADDITIONAL_NET_PARAMS, SUMO_STEP # time horizon of a single rollout HORIZON = 3000 # number of rollouts per training iteration N_ROLLOUTS = 20 # 1000 # number of parallel workers N_CPUS = 8 # We place one autonomous vehicle and 22 human-driven vehicles in the network vehicles = VehicleParams() vehicles.add(veh_id="human", acceleration_controller=(IDMController, { "noise": 0.2 }), routing_controller=(ContinuousRouter, {}), num_vehicles=7) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=1) # import from baseline test ADDITIONAL_ENV_PARAMS = sugiyama_8.ADDITIONAL_ENV_PARAMS ADDITIONAL_ENV_PARAMS["ring_length"] = [100, 125] #
accel_data = (IDMController,{'a':1.3,'b':2.0,'noise':0.3}) highway_start_edge = '' if(WANT_GHOST_CELL): from flow.networks.i210_subnetwork_ghost_cell import I210SubNetwork, EDGES_DISTRIBUTION highway_start_edge = 'ghost0' else: from flow.networks.i210_subnetwork import I210SubNetwork, EDGES_DISTRIBUTION highway_start_edge = "119257914" vehicles = VehicleParams() inflow = InFlows() if ON_RAMP: vehicles.add( "human", num_vehicles=0, lane_change_params=SumoLaneChangeParams( lane_change_mode="strategic", ), acceleration_controller=accel_data, routing_controller=(I210Router, {}) ) # inflow.add(
from env_merge import myEnv ADDITIONAL_ENV_PARAMS = { "max_accel": 1, "max_decel": 1, } # time horizon of a single rollout HORIZON = 1000 # number of rollouts per training iteration N_ROLLOUTS = 20 # number of parallel workers N_CPUS = 2 vehicles = VehicleParams() vehicles.add( "rl", acceleration_controller=(IDMController, {}), lane_change_controller=(SimLaneChangeController, {}), #routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", # we use the speed mode "obey_safe_speed" for better dynamics at the merge ), num_vehicles=0) vehicles.add( "human", acceleration_controller=(IDMController, {}), lane_change_controller=(SimLaneChangeController, {}), #routing_controller=(ContinuousRouter, {}),
from flow.core.params import SumoParams from flow.core.params import SumoCarFollowingParams from flow.core.params import VehicleParams from flow.scenarios.figure_eight import ADDITIONAL_NET_PARAMS from flow.utils.registry import make_create_env from flow.utils.rllib import FlowParamsEncoder # time horizon of a single rollout HORIZON = 1500 # number of rollouts per training iteration N_ROLLOUTS = 4 # number of parallel workers N_CPUS = 2 # We place one autonomous vehicle and 13 human-driven vehicles in the network 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)
def run_task(*_): """Implement the run_task method needed to run experiments with rllab.""" sim_params = SumoParams(sim_step=0.1, render=False) vehicles = VehicleParams() vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=1) vehicles.add(veh_id="human", acceleration_controller=(IDMController, {}), 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) 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) 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(),
Horizon: 1500 steps """ from copy import deepcopy from flow.core.params import SumoParams, EnvParams, InitialConfig, NetParams, \ SumoCarFollowingParams from flow.core.params import VehicleParams from flow.controllers import IDMController, ContinuousRouter, RLController from flow.scenarios.figure_eight import ADDITIONAL_NET_PARAMS # time horizon of a single rollout HORIZON = 1500 # We place 8 autonomous vehicle and 8 human-driven vehicles in the network vehicles = VehicleParams() for i in range(7): vehicles.add(veh_id="human{}".format(i), acceleration_controller=(IDMController, { "noise": 0.2 }), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), num_vehicles=1) vehicles.add(veh_id="rl{}".format(i), acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", ), num_vehicles=1)
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
probability=0.25, departLane='free', departSpeed=20) inflow.add( veh_type='human_right', edge="edge17", probability=0.25, departLane='free', departSpeed=20) net_params = NetParams( inflows=inflow, additional_params=ADDITIONAL_NET_PARAMS) vehicles = VehicleParams() 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"
def bottleneck_example(flow_rate, horizon, restart_instance=False, render=None): """ Perform a simulation of vehicles on a bottleneck. Parameters ---------- flow_rate : float total inflow rate of vehicles into the bottleneck horizon : int time horizon restart_instance: bool, optional whether to restart the instance upon reset render: bool, optional specifies whether to use the gui during execution Returns ------- exp: flow.core.experiment.Experiment A non-rl experiment demonstrating the performance of human-driven vehicles on a bottleneck. """ if render is None: render = False sim_params = AimsunParams(sim_step=0.5, render=render, restart_instance=restart_instance) vehicles = VehicleParams() vehicles.add(veh_id="human", num_vehicles=1) additional_env_params = { "target_velocity": 40, "max_accel": 1, "max_decel": 1, "lane_change_duration": 5, "add_rl_if_exit": False, "disable_tb": DISABLE_TB, "disable_ramp_metering": DISABLE_RAMP_METER } env_params = EnvParams(horizon=horizon, additional_params=additional_env_params) inflow = InFlows() inflow.add(veh_type="human", edge="1", vehsPerHour=flow_rate, departLane="random", departSpeed=10) traffic_lights = TrafficLightParams() if not DISABLE_TB: traffic_lights.add(node_id="2") if not DISABLE_RAMP_METER: traffic_lights.add(node_id="3") additional_net_params = {"scaling": SCALING, "speed_limit": 30 / 3.6} net_params = NetParams(inflows=inflow, additional_params=additional_net_params) initial_config = InitialConfig(spacing="random", min_gap=5, lanes_distribution=float("inf"), edges_distribution=["2", "3", "4", "5"]) scenario = BottleneckScenario(name="bay_bridge_toll", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) env = BottleneckEnv(env_params, sim_params, scenario, simulator='aimsun') return Experiment(env)
additional_env_params = { 'target_velocity': 50, 'switch_time': 3.0, 'num_observed': 2, 'discrete': False, 'tl_type': 'controlled' } additional_net_params = { 'speed_limit': 35, 'grid_array': grid_array, 'horizontal_lanes': 1, 'vertical_lanes': 1 } vehicles = VehicleParams() vehicles.add( veh_id='idm', acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( minGap=2.5, decel=7.5, # avoid collisions at emergency stops max_speed=V_ENTER, speed_mode="all_checks", ), routing_controller=(GridRouter, {}), num_vehicles=tot_cars) flow_params = dict( # name of the experiment exp_tag='green_wave',
def make_flow_params(n_rows, n_columns, edge_inflow): """ Generate the flow params for the experiment. Parameters ---------- n_rows : int number of rows in the traffic light grid n_columns : int number of columns in the traffic light grid edge_inflow : float Returns ------- dict flow_params object """ # we place a sufficient number of vehicles to ensure they confirm with the # total number specified above. We also use a "right_of_way" speed mode to # support traffic light compliance vehicles = VehicleParams() num_vehicles = (N_LEFT + N_RIGHT) * n_columns + (N_BOTTOM + N_TOP) * n_rows vehicles.add( veh_id="human", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( min_gap=2.5, max_speed=V_ENTER, decel=7.5, # avoid collisions at emergency stops speed_mode="right_of_way", ), routing_controller=(GridRouter, {}), num_vehicles=num_vehicles) # inflows of vehicles are place on all outer edges (listed here) outer_edges = [] outer_edges += ["left{}_{}".format(n_rows, i) for i in range(n_columns)] outer_edges += ["right0_{}".format(i) for i in range(n_rows)] outer_edges += ["bot{}_0".format(i) for i in range(n_rows)] outer_edges += ["top{}_{}".format(i, n_columns) for i in range(n_rows)] # equal inflows for each edge (as dictate by the EDGE_INFLOW constant) inflow = InFlows() for edge in outer_edges: inflow.add(veh_type="human", edge=edge, vehs_per_hour=edge_inflow, departLane="free", departSpeed=V_ENTER) flow_params = dict( # name of the experiment exp_tag="grid_0_{}x{}_i{}_multiagent".format(n_rows, n_columns, edge_inflow), # name of the flow environment the experiment is running on env_name=MultiTrafficLightGridPOEnv, # name of the 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=False, ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, additional_params={ "target_velocity": 50, "switch_time": 3, "num_observed": 2, "discrete": False, "tl_type": "actuated", "num_local_edges": 4, "num_local_lights": 4, }, ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams( inflows=inflow, additional_params={ "speed_limit": V_ENTER + 5, # inherited from grid0 benchmark "grid_array": { "short_length": SHORT_LENGTH, "inner_length": INNER_LENGTH, "long_length": LONG_LENGTH, "row_num": n_rows, "col_num": n_columns, "cars_left": N_LEFT, "cars_right": N_RIGHT, "cars_top": N_TOP, "cars_bot": N_BOTTOM, }, "horizontal_lanes": 1, "vertical_lanes": 1, }, ), # vehicles to be placed in the network at the start of a rollout (see # flow.core.params.VehicleParams) veh=vehicles, # parameters specifying the positioning of vehicles upon initialization # or reset (see flow.core.params.InitialConfig) initial=InitialConfig( spacing='custom', shuffle=True, ), ) return flow_params
def 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 network 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 network object self.network = self.network.__class__( name=self.network.orig_name, vehicles=vehicles, net_params=net_params, initial_config=self.initial_config, traffic_lights=self.network.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
MAIN_HUMAN = 90 MAIN_RL = 10 MERGE_HUMAN = 30 VEHICLE_NUMBER = MAIN_HUMAN + MAIN_RL + MERGE_HUMAN # We consider a highway network with an upstream merging lane producing # shockwaves additional_net_params = deepcopy(ADDITIONAL_NET_PARAMS) additional_net_params["merge_lanes"] = 1 additional_net_params["highway_lanes"] = 1 additional_net_params["pre_merge_length"] = 4000 additional_net_params["angle"] = pi / 36 additional_net_params["merge_length"] = 4500 additional_net_params["post_merge_length"] = 1000 additional_net_params["INFLOW_EDGE_LEN"] = 1000 # RL vehicles constitute 5% of the total number of vehicles vehicles = VehicleParams() vehicles.add(veh_id="human", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( speed_mode=9, sigma=0, ), num_vehicles=MAIN_HUMAN + MERGE_HUMAN) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), car_following_params=SumoCarFollowingParams( speed_mode=9, sigma=0, ), num_vehicles=MAIN_RL)
# percent of autonomous vehicles RL_PENETRATION = [0.1, 0.25, 0.33][EXP_NUM] # num_rl term (see ADDITIONAL_ENV_PARAMs) #NUM_RL = [5, 13, 17][EXP_NUM] NUM_RL = [30, 250, 333][EXP_NUM] ## We consider a highway network with an upstream merging lane producing # shockwaves additional_net_params = ADDITIONAL_NET_PARAMS.copy() #additional_net_params["merge_lanes"] = 1 #additional_net_params["highway_lanes"] = 1 #additional_net_params["pre_merge_length"] = 500 # RL vehicles constitute 5% of the total number of vehicles # Daniel: adding vehicles and flow from osm.passenger.trips.xml vehicles = VehicleParams() vehicles.add( veh_id="human", acceleration_controller=( SimCarFollowingController, { #"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=9, #"all_checks", #"all_checks", #no_collide", #decel=7.5, # avoid collisions at emergency stops # desired time-gap from leader #tau=2, #7,
def para_produce_rl(HORIZON=3000): vehicles = VehicleParams() vehicles.add(veh_id="human", acceleration_controller=(IDMController, { "noise": 0.2 }), car_following_params=SumoCarFollowingParams(min_gap=0), routing_controller=(ContinuousRouter, {}), num_vehicles=21) vehicles.add(veh_id="rl", acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), num_vehicles=1) flow_params = dict( # name of the experiment exp_tag="stabilizing_the_ring", # name of the flow environment the experiment is running on env_name=WaveAttenuationPOEnv, # 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(sim_step=0.1, render=False, restart_instance=False), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, warmup_steps=750, clip_actions=False, additional_params={ "max_accel": 1, "max_decel": 1, "ring_length": [220, 270], }, ), # network-related parameters (see flow.core.params.NetParams and the # network's documentation or ADDITIONAL_NET_PARAMS component) net=NetParams(additional_params={ "length": 260, "lanes": 1, "speed_limit": 30, "resolution": 40, }, ), # 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()) flow_params['env'].horizon = HORIZON return flow_params
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, 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)]
def para_produce_rl(HORIZON = 3000,NUM_AUTOMATED = 7): # time horizon of a single rollout HORIZON = 1500 # number of rollouts per training iteration N_ROLLOUTS = 20 # number of parallel workers N_CPUS = 2 # number of automated vehicles. Must be less than or equal to 22. NUM_AUTOMATED = NUM_AUTOMATED assert NUM_AUTOMATED in [1, 2, 7, 14], \ "num_automated must be one of [1, 2, 7 14]" # desired velocity for all vehicles in the network, in m/s TARGET_VELOCITY = 20 # maximum acceleration for autonomous vehicles, in m/s^2 MAX_ACCEL = 3 # maximum deceleration for autonomous vehicles, in m/s^2 MAX_DECEL = 3 # We evenly distribute the automated vehicles in the network. num_human = 14 - NUM_AUTOMATED human_per_automated = int(num_human / NUM_AUTOMATED) vehicles = VehicleParams() for i in range(NUM_AUTOMATED): # Add one automated vehicle. vehicles.add( veh_id="rl_{}".format(i), acceleration_controller=(RLController, {}), routing_controller=(ContinuousRouter, {}), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", accel=MAX_ACCEL, decel=MAX_DECEL, ), num_vehicles=1) # Add a fraction of the human driven vehicles. vehicles.add( veh_id="human_{}".format(i), acceleration_controller=(IDMController, { "noise": 0.2 }), car_following_params=SumoCarFollowingParams( speed_mode="obey_safe_speed", decel=1.5 ), routing_controller=(ContinuousRouter, {}), num_vehicles=human_per_automated) flow_params = dict( # name of the experiment exp_tag="multiagent_figure_eight", # name of the flow environment the experiment is running on env_name=AccelEnv, # name of the network class the experiment is running on network=FigureEightNetwork, # simulator that is used by the experiment simulator='traci', # sumo-related parameters (see flow.core.params.SumoParams) sim=SumoParams( sim_step=0.1, render=False, restart_instance=False ), # environment related parameters (see flow.core.params.EnvParams) env=EnvParams( horizon=HORIZON, additional_params={ 'target_velocity': TARGET_VELOCITY, 'max_accel': MAX_ACCEL, 'max_decel': MAX_DECEL, 'sort_vehicles':False }, ), # 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.copy(), ), # 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()) flow_params['env'].horizon = HORIZON return flow_params
# inflow rate at the highway FLOW_RATE = 2000 # percent of autonomous vehicles RL_PENETRATION = [0.1, 0.25, 0.33][EXP_NUM] # num_rl term (see ADDITIONAL_ENV_PARAMs) NUM_RL = [5, 13, 17][EXP_NUM] # We consider a highway network with an upstream merging lane producing # shockwaves additional_net_params = deepcopy(ADDITIONAL_NET_PARAMS) additional_net_params["merge_lanes"] = 1 additional_net_params["highway_lanes"] = 1 additional_net_params["pre_merge_length"] = 500 # RL vehicles constitute 5% of the total number of vehicles vehicles = VehicleParams() vehicles.add( veh_id="human", acceleration_controller=(SimCarFollowingController, { # "noise": 0.2 }), car_following_params=SumoCarFollowingParams( speed_mode=9, ), num_vehicles=5) vehicles.add( veh_id="human2", acceleration_controller=(SimCarFollowingController, {}), car_following_params=SumoCarFollowingParams( speed_mode=9, ),
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 }) scenario = BottleneckScenario(name="bay_bridge_toll", vehicles=vehicles, net_params=net_params) env = DesiredVelocityEnv(env_params, sim_params, scenario) # reset the environment and get a new inflow rate env.reset() expected_inflow = 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 figure_eight_exp_setup(sim_params=None, vehicles=None, env_params=None, net_params=None, initial_config=None, traffic_lights=None): """ Create an environment and scenario pair for figure eight test experiments. Parameters ---------- sim_params : flow.core.params.SumoParams sumo-related configuration parameters, defaults to a time step of 0.1s and no sumo-imposed failsafe on human or rl vehicles vehicles : Vehicles type vehicles to be placed in the network, default is one vehicles with an IDM acceleration controller and ContinuousRouter routing controller. env_params : flow.core.params.EnvParams environment-specific parameters, defaults to a environment with no failsafes, where other parameters do not matter for non-rl runs net_params : flow.core.params.NetParams network-specific configuration parameters, defaults to a figure eight with a 30 m radius and "no_internal_links" set to False initial_config : flow.core.params.InitialConfig specifies starting positions of vehicles, defaults to evenly distributed vehicles across the length of the network traffic_lights: flow.core.params.TrafficLightParams traffic light signals, defaults to no traffic lights in the network """ logging.basicConfig(level=logging.WARNING) if sim_params is None: # set default sim_params configuration sim_params = SumoParams(sim_step=0.1, render=False) if vehicles is None: # set default vehicles configuration vehicles = VehicleParams() vehicles.add(veh_id="idm", acceleration_controller=(IDMController, {}), car_following_params=SumoCarFollowingParams( speed_mode="aggressive", ), routing_controller=(ContinuousRouter, {}), num_vehicles=1) if env_params is None: # set default env_params configuration additional_env_params = { "target_velocity": 8, "max_accel": 1, "max_decel": 1, "sort_vehicles": False } env_params = EnvParams(additional_params=additional_env_params) if net_params is None: # set default net_params configuration additional_net_params = { "radius_ring": 30, "lanes": 1, "speed_limit": 30, "resolution": 40 } net_params = NetParams(no_internal_links=False, additional_params=additional_net_params) if initial_config is None: # set default initial_config configuration initial_config = InitialConfig(lanes_distribution=1) if traffic_lights is None: # set default to no traffic lights traffic_lights = TrafficLightParams() # create the scenario scenario = Figure8Scenario(name="FigureEightTest", vehicles=vehicles, net_params=net_params, initial_config=initial_config, traffic_lights=traffic_lights) # create the environment env = AccelEnv(env_params=env_params, sim_params=sim_params, scenario=scenario) # reset the environment env.reset() return env, scenario
------- flow.core.params.InitialConfig parameters specifying the initial configuration of vehicles in the network flow.core.params.NetParams network-specific parameters used to generate the network """ additional_init_params = {'enter_speed': enter_speed} initial = InitialConfig( spacing='custom', additional_params=additional_init_params) net = NetParams(additional_params=add_net_params) return initial, net vehicles = VehicleParams() vehicles.add( veh_id="human", routing_controller=(GridRouter, {}), car_following_params=SumoCarFollowingParams( min_gap=2.5, decel=7.5, # avoid collisions at emergency stops ), num_vehicles=tot_cars) env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS) tl_logic = TrafficLightParams(baseline=False) phases = [{ "duration": "31", "minDur": "8",