def setUp(self):
        vehicles = VehicleParams()
        vehicles.add("rl", acceleration_controller=(RLController, {}))
        vehicles.add("human", acceleration_controller=(IDMController, {}))

        self.sim_params = SumoParams()
        self.scenario = MergeScenario(
            name="test_merge",
            vehicles=vehicles,
            net_params=NetParams(additional_params=MERGE_PARAMS.copy()),
        )
        self.env_params = EnvParams(additional_params={
            "max_accel": 3,
            "max_decel": 3,
            "target_velocity": 25,
            "num_rl": 5,
        })
N_ROLLOUTS = 15  #1#20
# number of parallel workers
N_CPUS = 15  #8#2

# inflow rate at the highway
FLOW_RATE = 2000
MERGE_RATE = 200
# 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]
NUM_RL = 5
## 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, {}),
Example #3
0
File: merge.py Project: alt113/flow
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)
Example #4
0
def dissipating_waves(render=None):

    sim_params = SumoParams(sim_step=0.1, render=True, restart_instance=True)

    if render is not None:
        sim_params.render = render

    # Setup vehicle types
    vehicles = VehicleParams()
    vehicles.add(veh_id="human",
                 acceleration_controller=(IDMController, {
                     "noise": 0.2
                 }),
                 car_following_params=SumoCarFollowingParams(
                     speed_mode="obey_safe_speed", ),
                 num_vehicles=5)
    vehicles.add(veh_id="rl",
                 acceleration_controller=(RLController, {}),
                 car_following_params=SumoCarFollowingParams(
                     speed_mode="obey_safe_speed", ),
                 num_vehicles=0)

    # Vehicles are introduced from both sides of merge, with RL vehicles entering
    # from the highway portion as well
    inflow = InFlows()
    inflow.add(veh_type="human",
               edge="inflow_highway",
               vehs_per_hour=(1 - RL_PENETRATION) * FLOW_RATE,
               depart_lane="free",
               depart_speed=10)
    inflow.add(veh_type="rl",
               edge="inflow_highway",
               vehs_per_hour=RL_PENETRATION * FLOW_RATE,
               depart_lane="free",
               depart_speed=10)
    inflow.add(veh_type="human",
               edge="inflow_merge",
               vehs_per_hour=100,
               depart_lane="free",
               depart_speed=7.5)

    # Set parameters for the network
    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    additional_net_params["pre_merge_length"] = 500
    additional_net_params["post_merge_length"] = 200
    additional_net_params["merge_lanes"] = 1
    additional_net_params["highway_lanes"] = 1
    net_params = NetParams(inflows=inflow,
                           additional_params=additional_net_params)

    # Setup the scenario
    initial_config = InitialConfig()
    scenario = MergeScenario(name='testing',
                             vehicles=vehicles,
                             net_params=net_params,
                             initial_config=initial_config)

    # Setup the environment
    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)
    env = MergePOEnv(env_params, sim_params, scenario)
    return Experiment(env)
Example #5
0
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)
Example #6
0
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(),
Example #7
0
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