예제 #1
0
# inflow rate at the highway
FLOW_RATE = 2000
# percent of autonomous vehicles
RL_PENETRATION = [0.1, 0.25, 0.33][EXP_NUM]
# num_rl term (see ADDITIONAL_ENV_PARAMs)
NUM_RL = [5, 13, 17][EXP_NUM]

# We consider a highway network with an upstream merging lane producing
# shockwaves
additional_net_params = ADDITIONAL_NET_PARAMS.copy()
additional_net_params["merge_lanes"] = 1
additional_net_params["highway_lanes"] = 1
additional_net_params["pre_merge_length"] = 500

# RL vehicles constitute 5% of the total number of vehicles
vehicles = Vehicles()
vehicles.add(veh_id="human",
             acceleration_controller=(IDMController, {
                 "noise": 0.2
             }),
             speed_mode="no_collide",
             num_vehicles=5)
vehicles.add(veh_id="rl",
             acceleration_controller=(RLController, {}),
             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",
예제 #2
0
def bay_bridge_bottleneck_example(sumo_binary=None, use_traffic_lights=False):
    """
    Performs a non-RL simulation of the bottleneck portion of the Oakland-San
    Francisco Bay Bridge. This consists of the toll booth and sections of the
    road leading up to it.

    Parameters
    ----------
    sumo_binary: 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 sumo_binary is not None:
        sumo_params.sumo_binary = sumo_binary

    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(in_flows=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",
                                     generator_class=BayBridgeTollGenerator,
                                     vehicles=vehicles,
                                     net_params=net_params,
                                     initial_config=initial_config)

    env = BayBridgeEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
예제 #3
0
from rllab.misc.instrument import run_experiment_lite
from rllab.algos.ppo import PPO
from rllab.baselines.linear_feature_baseline import LinearFeatureBaseline
from rllab.policies.gaussian_gru_policy import GaussianGRUPolicy

SCALING = 1
NUM_LANES = 4 * SCALING  # number of lanes in the widest highway
DISABLE_TB = True
DISABLE_RAMP_METER = True
AV_FRAC = .1
N_CPUS = 32
i = 0

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

vehicles = Vehicles()

vehicles.add(
    veh_id="human",
    speed_mode=9,
    lane_change_controller=(SumoLaneChangeController, {}),
    routing_controller=(ContinuousRouter, {}),
    lane_change_mode=0,  # 1621,#0b100000101,
    num_vehicles=1 * SCALING)
vehicles.add(veh_id="followerstopper",
             acceleration_controller=(RLController, {
                 "fail_safe": "instantaneous"
             }),
             lane_change_controller=(SumoLaneChangeController, {}),
             routing_controller=(ContinuousRouter, {}),
             speed_mode=9,
예제 #4
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,
                     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
    }

    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()
예제 #5
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)
예제 #6
0
def run_task(_):
    sumo_params = SumoParams(sumo_binary="sumo-gui",
                             sim_step=0.2,
                             restart_instance=True)

    # RL vehicles constitute 5% of the total number of vehicles
    vehicles = Vehicles()
    vehicles.add(veh_id="human",
                 acceleration_controller=(IDMController, {
                     "noise": 0.2
                 }),
                 speed_mode="no_collide",
                 num_vehicles=5)
    vehicles.add(veh_id="rl",
                 acceleration_controller=(RLController, {}),
                 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",
               edge="inflow_highway",
               vehs_per_hour=(1 - RL_PENETRATION) * FLOW_RATE,
               departLane="free",
               departSpeed=10)
    inflow.add(veh_type="rl",
               edge="inflow_highway",
               vehs_per_hour=RL_PENETRATION * FLOW_RATE,
               departLane="free",
               departSpeed=10)
    inflow.add(veh_type="human",
               edge="inflow_merge",
               vehs_per_hour=100,
               departLane="free",
               departSpeed=7.5)

    additional_env_params = {
        "target_velocity": 25,
        "num_rl": 5,
        "max_accel": 1.5,
        "max_decel": 1.5
    }
    env_params = EnvParams(horizon=HORIZON,
                           sims_per_step=5,
                           warmup_steps=0,
                           additional_params=additional_env_params)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    additional_net_params["merge_lanes"] = 1
    additional_net_params["highway_lanes"] = 1
    additional_net_params["pre_merge_length"] = 500
    net_params = NetParams(in_flows=inflow,
                           no_internal_links=False,
                           additional_params=additional_net_params)

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

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

    env_name = "WaveAttenuationMergePOEnv"
    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, 32),
    )

    baseline = LinearFeatureBaseline(env_spec=env.spec)

    algo = TRPO(
        env=env,
        policy=policy,
        baseline=baseline,
        batch_size=18000,
        max_path_length=horizon,
        n_itr=1000,
        # whole_paths=True,
        discount=0.999,
    )
    algo.train(),
예제 #7
0
    def test_encoder_and_get_flow_params(self):
        """Tests both FlowParamsEncoder and get_flow_params.

        FlowParamsEncoder is used to serialize the data from a flow_params dict
        for replay by the visualizer later. Then, the get_flow_params method is
        used to try and read the parameters from the config file, and is
        checked to match expected results.
        """
        # use a flow_params dict derived from flow/benchmarks/merge0.py
        vehicles = Vehicles()
        vehicles.add(
            veh_id="human",
            acceleration_controller=(IDMController, {}),
            sumo_car_following_params=SumoCarFollowingParams(
                speed_mode="no_collide", ),
            # for testing coverage purposes, we add a routing controller
            routing_controller=(ContinuousRouter, {}),
            num_vehicles=5)
        vehicles.add(veh_id="rl",
                     acceleration_controller=(RLController, {}),
                     sumo_car_following_params=SumoCarFollowingParams(
                         speed_mode="no_collide", ),
                     num_vehicles=0)

        inflow = InFlows()
        inflow.add(veh_type="human",
                   edge="inflow_highway",
                   vehs_per_hour=1800,
                   departLane="free",
                   departSpeed=10)
        inflow.add(veh_type="rl",
                   edge="inflow_highway",
                   vehs_per_hour=200,
                   departLane="free",
                   departSpeed=10)
        inflow.add(veh_type="human",
                   edge="inflow_merge",
                   vehs_per_hour=100,
                   departLane="free",
                   departSpeed=7.5)

        flow_params = dict(
            exp_tag="merge_0",
            env_name="WaveAttenuationMergePOEnv",
            scenario="MergeScenario",
            sumo=SumoParams(
                restart_instance=True,
                sim_step=0.5,
                render=False,
            ),
            env=EnvParams(
                horizon=750,
                sims_per_step=2,
                warmup_steps=0,
                additional_params={
                    "max_accel": 1.5,
                    "max_decel": 1.5,
                    "target_velocity": 20,
                    "num_rl": 5,
                },
            ),
            net=NetParams(
                inflows=inflow,
                no_internal_links=False,
                additional_params={
                    "merge_length": 100,
                    "pre_merge_length": 500,
                    "post_merge_length": 100,
                    "merge_lanes": 1,
                    "highway_lanes": 1,
                    "speed_limit": 30,
                },
            ),
            veh=vehicles,
            initial=InitialConfig(),
            tls=TrafficLightParams(),
        )

        # create an config dict with space for the flow_params dict
        config = {"env_config": {}}

        # save the flow params for replay
        flow_json = json.dumps(flow_params,
                               cls=FlowParamsEncoder,
                               sort_keys=True,
                               indent=4)
        config['env_config']['flow_params'] = flow_json

        # dump the config so we can fetch it
        json_out_file = 'params.json'
        with open(os.path.expanduser(json_out_file), 'w+') as outfile:
            json.dump(config,
                      outfile,
                      cls=FlowParamsEncoder,
                      sort_keys=True,
                      indent=4)

        # fetch values using utility function `get_flow_params`
        imported_flow_params = get_flow_params(config)

        # delete the created file
        os.remove(os.path.expanduser('params.json'))

        # test that this inflows are correct
        self.assertTrue(imported_flow_params["net"].inflows.__dict__ ==
                        flow_params["net"].inflows.__dict__)

        imported_flow_params["net"].inflows = None
        flow_params["net"].inflows = None

        # make sure the rest of the imported flow_params match the originals
        self.assertTrue(imported_flow_params["env"].__dict__ ==
                        flow_params["env"].__dict__)
        self.assertTrue(imported_flow_params["initial"].__dict__ ==
                        flow_params["initial"].__dict__)
        self.assertTrue(imported_flow_params["tls"].__dict__ ==
                        flow_params["tls"].__dict__)
        self.assertTrue(imported_flow_params["sumo"].__dict__ ==
                        flow_params["sumo"].__dict__)
        self.assertTrue(imported_flow_params["net"].__dict__ ==
                        flow_params["net"].__dict__)

        self.assertTrue(
            imported_flow_params["exp_tag"] == flow_params["exp_tag"])
        self.assertTrue(
            imported_flow_params["env_name"] == flow_params["env_name"])
        self.assertTrue(
            imported_flow_params["scenario"] == flow_params["scenario"])

        def search_dicts(obj1, obj2):
            """Searches through dictionaries as well as lists of dictionaries
            recursively to determine if any two components are mismatched."""
            for key in obj1.keys():
                # if an next element is a list, either compare the two lists,
                # or if the lists contain dictionaries themselves, look at each
                # dictionary component recursively to check for mismatches
                if isinstance(obj1[key], list):
                    if len(obj1[key]) > 0:
                        if isinstance(obj1[key][0], dict):
                            for i in range(len(obj1[key])):
                                if not search_dicts(obj1[key][i],
                                                    obj2[key][i]):
                                    return False
                        elif obj1[key] != obj2[key]:
                            return False
                # if the next element is a dict, run through it recursively to
                # determine if the separate elements of the dict match
                if isinstance(obj1[key], (dict, collections.OrderedDict)):
                    if not search_dicts(obj1[key], obj2[key]):
                        return False
                # if it is neither a list or a dictionary, compare to determine
                # if the two elements match
                elif obj1[key] != obj2[key]:
                    # if the two elements that are being compared are objects,
                    # make sure that they are the same type
                    if not isinstance(obj1[key], type(obj2[key])):
                        return False
            return True

        # make sure that the Vehicles class that was imported matches the
        # original one
        self.assertTrue(
            search_dicts(imported_flow_params["veh"].__dict__,
                         flow_params["veh"].__dict__))
예제 #8
0
def run_task(*_):
    """Implement the run_task method needed to run experiments with rllab."""
    sumo_params = SumoParams(sim_step=0.1, sumo_binary="sumo-gui")

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

    additional_env_params = {
        "target_velocity": 20,
        "max_accel": 3,
        "max_decel": 3
    }
    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,
                               Figure8Generator,
                               vehicles,
                               net_params,
                               initial_config=initial_config)

    env_name = "AccelEnv"
    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=(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(),
예제 #9
0
def loop_merge_example(render=None):
    """
    Perform a simulation of vehicles on a loop merge.

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

    Returns
    -------
    exp: flow.core.SumoExperiment type
        A non-rl experiment demonstrating the performance of human-driven
        vehicles on a loop merge.
    """
    sumo_params = SumoParams(sim_step=0.1,
                             emission_path="./data/",
                             render=True)

    if render is not None:
        sumo_params.render = render

    # note that the vehicles are added sequentially by the generator,
    # so place the merging vehicles after the vehicles in the ring
    vehicles = Vehicles()
    vehicles.add(veh_id="idm",
                 acceleration_controller=(IDMController, {}),
                 lane_change_controller=(SumoLaneChangeController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 num_vehicles=7,
                 speed_mode="no_collide",
                 sumo_car_following_params=SumoCarFollowingParams(minGap=0.0,
                                                                  tau=0.5),
                 sumo_lc_params=SumoLaneChangeParams())
    vehicles.add(veh_id="merge-idm",
                 acceleration_controller=(IDMController, {}),
                 lane_change_controller=(SumoLaneChangeController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 num_vehicles=10,
                 speed_mode="no_collide",
                 sumo_car_following_params=SumoCarFollowingParams(minGap=0.01,
                                                                  tau=0.5),
                 sumo_lc_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",
        generator_class=TwoLoopOneMergingGenerator,
        vehicles=vehicles,
        net_params=net_params,
        initial_config=initial_config)

    env = AccelEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
예제 #10
0
N_ROWS = 3
# number of columns of bidirectional lanes
N_COLUMNS = 3
# length of inner edges in the grid network
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)]
예제 #11
0
from flow.utils.rllib import FlowParamsEncoder
from flow.core.params import SumoParams, EnvParams, InitialConfig, NetParams, \
    SumoCarFollowingParams
from flow.core.vehicles import Vehicles
from flow.controllers import IDMController, ContinuousRouter, RLController
from flow.scenarios.figure_eight import ADDITIONAL_NET_PARAMS

# time horizon of a single rollout
HORIZON = 1500
# number of rollouts per training iteration
N_ROLLOUTS = 20
# number of parallel workers
N_CPUS = 2

# We place one autonomous vehicle and 13 human-driven vehicles in the network
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=13)
vehicles.add(
    veh_id='rl',
    acceleration_controller=(RLController, {}),
    routing_controller=(ContinuousRouter, {}),
    sumo_car_following_params=SumoCarFollowingParams(
예제 #12
0
from flow.core.params import SumoParams, EnvParams, InitialConfig, NetParams, \
    InFlows
from flow.core.traffic_lights import TrafficLights
from flow.core.vehicles import Vehicles
from flow.controllers import RLController, ContinuousRouter

# time horizon of a single rollout
HORIZON = 1000

SCALING = 2
NUM_LANES = 4 * SCALING  # number of lanes in the widest highway
DISABLE_TB = True
DISABLE_RAMP_METER = True
AV_FRAC = .10

vehicles = Vehicles()
vehicles.add(veh_id="rl",
             acceleration_controller=(RLController, {}),
             routing_controller=(ContinuousRouter, {}),
             speed_mode=9,
             lane_change_mode=0,
             num_vehicles=1 * SCALING)
vehicles.add(veh_id="human",
             speed_mode=9,
             routing_controller=(ContinuousRouter, {}),
             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)]
예제 #13
0
from flow.core.params import SumoParams, EnvParams, InitialConfig, NetParams, \
    InFlows, SumoCarFollowingParams, SumoLaneChangeParams
from flow.core.params import TrafficLightParams
from flow.core.vehicles import Vehicles
from flow.controllers import RLController, ContinuousRouter

# time horizon of a single rollout
HORIZON = 1000

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 = Vehicles()
vehicles.add(veh_id="human",
             routing_controller=(ContinuousRouter, {}),
             sumo_car_following_params=SumoCarFollowingParams(speed_mode=9, ),
             sumo_lc_params=SumoLaneChangeParams(lane_change_mode=0, ),
             num_vehicles=1 * SCALING)
vehicles.add(veh_id="rl",
             acceleration_controller=(RLController, {}),
             routing_controller=(ContinuousRouter, {}),
             sumo_car_following_params=SumoCarFollowingParams(speed_mode=9, ),
             sumo_lc_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)]
예제 #14
0
def bottleneck1_baseline(num_runs, render=True):
    """Run script for the bottleneck1 baseline.

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

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

    controlled_segments = [("1", 1, False), ("2", 2, True), ("3", 2, True),
                           ("4", 2, True), ("5", 1, False)]
    num_observed_segments = [("1", 1), ("2", 3), ("3", 3), ("4", 3), ("5", 1)]
    additional_env_params = {
        "target_velocity": 40,
        "disable_tb": True,
        "disable_ramp_metering": True,
        "controlled_segments": controlled_segments,
        "symmetric": False,
        "observed_segments": num_observed_segments,
        "reset_inflow": False,
        "lane_change_duration": 5,
        "max_accel": 3,
        "max_decel": 3,
        "inflow_range": [1000, 2000]
    }

    # flow rate
    flow_rate = 1900 * SCALING

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

    traffic_lights = TrafficLights()
    if not DISABLE_TB:
        traffic_lights.add(node_id="2")
    if not DISABLE_RAMP_METER:
        traffic_lights.add(node_id="3")

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

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

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

    initial_config = InitialConfig(
        spacing="uniform",
        min_gap=5,
        lanes_distribution=float("inf"),
        edges_distribution=["2", "3", "4", "5"],
    )

    scenario = BottleneckScenario(name="bay_bridge_toll",
                                  vehicles=vehicles,
                                  net_params=net_params,
                                  initial_config=initial_config,
                                  traffic_lights=traffic_lights)

    env = DesiredVelocityEnv(env_params, sumo_params, scenario)

    exp = SumoExperiment(env, scenario)

    results = exp.run(num_runs, HORIZON)

    return np.mean(results["returns"]), np.std(results["returns"])
예제 #15
0
def run_task(*_):
    tot_cars = 8
    auton_cars = 5
    human_cars = tot_cars - auton_cars

    sumo_params = SumoParams(time_step=0.1,
                             human_speed_mode="no_collide",
                             rl_speed_mode="no_collide",
                             human_lane_change_mode="strategic",
                             rl_lane_change_mode="no_lat_collide",
                             sumo_binary="sumo-gui")

    vehicles = Vehicles()
    vehicles.add_vehicles("rl", (RLController, {}), None,
                          (ContinuousRouter, {}), 0, auton_cars)
    vehicles.add_vehicles("cfm", (IDMController, {}), None,
                          (ContinuousRouter, {}), 0, human_cars)

    additional_env_params = {
        "target_velocity": 8,
        "max-deacc": 3,
        "max-acc": 3,
        "num_steps": 500
    }
    env_params = EnvParams(additional_params=additional_env_params)

    additional_net_params = {
        "length": 200,
        "lanes": 2,
        "speed_limit": 35,
        "resolution": 40
    }
    net_params = NetParams(additional_params=additional_net_params)

    initial_config = InitialConfig()

    scenario = LoopScenario("rl-test", CircleGenerator, vehicles, net_params,
                            initial_config)

    env_name = "SimpleLaneChangingAccelerationEnvironment"
    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)
    logging.info("Experiment Set Up complete")

    print("experiment initialized")

    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=10000,
        max_path_length=horizon,
        # whole_paths=True,
        n_itr=2,
        # discount=0.99,
        # step_size=0.01,
    )
    algo.train()
예제 #16
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
    -------
        SumoExperiment
            class needed to run simulations
    """
    # We place 1 autonomous vehicle and 13 human-driven vehicles in the network
    vehicles = Vehicles()
    vehicles.add(veh_id="human",
                 acceleration_controller=(IDMController, {
                     "noise": 0.2
                 }),
                 routing_controller=(ContinuousRouter, {}),
                 speed_mode="no_collide",
                 num_vehicles=14)

    sumo_params = SumoParams(
        sim_step=0.1,
        render=render,
    )

    env_params = EnvParams(
        horizon=HORIZON,
        evaluate=True,  # Set to True to evaluate traffic metrics
        additional_params={
            "target_velocity": 20,
            "max_accel": 3,
            "max_decel": 3,
        },
    )

    initial_config = InitialConfig()

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

    scenario = Figure8Scenario(name="figure_eight",
                               vehicles=vehicles,
                               net_params=net_params,
                               initial_config=initial_config)

    env = AccelEnv(env_params, sumo_params, scenario)

    exp = SumoExperiment(env, scenario)

    results = exp.run(num_runs, HORIZON)
    avg_speed = np.mean(results["mean_returns"])

    return avg_speed
예제 #17
0
def run_task(*_):
    sumo_params = SumoParams(sim_step=0.2, sumo_binary="sumo")

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

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

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

    additional_env_params = {"target_velocity": 20, "max-deacc": -1.5,
                             "max-acc": 1}
    env_params = EnvParams(horizon=HORIZON,
                           additional_params=additional_env_params)

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

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

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

    env_name = "TwoLoopsMergePOEnv"
    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=(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()
예제 #18
0
def grid_mxn_exp_setup(row_num=1,
                       col_num=1,
                       sumo_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
    sumo_params: SumoParams type
        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: EnvParams type
        environment-specific parameters, defaults to a environment with
        failsafes, where other parameters do not matter for non-rl runs
    net_params: NetParams type
        network-specific configuration parameters, defaults to a 1x1 grid
        which traffic lights on and "no_internal_links" set to False
    initial_config: InitialConfig type
        specifies starting positions of vehicles, defaults to evenly
        distributed vehicles across the length of the network
    tl_logic: TrafficLights type
        specifies logic of any traffic lights added to the system
    """
    logging.basicConfig(level=logging.WARNING)

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

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

    if vehicles is None:
        total_vehicles = 20
        vehicles = Vehicles()
        vehicles.add(veh_id="idm",
                     acceleration_controller=(IDMController, {}),
                     sumo_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="uniform",
                                       additional_params={"enter_speed": 30})

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

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

    return env, scenario
예제 #19
0
from flow.core.vehicles import Vehicles

# time horizon of a single rollout
HORIZON = 100
# number of rollouts per training iteration
N_ROLLOUTS = 10
# number of parallel workers
N_CPUS = 2

RING_RADIUS = 100
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 = Vehicles()
# Inner ring vehicles
vehicles.add(veh_id='human',
             acceleration_controller=(IDMController, {
                 'noise': 0.2
             }),
             lane_change_controller=(SumoLaneChangeController, {}),
             routing_controller=(ContinuousRouter, {}),
             num_vehicles=6,
             sumo_car_following_params=SumoCarFollowingParams(minGap=0.0,
                                                              tau=0.5),
             sumo_lc_params=SumoLaneChangeParams())
# A single learning agent in the inner ring
vehicles.add(veh_id='rl',
             acceleration_controller=(RLController, {}),
             lane_change_controller=(SumoLaneChangeController, {}),
예제 #20
0
def variable_lanes_exp_setup(sumo_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
    ----------
    sumo_params: SumoParams type
        sumo-related configuration parameters, defaults to a time step of 0.1s
        and no sumo-imposed failsafe on human or rl vehicles
    vehicles: Vehicles type
        vehicles to be placed in the network, default is one vehicles with an
        IDM acceleration controller and ContinuousRouter routing controller.
    env_params: EnvParams type
        environment-specific parameters, defaults to a environment with no
        failsafes, where other parameters do not matter for non-rl runs
    net_params: NetParams type
        network-specific configuration parameters, defaults to a figure eight
        with a 30 m radius and "no_internal_links" set to False
    initial_config: InitialConfig type
        specifies starting positions of vehicles, defaults to evenly
        distributed vehicles across the length of the network
    traffic_lights: TrafficLights type
        traffic light signals, defaults to no traffic lights in the network
    """
    logging.basicConfig(level=logging.WARNING)

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

    if vehicles is None:
        # set default vehicles configuration
        vehicles = Vehicles()
        vehicles.add(veh_id="idm",
                     acceleration_controller=(IDMController, {}),
                     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,
            "num_steps": 500
        }
        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 = TrafficLights()

    # create the scenario
    scenario = LoopScenario(name="VariableLaneRingRoadTest",
                            generator_class=VariableLanesGenerator,
                            vehicles=vehicles,
                            net_params=net_params,
                            initial_config=initial_config,
                            traffic_lights=traffic_lights)

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

    return env, scenario
예제 #21
0
    def test_make_create_env(self):
        """Tests that the make_create_env methods generates an environment with
        the expected flow parameters."""
        # use a flow_params dict derived from flow/benchmarks/figureeight0.py
        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=13)
        vehicles.add(veh_id="rl",
                     acceleration_controller=(RLController, {}),
                     routing_controller=(ContinuousRouter, {}),
                     sumo_car_following_params=SumoCarFollowingParams(
                         speed_mode="no_collide", ),
                     num_vehicles=1)

        flow_params = dict(
            exp_tag="figure_eight_0",
            env_name="AccelEnv",
            scenario="Figure8Scenario",
            sumo=SumoParams(
                sim_step=0.1,
                render=False,
            ),
            env=EnvParams(
                horizon=1500,
                additional_params={
                    "target_velocity": 20,
                    "max_accel": 3,
                    "max_decel": 3,
                },
            ),
            net=NetParams(
                no_internal_links=False,
                additional_params={
                    "radius_ring": 30,
                    "lanes": 1,
                    "speed_limit": 30,
                    "resolution": 40,
                },
            ),
            veh=vehicles,
            initial=InitialConfig(),
            tls=TrafficLightParams(),
        )

        # some random version number for testing
        v = 23434

        # call make_create_env
        create_env, env_name = make_create_env(params=flow_params, version=v)

        # check that the name is correct
        self.assertEqual(env_name, '{}-v{}'.format(flow_params["env_name"], v))

        # create the gym environment
        env = create_env()

        # Note that we expect the port number in sumo_params to change, and
        # that this feature is in fact needed to avoid race conditions
        flow_params["sumo"].port = env.sumo_params.port

        # check that each of the parameter match
        self.assertEqual(env.env_params.__dict__, flow_params["env"].__dict__)
        self.assertEqual(env.sumo_params.__dict__,
                         flow_params["sumo"].__dict__)
        self.assertEqual(env.traffic_lights.__dict__,
                         flow_params["tls"].__dict__)
        self.assertEqual(env.scenario.net_params.__dict__,
                         flow_params["net"].__dict__)
        self.assertEqual(env.scenario.net_params.__dict__,
                         flow_params["net"].__dict__)
        self.assertEqual(env.scenario.initial_config.__dict__,
                         flow_params["initial"].__dict__)
        self.assertEqual(env.__class__.__name__, flow_params["env_name"])
        self.assertEqual(env.scenario.__class__.__name__,
                         flow_params["scenario"])
예제 #22
0
def setup_bottlenecks(sumo_params=None,
                      vehicles=None,
                      env_params=None,
                      net_params=None,
                      initial_config=None,
                      traffic_lights=None,
                      inflow=None,
                      scaling=1):
    """
    Create an environment and scenario pair for grid 1x1 test experiments.

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

    Parameters
    ----------
    sumo_params: SumoParams type
        sumo-related configuration parameters, defaults to a time step of 0.1s
        and no sumo-imposed failsafe on human or rl vehicles
    vehicles: Vehicles type
        vehicles to be placed in the network, default is 5 vehicles per edge
        for a total of 20 vehicles with an IDM acceleration controller and
        GridRouter routing controller.
    env_params: EnvParams type
        environment-specific parameters, defaults to a environment with
        failsafes, where other parameters do not matter for non-rl runs
    net_params: NetParams type
        network-specific configuration parameters, defaults to a 1x1 grid
        which traffic lights on and "no_internal_links" set to False
    initial_config: InitialConfig type
        specifies starting positions of vehicles, defaults to evenly
        distributed vehicles across the length of the network
    traffic_lights: TrafficLights type
        specifies logic of any traffic lights added to the system
    """
    if sumo_params is None:
        # set default sumo_params configuration
        sumo_params = SumoParams(sim_step=0.1, render=False)

    if vehicles is None:
        vehicles = Vehicles()

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

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

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

    if traffic_lights is None:
        traffic_lights = TrafficLights()

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

    if initial_config is None:
        initial_config = InitialConfig(spacing="random",
                                       min_gap=5,
                                       lanes_distribution=float("inf"),
                                       edges_distribution=["2", "3", "4", "5"])

    scenario = BottleneckScenario(name="bay_bridge_toll",
                                  generator_class=BottleneckGenerator,
                                  vehicles=vehicles,
                                  net_params=net_params,
                                  initial_config=initial_config,
                                  traffic_lights=traffic_lights)

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

    return env, scenario
예제 #23
0
from flow.core.experiment import SumoExperiment
from flow.envs.two_intersection import TwoIntersectionEnvironment
from flow.scenarios.intersections.gen import TwoWayIntersectionGenerator
from flow.scenarios.intersections.intersection_scenario import *
from flow.controllers.car_following_models import *

import logging

logging.basicConfig(level=logging.INFO)

sumo_params = SumoParams(time_step=0.1,
                         emission_path="./data/",
                         sumo_binary="sumo-gui")

vehicles = Vehicles()
vehicles.add_vehicles("idm", (IDMController, {}), None, None, 0, 20)

intensity = .2
v_enter = 10

env_params = EnvParams(
    additional_params={
        "target_velocity": v_enter,
        "max-deacc": -6,
        "max-acc": 3,
        "control-length": 150,
        "max_speed": v_enter
    })

additional_net_params = {
예제 #24
0
Observation Dimension: (28, )

Horizon: 1500 steps
"""

from copy import deepcopy
from flow.core.params import SumoParams, EnvParams, InitialConfig, NetParams
from flow.core.vehicles import Vehicles
from flow.controllers import ContinuousRouter, RLController
from flow.scenarios.figure_eight import ADDITIONAL_NET_PARAMS

# time horizon of a single rollout
HORIZON = 1500

# We place 16 autonomous vehicle and 0 human-driven vehicles in the network
vehicles = Vehicles()
vehicles.add(
    veh_id="rl",
    acceleration_controller=(RLController, {}),
    routing_controller=(ContinuousRouter, {}),
    speed_mode="no_collide",
    num_vehicles=14)

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

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

    # name of the scenario class the experiment is running on
예제 #25
0
def bottleneck_example(flow_rate, horizon, sumo_binary=None):

    if sumo_binary is None:
        sumo_binary = "sumo"
    sumo_params = SumoParams(sim_step=0.5,
                             sumo_binary=sumo_binary,
                             overtake_right=False,
                             restart_instance=True)

    vehicles = Vehicles()

    vehicles.add(veh_id="human",
                 speed_mode=25,
                 lane_change_controller=(SumoLaneChangeController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 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 = TrafficLights()
    if not DISABLE_TB:
        traffic_lights.add(node_id="2")
    if not DISABLE_RAMP_METER:
        traffic_lights.add(node_id="3")

    additional_net_params = {"scaling": SCALING}
    net_params = NetParams(in_flows=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",
                                  generator_class=BottleneckGenerator,
                                  vehicles=vehicles,
                                  net_params=net_params,
                                  initial_config=initial_config,
                                  traffic_lights=traffic_lights)

    env = BottleneckEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
예제 #26
0
def run_task(*_):
    """Implement the run_task method needed to run experiments with rllab."""
    sumo_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 = Vehicles()
    # Inner ring vehicles
    vehicles.add(veh_id="human",
                 acceleration_controller=(IDMController, {
                     "noise": 0.2
                 }),
                 lane_change_controller=(SumoLaneChangeController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 num_vehicles=6,
                 sumo_car_following_params=SumoCarFollowingParams(minGap=0.0,
                                                                  tau=0.5),
                 sumo_lc_params=SumoLaneChangeParams())

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

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

    env_params = EnvParams(horizon=HORIZON,
                           additional_params={
                               "max_accel": 3,
                               "max_decel": 3,
                               "target_velocity": 10,
                               "n_preceding": 2,
                               "n_following": 2,
                               "n_merging_in": 2,
                           })

    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=exp_tag,
                                          vehicles=vehicles,
                                          net_params=net_params,
                                          initial_config=initial_config)

    env_name = "TwoLoopsMergePOEnv"
    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=(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()
예제 #27
0
def figure_eight_baseline(num_runs, flow_params, render=True):
    """Run script for all figure eight baselines.

    Parameters
    ----------
        num_runs : int
            number of rollouts the performance of the environment is evaluated
            over
        flow_params : dict
            the flow meta-parameters describing the structure of a benchmark.
            Must be one of the figure eight flow_params
        render : bool, optional
            specifies whether to use sumo's gui during execution

    Returns
    -------
        SumoExperiment
            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', TrafficLights())

    # 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, {}),
                 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 = SumoExperiment(env, scenario)

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

    return avg_speed
예제 #28
0
# inflow rate at the highway
FLOW_RATE = 2000
# percent of autonomous vehicles
RL_PENETRATION = 0.333
# num_rl term (see ADDITIONAL_ENV_PARAMs)
NUM_RL = 17

# We consider a highway network with an upstream merging lane producing
# 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"] = 500

# RL vehicles constitute 5% of the total number of vehicles
vehicles = Vehicles()
vehicles.add(veh_id="human",
             acceleration_controller=(SumoCarFollowingController, {}),
             sumo_car_following_params=SumoCarFollowingParams(
                 speed_mode="no_collide", ),
             num_vehicles=5)
vehicles.add(veh_id="rl",
             acceleration_controller=(RLController, {}),
             sumo_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",
예제 #29
0
from flow.utils.registry import make_create_env
from flow.utils.rllib import FlowParamsEncoder
from flow.core.params import SumoParams, EnvParams, InitialConfig, NetParams
from flow.core.vehicles import Vehicles
from flow.controllers import RLController, IDMController, ContinuousRouter

# time horizon of a single rollout
HORIZON = 3000
# number of rollouts per training iteration
N_ROLLOUTS = 20
# number of parallel workers
N_CPUS = 2

# We place one autonomous vehicle and 22 human-driven vehicles in the network
vehicles = Vehicles()
vehicles.add(
    veh_id="human",
    acceleration_controller=(IDMController, {
        "noise": 0.2
    }),
    routing_controller=(ContinuousRouter, {}),
    num_vehicles=21)
vehicles.add(
    veh_id="rl",
    acceleration_controller=(RLController, {}),
    routing_controller=(ContinuousRouter, {}),
    num_vehicles=1)

flow_params = dict(
    # name of the experiment
from flow.core.params import SumoParams, EnvParams, InitialConfig, NetParams
from flow.core.vehicles import Vehicles
from flow.core.experiment import SumoExperiment

from flow.envs.loop_merges import SimpleLoopMergesEnvironment
from flow.scenarios.loop_merges.gen import LoopMergesGenerator
from flow.scenarios.loop_merges.loop_merges_scenario import LoopMergesScenario

from numpy import pi

logging.basicConfig(level=logging.INFO)

sumo_params = SumoParams(time_step=0.1, emission_path="./data/", human_speed_mode="no_collide",
                         sumo_binary="sumo-gui")

vehicles = Vehicles()
vehicles.add_vehicles("idm", (IDMController, {}), (StaticLaneChanger, {}), None, 0, 14)
vehicles.add_vehicles("merge-idm", (IDMController, {}), (StaticLaneChanger, {}), None, 0, 14)

additional_env_params = {"target_velocity": 8, "fail-safe": "None"}
env_params = EnvParams(additional_params=additional_env_params)

additional_net_params = {"merge_in_length": 500, "merge_in_angle": pi/9,
                         "merge_out_length": 500, "merge_out_angle": pi * 17/9,
                         "ring_radius": 400 / (2 * pi), "resolution": 40, "lanes": 1, "speed_limit": 30}
net_params = NetParams(no_internal_links=False, additional_params=additional_net_params)

initial_config = InitialConfig(spacing="custom",
                               additional_params={"merge_bunching": 250})

scenario = LoopMergesScenario("loop-merges", LoopMergesGenerator, vehicles, net_params,