示例#1
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 scenario variable-lane ring road.

    Each edge in this scenario 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 and "no_internal_links" set to False
    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()

    # create the scenario
    scenario = VariableLanesScenario(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,
                   scenario=scenario)

    # reset the environment
    env.reset()

    return env, scenario
示例#2
0
# shockwaves
additional_net_params = deepcopy(ADDITIONAL_NET_PARAMS)
additional_net_params["merge_lanes"] = 1
additional_net_params["highway_lanes"] = 1
additional_net_params["pre_merge_length"] = 4000
additional_net_params["angle"] = pi/36
additional_net_params["merge_length"] = 4500
additional_net_params["post_merge_length"] = 1000
additional_net_params["INFLOW_EDGE_LEN"] = 1000
# RL vehicles constitute 5% of the total number of vehicles
vehicles = VehicleParams()
vehicles.add(
    veh_id="human",
    acceleration_controller=(SimCarFollowingController, {}),
    car_following_params=SumoCarFollowingParams(
        speed_mode=9,
    ),
    num_vehicles=MAIN_HUMAN+MERGE_HUMAN)
vehicles.add(
    veh_id="rl",
    acceleration_controller=(RLController, {}),
    car_following_params=SumoCarFollowingParams(
        speed_mode=9,
    ),
    num_vehicles=MAIN_RL)

# Vehicles are introduced from both sides of merge, with RL vehicles entering
# from the highway portion as well
inflow = InFlows()
'''
inflow.add(
def main():
    #0. Set initial variables
    label = args.label
    load_path = args.load_path
    mode = args.mode
    if args.train == 'True':
        train = True
    else:
        train = False

    if args.attack == 'True':
        attack = True
    else:
        attack = False

    experiments = 2
    runs = 3000
    steps_per_run = 250

    #1. Set the logs object, creating the logs paths, if it does not exists yet, and the experiments logs path
    save_logs = SaveLogs(label, experiments, runs, steps_per_run)
    save_logs.create_logs_path()
    save_logs.create_experiments_logs_path()

    #2. Iniciate the variables used to store informations from the simulation
    performance = np.zeros(runs)
    collisions = np.zeros(runs)
    rewards = np.zeros(runs)
    q_values = np.zeros(runs)
    loss = np.zeros(runs)

    #3. Run a set number of experiments
    for i in range(experiments):
        #1. Create a Vehicle object containing the vehicles that will be in the simulation
        # The tau parameter must be lower than the simulation step in order to allow collisions
        sumo_car_following_params = SumoCarFollowingParams(sigma=1,
                                                           security=False,
                                                           tau=0.1)
        vehicles = Vehicles()
        # The speed mode parameter controlls how the vehicle controlls the speed. All the options for this parameter
        #can be found in: http://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#speed_mode_.280xb3.29
        vehicles.add(veh_id="idm",
                     acceleration_controller=(IDMController, {
                         'T': 0.1,
                         's0': 0.1
                     }),
                     routing_controller=(GridRouter, {}),
                     num_vehicles=4,
                     speed_mode=1100,
                     lane_change_mode='aggressive',
                     sumo_car_following_params=sumo_car_following_params)
        vehicles.add(veh_id="rl",
                     acceleration_controller=(RLController, {}),
                     routing_controller=(GridRouter, {}),
                     num_vehicles=1,
                     speed_mode=0000,
                     lane_change_mode='aggressive',
                     sumo_car_following_params=sumo_car_following_params)

        #2. Initite the parameters for a sumo simulation and the initial configurations of the simulation
        sumo_params = SumoParams(sim_step=0.5, render=True)

        edges_distribution = [
            'bottom', 'right'
        ]  #set the vehicles to just start on the bottom and right edges
        initial_config = InitialConfig(edges_distribution=edges_distribution,
                                       spacing='custom')

        #3. Set the environment parameter
        env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

        #4. Set the netfile parameters with the path to the .net.xml network file
        net_params = NetParams(netfile=os.getcwd() +
                               '/sumo/one_junction.net.xml')

        #5. Create instances of the scenario and environment
        scenario = OneJunctionScenario(  # we use the NetFileScenario scenario class for one junction scenario... 
            name="test_NetFile_scenario",
            generator_class=
            OneJunctionGenerator,  # ... as well as the newly netfile generator class
            vehicles=vehicles,
            net_params=net_params,
            initial_config=initial_config)

        env = OneJuntionCrashEnv(env_params, sumo_params, scenario)

        #6. create a instance of a sumo experiment
        exp = Experiment(env, scenario)

        #7. Run the sumo simulation for a set number of runs and time steps per run
        if mode == 'te':
            info = exp.run_train_eval(runs,
                                      steps_per_run,
                                      run=i,
                                      saveLogs=save_logs,
                                      train=True,
                                      load_path=load_path)
        elif mode == 't':
            info = exp.run_train(runs,
                                 steps_per_run,
                                 run=i,
                                 saveLogs=save_logs,
                                 train=True,
                                 load_path=load_path)
        elif mode == 'e':
            info = exp.run_eval(runs,
                                steps_per_run,
                                run=i,
                                saveLogs=save_logs,
                                attack=attack,
                                epsilon=Config.EPSILON_ATTACK,
                                load_path=load_path)

        performance = performance + info['performance']
        collisions = collisions + info['collisions']
        rewards = rewards + info['returns']

        save_logs.save_graph(label, 'exp' + str(i), info['performance'],
                             info['collisions'], info['returns'], info['loss'],
                             info['q_values'])

    #4. Average the total performance of the experiments
    performance = performance / experiments
    collisions = collisions / experiments
    rewards = rewards / experiments

    #5. Store all the statitics of the simulation
    save_logs.save_config_and_statistics()
    save_logs.save_detection_informations()

    #6. Save the graphs produced
    save_logs.save_graph(label, 'final', performance, collisions, rewards,
                         None, None)
additional_env_params.update({
    'max_accel': 1,
    'max_decel': 1,
    'target_velocity': 30
})

# CREATE VEHICLE TYPES AND INFLOWS

vehicles = VehicleParams()
inflows = InFlows()

# human vehicles
vehicles.add(
    veh_id="idm",
    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))

# autonomous vehicles
vehicles.add(veh_id='rl', acceleration_controller=(RLController, {}))

# add human vehicles on the highway
inflows.add(veh_type="idm",
            edge="highway_0",
            vehs_per_hour=HIGHWAY_INFLOW_RATE,
            depart_lane="free",
            depart_speed="max",
            name="idm_highway_inflow")

# add autonomous vehicles on the highway
# 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 = 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",
示例#6
0
def figure_eight_baseline(num_runs, render=True):
    """Run script for all figure eight 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
    -------
        Experiment
            class needed to run simulations
    """
    exp_tag = flow_params['exp_tag']
    sumo_params = flow_params['sumo']
    env_params = flow_params['env']
    net_params = flow_params['net']
    initial_config = flow_params.get('initial', InitialConfig())
    traffic_lights = flow_params.get('tls', TrafficLightParams())

    # modify the rendering to match what is requested
    sumo_params.render = render

    # set the evaluation flag to True
    env_params.evaluate = True

    # we want no autonomous vehicles in the simulation
    vehicles = Vehicles()
    vehicles.add(veh_id='human',
                 acceleration_controller=(IDMController, {
                     'noise': 0.2
                 }),
                 routing_controller=(ContinuousRouter, {}),
                 sumo_car_following_params=SumoCarFollowingParams(
                     speed_mode='no_collide', ),
                 num_vehicles=14)

    # import the scenario class
    module = __import__('flow.scenarios', fromlist=[flow_params['scenario']])
    scenario_class = getattr(module, flow_params['scenario'])

    # create the scenario object
    scenario = scenario_class(name=exp_tag,
                              vehicles=vehicles,
                              net_params=net_params,
                              initial_config=initial_config,
                              traffic_lights=traffic_lights)

    # import the environment class
    module = __import__('flow.envs', fromlist=[flow_params['env_name']])
    env_class = getattr(module, flow_params['env_name'])

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

    exp = Experiment(env)

    results = exp.run(num_runs, env_params.horizon)
    avg_speed = np.mean(results['mean_returns'])

    return avg_speed
示例#7
0
        "tl_type": "controlled"
    }

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

vehicles = Vehicles()
vehicles.add(
    veh_id="idm",
    acceleration_controller=(SumoCarFollowingController, {}),
    sumo_car_following_params=SumoCarFollowingParams(
        minGap=2.5,
        max_speed=v_enter,
    ),
    routing_controller=(GridRouter, {}),
    num_vehicles=tot_cars,
    speed_mode="all_checks")

initial_config, net_params = \
    get_non_flow_params(v_enter, additional_net_params)

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

    # name of the flow environment the experiment is running on
    env_name="PO_TrafficLightGridEnv",
# initial configuration to vehicles
initial_config = InitialConfig(spacing="random")

# vehicles class
from flow.core.params import VehicleParams

# vehicles dynamics models
from flow.controllers import IDMController, ContinuousRouter
from flow.core.params import SumoCarFollowingParams

vehicles = VehicleParams()
vehicles.add(
    "human",
    acceleration_controller=(IDMController, {}),
    car_following_params=SumoCarFollowingParams(speed_mode='aggressive'),
    routing_controller=(ContinuousRouter, {}),
    num_vehicles=13)

from flow.controllers import RLController
from flow.core.params import SumoLaneChangeParams
from flow.controllers.base_lane_changing_controller import BaseLaneChangeController


class RLLaneChangeController(BaseLaneChangeController):
    """A lane-changing model used to move vehicles into lane 0."""
    def __init__(self,
                 veh_id,
                 lane_change_params=SumoLaneChangeParams(
                     lane_change_mode='aggressive')):
        """Instantiate an RL Controller."""
示例#9
0
NUM_MERGE_HUMANS = 9
NUM_MERGE_RL = 1

# note that the vehicles are added sequentially by the scenario,
# so place the merging vehicles after the vehicles in the ring
vehicles = VehicleParams()
# Inner ring vehicles
vehicles.add(
    veh_id='human',
    acceleration_controller=(IDMController, {
        'noise': 0.2
    }),
    lane_change_controller=(SimLaneChangeController, {}),
    routing_controller=(ContinuousRouter, {}),
    num_vehicles=6,
    car_following_params=SumoCarFollowingParams(minGap=0.0, tau=0.5),
    lane_change_params=SumoLaneChangeParams())
# A single learning agent in the inner ring
vehicles.add(
    veh_id='rl',
    acceleration_controller=(RLController, {}),
    lane_change_controller=(SimLaneChangeController, {}),
    routing_controller=(ContinuousRouter, {}),
    num_vehicles=1,
    car_following_params=SumoCarFollowingParams(
        minGap=0.01,
        tau=0.5,
        speed_mode="obey_safe_speed",
    ),
    lane_change_params=SumoLaneChangeParams())
# Outer ring vehicles
示例#10
0
}

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

vehicles = VehicleParams()
vehicles.add(
    veh_id='idm',
    acceleration_controller=(SimCarFollowingController, {}),
    car_following_params=SumoCarFollowingParams(
        minGap=2.5,
        decel=7.5,  # avoid collisions at emergency stops
        max_speed=V_ENTER,
        speed_mode="all_checks",
    ),
    routing_controller=(GridRouter, {}),
    num_vehicles=tot_cars)

# collect the initialization and network-specific parameters based on the
# choice to use inflows or not
if USE_INFLOWS:
    initial_config, net_params = get_inflow_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)
示例#11
0
def para_produce_rl(HORIZON=750, NUM_AUTOMATED=4):

    # time horizon of a single rollout
    HORIZON = 750
    # number of rollouts per training iteration
    N_ROLLOUTS = 20
    # number of parallel workers
    N_CPUS = 4
    # number of automated vehicles. Must be less than or equal to 22.
    NUM_AUTOMATED = NUM_AUTOMATED

    # We evenly distribute the automated vehicles in the network.
    num_human = 40 - NUM_AUTOMATED
    humans_remaining = num_human

    vehicles = VehicleParams()
    for i in range(NUM_AUTOMATED):
        # Add one automated vehicle.
        vehicles.add(veh_id="rl_{}".format(i),
                     acceleration_controller=(RLController, {}),
                     routing_controller=(MinicityRouter, {}),
                     initial_speed=0,
                     car_following_params=SumoCarFollowingParams(
                         speed_mode="obey_safe_speed", ),
                     num_vehicles=1)
        # Add a fraction of the remaining human vehicles.
        vehicles_to_add = round(humans_remaining / (NUM_AUTOMATED - i))
        humans_remaining -= vehicles_to_add
        vehicles.add(veh_id="human_{}".format(i),
                     acceleration_controller=(IDMController, {
                         "noise": 0.2
                     }),
                     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=vehicles_to_add)

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

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

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

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

            # sumo-related parameters (see flow.core.params.SumoParams)
            sim=SumoParams(sim_step=0.25,
                           render=False,
                           save_render=True,
                           sight_radius=5,
                           pxpm=3,
                           show_radius=True,
                           restart_instance=True),

            # environment related parameters (see flow.core.params.EnvParams)
            env=EnvParams(horizon=750,
                          additional_params=ADDITIONAL_ENV_PARAMS),
            # network-related parameters (see flow.core.params.NetParams and the
            # network's documentation or ADDITIONAL_NET_PARAMS component)
            # net=NetParams(
            #     additional_params={
            #         "length": 260,
            #         "lanes": 1,
            #         "speed_limit": 30,
            #         "resolution": 40,
            #     }, ),
            net=NetParams(),

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

    flow_params['env'].horizon = HORIZON
    return flow_params
示例#12
0
def get_flow_params(fixed_density,
                    stopping_penalty,
                    acceleration_penalty,
                    evaluate=False,
                    multiagent=False,
                    imitation=False):
    """Return the flow-specific parameters of the ring road network.

    This scenario consists of 50 (if density is fixed) or 50-75 vehicles (5 of
    which are automated) are placed on a sing-lane circular track of length
    1500m. In the absence of the automated vehicle, the human-driven vehicles
    exhibit stop-and-go instabilities brought about by the string-unstable
    characteristic of human car-following dynamics. Within this setting, the
    RL vehicles are tasked with dissipating the formation and propagation of
    stop-and-go waves via an objective function that rewards maximizing
    system-level speeds.

    Parameters
    ----------
    fixed_density : bool
        specifies whether the number of human-driven vehicles updates in
        between resets
    stopping_penalty : bool
        whether to include a stopping penalty
    acceleration_penalty : bool
        whether to include a regularizing penalty for accelerations by the AVs
    evaluate : bool
        whether to compute the evaluation reward
    multiagent : bool
        whether the automated vehicles are via a single-agent policy or a
        shared multi-agent policy with the actions of individual vehicles
        assigned by a separate policy call
    imitation : bool
        whether to use the imitation environment

    Returns
    -------
    dict
        flow-related parameters, consisting of the following keys:

        * exp_tag: name of the experiment
        * env_name: environment class of the flow environment the experiment
          is running on. (note: must be in an importable module.)
        * network: network class the experiment uses.
        * simulator: simulator that is used by the experiment (e.g. aimsun)
        * sim: simulation-related parameters (see flow.core.params.SimParams)
        * env: environment related parameters (see flow.core.params.EnvParams)
        * net: network-related parameters (see flow.core.params.NetParams and
          the network's documentation or ADDITIONAL_NET_PARAMS component)
        * veh: vehicles to be placed in the network at the start of a rollout
          (see flow.core.params.VehicleParams)
        * initial (optional): parameters affecting the positioning of vehicles
          upon initialization/reset (see flow.core.params.InitialConfig)
        * tls (optional): traffic lights to be introduced to specific nodes
          (see flow.core.params.TrafficLightParams)
    """
    vehicles = VehicleParams()
    for i in range(NUM_AUTOMATED):
        vehicles.add(
            veh_id="human_{}".format(i),
            acceleration_controller=(IDMController, {
                "a": 1.3,
                "b": 2.0,
                "noise": 0.3 if INCLUDE_NOISE else 0.0
            }),
            routing_controller=(ContinuousRouter, {}),
            car_following_params=SumoCarFollowingParams(min_gap=0.5, ),
            lane_change_params=SumoLaneChangeParams(lane_change_mode=1621, ),
            num_vehicles=NUM_VEHICLES[0] - NUM_AUTOMATED if i == 0 else 0)
        vehicles.add(
            veh_id="rl_{}".format(i),
            acceleration_controller=(RLController, {}),
            routing_controller=(ContinuousRouter, {}),
            car_following_params=SumoCarFollowingParams(min_gap=0.5, ),
            lane_change_params=SumoLaneChangeParams(
                lane_change_mode=0,  # no lane changes by automated vehicles
            ),
            num_vehicles=NUM_AUTOMATED if i == 0 else 0)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    additional_net_params["length"] = RING_LENGTH
    additional_net_params["lanes"] = NUM_LANES

    if multiagent:
        if imitation:
            env_name = None  # to be added later
        else:
            env_name = AVClosedMultiAgentEnv
    else:
        if imitation:
            env_name = AVClosedEnv
        else:
            env_name = AVClosedImitationEnv

    return dict(
        # name of the experiment
        exp_tag='multilane-ring',

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

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

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

        # sumo-related parameters (see flow.core.params.SumoParams)
        sim=SumoParams(
            use_ballistic=True,
            render=False,
            sim_step=0.5,
        ),

        # environment related parameters (see flow.core.params.EnvParams)
        env=EnvParams(
            horizon=1800,
            warmup_steps=50,
            sims_per_step=2,
            evaluate=evaluate,
            additional_params={
                "max_accel": 1,
                "max_decel": 1,
                "target_velocity": 30,
                "stopping_penalty": stopping_penalty,
                "acceleration_penalty": acceleration_penalty,
                "num_vehicles": None if fixed_density else NUM_VEHICLES,
                "even_distribution": False,
                "sort_vehicles": True,
                "expert_model": (IDMController, {
                    "a": 1.3,
                    "b": 2.0,
                }),
            },
        ),

        # network-related parameters (see flow.core.params.NetParams and the
        # network's documentation or ADDITIONAL_NET_PARAMS component)
        net=NetParams(additional_params=additional_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 init/reset
        # (see flow.core.params.InitialConfig)
        initial=InitialConfig(
            spacing="random",
            min_gap=0.5,
            shuffle=True,
        ),
    )
示例#13
0
def bay_bridge_example(sumo_binary=None,
                       use_inflows=False,
                       use_traffic_lights=False):
    """
    Performs a simulation of human-driven vehicle on the Oakland-San Francisco
    Bay Bridge.

    Parameters
    ----------
    sumo_binary: bool, optional
        specifies whether to use sumo's gui during execution
    use_inflows: bool, optional
        whether to activate inflows from the peripheries of the network
    use_traffic_lights: bool, optional
        whether to activate the traffic lights in the scenario

    Returns
    -------
    exp: flow.core.SumoExperiment type
        A non-rl experiment demonstrating the performance of human-driven
        vehicles simulated by sumo on the Bay Bridge.
    """
    sumo_params = SumoParams(sim_step=0.6, overtake_right=True)

    if sumo_binary is not None:
        sumo_params.sumo_binary = sumo_binary

    sumo_car_following_params = SumoCarFollowingParams(speedDev=0.2)
    sumo_lc_params = SumoLaneChangeParams(
        lcAssertive=20,
        lcPushy=0.8,
        lcSpeedGain=4.0,
        model="LC2013",
        # lcKeepRight=0.8
    )

    vehicles = Vehicles()
    vehicles.add(veh_id="human",
                 acceleration_controller=(SumoCarFollowingController, {}),
                 routing_controller=(BayBridgeRouter, {}),
                 speed_mode="all_checks",
                 lane_change_mode="no_lat_collide",
                 sumo_car_following_params=sumo_car_following_params,
                 sumo_lc_params=sumo_lc_params,
                 num_vehicles=1400)

    additional_env_params = {}
    env_params = EnvParams(additional_params=additional_env_params)

    traffic_lights = TrafficLights()

    inflow = InFlows()

    if use_inflows:
        # south
        inflow.add(veh_type="human",
                   edge="183343422",
                   vehsPerHour=528,
                   departLane="0",
                   departSpeed=20)
        inflow.add(veh_type="human",
                   edge="183343422",
                   vehsPerHour=864,
                   departLane="1",
                   departSpeed=20)
        inflow.add(veh_type="human",
                   edge="183343422",
                   vehsPerHour=600,
                   departLane="2",
                   departSpeed=20)

        inflow.add(veh_type="human",
                   edge="393649534",
                   probability=0.1,
                   departLane="0",
                   departSpeed=20)  # no data for this

        # west
        inflow.add(veh_type="human",
                   edge="11189946",
                   vehsPerHour=1752,
                   departLane="0",
                   departSpeed=20)
        inflow.add(veh_type="human",
                   edge="11189946",
                   vehsPerHour=2136,
                   departLane="1",
                   departSpeed=20)
        inflow.add(veh_type="human",
                   edge="11189946",
                   vehsPerHour=576,
                   departLane="2",
                   departSpeed=20)

        # north
        inflow.add(veh_type="human",
                   edge="28413687#0",
                   vehsPerHour=2880,
                   departLane="0",
                   departSpeed=20)
        inflow.add(veh_type="human",
                   edge="28413687#0",
                   vehsPerHour=2328,
                   departLane="1",
                   departSpeed=20)
        inflow.add(veh_type="human",
                   edge="28413687#0",
                   vehsPerHour=3060,
                   departLane="2",
                   departSpeed=20)
        inflow.add(veh_type="human",
                   edge="11198593",
                   probability=0.1,
                   departLane="0",
                   departSpeed=20)  # no data for this
        inflow.add(veh_type="human",
                   edge="11197889",
                   probability=0.1,
                   departLane="0",
                   departSpeed=20)  # no data for this

        # midway through bridge
        inflow.add(veh_type="human",
                   edge="35536683",
                   probability=0.1,
                   departLane="0",
                   departSpeed=20)  # no data for this

    net_params = NetParams(in_flows=inflow, no_internal_links=False)
    net_params.netfile = NETFILE

    # download the netfile from AWS
    if use_traffic_lights:
        my_url = "https://s3-us-west-1.amazonaws.com/flow.netfiles/" \
                 "bay_bridge_TL_all_green.net.xml"
    else:
        my_url = "https://s3-us-west-1.amazonaws.com/flow.netfiles/" \
                 "bay_bridge_junction_fix.net.xml"
    my_file = urllib.request.urlopen(my_url)
    data_to_write = my_file.read()

    with open(
            os.path.join(os.path.dirname(os.path.abspath(__file__)), NETFILE),
            "wb+") as f:
        f.write(data_to_write)

    initial_config = InitialConfig(spacing="uniform", min_gap=15)

    scenario = BayBridgeScenario(name="bay_bridge",
                                 generator_class=BayBridgeGenerator,
                                 vehicles=vehicles,
                                 traffic_lights=traffic_lights,
                                 net_params=net_params,
                                 initial_config=initial_config)

    env = BayBridgeEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
示例#14
0
文件: grid1.py 项目: duthedd/flow
def grid1_baseline(num_runs, render=True):
    """Run script for the grid1 baseline.

    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 place a sufficient number of vehicles to ensure they confirm with the
    # total number specified above. We also use a "right_of_way" speed mode to
    # support traffic light compliance
    vehicles = Vehicles()
    vehicles.add(veh_id="human",
                 acceleration_controller=(SumoCarFollowingController, {}),
                 sumo_car_following_params=SumoCarFollowingParams(
                     min_gap=2.5,
                     max_speed=V_ENTER,
                 ),
                 routing_controller=(GridRouter, {}),
                 num_vehicles=(N_LEFT+N_RIGHT)*N_COLUMNS +
                              (N_BOTTOM+N_TOP)*N_ROWS,
                 speed_mode="right_of_way")

    # inflows of vehicles are place on all outer edges (listed here)
    outer_edges = []
    outer_edges += ["left{}_{}".format(N_ROWS, i) for i in range(N_COLUMNS)]
    outer_edges += ["right0_{}".format(i) for i in range(N_ROWS)]
    outer_edges += ["bot{}_0".format(i) for i in range(N_ROWS)]
    outer_edges += ["top{}_{}".format(i, N_COLUMNS) for i in range(N_ROWS)]

    # equal inflows for each edge (as dictate by the EDGE_INFLOW constant)
    inflow = InFlows()
    for edge in outer_edges:
        inflow.add(veh_type="human", edge=edge, vehs_per_hour=EDGE_INFLOW,
                   departLane="free", departSpeed="max")

    # define the traffic light logic
    tl_logic = TrafficLights(baseline=False)
    phases = [{"duration": "31", "minDur": "5", "maxDur": "45",
               "state": "GGGrrrGGGrrr"},
              {"duration": "2", "minDur": "2", "maxDur": "2",
               "state": "yyyrrryyyrrr"},
              {"duration": "31", "minDur": "5", "maxDur": "45",
               "state": "rrrGGGrrrGGG"},
              {"duration": "2", "minDur": "2", "maxDur": "2",
               "state": "rrryyyrrryyy"}]
    for i in range(N_ROWS*N_COLUMNS):
        tl_logic.add("center"+str(i), tls_type="actuated", phases=phases,
                     programID=1)

    net_params = NetParams(
            inflows=inflow,
            no_internal_links=False,
            additional_params={
                "speed_limit": V_ENTER + 5,
                "grid_array": {
                    "short_length": SHORT_LENGTH,
                    "inner_length": INNER_LENGTH,
                    "long_length": LONG_LENGTH,
                    "row_num": N_ROWS,
                    "col_num": N_COLUMNS,
                    "cars_left": N_LEFT,
                    "cars_right": N_RIGHT,
                    "cars_top": N_TOP,
                    "cars_bot": N_BOTTOM,
                },
                "horizontal_lanes": 1,
                "vertical_lanes": 1,
            },
        )

    sumo_params = SumoParams(
            restart_instance=False,
            sim_step=1,
            render=render,
        )

    env_params = EnvParams(
            evaluate=True,  # Set to True to evaluate traffic metrics
            horizon=HORIZON,
            additional_params={
                "target_velocity": 50,
                "switch_time": 2,
                "num_observed": 2,
                "discrete": False,
                "tl_type": "actuated"
            },
        )

    initial_config = InitialConfig(shuffle=True)

    scenario = SimpleGridScenario(name="grid",
                                  vehicles=vehicles,
                                  net_params=net_params,
                                  initial_config=initial_config,
                                  traffic_lights=tl_logic)

    env = PO_TrafficLightGridEnv(env_params, sumo_params, scenario)

    exp = SumoExperiment(env, scenario)

    results = exp.run(num_runs, HORIZON)
    total_delay = np.mean(results["returns"])

    return total_delay
示例#15
0
def run_task(*_):
    """Implement the run_task method needed to run experiments with rllab."""
    sim_params = SumoParams(sim_step=0.2, render=True)

    # note that the vehicles are added sequentially by the scenario,
    # so place the merging vehicles after the vehicles in the ring
    vehicles = VehicleParams()
    # Inner ring vehicles
    vehicles.add(
        veh_id="human",
        acceleration_controller=(IDMController, {
            "noise": 0.2
        }),
        lane_change_controller=(SimLaneChangeController, {}),
        routing_controller=(ContinuousRouter, {}),
        num_vehicles=6,
        car_following_params=SumoCarFollowingParams(minGap=0.0, tau=0.5),
        lane_change_params=SumoLaneChangeParams())

    # A single learning agent in the inner ring
    vehicles.add(
        veh_id="rl",
        acceleration_controller=(RLController, {}),
        lane_change_controller=(SimLaneChangeController, {}),
        routing_controller=(ContinuousRouter, {}),
        num_vehicles=1,
        car_following_params=SumoCarFollowingParams(
            minGap=0.01,
            tau=0.5,
            speed_mode="obey_safe_speed"
        ),
        lane_change_params=SumoLaneChangeParams())

    # Outer ring vehicles
    vehicles.add(
        veh_id="merge-human",
        acceleration_controller=(IDMController, {
            "noise": 0.2
        }),
        lane_change_controller=(SimLaneChangeController, {}),
        routing_controller=(ContinuousRouter, {}),
        num_vehicles=10,
        car_following_params=SumoCarFollowingParams(minGap=0.0, tau=0.5),
        lane_change_params=SumoLaneChangeParams())

    env_params = EnvParams(
        horizon=HORIZON,
        additional_params={
            "target_velocity": 10,
            "max_accel": 3,
            "max_decel": 3,
            "sort_vehicles": False
        })

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    additional_net_params["ring_radius"] = 50
    additional_net_params["inner_lanes"] = 1
    additional_net_params["outer_lanes"] = 1
    additional_net_params["lane_length"] = 75
    net_params = NetParams(
        no_internal_links=False, additional_params=additional_net_params)

    initial_config = InitialConfig(x0=50, spacing="uniform")

    scenario = TwoLoopsOneMergingScenario(
        name=exp_tag,
        vehicles=vehicles,
        net_params=net_params,
        initial_config=initial_config)

    env_name = "AccelEnv"
    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)
    horizon = env.horizon
    env = normalize(env)

    policy = GaussianMLPPolicy(env_spec=env.spec, hidden_sizes=(100, 50, 25))

    baseline = LinearFeatureBaseline(env_spec=env.spec)

    algo = TRPO(
        env=env,
        policy=policy,
        baseline=baseline,
        batch_size=64 * 3 * horizon,
        max_path_length=horizon,
        # whole_paths=True,
        n_itr=1000,
        discount=0.999,
        # step_size=0.01,
    )
    algo.train()
示例#16
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}
    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)
示例#17
0
def bay_bridge_toll_example(render=None, use_traffic_lights=False):
    """Perform a simulation of the toll portion of the Bay Bridge.

    This consists of the toll booth and sections of the road leading up to it.

    Parameters
    ----------
    render : bool, optional
        specifies whether to use sumo's gui during execution
    use_traffic_lights: bool, optional
        whether to activate the traffic lights in the scenario

    Note
    ----
    Unlike the bay_bridge_example, inflows are always activated here.
    """
    sumo_params = SumoParams(sim_step=0.4, overtake_right=True)

    if render is not None:
        sumo_params.render = render

    sumo_car_following_params = SumoCarFollowingParams(speedDev=0.2)
    sumo_lc_params = SumoLaneChangeParams(model="LC2013",
                                          lcCooperative=0.2,
                                          lcSpeedGain=15)

    vehicles = Vehicles()

    vehicles.add(veh_id="human",
                 acceleration_controller=(SumoCarFollowingController, {}),
                 routing_controller=(BayBridgeRouter, {}),
                 speed_mode="all_checks",
                 lane_change_mode="no_lat_collide",
                 sumo_car_following_params=sumo_car_following_params,
                 sumo_lc_params=sumo_lc_params,
                 num_vehicles=50)

    additional_env_params = {}
    env_params = EnvParams(additional_params=additional_env_params)

    inflow = InFlows()

    inflow.add(veh_type="human",
               edge="393649534",
               probability=0.2,
               departLane="random",
               departSpeed=10)
    inflow.add(veh_type="human",
               edge="4757680",
               probability=0.2,
               departLane="random",
               departSpeed=10)
    inflow.add(veh_type="human",
               edge="32661316",
               probability=0.2,
               departLane="random",
               departSpeed=10)
    inflow.add(veh_type="human",
               edge="90077193#0",
               vehs_per_hour=2000,
               departLane="random",
               departSpeed=10)

    net_params = NetParams(inflows=inflow,
                           no_internal_links=False,
                           netfile=NETFILE)

    # download the netfile from AWS
    if use_traffic_lights:
        my_url = "https://s3-us-west-1.amazonaws.com/flow.netfiles/" \
                 "bay_bridge_TL_all_green.net.xml"
    else:
        my_url = "https://s3-us-west-1.amazonaws.com/flow.netfiles/" \
                 "bay_bridge_junction_fix.net.xml"
    my_file = urllib.request.urlopen(my_url)
    data_to_write = my_file.read()

    with open(
            os.path.join(os.path.dirname(os.path.abspath(__file__)), NETFILE),
            "wb+") as f:
        f.write(data_to_write)

    initial_config = InitialConfig(
        spacing="uniform",  # "random",
        min_gap=15)

    scenario = BayBridgeTollScenario(name="bay_bridge_toll",
                                     vehicles=vehicles,
                                     net_params=net_params,
                                     initial_config=initial_config)

    env = BayBridgeEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
示例#18
0
def loop_merge_example(render=None):
    """
    Perform a simulation of vehicles on a loop 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 loop merge.
    """
    sim_params = SumoParams(sim_step=0.1, emission_path="./data/", render=True)

    if render is not None:
        sim_params.render = render

    # note that the vehicles are added sequentially by the scenario,
    # so place the merging vehicles after the vehicles in the ring
    vehicles = VehicleParams()
    vehicles.add(veh_id="idm",
                 acceleration_controller=(IDMController, {}),
                 lane_change_controller=(SimLaneChangeController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 num_vehicles=7,
                 car_following_params=SumoCarFollowingParams(
                     minGap=0.0,
                     tau=0.5,
                     speed_mode="no_collide",
                 ),
                 lane_change_params=SumoLaneChangeParams())
    vehicles.add(veh_id="merge-idm",
                 acceleration_controller=(IDMController, {}),
                 lane_change_controller=(SimLaneChangeController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 num_vehicles=10,
                 car_following_params=SumoCarFollowingParams(
                     minGap=0.01,
                     tau=0.5,
                     speed_mode="no_collide",
                 ),
                 lane_change_params=SumoLaneChangeParams())

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    additional_net_params["ring_radius"] = 50
    additional_net_params["inner_lanes"] = 1
    additional_net_params["outer_lanes"] = 1
    additional_net_params["lane_length"] = 75
    net_params = NetParams(no_internal_links=False,
                           additional_params=additional_net_params)

    initial_config = InitialConfig(x0=50,
                                   spacing="uniform",
                                   additional_params={"merge_bunching": 0})

    scenario = TwoLoopsOneMergingScenario(name="two-loop-one-merging",
                                          vehicles=vehicles,
                                          net_params=net_params,
                                          initial_config=initial_config)

    env = AccelEnv(env_params, sim_params, scenario)

    return Experiment(env)
示例#19
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 the gui during execution

    Returns
    -------
        flow.core.experiment.Experiment
            class needed to run simulations
    """
    exp_tag = flow_params['exp_tag']
    sim_params = flow_params['sim']
    env_params = flow_params['env']
    net_params = flow_params['net']
    initial_config = flow_params.get('initial', InitialConfig())
    traffic_lights = flow_params.get('tls', TrafficLightParams())

    # we want no autonomous vehicles in the simulation
    vehicles = VehicleParams()
    vehicles.add(veh_id='human',
                 car_following_params=SumoCarFollowingParams(speed_mode=9, ),
                 routing_controller=(ContinuousRouter, {}),
                 lane_change_params=SumoLaneChangeParams(
                     lane_change_mode=1621, ),
                 num_vehicles=1 * SCALING)

    # only include human vehicles in inflows
    flow_rate = 2300 * SCALING
    inflow = InFlows()
    inflow.add(veh_type='human',
               edge='1',
               vehs_per_hour=flow_rate,
               departLane='random',
               departSpeed=10)
    net_params.inflows = inflow

    # modify the rendering to match what is requested
    sim_params.render = render

    # set the evaluation flag to True
    env_params.evaluate = True

    # import the network class
    network_class = flow_params['network']

    # create the network object
    network = network_class(name=exp_tag,
                            vehicles=vehicles,
                            net_params=net_params,
                            initial_config=initial_config,
                            traffic_lights=traffic_lights)

    # import the environment class
    env_class = flow_params['env_name']

    # create the environment object
    env = env_class(env_params, sim_params, network)

    exp = Experiment(env)

    results = exp.run(num_runs, env_params.horizon)

    return np.mean(results['returns']), np.std(results['returns'])
示例#20
0
# number of parallel workers
N_CPUS = 2
# number of rollouts per training iteration
N_ROLLOUTS = N_CPUS * 4

SCALING = 1
NUM_LANES = 4 * SCALING  # number of lanes in the widest highway
DISABLE_TB = True
DISABLE_RAMP_METER = True
AV_FRAC = 0.10

vehicles = VehicleParams()
vehicles.add(veh_id="human",
             lane_change_controller=(SimLaneChangeController, {}),
             routing_controller=(ContinuousRouter, {}),
             car_following_params=SumoCarFollowingParams(
                 speed_mode="all_checks", ),
             lane_change_params=SumoLaneChangeParams(lane_change_mode=0, ),
             num_vehicles=1 * SCALING)
vehicles.add(veh_id="followerstopper",
             acceleration_controller=(RLController, {}),
             lane_change_controller=(SimLaneChangeController, {}),
             routing_controller=(ContinuousRouter, {}),
             car_following_params=SumoCarFollowingParams(speed_mode=9, ),
             lane_change_params=SumoLaneChangeParams(lane_change_mode=0, ),
             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,
示例#21
0
vehicles = VehicleParams()
vehicles.add(
    veh_id="human",
    acceleration_controller=(
        IDMController,
        {
            "noise": 0.2,
            #"T":7,
        }),
    lane_change_controller=(SimLaneChangeController, {}),
    #routing_controller=(ContinuousRouter, {}),
    car_following_params=SumoCarFollowingParams(
        # Define speed mode that will minimize collisions: https://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#speed_mode_.280xb3.29
        speed_mode=
        "right_of_way",  #"right_of_way", #"right_of_way", #"all_checks", #no_collide",
        decel=7.5,  # avoid collisions at emergency stops 
        # desired time-gap from leader
        tau=4,  #7,
        speed_factor=1,
        speed_dev=0.1,
    ),
    lane_change_params=SumoLaneChangeParams(
        model="SL2015",
        # Define a lane changing mode that will allow lane changes
        # See: https://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#lane_change_mode_.280xb6.29
        # and: ~/local/flow_2019_07/flow/core/params.py, see LC_MODES = {"aggressive": 0 /*bug, 0 is no lane-changes*/, "no_lat_collide": 512, "strategic": 1621}, where "strategic" is the default behavior
        lane_change_mode=
        1621,  #0b011000000001, # (like default 1621 mode, but no lane changes other than strategic to follow route, # 512, #(collision avoidance and safety gap enforcement) # "strategic", 
        #lc_speed_gain=1000000,
        #lc_pushy=0, #0.5, #1,
        #lc_assertive=5, #20,
        #lcSpeedGainLookahead=2,
示例#22
0
LONG_LENGTH = 100
# length of edges that vehicles start on
SHORT_LENGTH = 300
# number of vehicles originating in the left, right, top, and bottom edges
N_LEFT, N_RIGHT, N_TOP, N_BOTTOM = 1, 1, 1, 1

# we place a sufficient number of vehicles to ensure they confirm with the
# total number specified above. We also use a "right_of_way" speed mode to
# support traffic light compliance
vehicles = VehicleParams()
vehicles.add(
    veh_id="human",
    acceleration_controller=(SimCarFollowingController, {}),
    car_following_params=SumoCarFollowingParams(
        min_gap=2.5,
        max_speed=V_ENTER,
        decel=7.5,  # avoid collisions at emergency stops
        speed_mode="right_of_way",
    ),
    routing_controller=(GridRouter, {}),
    num_vehicles=(N_LEFT + N_RIGHT) * N_COLUMNS + (N_BOTTOM + N_TOP) * N_ROWS)

# inflows of vehicles are place on all outer edges (listed here)
outer_edges = []
outer_edges += ["left{}_{}".format(N_ROWS, i) for i in range(N_COLUMNS)]
outer_edges += ["right0_{}".format(i) for i in range(N_ROWS)]
outer_edges += ["bot{}_0".format(i) for i in range(N_ROWS)]
outer_edges += ["top{}_{}".format(i, N_COLUMNS) for i in range(N_ROWS)]

# equal inflows for each edge (as dictate by the EDGE_INFLOW constant)
inflow = InFlows()
for edge in outer_edges:
示例#23
0
INNER_LENGTH = 300
# length of final edge in route
LONG_LENGTH = 100
# length of edges that vehicles start on
SHORT_LENGTH = 300
# number of vehicles originating in the left, right, top, and bottom edges
N_LEFT, N_RIGHT, N_TOP, N_BOTTOM = 1, 1, 1, 1

# we place a sufficient number of vehicles to ensure they confirm with the
# total number specified above. We also use a "right_of_way" speed mode to
# support traffic light compliance
vehicles = Vehicles()
vehicles.add(veh_id="human",
             acceleration_controller=(SumoCarFollowingController, {}),
             sumo_car_following_params=SumoCarFollowingParams(
                 min_gap=2.5,
                 max_speed=V_ENTER,
             ),
             routing_controller=(GridRouter, {}),
             num_vehicles=(N_LEFT + N_RIGHT) * N_COLUMNS +
             (N_BOTTOM + N_TOP) * N_ROWS,
             speed_mode="right_of_way")

# inflows of vehicles are place on all outer edges (listed here)
outer_edges = []
outer_edges += ["left{}_{}".format(N_ROWS, i) for i in range(N_COLUMNS)]
outer_edges += ["right0_{}".format(i) for i in range(N_ROWS)]
outer_edges += ["bot{}_0".format(i) for i in range(N_ROWS)]
outer_edges += ["top{}_{}".format(i, N_COLUMNS) for i in range(N_ROWS)]

# equal inflows for each edge (as dictate by the EDGE_INFLOW constant)
inflow = InFlows()
示例#24
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)
示例#25
0
def run_task(*_):
    """Implement the run_task method needed to run experiments with rllab."""
    V_ENTER = 30
    INNER_LENGTH = 300
    LONG_LENGTH = 100
    SHORT_LENGTH = 300
    N_ROWS = 3
    N_COLUMNS = 3
    NUM_CARS_LEFT = 1
    NUM_CARS_RIGHT = 1
    NUM_CARS_TOP = 1
    NUM_CARS_BOT = 1
    tot_cars = (NUM_CARS_LEFT + NUM_CARS_RIGHT) * N_COLUMNS \
        + (NUM_CARS_BOT + NUM_CARS_TOP) * 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=1, render=True)

    vehicles = VehicleParams()
    vehicles.add(veh_id="idm",
                 acceleration_controller=(SimCarFollowingController, {}),
                 car_following_params=SumoCarFollowingParams(
                     min_gap=2.5,
                     tau=1.1,
                     max_speed=V_ENTER,
                     speed_mode="all_checks"),
                 routing_controller=(GridRouter, {}),
                 num_vehicles=tot_cars)

    tl_logic = TrafficLightParams(baseline=False)

    additional_env_params = {
        "target_velocity": 50,
        "switch_time": 3.0,
        "num_observed": 2,
        "discrete": False,
        "tl_type": "controlled"
    }
    env_params = EnvParams(additional_params=additional_env_params)

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

    if USE_INFLOWS:
        initial_config, net_params = get_flow_params(
            v_enter=V_ENTER,
            vehs_per_hour=EDGE_INFLOW,
            col_num=N_COLUMNS,
            row_num=N_ROWS,
            add_net_params=additional_net_params)
    else:
        initial_config, net_params = get_non_flow_params(
            V_ENTER, additional_net_params)

    scenario = SimpleGridScenario(name="grid-intersection",
                                  vehicles=vehicles,
                                  net_params=net_params,
                                  initial_config=initial_config,
                                  traffic_lights=tl_logic)

    env_name = "PO_TrafficLightGridEnv"
    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)
    horizon = env.horizon
    env = normalize(env)

    policy = GaussianMLPPolicy(env_spec=env.spec, hidden_sizes=(32, 32))

    baseline = LinearFeatureBaseline(env_spec=env.spec)

    algo = TRPO(
        env=env,
        policy=policy,
        baseline=baseline,
        batch_size=40000,
        max_path_length=horizon,
        # whole_paths=True,
        n_itr=800,
        discount=0.999,
        # step_size=0.01,
    )
    algo.train()
示例#26
0
        VEH_COLORS = ['red', 'red'] if NEAREST_MERGE else ['red', 'green']

        print("starts test for %d cav 0, %d cav 1" %
              (NUM_MERGE_0, NUM_MERGE_1))
        #######################################################

        Router = NearestMergeRouter if NEAREST_MERGE else SpecificMergeRouter

        vehicles = VehicleParams()
        vehicles.add(
            veh_id="human",
            lane_change_params=SumoLaneChangeParams('only_strategic_safe'),
            car_following_params=SumoCarFollowingParams(
                speed_mode='right_of_way',
                min_gap=5,
                tau=0.5,
                max_speed=MAX_HV_SPEED),
            acceleration_controller=(IDMController, {}),
            routing_controller=(Router, {}),
        )

        vehicles.add(
            veh_id="merge_0",
            lane_change_params=SumoLaneChangeParams('no_strategic_aggressive'),
            car_following_params=SumoCarFollowingParams(
                speed_mode='no_collide',
                min_gap=1,
                tau=0.5,
                max_speed=MAX_CAV_SPEED),
            acceleration_controller=(RLController, {}),
示例#27
0
def run_task(*_):
    """Implement the run_task method needed to run experiments with rllab."""
    v_enter = 10
    inner_length = 300
    long_length = 100
    short_length = 300
    n = 3
    m = 3
    num_cars_left = 1
    num_cars_right = 1
    num_cars_top = 1
    num_cars_bot = 1
    tot_cars = (num_cars_left + num_cars_right) * m \
        + (num_cars_bot + num_cars_top) * n

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

    sumo_params = SumoParams(sim_step=1, render=True)

    vehicles = Vehicles()
    vehicles.add(veh_id="idm",
                 acceleration_controller=(SumoCarFollowingController, {}),
                 sumo_car_following_params=SumoCarFollowingParams(
                     min_gap=2.5, tau=1.1, max_speed=v_enter),
                 routing_controller=(GridRouter, {}),
                 num_vehicles=tot_cars,
                 speed_mode="all_checks")

    tl_logic = TrafficLights(baseline=False)

    additional_env_params = {
        "target_velocity": 50,
        "switch_time": 3.0,
        "num_observed": 2,
        "discrete": False,
        "tl_type": "controlled"
    }
    env_params = EnvParams(additional_params=additional_env_params)

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

    initial_config, net_params = get_flow_params(10, 300, n, m,
                                                 additional_net_params)

    scenario = SimpleGridScenario(name="grid-intersection",
                                  vehicles=vehicles,
                                  net_params=net_params,
                                  initial_config=initial_config,
                                  traffic_lights=tl_logic)

    env_name = "PO_TrafficLightGridEnv"
    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 = GaussianMLPPolicy(env_spec=env.spec, hidden_sizes=(32, 32))

    baseline = LinearFeatureBaseline(env_spec=env.spec)

    algo = TRPO(
        env=env,
        policy=policy,
        baseline=baseline,
        batch_size=40000,
        max_path_length=horizon,
        # whole_paths=True,
        n_itr=800,
        discount=0.999,
        # step_size=0.01,
    )
    algo.train()
示例#28
0
def run_task(*_):
    """Implement the run_task method needed to run experiments with rllab."""
    sim_params = SumoParams(sim_step=0.1, render=True)

    vehicles = VehicleParams()
    vehicles.add(
        veh_id="rl",
        acceleration_controller=(RLController, {}),
        routing_controller=(ContinuousRouter, {}),
        car_following_params=SumoCarFollowingParams(
            speed_mode="obey_safe_speed",
        ),
        num_vehicles=1)
    vehicles.add(
        veh_id="idm",
        acceleration_controller=(IDMController, {
            "noise": 0.2
        }),
        routing_controller=(ContinuousRouter, {}),
        car_following_params=SumoCarFollowingParams(
            speed_mode="obey_safe_speed",
        ),
        num_vehicles=13)

    additional_env_params = {
        "target_velocity": 20,
        "max_accel": 3,
        "max_decel": 3,
        "sort_vehicles": False
    }
    env_params = EnvParams(
        horizon=HORIZON, additional_params=additional_env_params)

    additional_net_params = {
        "radius_ring": 30,
        "lanes": 1,
        "speed_limit": 30,
        "resolution": 40
    }
    net_params = NetParams(
        no_internal_links=False, additional_params=additional_net_params)

    initial_config = InitialConfig(spacing="uniform")

    print("XXX name", exp_tag)
    scenario = Figure8Scenario(
        exp_tag,
        vehicles,
        net_params,
        initial_config=initial_config)

    env_name = "AccelEnv"
    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)
    horizon = env.horizon
    env = normalize(env)

    policy = GaussianMLPPolicy(env_spec=env.spec, hidden_sizes=(16, 16))

    baseline = LinearFeatureBaseline(env_spec=env.spec)

    algo = TRPO(
        env=env,
        policy=policy,
        baseline=baseline,
        batch_size=15000,
        max_path_length=horizon,
        n_itr=500,
        # whole_paths=True,
        discount=0.999,
        # step_size=v["step_size"],
    )
    algo.train(),
示例#29
0
# percent of autonomous vehicles
RL_PENETRATION = 0.06
# num_rl term (see ADDITIONAL_ENV_PARAMs)
NUM_RL = 3

# We consider a highway network with an upstream merging lane producing
# shockwaves
# is the following OK

# RL vehicles constitute 5% of the total number of vehicles
vehicles = VehicleParams()
vehicles.add(
    veh_id="human",
    acceleration_controller=(SimCarFollowingController, {}),
    car_following_params=SumoCarFollowingParams(
      speed_mode="no_collide",
    ),
    num_vehicles=5)
vehicles.add(
    veh_id="rl",
    acceleration_controller=(RLController, {}),
    car_following_params=SumoCarFollowingParams(
      speed_mode="no_collide",
    ),
    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",
示例#30
0
def grid_mxn_exp_setup(row_num=1,
                       col_num=1,
                       sim_params=None,
                       vehicles=None,
                       env_params=None,
                       net_params=None,
                       initial_config=None,
                       tl_logic=None):
    """
    Create an environment and scenario pair for grid 1x1 test experiments.

    Parameters
    ----------
    row_num: int, optional
        number of horizontal rows of edges in the grid network
    col_num: int, optional
        number of vertical columns of edges in the grid network
    sim_params : flow.core.params.SumoParams
        sumo-related configuration parameters, defaults to a time step of 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 : flow.core.params.EnvParams
        environment-specific parameters, defaults to a environment with
        failsafes, where other parameters do not matter for non-rl runs
    net_params : flow.core.params.NetParams
        network-specific configuration parameters, defaults to a 1x1 grid
        which traffic lights on and "no_internal_links" set to False
    initial_config : flow.core.params.InitialConfig
        specifies starting positions of vehicles, defaults to evenly
        distributed vehicles across the length of the network
    tl_logic: flow.core.params.TrafficLightParams
        specifies logic of any traffic lights added to the system
    """
    logging.basicConfig(level=logging.WARNING)

    if tl_logic is None:
        tl_logic = TrafficLightParams(baseline=False)

    if sim_params is None:
        # set default sim_params configuration
        sim_params = SumoParams(sim_step=1, render=False)

    if vehicles is None:
        total_vehicles = 20
        vehicles = VehicleParams()
        vehicles.add(veh_id="idm",
                     acceleration_controller=(IDMController, {}),
                     car_following_params=SumoCarFollowingParams(min_gap=2.5,
                                                                 tau=1.1,
                                                                 max_speed=30),
                     routing_controller=(GridRouter, {}),
                     num_vehicles=total_vehicles)

    if env_params is None:
        # set default env_params configuration
        additional_env_params = {
            "target_velocity": 50,
            "switch_time": 3.0,
            "tl_type": "controlled",
            "discrete": False
        }

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

    if net_params is None:
        # set default net_params configuration
        total_vehicles = vehicles.num_vehicles
        grid_array = {
            "short_length": 100,
            "inner_length": 300,
            "long_length": 3000,
            "row_num": row_num,
            "col_num": col_num,
            "cars_left": int(total_vehicles / 4),
            "cars_right": int(total_vehicles / 4),
            "cars_top": int(total_vehicles / 4),
            "cars_bot": int(total_vehicles / 4)
        }

        additional_net_params = {
            "length": 200,
            "lanes": 2,
            "speed_limit": 35,
            "resolution": 40,
            "grid_array": grid_array,
            "horizontal_lanes": 1,
            "vertical_lanes": 1
        }

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

    if initial_config is None:
        # set default initial_config configuration
        initial_config = InitialConfig(spacing="custom",
                                       additional_params={"enter_speed": 30})

    # create the scenario
    scenario = SimpleGridScenario(name="Grid1x1Test",
                                  vehicles=vehicles,
                                  net_params=net_params,
                                  initial_config=initial_config,
                                  traffic_lights=tl_logic)

    # create the environment
    env = GreenWaveTestEnv(env_params=env_params,
                           sim_params=sim_params,
                           scenario=scenario)

    # reset the environment
    env.reset()

    return env, scenario