Example #1
0
File: merge.py Project: mljack/flow
def merge_example(sumo_binary=None):
    sumo_params = SumoParams(sumo_binary="sumo-gui",
                             emission_path="./data/",
                             sim_step=0.2,
                             restart_instance=True)

    if sumo_binary is not None:
        sumo_params.sumo_binary = sumo_binary

    vehicles = Vehicles()
    vehicles.add(veh_id="human",
                 acceleration_controller=(IDMController, {
                     "noise": 0.2
                 }),
                 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=(1 - 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_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(in_flows=inflow,
                           no_internal_links=False,
                           additional_params=additional_net_params)

    initial_config = InitialConfig(spacing="uniform",
                                   perturbation=5.0,
                                   lanes_distribution=float("inf"))

    scenario = MergeScenario(name="merge-baseline",
                             generator_class=MergeGenerator,
                             vehicles=vehicles,
                             net_params=net_params,
                             initial_config=initial_config)

    env = WaveAttenuationMergePOEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
Example #2
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",
                             generator_class=MergeGenerator,
                             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
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 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 #5
0
    additional_params={
        "max_accel": 1.5,
        "max_decel": 1.5,
        "target_velocity": 20,
        "num_rl": NUM_RL,
    },
)

initial_config = InitialConfig()

net_params = NetParams(
    in_flows=inflow,
    no_internal_links=False,
    additional_params=additional_net_params,
)

scenario = MergeScenario(name="merge",
                         generator_class=MergeGenerator,
                         vehicles=vehicles,
                         net_params=net_params,
                         initial_config=initial_config)

env = WaveAttenuationMergePOEnv(env_params, sumo_params, scenario)

exp = SumoExperiment(env, scenario)

num_runs = 5
results = exp.run(num_runs, HORIZON)
avg_speed = np.mean(results["mean_returns"])
print('The average speed across {} runs is {}'.format(num_runs, avg_speed))