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=osp.abspath(osp.join(osp.dirname(__file__), '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, no_internal_links=False, 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 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 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 = params.AimsunParams(render=True, emission_path="./data/", sim_step=0.2, restart_instance=False) if render is not None: sim_params.render = render vehicles = params.VehicleParams() vehicles.add(veh_id="human", acceleration_controller=(IDMController, { "noise": 0.2 }), num_vehicles=5) env_params = params.EnvParams(additional_params=ADDITIONAL_ENV_PARAMS, sims_per_step=5, warmup_steps=0) inflow = params.InFlows() inflow.add(veh_type="human", edge="inflow_highway", vehs_per_hour=HIGHWAY_RATE, departLane="free", departSpeed=10) inflow.add(veh_type="human", edge="inflow_merge", vehs_per_hour=MERGE_RATE, 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 = params.NetParams(inflows=inflow, no_internal_links=False, additional_params=additional_net_params) initial_config = params.InitialConfig() scenario = MergeScenario(name="merge-baseline", vehicles=vehicles, net_params=net_params, initial_config=initial_config) env = WaveAttenuationMergePOEnv(env_params, sim_params, scenario, simulator='aimsun') return Experiment(env)
def merge_baseline(num_runs, render=True): """Run script for all merge baselines. Parameters ---------- num_runs : int number of rollouts the performance of the environment is evaluated over render: bool, optional specifies whether to use sumo's gui during execution Returns ------- SumoExperiment class needed to run simulations """ # We 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 = Vehicles() vehicles.add(veh_id="human", acceleration_controller=(SumoCarFollowingController, {}), speed_mode=9, num_vehicles=5) # 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=FLOW_RATE, departLane="free", departSpeed=10) inflow.add(veh_type="human", edge="inflow_merge", vehs_per_hour=100, departLane="free", departSpeed=7.5) sumo_params = SumoParams( restart_instance=True, sim_step=0.5, # time step decreased to prevent occasional crashes render=render, ) env_params = EnvParams( horizon=HORIZON, sims_per_step=5, # value raised to ensure sec/step match experiment warmup_steps=0, evaluate=True, # Set to True to evaluate traffic metric performance additional_params={ "max_accel": 1.5, "max_decel": 1.5, "target_velocity": 20, "num_rl": NUM_RL, }, ) initial_config = InitialConfig() net_params = NetParams( inflows=inflow, no_internal_links=False, additional_params=additional_net_params, ) scenario = MergeScenario(name="merge", vehicles=vehicles, net_params=net_params, initial_config=initial_config) env = WaveAttenuationMergePOEnv(env_params, sumo_params, scenario) exp = SumoExperiment(env, scenario) results = exp.run(num_runs, HORIZON) avg_speed = np.mean(results["mean_returns"]) return avg_speed