Esempio n. 1
0
def start():
    """Start a environment object with ray."""
    sim_params = SumoParams(sim_step=0.1, render=False)

    vehicles = VehicleParams()
    vehicles.add(
        veh_id="idm",
        acceleration_controller=(IDMController, {}),
        routing_controller=(ContinuousRouter, {}),
        num_vehicles=22)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    net_params = NetParams(additional_params=additional_net_params)

    initial_config = InitialConfig(bunching=20)

    network = RingNetwork(
        name="ring",
        vehicles=vehicles,
        net_params=net_params,
        initial_config=initial_config)

    env = AccelEnv(env_params, sim_params, network)
    env._close()
Esempio n. 2
0
def minicity_example(render=None,
                     save_render=None,
                     sight_radius=None,
                     pxpm=None,
                     show_radius=None):
    """Perform a simulation of modified minicity of University of Delaware.

    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 the minicity network.
    """
    sim_params = SumoParams(sim_step=0.25)

    # update sim_params values if provided as inputs
    sim_params.render = render or sim_params.render
    sim_params.save_render = save_render or sim_params.save_render
    sim_params.sight_radius = sight_radius or sim_params.sight_radius
    sim_params.pxpm = pxpm or sim_params.pxpm
    sim_params.show_radius = show_radius or sim_params.show_radius

    vehicles = VehicleParams()
    vehicles.add(veh_id="idm",
                 acceleration_controller=(IDMController, {}),
                 routing_controller=(MinicityRouter, {}),
                 car_following_params=SumoCarFollowingParams(speed_mode=1, ),
                 lane_change_params=SumoLaneChangeParams(
                     lane_change_mode="no_lat_collide", ),
                 initial_speed=0,
                 num_vehicles=90)
    vehicles.add(veh_id="rl",
                 acceleration_controller=(RLController, {}),
                 routing_controller=(MinicityRouter, {}),
                 car_following_params=SumoCarFollowingParams(
                     speed_mode="obey_safe_speed", ),
                 initial_speed=0,
                 num_vehicles=10)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    net_params = NetParams()

    initial_config = InitialConfig(spacing="random", min_gap=5)
    network = MiniCityNetwork(name="minicity",
                              vehicles=vehicles,
                              initial_config=initial_config,
                              net_params=net_params)

    env = AccelEnv(env_params, sim_params, network)

    return Experiment(env)
Esempio n. 3
0
def figure_eight_example(render=None):
    """
    Perform a simulation of vehicles on a figure eight.

    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 figure eight.
    """
    sim_params = SumoParams(render=True)

    if render is not None:
        sim_params.render = render

    vehicles = VehicleParams()
    vehicles.add(
        veh_id="idm",
        acceleration_controller=(IDMController, {}),
        lane_change_controller=(StaticLaneChanger, {}),
        routing_controller=(ContinuousRouter, {}),
        car_following_params=SumoCarFollowingParams(
            speed_mode="obey_safe_speed",
            decel=1.5,
        ),
        initial_speed=0,
        num_vehicles=14)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    net_params = NetParams(additional_params=additional_net_params)

    network = FigureEightNetwork(
        name="FigureEight",
        vehicles=vehicles,
        net_params=net_params)

    env = AccelEnv(env_params, sim_params, network)

    return Experiment(env)
Esempio n. 4
0
def sugiyama_example(render=None):
    """
    Perform a simulation of vehicles on a ring road.

    Parameters
    ----------
    render : bool, optional
        specifies whether to use sumo's gui during execution

    Returns
    -------
    exp: flow.core.Experiment type
        A non-rl experiment demonstrating the performance of human-driven
        vehicles on a ring road.
    """
    sumo_params = SumoParams(sim_step=SUMO_STEP, render=True)

    if render is not None:
        sumo_params.render = render

    vehicles = VehicleParams()
    vehicles.add(veh_id="idm",
                 acceleration_controller=(IDMController, {
                     "noise": 0.2
                 }),
                 routing_controller=(ContinuousRouter, {}),
                 num_vehicles=8)

    # in sugiyama taken from .../flow/scenarios/loop/loop_accel.py
    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    # in sugiyama taken from .../flow/scenarios/loop.py
    net_params = NetParams(additional_params=ADDITIONAL_NET_PARAMS)

    initial_config = InitialConfig(bunching=20)

    scenario = LoopScenario(name="sugiyama",
                            vehicles=vehicles,
                            net_params=net_params,
                            initial_config=initial_config)

    env = AccelEnv(env_params, sumo_params, scenario)

    return Experiment(env)
Esempio n. 5
0
def sugiyama_example(render=None):
    """
    Perform a simulation of vehicles on a ring road.

    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 ring road.
    """
    sim_params = SumoParams(sim_step=0.1, render=True)

    if render is not None:
        sim_params.render = render

    vehicles = VehicleParams()
    vehicles.add(veh_id="idm",
                 acceleration_controller=(IDMController, {}),
                 car_following_params=SumoCarFollowingParams(min_gap=0),
                 routing_controller=(ContinuousRouter, {}),
                 num_vehicles=22)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    net_params = NetParams(additional_params=additional_net_params)

    initial_config = InitialConfig(bunching=20)

    network = RingNetwork(name="sugiyama",
                          vehicles=vehicles,
                          net_params=net_params,
                          initial_config=initial_config)

    env = AccelEnv(env_params, sim_params, network)

    return Experiment(env)
Esempio n. 6
0
def traffic_light_grid_example(render=None, use_inflows=False):
    """
    Perform a simulation of vehicles on a traffic light grid.

    Parameters
    ----------
    render: bool, optional
        specifies whether to use the gui during execution
    use_inflows : bool, optional
        set to True if you would like to run the experiment with inflows of
        vehicles from the edges, and False otherwise

    Returns
    -------
    exp: flow.core.experiment.Experiment
        A non-rl experiment demonstrating the performance of human-driven
        vehicles and balanced traffic lights on a traffic light grid.
    """
    v_enter = 10
    inner_length = 300
    long_length = 500
    short_length = 300
    n_rows = 2
    n_columns = 3
    num_cars_left = 20
    num_cars_right = 20
    num_cars_top = 20
    num_cars_bot = 20
    tot_cars = (num_cars_left + num_cars_right) * n_columns \
        + (num_cars_top + num_cars_bot) * n_rows

    grid_array = {
        "short_length": short_length,
        "inner_length": inner_length,
        "long_length": long_length,
        "row_num": n_rows,
        "col_num": n_columns,
        "cars_left": num_cars_left,
        "cars_right": num_cars_right,
        "cars_top": num_cars_top,
        "cars_bot": num_cars_bot
    }

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

    if render is not None:
        sim_params.render = render

    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",
        "maxDur": "45",
        "state": "GrGrGrGrGrGr"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "state": "yryryryryryr"
    }, {
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "state": "rGrGrGrGrGrG"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "state": "ryryryryryry"
    }]
    tl_logic.add("center0", phases=phases, programID=1)
    tl_logic.add("center1", phases=phases, programID=1)
    tl_logic.add("center2", phases=phases, programID=1, tls_type="actuated")

    additional_net_params = {
        "grid_array": grid_array,
        "speed_limit": 35,
        "horizontal_lanes": 1,
        "vertical_lanes": 1
    }

    if use_inflows:
        initial_config, net_params = get_flow_params(
            col_num=n_columns,
            row_num=n_rows,
            additional_net_params=additional_net_params)
    else:
        initial_config, net_params = get_non_flow_params(
            enter_speed=v_enter, add_net_params=additional_net_params)

    network = TrafficLightGridNetwork(name="grid-intersection",
                                      vehicles=vehicles,
                                      net_params=net_params,
                                      initial_config=initial_config,
                                      traffic_lights=tl_logic)

    env = AccelEnv(env_params, sim_params, network)

    return Experiment(env)
Esempio n. 7
0
def highway_ramps_example(render=None):
    """
    Perform a simulation of vehicles on a highway section with ramps.

    Parameters
    ----------
    render: bool, optional
        Specifies whether or not to use the GUI during the simulation.

    Returns
    -------
    exp: flow.core.experiment.Experiment
        A non-RL experiment demonstrating the performance of human-driven
        vehicles on a highway section with on and off ramps.
    """
    sim_params = SumoParams(render=True,
                            emission_path="./data/",
                            sim_step=0.2,
                            restart_instance=True)

    if render is not None:
        sim_params.render = render

    vehicles = VehicleParams()
    vehicles.add(
        veh_id="human",
        car_following_params=SumoCarFollowingParams(
            speed_mode="obey_safe_speed",  # for safer behavior at the merges
            tau=1.5  # larger distance between cars
        ),
        lane_change_params=SumoLaneChangeParams(lane_change_mode=1621))

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS,
                           sims_per_step=5,
                           warmup_steps=0)

    inflows = InFlows()
    inflows.add(veh_type="human",
                edge="highway_0",
                vehs_per_hour=HIGHWAY_INFLOW_RATE,
                depart_lane="free",
                depart_speed="max",
                name="highway_flow")
    for i in range(len(additional_net_params["on_ramps_pos"])):
        inflows.add(veh_type="human",
                    edge="on_ramp_{}".format(i),
                    vehs_per_hour=ON_RAMPS_INFLOW_RATE,
                    depart_lane="first",
                    depart_speed="max",
                    name="on_ramp_flow")

    net_params = NetParams(inflows=inflows,
                           additional_params=additional_net_params)

    initial_config = InitialConfig()  # no vehicles initially

    network = HighwayRampsNetwork(name="highway-ramp",
                                  vehicles=vehicles,
                                  net_params=net_params,
                                  initial_config=initial_config)

    env = AccelEnv(env_params, sim_params, network)

    return Experiment(env)
Esempio n. 8
0
def variable_lanes_exp_setup(sim_params=None,
                             vehicles=None,
                             env_params=None,
                             net_params=None,
                             initial_config=None,
                             traffic_lights=None):
    """
    Create an environment and network variable-lane ring road.

    Each edge in this network can have a different number of lanes. Used for
    test purposes.

    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
    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 = {
            "length": 230,
            "lanes": 1,
            "speed_limit": 30,
            "resolution": 40
        }
        net_params = NetParams(additional_params=additional_net_params)

    if initial_config is None:
        # set default initial_config configuration
        initial_config = InitialConfig()

    if traffic_lights is None:
        # set default to no traffic lights
        traffic_lights = TrafficLightParams()

    flow_params = dict(
        # name of the experiment
        exp_tag="VariableLaneRingRoadTest",

        # name of the flow environment the experiment is running on
        env_name=AccelEnv,

        # name of the network class the experiment is running on
        network=VariableLanesNetwork,

        # simulator that is used by the experiment
        simulator='traci',

        # sumo-related parameters (see flow.core.params.SumoParams)
        sim=sim_params,

        # environment related parameters (see flow.core.params.EnvParams)
        env=env_params,
        # network-related parameters (see flow.core.params.NetParams and the
        # network's documentation or ADDITIONAL_NET_PARAMS component)
        net=net_params,

        # 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=initial_config,

        # traffic lights to be introduced to specific nodes (see
        # flow.core.params.TrafficLightParams)
        tls=traffic_lights,
    )

    # create the network
    network = VariableLanesNetwork(name="VariableLaneRingRoadTest",
                                   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,
                   network=network)

    # reset the environment
    env.reset()

    return env, network, flow_params
Esempio n. 9
0
    'lanes': 3,
    'speed_limit': 10,
    'resolution': 40
})

initial_config = InitialConfig(spacing="uniform", perturbation=1)

traffic_lights = TrafficLightParams()

sumo_params = SumoParams(sim_step=0.1, render=True, emission_path='data')

env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

# create the network object
network = RingNetwork(name="ring_example",
                      vehicles=vehicles,
                      net_params=net_params,
                      initial_config=initial_config,
                      traffic_lights=traffic_lights)

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

# create the experiment object
exp = Experiment(env)

# run the experiment for a set number of rollouts / time steps
_ = exp.run(1, 3000, convert_to_csv=True)

print(ADDITIONAL_ENV_PARAMS)
Esempio n. 10
0
def ring_road_exp_setup(sim_params=None,
                        vehicles=None,
                        env_params=None,
                        net_params=None,
                        initial_config=None,
                        traffic_lights=None):
    """
    Create an environment and network pair for ring road 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 single lane
        ring road of length 230 m
    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, {}),
                     routing_controller=(ContinuousRouter, {}),
                     car_following_params=SumoCarFollowingParams(
                         speed_mode="aggressive", ),
                     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 = {
            "length": 230,
            "lanes": 1,
            "speed_limit": 30,
            "resolution": 40
        }
        net_params = NetParams(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 network
    network = RingNetwork(name="RingRoadTest",
                          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,
                   network=network)

    # reset the environment
    env.reset()

    return env, network
Esempio n. 11
0
def traffic_light_grid_example(render=None):
    """
    Perform a simulation of vehicles on a traffic light grid.

    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 and balanced traffic lights on a traffic light grid.
    """
    inner_length = 300
    long_length = 500
    short_length = 300
    N_ROWS = 2
    N_COLUMNS = 3
    num_cars_left = 20
    num_cars_right = 20
    num_cars_top = 20
    num_cars_bot = 20
    tot_cars = (num_cars_left + num_cars_right) * N_COLUMNS \
        + (num_cars_top + num_cars_bot) * N_ROWS

    grid_array = {
        "short_length": short_length,
        "inner_length": inner_length,
        "long_length": long_length,
        "row_num": N_ROWS,
        "col_num": N_COLUMNS,
        "cars_left": num_cars_left,
        "cars_right": num_cars_right,
        "cars_top": num_cars_top,
        "cars_bot": num_cars_bot
    }

    sim_params = AimsunParams(sim_step=0.5, render=True)

    if render is not None:
        sim_params.render = render

    vehicles = VehicleParams()
    vehicles.add(
        veh_id="human",
        num_vehicles=tot_cars)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    tl_logic = TrafficLightParams(baseline=False)
    phases = [{
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "yellow": "3",
        "state": "GGGrrrGGGrrr"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "yellow": "3",
        "state": "yyyrrryyyrrr"
    }, {
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "yellow": "3",
        "state": "rrrGGGrrrGGG"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "yellow": "3",
        "state": "rrryyyrrryyy"
    }]
    tl_logic.add("center0", phases=phases, programID=1)
    tl_logic.add("center1", phases=phases, programID=1)
    tl_logic.add("center2", tls_type="actuated", phases=phases, programID=1)
    tl_logic.add("center3", phases=phases, programID=1)
    tl_logic.add("center4", phases=phases, programID=1)
    tl_logic.add("center5", tls_type="actuated", phases=phases, programID=1)

    additional_net_params = {
        "grid_array": grid_array,
        "speed_limit": 35,
        "horizontal_lanes": 1,
        "vertical_lanes": 1
    }
    net_params = NetParams(additional_params=additional_net_params)

    initial_config = InitialConfig(spacing='custom')

    network = TrafficLightGridNetwork(
        name="grid-intersection",
        vehicles=vehicles,
        net_params=net_params,
        initial_config=initial_config,
        traffic_lights=tl_logic)

    env = AccelEnv(env_params, sim_params, network, simulator='aimsun')

    return Experiment(env)
Esempio n. 12
0
def da_yuan_example(render=None, use_inflows=True):
    """
    Perform a simulation of vehicles on a traffic light grid.

    Parameters
    ----------
    render: bool, optional
        specifies whether to use the gui during execution
    use_inflows : bool, optional
        set to True if you would like to run the experiment with inflows of
        vehicles from the edges, and False otherwise

    Returns
    -------
    exp: flow.core.experiment.Experiment
        A non-rl experiment demonstrating the performance of human-driven
        vehicles and balanced traffic lights on a traffic light grid.
    """
    v_enter = 10
    sim_params = SumoParams(sim_step=0.05, render=True)

    if render is not None:
        sim_params.render = render

    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=6)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    tl_logic = TrafficLightParams(baseline=False)
    phases = [{
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "state": "rrGGGG"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "state": "rryyyy"
    }, {
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "state": "GGrrrr"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "state": "yyrrrr"
    }]
    tl_logic.add("inner0", phases=phases, programID=1)
    tl_logic.add("inner1", phases=phases, programID=1)
    tl_logic.add("inner2", phases=phases, programID=1, tls_type="actuated")
    tl_logic.add("inner3", phases=phases, programID=1, tls_type="actuated")

    additional_net_params = {
        "speed_limit": 35,
        "horizontal_lanes": 1,
        "vertical_lanes": 1
    }

    if use_inflows:
        initial_config, net_params = get_flow_params(
            additional_net_params=additional_net_params)
    else:
        initial_config, net_params = get_non_flow_params(enter_speed=v_enter)

    network = DaYuanNetwork(name="grid-intersection",
                            vehicles=vehicles,
                            net_params=net_params,
                            initial_config=initial_config,
                            traffic_lights=tl_logic)

    env = AccelEnv(env_params, sim_params, network)

    return Experiment(env)