Exemplo n.º 1
0
def make_flow_params(n_rows, n_columns, edge_inflow):
    """
    Generate the flow params for the experiment.

    Parameters
    ----------
    n_rows : int
        number of rows in the traffic light grid
    n_columns : int
        number of columns in the traffic light grid
    edge_inflow : float

    Returns
    -------
    dict
        flow_params object
    """
    # 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()
    num_vehicles = (N_LEFT + N_RIGHT) * n_columns + (N_BOTTOM + N_TOP) * n_rows
    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=num_vehicles)

    # 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=V_ENTER)

    flow_params = dict(
        # name of the experiment
        exp_tag="grid_0_{}x{}_i{}_multiagent".format(n_rows, n_columns,
                                                     edge_inflow),

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

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

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

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

        # environment related parameters (see flow.core.params.EnvParams)
        env=EnvParams(
            horizon=HORIZON,
            additional_params={
                "target_velocity": 50,
                "switch_time": 3,
                "num_observed": 2,
                "discrete": False,
                "tl_type": "actuated",
                "num_local_edges": 4,
                "num_local_lights": 4,
            },
        ),

        # network-related parameters (see flow.core.params.NetParams and the
        # network's documentation or ADDITIONAL_NET_PARAMS component)
        net=NetParams(
            inflows=inflow,
            additional_params={
                "speed_limit": V_ENTER + 5,  # inherited from grid0 benchmark
                "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,
            },
        ),

        # 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
        # or reset (see flow.core.params.InitialConfig)
        initial=InitialConfig(
            spacing='custom',
            shuffle=True,
        ),
    )
    return flow_params
Exemplo n.º 2
0
    my_url = "http://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__)), TEMPLATE),
          "wb+") as f:
    f.write(data_to_write)

vehicles = VehicleParams()
vehicles.add(
    veh_id="human",
    acceleration_controller=(SimCarFollowingController, {}),
    routing_controller=(BayBridgeRouter, {}),
    car_following_params=SumoCarFollowingParams(
        speedDev=0.2,
        speed_mode="all_checks",
    ),
    lane_change_params=SumoLaneChangeParams(
        lc_assertive=20,
        lc_pushy=0.8,
        lc_speed_gain=4.0,
        model="LC2013",
        lane_change_mode="no_lc_safe",
        # lcKeepRight=0.8
    ),
    num_vehicles=1400)

inflow = InFlows()

if USE_INFLOWS:
    # south
Exemplo n.º 3
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=31, ),
                 lane_change_params=SumoLaneChangeParams(
                     lane_change_mode=00, ),
                 num_vehicles=1)
    num_observed_segments = [("1", 1), ("2", 3), ("3", 3), ("4", 3), ("5", 1)]
    controlled_segments = [("1", 1, False), ("2", 2, True), ("3", 2, True),
                           ("4", 2, True), ("5", 1, False)]
    additional_env_params = {
        "target_velocity": 40,
        "disable_tb": True,
        "disable_ramp_metering": True,
        "symmetric": True,
        "observed_segments": num_observed_segments,
        "controlled_segments": controlled_segments,
        "reset_inflow": False,
        "lane_change_duration": 5,
        "max_accel": 2,
        "max_decel": 2,
        "inflow_range": [1000, 2000]
    }
    env_params = EnvParams(horizon=horizon,
                           additional_params=additional_env_params)

    inflow = InFlows()
    inflow.add(veh_type="human",
               edge="1",
               vehsPerHour=flow_rate,
               departLane="random",
               departSpeed=10)

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

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

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

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

    env = DesiredVelocityEnv(env_params, sim_params, scenario)

    return BottleneckDensityExperiment(env)
Exemplo n.º 4
0
from flow.core.params import VehicleParams
from flow.controllers import IDMController, ContinuousRouter, RLController
from flow.networks.figure_eight import ADDITIONAL_NET_PARAMS

# time horizon of a single rollout
HORIZON = 1500

# We place 1 autonomous vehicle and 13 human-driven vehicles in the network
vehicles = VehicleParams()
vehicles.add(veh_id="human",
             acceleration_controller=(IDMController, {
                 "noise": 0.2
             }),
             routing_controller=(ContinuousRouter, {}),
             car_following_params=SumoCarFollowingParams(
                 speed_mode="obey_safe_speed",
                 decel=1.5,
             ),
             num_vehicles=13)
vehicles.add(veh_id="rl",
             acceleration_controller=(RLController, {}),
             routing_controller=(ContinuousRouter, {}),
             car_following_params=SumoCarFollowingParams(
                 speed_mode="obey_safe_speed", ),
             num_vehicles=1)

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

    # name of the flow environment the experiment is running on
    env_name=AccelEnv,
Exemplo n.º 5
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)

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',

    # name of the scenario class the experiment is running on
    scenario='SimpleGridScenario',
Exemplo n.º 6
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 = VehicleParams()
        vehicles.add(
            veh_id="human",
            acceleration_controller=(IDMController, {}),
            car_following_params=SumoCarFollowingParams(
                speed_mode="obey_safe_speed", ),
            # for testing coverage purposes, we add a routing controller
            routing_controller=(ContinuousRouter, {}),
            num_vehicles=5)
        vehicles.add(veh_id="rl",
                     acceleration_controller=(RLController, {}),
                     car_following_params=SumoCarFollowingParams(
                         speed_mode="obey_safe_speed", ),
                     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",
            sim=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,
                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["sim"].__dict__ ==
                        flow_params["sim"].__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__))
# Daniel: adding vehicles and flow from osm.passenger.trips.xml
vehicles = VehicleParams()
vehicles.add(
    veh_id="human",
    acceleration_controller=(
        SimCarFollowingController,
        {
            #"noise": 0.2
        }),
    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=9,  #"all_checks", #"all_checks", #no_collide",
        #decel=7.5,  # avoid collisions at emergency stops
        # desired time-gap from leader
        #tau=2, #7,
        #min_gap=2.5,
        #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,
        # the following two replace default values which are not read well by xml parser
Exemplo n.º 8
0
    'max_decel': 1,
    'target_velocity': 30
})


# CREATE VEHICLE TYPES AND INFLOWS

vehicles = VehicleParams()
inflows = InFlows()

# human vehicles
vehicles.add(
    veh_id="human",
    acceleration_controller=(SimCarFollowingController, {}),
    car_following_params=SumoCarFollowingParams(
        speed_mode=9,  # for safer behavior at the merges
        #tau=1.5  # larger distance between cars
    ),
    #lane_change_params=SumoLaneChangeParams(lane_change_mode=1621)
    num_vehicles=5)

# autonomous vehicles
vehicles.add(
    veh_id="rl",
    acceleration_controller=(RLController, {}),
    car_following_params=SumoCarFollowingParams(
        speed_mode=9,
    ),
    num_vehicles=0)

# Vehicles are introduced from both sides of merge, with RL vehicles entering
# from the highway portion as well
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:
        vehicles_per_edge = 5
        num_edges = 2 * (row_num + col_num)
        total_vehicles = num_edges * vehicles_per_edge
        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
        num_entries = 2 * row_num + 2 * col_num
        assert total_vehicles % num_entries == 0, "{} total vehicles should " \
                                                  "be divisible by {" \
                                                  "}".format(total_vehicles,
                                                             num_entries)
        grid_array = {
            "short_length": 100,
            "inner_length": 300,
            "long_length": 3000,
            "row_num": row_num,
            "col_num": col_num,
            "cars_left": int(total_vehicles / num_entries),
            "cars_right": int(total_vehicles / num_entries),
            "cars_top": int(total_vehicles / num_entries),
            "cars_bot": int(total_vehicles / num_entries)
        }

        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
Exemplo n.º 10
0
def merge_example(render=None):
    """
    Perform a simulation of vehicles on a merge.

    Parameters
    ----------
    render: bool, optional
        specifies whether to use the gui during execution

    Returns
    -------
    exp: flow.core.experiment.Experiment
        A non-rl experiment demonstrating the performance of human-driven
        vehicles on a merge.
    """
    sim_params = SumoParams(render=True,
                            emission_path="./data/",
                            sim_step=0.2,
                            restart_instance=False)

    if render is not None:
        sim_params.render = render

    vehicles = VehicleParams()
    vehicles.add(veh_id="human",
                 acceleration_controller=(IDMController, {
                     "noise": 0.2
                 }),
                 car_following_params=SumoCarFollowingParams(
                     speed_mode="obey_safe_speed", ),
                 num_vehicles=5)

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

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

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

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

    network = MergeNetwork(name="merge-baseline",
                           vehicles=vehicles,
                           net_params=net_params,
                           initial_config=initial_config)

    env = MergePOEnv(env_params, sim_params, network)

    return Experiment(env)
Exemplo n.º 11
0
# 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 = VehicleParams()
vehicles.add(veh_id="human",
             acceleration_controller=(SimCarFollowingController, {}),
             car_following_params=SumoCarFollowingParams(
                 min_gap=2.5,
                 max_speed=V_ENTER,
                 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()
Exemplo n.º 12
0
    def reset(self):
        """Reset the environment with a new inflow rate.

        The diverse set of inflows are used to generate a policy that is more
        robust with respect to the inflow rate. The inflow rate is update by
        creating a new network similar to the previous one, but with a new
        Inflow object with a rate within the additional environment parameter
        "inflow_range", which is a list consisting of the smallest and largest
        allowable inflow rates.

        **WARNING**: The inflows assume there are vehicles of type
        "followerstopper" and "human" within the VehicleParams object.
        """
        add_params = self.env_params.additional_params
        if add_params.get("reset_inflow"):
            inflow_range = add_params.get("inflow_range")
            flow_rate = np.random.uniform(min(inflow_range),
                                          max(inflow_range)) * self.scaling

            # We try this for 100 trials in case unexpected errors during
            # instantiation.
            for _ in range(100):
                try:
                    # introduce new inflows within the pre-defined inflow range
                    inflow = InFlows()
                    inflow.add(
                        veh_type="followerstopper",  # FIXME: make generic
                        edge="1",
                        vehs_per_hour=flow_rate * .1,
                        departLane="random",
                        departSpeed=10)
                    inflow.add(veh_type="human",
                               edge="1",
                               vehs_per_hour=flow_rate * .9,
                               departLane="random",
                               departSpeed=10)

                    # all other network parameters should match the previous
                    # environment (we only want to change the inflow)
                    additional_net_params = {
                        "scaling":
                        self.scaling,
                        "speed_limit":
                        self.net_params.additional_params['speed_limit']
                    }
                    net_params = NetParams(
                        inflows=inflow,
                        additional_params=additional_net_params)

                    vehicles = VehicleParams()
                    vehicles.add(
                        veh_id="human",  # FIXME: make generic
                        car_following_params=SumoCarFollowingParams(
                            speed_mode=9, ),
                        lane_change_controller=(SimLaneChangeController, {}),
                        routing_controller=(ContinuousRouter, {}),
                        lane_change_params=SumoLaneChangeParams(
                            lane_change_mode=0,  # 1621,#0b100000101,
                        ),
                        num_vehicles=1 * self.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 * self.scaling)

                    # recreate the network object
                    self.network = self.network.__class__(
                        name=self.network.orig_name,
                        vehicles=vehicles,
                        net_params=net_params,
                        initial_config=self.initial_config,
                        traffic_lights=self.network.traffic_lights)
                    observation = super().reset()

                    # reset the timer to zero
                    self.time_counter = 0

                    return observation

                except Exception as e:
                    print('error on reset ', e)

        # perform the generic reset function
        observation = super().reset()

        # reset the timer to zero
        self.time_counter = 0

        return observation
Exemplo n.º 13
0
def para_produce_rl(HORIZON = 3000,NUM_AUTOMATED = 7):
	
    # time horizon of a single rollout
    HORIZON = 1500
    # number of rollouts per training iteration
    N_ROLLOUTS = 20
    # number of parallel workers
    N_CPUS = 2
    # number of automated vehicles. Must be less than or equal to 22.
    NUM_AUTOMATED = NUM_AUTOMATED
    assert NUM_AUTOMATED in [1, 2, 7, 14], \
    "num_automated must be one of [1, 2, 7 14]"
    # desired velocity for all vehicles in the network, in m/s
    TARGET_VELOCITY = 20
    # maximum acceleration for autonomous vehicles, in m/s^2
    MAX_ACCEL = 3
    # maximum deceleration for autonomous vehicles, in m/s^2
    MAX_DECEL = 3

    # We evenly distribute the automated vehicles in the network.
    num_human = 14 - NUM_AUTOMATED
    human_per_automated = int(num_human / NUM_AUTOMATED)

    vehicles = VehicleParams()
    for i in range(NUM_AUTOMATED):
        # Add one automated vehicle.
        vehicles.add(
            veh_id="rl_{}".format(i),
            acceleration_controller=(RLController, {}),
            routing_controller=(ContinuousRouter, {}),
            car_following_params=SumoCarFollowingParams(
                speed_mode="obey_safe_speed",
                accel=MAX_ACCEL,
                decel=MAX_DECEL,
            ),
            num_vehicles=1)

        # Add a fraction of the human driven vehicles.
        vehicles.add(
            veh_id="human_{}".format(i),
            acceleration_controller=(IDMController, {
                "noise": 0.2
            }),
            car_following_params=SumoCarFollowingParams(
                speed_mode="obey_safe_speed",
                decel=1.5
            ),
            routing_controller=(ContinuousRouter, {}),
            num_vehicles=human_per_automated)


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

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

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

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

        # sumo-related parameters (see flow.core.params.SumoParams)
        sim=SumoParams(
            sim_step=0.1,
            render=False,
            restart_instance=False
        ),

        # environment related parameters (see flow.core.params.EnvParams)
        env=EnvParams(
            horizon=HORIZON,
            additional_params={
                'target_velocity': TARGET_VELOCITY,
                'max_accel': MAX_ACCEL,
                'max_decel': MAX_DECEL,
                'sort_vehicles':False
            },
        ),

        # 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.copy(),
        ),

        # 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())

    flow_params['env'].horizon = HORIZON
    return flow_params
Exemplo n.º 14
0
def para_produce_rl(HORIZON=3000):

    vehicles = VehicleParams()
    vehicles.add(veh_id="human",
                 acceleration_controller=(IDMController, {
                     "noise": 0.2
                 }),
                 car_following_params=SumoCarFollowingParams(min_gap=0),
                 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
        exp_tag="stabilizing_the_ring",

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

        # 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(sim_step=0.1, render=False, restart_instance=False),

        # environment related parameters (see flow.core.params.EnvParams)
        env=EnvParams(
            horizon=HORIZON,
            warmup_steps=750,
            clip_actions=False,
            additional_params={
                "max_accel": 1,
                "max_decel": 1,
                "ring_length": [220, 270],
            },
        ),

        # 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,
        }, ),

        # 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())
    flow_params['env'].horizon = HORIZON
    return flow_params
Exemplo n.º 15
0
Arquivo: base.py Projeto: GAIPS/ILU-RL
    def __init__(self,
                 network_id,
                 horizon=360,
                 net_params=None,
                 vehicles=None,
                 demand_type='constant',
                 demand_mode='step',
                 initial_config=None,
                 tls_type='rl'):

        """Builds a new network from inflows -- the resulting
        vehicle trips will be stochastic use it for training"""
        self.network_id = network_id

        baseline = (tls_type == 'actuated')
        self.cycle_time, self.programs = get_tls_custom(
                                network_id, baseline=baseline)

        if initial_config is None:
            initial_config = InitialConfig(
                edges_distribution=tuple(get_routes(network_id).keys())
            )

        if net_params is None:
            #TODO: check vtype
            if vehicles is None:
                vehicles = VehicleParams()
                vehicles.add(
                    veh_id="human",
                    car_following_params=SumoCarFollowingParams(
                        min_gap=2.5,
                        decel=7.5,  # Avoid collisions at emergency stops.
                    ),
                    lane_change_params=SumoLaneChangeParams(
                        lane_change_mode='strategic'
                    )
                )

            inflows = InFlows(network_id, horizon, demand_type,
                              demand_mode=demand_mode, initial_config=initial_config)

            net_params = NetParams(inflows,
                                   template=get_path(network_id, 'net'))

        # static program (required for rl)
        tls_logic = TrafficLightParams(baseline=False)
        if tls_type not in ('actuated', ):
            programs = get_logic(network_id)
            if programs:
                for prog in programs:
                    node_id = prog.pop('id')
                    prog['tls_type'] = prog.pop('type')
                    prog['programID'] = int(prog.pop('programID')) + 1
                    tls_logic.add(node_id, **prog)
        else:
            for tls_id, tls_args in self.programs.items():
                tls_logic.add(tls_id, **tls_args)

        super(Network, self).__init__(
                 network_id,
                 vehicles,
                 net_params,
                 initial_config=initial_config,
                 traffic_lights=tls_logic
        )

        self.nodes = self.specify_nodes(net_params)
        self.edges = self.specify_edges(net_params)
        self.connections = self.specify_connections(net_params)
        self.types = self.specify_types(net_params)
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
Exemplo n.º 17
0
def grid0_baseline(num_runs, sumo_binary="sumo-gui"):
    """Run script for the grid0 baseline.

    Parameters
    ----------
        num_runs : int
            number of rollouts the performance of the environment is evaluated
            over
        sumo_binary: str, 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": "8",
        "maxDur": "45",
        "state": "GGGrrrGGGrrr"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "state": "yyyrrryyyrrr"
    }, {
        "duration": "31",
        "minDur": "8",
        "maxDur": "45",
        "state": "rrrGGGrrrGGG"
    }, {
        "duration": "6",
        "minDur": "3",
        "maxDur": "6",
        "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(
        in_flows=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,
        sumo_binary=sumo_binary,
    )

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

    initial_config = InitialConfig(shuffle=True)

    scenario = SimpleGridScenario(name="grid",
                                  generator_class=SimpleGridGenerator,
                                  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
Exemplo n.º 18
0
# inflow rate at the highway
FLOW_RATE = 2000
# 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",
           edge="inflow_highway",
           vehs_per_hour=(1 - RL_PENETRATION) * FLOW_RATE,
           departLane="free",
           departSpeed=10)
Exemplo n.º 19
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 = VehicleParams()
        vehicles.add(veh_id="human",
                     acceleration_controller=(IDMController, {
                         "noise": 0.2
                     }),
                     routing_controller=(ContinuousRouter, {}),
                     car_following_params=SumoCarFollowingParams(
                         speed_mode="obey_safe_speed", ),
                     num_vehicles=13)
        vehicles.add(veh_id="rl",
                     acceleration_controller=(RLController, {}),
                     routing_controller=(ContinuousRouter, {}),
                     car_following_params=SumoCarFollowingParams(
                         speed_mode="obey_safe_speed", ),
                     num_vehicles=1)

        flow_params = dict(
            exp_tag="figure_eight_0",
            env_name="AccelEnv",
            scenario="Figure8Scenario",
            simulator='traci',
            sim=SumoParams(
                sim_step=0.1,
                render=False,
            ),
            env=EnvParams(
                horizon=1500,
                additional_params={
                    "target_velocity": 20,
                    "max_accel": 3,
                    "max_decel": 3,
                    "sort_vehicles": False
                },
            ),
            net=NetParams(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 sim_params to change, and
        # that this feature is in fact needed to avoid race conditions
        flow_params["sim"].port = env.sim_params.port

        # check that each of the parameter match
        self.assertEqual(env.env_params.__dict__, flow_params["env"].__dict__)
        self.assertEqual(env.sim_params.__dict__, flow_params["sim"].__dict__)
        self.assertEqual(env.scenario.traffic_lights.__dict__,
                         flow_params["tls"].__dict__)
        self.assertEqual(env.net_params.__dict__, flow_params["net"].__dict__)
        self.assertEqual(env.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"])
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 = 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',
Exemplo n.º 21
0
    def add(self,
            veh_id,
            acceleration_controller=(SumoCarFollowingController, {}),
            lane_change_controller=(SumoLaneChangeController, {}),
            routing_controller=None,
            initial_speed=0,
            num_vehicles=1,
            speed_mode='all_checks',
            lane_change_mode="no_lat_collide",
            sumo_car_following_params=None,
            sumo_lc_params=None):
        """Adds a sequence of vehicles to the list of vehicles in the network.

        Parameters
        ----------
        veh_id: str
            base vehicle ID for the vehicles (will be appended by a number)
        acceleration_controller: tup, optional
            1st element: flow-specified acceleration controller
            2nd element: controller parameters (may be set to None to maintain
            default parameters)
        lane_change_controller: tup, optional
            1st element: flow-specified lane-changer controller
            2nd element: controller parameters (may be set to None to maintain
            default parameters)
        routing_controller: tup, optional
            1st element: flow-specified routing controller
            2nd element: controller parameters (may be set to None to maintain
            default parameters)
        initial_speed: float, optional
            initial speed of the vehicles being added (in m/s)
        num_vehicles: int, optional
            number of vehicles of this type to be added to the network
        speed_mode: str or int, optional
            may be one of the following:
             - "no_collide" (default): Human and RL cars are preventing from
               reaching speeds that may cause crashes (also serves as a
               failsafe).
             - "aggressive": Human and RL cars are not limited by sumo with
               regard to their accelerations, and can crash longitudinally
             - "all_checks": all sumo safety checks are activated
             - "custom_model": respect safe speed, right of way and
               brake hard at red lights if needed. DOES NOT respect
               max accel and decel which enables emergency stopping.
               Necessary to prevent custom models from crashing
             - int values may be used to define custom speed mode for the given
               vehicles, specified at:
               http://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#speed_mode_.280xb3.29
        lane_change_mode: str or int, optional
            may be one of the following:
            - "strategic": Human cars make lane changes in accordance with SUMO
              to provide speed boosts
            - "no_lat_collide": Human cars will not make lane changes, RL cars
              can lane change into any space, no matter how likely it is to
              crash (default)
            - "aggressive": RL cars are not limited by sumo with regard to
              their lane-change actions, and can crash longitudinally
            - int values may be used to define custom lane change modes for the
              given vehicles, specified at:
              http://sumo.dlr.de/wiki/TraCI/Change_Vehicle_State#lane_change_mode_.280xb6.29
        sumo_car_following_params: flow.core.params.SumoCarFollowingParams type
            Params object specifying attributes for Sumo car following model.
        sumo_lc_params: flow.core.params.SumoLaneChangeParams type
            Params object specifying attributes for Sumo lane changing model.
        """
        if sumo_car_following_params is None:
            sumo_car_following_params = SumoCarFollowingParams()

        if sumo_lc_params is None:
            sumo_lc_params = SumoLaneChangeParams()

        type_params = {}
        type_params.update(sumo_car_following_params.controller_params)
        type_params.update(sumo_lc_params.controller_params)

        # If a vehicle is not sumo or RL, let the minGap be zero so that it
        # does not tamper with the dynamics of the controller
        if acceleration_controller[0] != SumoCarFollowingController \
                and acceleration_controller[0] != RLController:
            type_params["minGap"] = 0.0

        # adjust the speed mode value
        if isinstance(speed_mode, str) and speed_mode in SPEED_MODES:
            speed_mode = SPEED_MODES[speed_mode]
        elif not (isinstance(speed_mode, int)
                  or isinstance(speed_mode, float)):
            logging.error("Setting speed mode of {0} to "
                          "default.".format(veh_id))
            speed_mode = SPEED_MODES["no_collide"]

        # adjust the lane change mode value
        if isinstance(lane_change_mode, str) and lane_change_mode in LC_MODES:
            lane_change_mode = LC_MODES[lane_change_mode]
        elif not (isinstance(lane_change_mode, int)
                  or isinstance(lane_change_mode, float)):
            logging.error("Setting lane change mode of {0} to "
                          "default.".format(veh_id))
            lane_change_mode = LC_MODES["no_lat_collide"]

        # this dict will be used when trying to introduce new vehicles into
        # the network via a flow
        self.type_parameters[veh_id] = \
            {"acceleration_controller": acceleration_controller,
             "lane_change_controller": lane_change_controller,
             "routing_controller": routing_controller,
             "initial_speed": initial_speed,
             "speed_mode": speed_mode,
             "lane_change_mode": lane_change_mode,
             "sumo_car_following_params": sumo_car_following_params,
             "sumo_lc_params": sumo_lc_params}

        self.initial.append({
            "veh_id": veh_id,
            "acceleration_controller": acceleration_controller,
            "lane_change_controller": lane_change_controller,
            "routing_controller": routing_controller,
            "initial_speed": initial_speed,
            "num_vehicles": num_vehicles,
            "speed_mode": speed_mode,
            "lane_change_mode": lane_change_mode,
            "sumo_car_following_params": sumo_car_following_params,
            "sumo_lc_params": sumo_lc_params})

        # this is used to return the actual headways from the vehicles class
        self.minGap[veh_id] = type_params["minGap"]

        for i in range(num_vehicles):
            v_id = veh_id + '_%d' % i

            # add the vehicle to the list of vehicle ids
            self.__ids.append(v_id)

            self.__vehicles[v_id] = dict()

            # specify the type
            self.__vehicles[v_id]["type"] = veh_id

            # specify the acceleration controller class
            self.__vehicles[v_id]["acc_controller"] = \
                acceleration_controller[0](
                    v_id,
                    sumo_cf_params=sumo_car_following_params,
                    **acceleration_controller[1])

            # specify the lane-changing controller class
            self.__vehicles[v_id]["lane_changer"] = \
                lane_change_controller[0](veh_id=v_id,
                                          **lane_change_controller[1])

            # specify the routing controller class
            if routing_controller is not None:
                self.__vehicles[v_id]["router"] = \
                    routing_controller[0](veh_id=v_id,
                                          router_params=routing_controller[1])
            else:
                self.__vehicles[v_id]["router"] = None

            # specify the speed of vehicles at the start of a rollout
            self.__vehicles[v_id]["initial_speed"] = initial_speed

            # check if the vehicle is human-driven or autonomous
            if acceleration_controller[0] == RLController:
                self.__rl_ids.append(v_id)
            else:
                self.__human_ids.append(v_id)

                # check if the vehicle's lane-changing / acceleration actions
                # are controlled by sumo or not.
                if acceleration_controller[0] != SumoCarFollowingController:
                    self.__controlled_ids.append(v_id)
                if lane_change_controller[0] != SumoLaneChangeController:
                    self.__controlled_lc_ids.append(v_id)

            # specify the speed and lane change mode for the vehicle
            self.__vehicles[v_id]["speed_mode"] = speed_mode
            self.__vehicles[v_id]["lane_change_mode"] = lane_change_mode

        # update the variables for the number of vehicles in the network
        self.num_vehicles = len(self.__ids)
        self.num_rl_vehicles = len(self.__rl_ids)

        # increase the number of unique types of vehicles in the network, and
        # add the type to the list of types
        self.num_types += 1
        self.types.append((veh_id, type_params))
Exemplo n.º 22
0
def bottleneck2_baseline(num_runs, render=True):
    """Run script for the bottleneck2 baseline.

    Parameters
    ----------
        num_runs : int
            number of rollouts the performance of the environment is evaluated
            over
        render : bool, 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=0, ),
                 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 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, sim_params, scenario)

    exp = Experiment(env)

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

    return np.mean(results['returns']), np.std(results['returns'])
Exemplo n.º 23
0
NUM_MERGE_HUMANS = 9
NUM_MERGE_RL = 1

# 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, {
Exemplo n.º 24
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="no_collide", ),
                 num_vehicles=1)
    vehicles.add(veh_id="idm",
                 acceleration_controller=(IDMController, {
                     "noise": 0.2
                 }),
                 routing_controller=(ContinuousRouter, {}),
                 car_following_params=SumoCarFollowingParams(
                     speed_mode="no_collide", ),
                 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(),
Exemplo n.º 25
0
RL_PENETRATION = 0.1
# num_rl term (see ADDITIONAL_ENV_PARAMs)
NUM_RL = 5

# 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 = VehicleParams()
vehicles.add(veh_id="human",
             acceleration_controller=(SimCarFollowingController, {}),
             car_following_params=SumoCarFollowingParams(speed_mode=9, ),
             num_vehicles=5)
vehicles.add(veh_id="rl",
             acceleration_controller=(RLController, {}),
             car_following_params=SumoCarFollowingParams(speed_mode=9, ),
             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,
           depart_lane="free",
           depart_speed=10)
inflow.add(veh_type="rl",
Exemplo n.º 26
0
# vehicles dynamics models
# time horizon of a single rollout
HORIZON = 2000
# number of rollouts per training iteration
N_ROLLOUTS = 5
# number of parallel workers
N_CPUS = 2

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

from flow.core.params import SumoParams

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

# EnvParams specifies environment and experiment-specific parameters that either affect the training process or the dynamics of various components within the network. For the environment WaveAttenuationPOEnv, these parameters are used to dictate bounds on the accelerations of the autonomous vehicles, as well as the range of ring lengths (and accordingly network densities) the agent is trained on.

#Finally, it is important to specify here the horizon of the experiment, which is the duration of one episode (during which the RL-agent acquire data).
Exemplo n.º 27
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:
Exemplo n.º 28
0
    def reset(self):
        add_params = self.env_params.additional_params
        if add_params.get("reset_inflow"):
            inflow_range = add_params.get("inflow_range")
            flow_rate = np.random.uniform(
                min(inflow_range), max(inflow_range)) * self.scaling
            for _ in range(100):
                try:
                    inflow = InFlows()
                    inflow.add(
                        veh_type="followerstopper",
                        edge="1",
                        vehs_per_hour=flow_rate * .1,
                        departLane="random",
                        departSpeed=10)
                    inflow.add(
                        veh_type="human",
                        edge="1",
                        vehs_per_hour=flow_rate * .9,
                        departLane="random",
                        departSpeed=10)

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

                    vehicles = Vehicles()
                    vehicles.add(
                        veh_id="human",
                        sumo_car_following_params=SumoCarFollowingParams(
                            speed_mode=9,
                        ),
                        lane_change_controller=(SumoLaneChangeController, {}),
                        routing_controller=(ContinuousRouter, {}),
                        sumo_lc_params=SumoLaneChangeParams(
                            lane_change_mode=0,  # 1621,#0b100000101,
                        ),
                        num_vehicles=1 * self.scaling)
                    vehicles.add(
                        veh_id="followerstopper",
                        acceleration_controller=(RLController, {}),
                        lane_change_controller=(SumoLaneChangeController, {}),
                        routing_controller=(ContinuousRouter, {}),
                        sumo_car_following_params=SumoCarFollowingParams(
                            speed_mode=9,
                        ),
                        sumo_lc_params=SumoLaneChangeParams(
                            lane_change_mode=0,
                        ),
                        num_vehicles=1 * self.scaling)
                    self.vehicles = vehicles

                    # delete the cfg and net files
                    net_path = self.scenario.net_path
                    net_name = net_path + self.scenario.name
                    cfg_path = self.scenario.cfg_path
                    cfg_name = cfg_path + self.scenario.name
                    for f in glob.glob(net_name + '*'):
                        os.remove(f)
                    for f in glob.glob(cfg_name + '*'):
                        os.remove(f)

                    self.scenario = self.scenario.__class__(
                        name=self.scenario.orig_name,
                        vehicles=vehicles,
                        net_params=net_params,
                        initial_config=self.scenario.initial_config,
                        traffic_lights=self.scenario.traffic_lights)
                    observation = super().reset()

                    # reset the timer to zero
                    self.time_counter = 0

                    return observation

                except Exception as e:
                    print('error on reset ', e)
                    # perform the generic reset function

        observation = super().reset()

        # reset the timer to zero
        self.time_counter = 0

        return observation
Exemplo n.º 29
0
# 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()
for edge in outer_edges:
Exemplo n.º 30
0
# time horizon of a single rollout
HORIZON = 1000
# number of rollouts per training iteration
N_ROLLOUTS = 20
# number of parallel workers
N_CPUS = 2

vehicles = VehicleParams()
vehicles.add(
    "rl",
    acceleration_controller=(IDMController, {}),
    lane_change_controller=(SimLaneChangeController, {}),
    #routing_controller=(ContinuousRouter, {}),
    car_following_params=SumoCarFollowingParams(
        speed_mode="obey_safe_speed",
        # we use the speed mode "obey_safe_speed" for better dynamics at the merge
    ),
    num_vehicles=0)
vehicles.add(
    "human",
    acceleration_controller=(IDMController, {}),
    lane_change_controller=(SimLaneChangeController, {}),
    #routing_controller=(ContinuousRouter, {}),
    car_following_params=SumoCarFollowingParams(
        speed_mode="obey_safe_speed",
        # we use the speed mode "obey_safe_speed" for better dynamics at the merge
    ),
    lane_change_params=SumoLaneChangeParams(lane_change_mode="strategic",
                                            lcpushy=1.0),
    num_vehicles=0)