예제 #1
0
    traffic_lights.add(node_id="2")
if not DISABLE_RAMP_METER:
    traffic_lights.add(node_id="3")

additional_net_params = {"scaling": SCALING}
net_params = NetParams(inflows=inflow,
                       no_internal_links=False,
                       additional_params=additional_net_params)

initial_config = InitialConfig(spacing="uniform",
                               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)


def run_task(*_):
    """Implement the run_task method needed to run experiments with rllab."""
    pass_params = (env_name, sumo_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=(64, ))
예제 #2
0
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)
예제 #3
0
def setup_bottlenecks(sumo_params=None,
                      vehicles=None,
                      env_params=None,
                      net_params=None,
                      initial_config=None,
                      traffic_lights=None,
                      inflow=None,
                      scaling=1):
    """
    Create an environment and scenario pair for grid 1x1 test experiments.

    Sumo-related configuration parameters, defaults to a time step of 1s
    and no sumo-imposed failsafe on human or rl vehicles

    Parameters
    ----------
    sumo_params: SumoParams type
        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 5 vehicles per edge
        for a total of 20 vehicles with an IDM acceleration controller and
        GridRouter routing controller.
    env_params: EnvParams type
        environment-specific parameters, defaults to a environment with
        failsafes, where other parameters do not matter for non-rl runs
    net_params: NetParams type
        network-specific configuration parameters, defaults to a 1x1 grid
        which traffic lights on and "no_internal_links" set to False
    initial_config: InitialConfig type
        specifies starting positions of vehicles, defaults to evenly
        distributed vehicles across the length of the network
    traffic_lights: TrafficLights type
        specifies logic of any traffic lights added to the system
    """
    if sumo_params is None:
        # set default sumo_params configuration
        sumo_params = SumoParams(sim_step=0.1, render=False)

    if vehicles is None:
        vehicles = Vehicles()

        vehicles.add(
            veh_id="human",
            speed_mode=25,
            lane_change_controller=(SumoLaneChangeController, {}),
            routing_controller=(ContinuousRouter, {}),
            lane_change_mode=1621,
            num_vehicles=1 * scaling)

    if env_params is None:
        additional_env_params = {
            "target_velocity": 40,
            "max_accel": 1,
            "max_decel": 1,
            "lane_change_duration": 5,
            "add_rl_if_exit": False,
            "disable_tb": True,
            "disable_ramp_metering": True
        }
        env_params = EnvParams(additional_params=additional_env_params)

    if inflow is None:
        inflow = InFlows()
        inflow.add(
            veh_type="human",
            edge="1",
            vehsPerHour=1000,
            departLane="random",
            departSpeed=10)

    if traffic_lights is None:
        traffic_lights = TrafficLights()

    if net_params is None:
        additional_net_params = {"scaling": scaling}
        net_params = NetParams(
            inflows=inflow,
            no_internal_links=False,
            additional_params=additional_net_params)

    if initial_config is None:
        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)

    # create the environment
    env = AccelEnv(
        env_params=env_params, sumo_params=sumo_params, scenario=scenario)

    return env, scenario
def bottleneck_example(flow_rate, horizon, restart_instance=False,
                       render=False, scaling=1, disable_ramp_meter=True, disable_tb=True,
                       lc_on=False, n_crit=8.0, q_max=None, q_min=None, feedback_coef=1, q_init=2300,
                       penetration_rate=0.4):
    """
    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
    scaling: int, optional
        This sets the number of lanes so that they go from 4 * scaling -> 2 * scaling -> 1 * scaling
    disable_tb: bool, optional
        whether the toll booth should be active
    disable_ramp_meter: bool, optional
        specifies if ALINEA should be active. For more details, look at the BottleneckEnv documentation
    lc_on: bool, optional
        if true, the vehicles have LC mode 1621 which is all safe lane changes allowed. Otherwise, it is 0 for
        no lane changing.
    n_crit: float, optional
        number of vehicles in the bottleneck we feedback around. Look at BottleneckEnv for details
    q_max: float, optional
        maximum permissible ALINEA flow. Look at BottleneckEnv for details
    q_min: float, optional
        minimum permissible ALINEA flow. Look at BottleneckEnv for details
    feedback_coeff: float, optional
        gain coefficient for ALINEA. Look at BottleneckEnv for details

    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,
        restart_instance=restart_instance)
    vehicles = VehicleParams()

    if lc_on:
        lc_mode = 1621
    else:
        lc_mode = 0

    controlled_segments = [("1", 1, True), ("2", 8, True), ("3", 3, True),
                           ("4", 1, True), ("5", 3, True)]  # 12 controllable segments
    num_observed_segments = [('1', 1), ('2', 3), ('3', 3), ('4', 3), ('5', 1)]
    v_regions = [23,
                 23, 23, 10, 5, 5, 23, 23, 23,
                 23, 23, 23,
                 23,
                 23, 23, 23]

    # set default q_max, q_min values
    if not q_max:
        if disable_ramp_meter:
            q_max = 14401
        else:
            q_max = 3000

    if not q_min:
        if disable_ramp_meter:
            q_min = 200
        else:
            q_min = 900

    additional_env_params = {
        "target_velocity": 40,
        "max_accel": 3,
        "max_decel": 3,
        "lane_change_duration": 5,
        "add_rl_if_exit": False,
        "disable_tb": disable_tb,
        "disable_ramp_metering": disable_ramp_meter,
        "n_crit": n_crit,
        "q_max": 14401,
        "q_min": 200,
        "q_init": q_init,
        "feedback_coeff": feedback_coef,
        "controlled_segments": controlled_segments,
        "inflow_range": [flow_rate, flow_rate],
        "reset_inflow": False,
        "symmetric": True,
        "observed_segments": num_observed_segments,
        "congest_penalty": False,
        "lc_mode": lc_mode,
        'start_inflow': flow_rate,
        'life_penalty': 0.00,
        'keep_past_actions': False,
        "num_sample_seconds": 0.5,
        "speed_reward": False,
        'fair_reward': False,  # This doesn't do anything, remove
        'exit_history_seconds': 10,  # This doesn't do anything, remove

    }

    if penetration_rate != 0.0:
        vehicles.add(
            veh_id="AV",
            lane_change_controller=(SimLaneChangeController, {}),
            routing_controller=(ContinuousRouter, {}),
            # acceleration_controller=(CFMController, {"v_des": 10, "d_des": 30, "k_d": 30, "k_v": 15}),
            # acceleration_controller=(HandTunedVelocityController, {"v_regions": v_regions}),
            # acceleration_controller=(DecentralizedALINEAController, {"stop_edge": "2", "stop_pos": 310, "additional_env_params": additional_env_params}),
            acceleration_controller=(FakeDecentralizedALINEAController, {"stop_edge": "2", "stop_pos": 310, "additional_env_params": additional_env_params}),
            car_following_params=SumoCarFollowingParams(
                speed_mode=31,
            ),
            lane_change_params=SumoLaneChangeParams(
                lane_change_mode=lc_mode
            ),
            num_vehicles=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)

    env_params = EnvParams(
        horizon=horizon, additional_params=additional_env_params)

    inflow = InFlows()

    if penetration_rate != 0.0:
        av_veh_per_hour = flow_rate * (penetration_rate)
        human_veh_per_hour = flow_rate * (1 - penetration_rate)

        inflow.add(
            veh_type="AV",
            edge="1",
            vehsPerHour=av_veh_per_hour,
            departLane="random",
            departSpeed=23)
    else:
        human_veh_per_hour = flow_rate

    inflow.add(
        veh_type="human",
        edge="1",
        vehsPerHour=human_veh_per_hour,
        departLane="random",
        departSpeed=23)

    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": 60}
    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 = DesiredVelocityEnv(env_params, sim_params, scenario)

    return BottleneckDensityExperiment(env, int(flow_rate))
예제 #5
0
def bottleneck1_baseline(num_runs, render=True):
    """Run script for the bottleneck1 baseline.

    Parameters
    ----------
        num_runs : int
            number of rollouts the performance of the environment is evaluated
            over
        render: str, optional
            specifies whether to use sumo's gui during execution

    Returns
    -------
        SumoExperiment
            class needed to run simulations
    """
    vehicles = Vehicles()
    vehicles.add(veh_id="human",
                 speed_mode=9,
                 routing_controller=(ContinuousRouter, {}),
                 lane_change_mode=1621,
                 num_vehicles=1 * SCALING)

    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)]
    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": False,
        "lane_change_duration": 5,
        "max_accel": 3,
        "max_decel": 3,
        "inflow_range": [1000, 2000]
    }

    # flow rate
    flow_rate = 1900 * SCALING

    # percentage of flow coming out of each lane
    inflow = InFlows()
    inflow.add(veh_type="human",
               edge="1",
               vehs_per_hour=flow_rate,
               departLane="random",
               departSpeed=10)

    traffic_lights = TrafficLights()
    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}
    net_params = NetParams(inflows=inflow,
                           no_internal_links=False,
                           additional_params=additional_net_params)

    sumo_params = SumoParams(
        sim_step=0.5,
        render=render,
        print_warnings=False,
        restart_instance=False,
    )

    env_params = EnvParams(
        evaluate=True,  # Set to True to evaluate traffic metrics
        warmup_steps=40,
        sims_per_step=1,
        horizon=HORIZON,
        additional_params=additional_env_params,
    )

    initial_config = InitialConfig(
        spacing="uniform",
        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 = DesiredVelocityEnv(env_params, sumo_params, scenario)

    exp = SumoExperiment(env, scenario)

    results = exp.run(num_runs, HORIZON)

    return np.mean(results["returns"]), np.std(results["returns"])