Esempio n. 1
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
Esempio n. 2
0
def bottleneck1_baseline(num_runs, sumo_binary="sumo-gui"):
    """Run script for the bottleneck1 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
    """
    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(in_flows=inflow,
                           no_internal_links=False,
                           additional_params=additional_net_params)

    sumo_params = SumoParams(
        sim_step=0.5,
        sumo_binary=sumo_binary,
        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",
                                  generator_class=BottleneckGenerator,
                                  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)
    avg_outflow = np.mean(
        [outflow[-1] for outflow in results["per_step_returns"]])

    return avg_outflow
Esempio n. 3
0
def evaluate_policy(benchmark, _get_actions, _get_states=None):
    """Evaluate the performance of a controller on a predefined benchmark.

    Parameters
    ----------
        benchmark : str
            name of the benchmark, must be printed as it is in the
            benchmarks folder; otherwise a ValueError will be raised
        _get_actions : method
            the mapping from states to actions for the RL agent(s)
        _get_states : method, optional
            a mapping from the environment object in Flow to some state, which
            overrides the _get_states method of the environment. Note that the
            same cannot be done for the actions.

    Returns
    -------
        float
            mean of the evaluation return of the benchmark from NUM_RUNS number
            of simulations
        float
            standard deviation of the evaluation return of the benchmark from
            NUM_RUNS number of simulations

    Raises
    ------
        ValueError
            If the specified benchmark is not available.
    """
    if benchmark not in AVAILABLE_BENCHMARKS.keys():
        raise ValueError(
            "benchmark {} is not available. Check spelling?".format(benchmark))

    # get the flow params from the benchmark
    flow_params = AVAILABLE_BENCHMARKS[benchmark]

    exp_tag = flow_params["exp_tag"]
    sumo_params = flow_params["sumo"]
    vehicles = flow_params["veh"]
    env_params = flow_params["env"]
    env_params.evaluate = True  # Set to true to get evaluation returns
    net_params = flow_params["net"]
    initial_config = flow_params.get("initial", InitialConfig())
    traffic_lights = flow_params.get("tls", TrafficLights())

    # import the environment, scenario, and generator classes
    module = __import__("flow.envs", fromlist=[flow_params["env_name"]])
    env_class = getattr(module, flow_params["env_name"])
    module = __import__("flow.scenarios", fromlist=[flow_params["scenario"]])
    scenario_class = getattr(module, flow_params["scenario"])
    module = __import__("flow.scenarios", fromlist=[flow_params["generator"]])
    generator_class = getattr(module, flow_params["generator"])

    # recreate the scenario and environment
    scenario = scenario_class(name=exp_tag,
                              generator_class=generator_class,
                              vehicles=vehicles,
                              net_params=net_params,
                              initial_config=initial_config,
                              traffic_lights=traffic_lights)

    # make sure the _get_states method of the environment is the one
    # specified by the user
    if _get_states is not None:

        class _env_class(env_class):
            def get_state(self):
                return _get_states(self)

        env_class = _env_class

    env = env_class(env_params=env_params,
                    sumo_params=sumo_params,
                    scenario=scenario)

    # create a SumoExperiment object with the "rl_actions" method as
    # described in the inputs. Note that the state may not be that which is
    # specified by the environment.
    exp = SumoExperiment(env=env, scenario=scenario)

    # run the experiment and return the reward
    res = exp.run(num_runs=NUM_RUNS,
                  num_steps=env.env_params.horizon,
                  rl_actions=_get_actions)

    return np.mean(res["returns"]), np.std(res["returns"])
Esempio n. 4
0
def merge_example(render=None):
    """
    Perform a simulation of vehicles on a 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 merge.
    """
    sumo_params = SumoParams(
        render=True,
        emission_path="./data/",
        sim_step=0.2,
        restart_instance=False,
        lateral_resolution=0.2,
    )

    if render is not None:
        sumo_params.render = render

    vehicles = Vehicles()
    vehicles.add(
        veh_id="1",
        acceleration_controller=(
            IDMController,
            {
                #    "noise": 0.2
            }),
        routing_controller=(WeaveRouter, {}),
        speed_mode="all_checks",
        lane_change_mode="strategic",
        num_vehicles=5)
    vehicles.add(
        veh_id="2",
        acceleration_controller=(
            IDMController,
            {
                #    "noise": 0.2
            }),
        routing_controller=(WeaveRouter, {}),
        speed_mode="all_checks",
        lane_change_mode="strategic",
        num_vehicles=5)
    vehicles.add(
        veh_id="3",
        acceleration_controller=(
            IDMController,
            {
                #    "noise": 0.2
            }),
        routing_controller=(WeaveRouter, {}),
        speed_mode="all_checks",
        lane_change_mode="strategic",
        num_vehicles=5)
    vehicles.add(
        veh_id="4",
        acceleration_controller=(
            IDMController,
            {
                #    "noise": 0.2
            }),
        routing_controller=(WeaveRouter, {}),
        speed_mode="all_checks",
        lane_change_mode="strategic",
        num_vehicles=5)

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

    inflow = InFlows()
    inflow.add(
        veh_type="1",
        edge="inflow_highway",
        vehs_per_hour=FLOW_RATE * 0.6,  # TODO: change
        departLane="free",
        departSpeed=10)
    inflow.add(
        veh_type="2",
        edge="inflow_highway",
        vehs_per_hour=FLOW_RATE * 0.4,  # TODO: change
        departLane="free",
        departSpeed=10)
    inflow.add(
        veh_type="3",
        edge="inflow_merge",
        vehs_per_hour=FLOW_RATE * 0.3,  # TODO: change
        departLane="free",
        departSpeed=7.5)
    inflow.add(
        veh_type="4",
        edge="inflow_merge",
        vehs_per_hour=FLOW_RATE * 0.1,  # TODO: change
        departLane="free",
        departSpeed=7.5)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    additional_net_params["merge_lanes"] = 1
    additional_net_params["diverge_lanes"] = 1
    additional_net_params["highway_lanes"] = 2
    additional_net_params["pre_merge_length"] = 300
    additional_net_params["post_merge_length"] = 200
    additional_net_params["post_diverge_length"] = 400
    additional_net_params["merge_length"] = 100
    additional_net_params["diverge_length"] = 200
    net_params = NetParams(inflows=inflow,
                           no_internal_links=False,
                           additional_params=additional_net_params)

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

    scenario = EntryExitScenario(name="merge-baseline",
                                 vehicles=vehicles,
                                 net_params=net_params,
                                 initial_config=initial_config)

    env = WaveAttenuationMergePOEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
Esempio n. 5
0
    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",
                              generator_class=BottleneckGenerator,
                              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)

num_runs = 2
results = exp.run(num_runs, HORIZON)
avg_outflow = np.mean([outflow[-1] for outflow in results["per_step_returns"]])
print('The average outflow over 500 seconds '
      'across {} runs is {}'.format(num_runs, avg_outflow))
Esempio n. 6
0
    def setUp(self):
        # create the environment and scenario classes for a ring road
        env, scenario = ring_road_exp_setup()

        # instantiate an experiment class
        self.exp = SumoExperiment(env, scenario)
Esempio n. 7
0
    def setUp(self):
        # create the environment and scenario classes for a ring road
        self.env, self.scenario = grid_mxn_exp_setup()

        # instantiate an experiment class
        self.exp = SumoExperiment(self.env, self.scenario)
Esempio n. 8
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)
Esempio n. 9
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
Esempio n. 10
0
def bottleneck_example(flow_rate, horizon, render=None):
    """
    Perform a simulation of vehicles on a bottleneck.

    Parameters
    ----------
    flow_rate : float
        total inflow rate of vehicles into the bottlneck
    horizon : int
        time horizon
    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 bottleneck.
    """
    if render is None:
        render = False

    sumo_params = SumoParams(sim_step=0.5,
                             render=render,
                             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(inflows=inflow,
                           no_internal_links=False,
                           additional_params=additional_net_params)

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

    scenario = BottleneckScenario(name="bay_bridge_toll",
                                  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)
Esempio n. 11
0
from flow.envs.loop_accel import SimpleAccelerationEnvironment
from flow.scenarios.loop.loop_scenario import LoopScenario

logging.basicConfig(level=logging.INFO)

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

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

env_params = EnvParams()

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("single-lane-one-contr", CircleGenerator, vehicles, net_params,
                        initial_config)

env = SimpleAccelerationEnvironment(env_params, sumo_params, scenario)

exp = SumoExperiment(env, scenario)

logging.info("Experiment Set Up complete")

exp.run(2, 1000)

exp.env.terminate()
Esempio n. 12
0
def bottleneck0_baseline(num_runs, render=True):
    """Run script for the bottleneck0 baseline.

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

    Returns
    -------
        SumoExperiment
            class needed to run simulations
    """
    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())

    # we want no autonomous vehicles in the simulation
    vehicles = Vehicles()
    vehicles.add(veh_id='human',
                 speed_mode=9,
                 routing_controller=(ContinuousRouter, {}),
                 lane_change_mode=0,
                 num_vehicles=1 * SCALING)

    # only include human vehicles in inflows
    flow_rate = 1900 * 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
    sumo_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, sumo_params, scenario)

    exp = SumoExperiment(env, scenario)

    results = exp.run(num_runs, env_params.horizon)
    return np.mean(results['returns']), np.std(results['returns'])
Esempio n. 13
0
def minicity_example(render=None,
                     save_render=None,
                     sight_radius=None,
                     pxpm=None,
                     show_radius=None):
    """
    Perform a simulation of vehicles on modified minicity of University of
    Delaware.

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

    if render is not None:
        sumo_params.render = render

    if save_render is not None:
        sumo_params.save_render = save_render

    if sight_radius is not None:
        sumo_params.sight_radius = sight_radius

    if pxpm is not None:
        sumo_params.pxpm = pxpm

    if show_radius is not None:
        sumo_params.show_radius = show_radius

    vehicles = Vehicles()
    vehicles.add(veh_id="idm",
                 acceleration_controller=(IDMController, {}),
                 routing_controller=(MinicityRouter, {}),
                 speed_mode=1,
                 lane_change_mode="no_lat_collide",
                 initial_speed=0,
                 num_vehicles=90)
    vehicles.add(veh_id="rl",
                 acceleration_controller=(RLController, {}),
                 routing_controller=(MinicityRouter, {}),
                 speed_mode="no_collide",
                 initial_speed=0,
                 num_vehicles=10)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    additional_net_params = ADDITIONAL_NET_PARAMS.copy()
    net_params = NetParams(no_internal_links=False,
                           additional_params=additional_net_params)

    initial_config = InitialConfig(spacing="random", min_gap=5)
    scenario = MiniCityScenario(name="minicity",
                                vehicles=vehicles,
                                initial_config=initial_config,
                                net_params=net_params)

    env = AccelEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
Esempio n. 14
0
def merge_baseline(num_runs, render=True):
    """Run script for all merge 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 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=(SumoCarFollowingController, {}),
                 speed_mode=9,
                 num_vehicles=5)

    # 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=FLOW_RATE,
               departLane="free", departSpeed=10)
    inflow.add(veh_type="human", edge="inflow_merge", vehs_per_hour=100,
               departLane="free", departSpeed=7.5)

    sumo_params = SumoParams(
        restart_instance=True,
        sim_step=0.5,  # time step decreased to prevent occasional crashes
        render=render,
    )

    env_params = EnvParams(
        horizon=HORIZON,
        sims_per_step=5,  # value raised to ensure sec/step match experiment
        warmup_steps=0,
        evaluate=True,  # Set to True to evaluate traffic metric performance
        additional_params={
            "max_accel": 1.5,
            "max_decel": 1.5,
            "target_velocity": 20,
            "num_rl": NUM_RL,
        },
    )

    initial_config = InitialConfig()

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

    scenario = MergeScenario(name="merge",
                             vehicles=vehicles,
                             net_params=net_params,
                             initial_config=initial_config)

    env = WaveAttenuationMergePOEnv(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
Esempio n. 15
0
class TestBottleneck(unittest.TestCase):
    def test_it_runs(self):
        self.env, self.scenario = setup_bottlenecks()
        self.exp = SumoExperiment(self.env, self.scenario)
        self.exp.run(5, 50)
    "start_time": 0,
    "end_time": 3000,
    "cfg_path": "traffic/flow-dev/leah/cfg/"
}

# initial_positions = [("top", 0), ("top", 70), ("top", 140), \
#                     ("left", 0), ("left", 70), ("left", 140), \
#                     ("bottom", 0), ("bottom", 70), ("bottom", 140), \
#                     ("right", 0), ("right", 70), ("right", 140)]

initial_config = {"shuffle": False}

scenario = LoopScenario("leah-test-exp", type_params, net_params,
                        cfg_params)  #, initial_config=initial_config)

exp = SumoExperiment(SimpleVelocityEnvironment, env_params, sumo_binary,
                     sumo_params, scenario)

logging.info("Experiment Set Up complete")

print("experiment initialized")

env = normalize(exp.env)

stub(globals())

for seed in [1]:  # [1, 5, 10, 73, 56]
    policy = GaussianMLPPolicy(env_spec=env.spec, hidden_sizes=(16, ))

    baseline = LinearFeatureBaseline(env_spec=env.spec)

    algo = TRPO(
Esempio n. 17
0
 def test_it_runs(self):
     self.env, self.scenario = setup_bottlenecks()
     self.exp = SumoExperiment(self.env, self.scenario)
     self.exp.run(5, 50)
Esempio n. 18
0
        "horizontal": 30,
        "vertical": 30
    }
}
net_params = NetParams(no_internal_links=False,
                       additional_params=additional_net_params)

cfg_params = {"start_time": 0, "end_time": 3000, "cfg_path": "debug/cfg/"}

initial_config = InitialConfig(spacing="custom",
                               additional_params={
                                   "intensity": intensity,
                                   "enter_speed": v_enter
                               })

scenario = TwoWayIntersectionScenario("two-way-intersection",
                                      TwoWayIntersectionGenerator,
                                      vehicles,
                                      net_params,
                                      initial_config=initial_config)

env = TwoIntersectionEnvironment(env_params, sumo_params, scenario)

exp = SumoExperiment(env, scenario)

logging.info("Experiment Set Up complete")

exp.run(1, 1500)

exp.env.terminate()
Esempio n. 19
0
class TestCollisions(unittest.TestCase):
    """Tests that collisions do not cause the experiments to terminate
    prematurely."""

    def test_collide(self):
        """Tests collisions in the absence of inflows."""
        # create the environment and scenario classes for a ring road
        sumo_params = SumoParams(sim_step=1, sumo_binary="sumo")
        total_vehicles = 20
        vehicles = Vehicles()
        vehicles.add(veh_id="idm",
                     acceleration_controller=(SumoCarFollowingController, {}),
                     routing_controller=(GridRouter, {}),
                     sumo_car_following_params=SumoCarFollowingParams(
                         tau=0.1, carFollowModel="Krauss", minGap=2.5),
                     num_vehicles=total_vehicles,
                     speed_mode=0b00000)
        grid_array = {"short_length": 100, "inner_length": 100,
                      "long_length": 100, "row_num": 1,
                      "col_num": 1,
                      "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 = {"speed_limit": 35, "grid_array": grid_array,
                                 "horizontal_lanes": 1, "vertical_lanes": 1,
                                 "traffic_lights": 1}

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

        self.env, self.scenario = grid_mxn_exp_setup(row_num=1, col_num=1,
                                                     sumo_params=sumo_params,
                                                     vehicles=vehicles,
                                                     net_params=net_params)

        # go through the env and set all the lights to green
        for i in range(self.env.rows * self.env.cols):
            self.env.traci_connection.trafficlight.setRedYellowGreenState(
                'center' + str(i), "gggggggggggg")

        # instantiate an experiment class
        self.exp = SumoExperiment(self.env, self.scenario)

        self.exp.run(50, 50)

    def test_collide_inflows(self):
        """Tests collisions in the presence of inflows."""
        # create the environment and scenario classes for a ring road
        sumo_params = SumoParams(sim_step=1, sumo_binary="sumo")
        total_vehicles = 12
        vehicles = Vehicles()
        vehicles.add(veh_id="idm",
                     acceleration_controller=(SumoCarFollowingController, {}),
                     routing_controller=(GridRouter, {}),
                     sumo_car_following_params=SumoCarFollowingParams(
                         tau=0.1, carFollowModel="Krauss", minGap=2.5),
                     num_vehicles=total_vehicles,
                     speed_mode=0b00000)
        grid_array = {"short_length": 100, "inner_length": 100,
                      "long_length": 100, "row_num": 1,
                      "col_num": 1,
                      "cars_left": 3,
                      "cars_right": 3,
                      "cars_top": 3,
                      "cars_bot": 3}

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

        inflows = InFlows()
        inflows.add(veh_type="idm", edge="bot0_0", vehsPerHour=1000)
        inflows.add(veh_type="idm", edge="top0_1", vehsPerHour=1000)

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

        self.env, self.scenario = grid_mxn_exp_setup(row_num=1, col_num=1,
                                                     sumo_params=sumo_params,
                                                     vehicles=vehicles,
                                                     net_params=net_params)

        # go through the env and set all the lights to green
        for i in range(self.env.rows * self.env.cols):
            self.env.traci_connection.trafficlight.setRedYellowGreenState(
                'center' + str(i), "gggggggggggg")

        # instantiate an experiment class
        self.exp = SumoExperiment(self.env, self.scenario)

        self.exp.run(50, 50)
Esempio n. 20
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)
Esempio n. 21
0
def grid_example(sumo_binary=None):
    inner_length = 300
    long_length = 500
    short_length = 300
    n = 2
    m = 3
    num_cars_left = 20
    num_cars_right = 20
    num_cars_top = 20
    num_cars_bot = 20
    tot_cars = (num_cars_left + num_cars_right) * m \
        + (num_cars_top + num_cars_bot) * 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=0.1, sumo_binary="sumo-gui")

    if sumo_binary is not None:
        sumo_params.sumo_binary = sumo_binary

    vehicles = Vehicles()
    vehicles.add(veh_id="human",
                 routing_controller=(GridRouter, {}),
                 num_vehicles=tot_cars)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    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"
    }]
    tl_logic.add("center0", phases=phases, programID=1)
    tl_logic.add("center1", phases=phases, programID=1)
    tl_logic.add("center2", tls_type="actuated", phases=phases, programID=1)

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

    initial_config = InitialConfig()

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

    env = AccelEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
Esempio n. 22
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)
Esempio n. 23
0
    def setUp(self):
        # create the environment and scenario classes for a ring road
        self.env, scenario = two_loops_one_merging_exp_setup()

        # instantiate an experiment class
        self.exp = SumoExperiment(self.env, scenario)
Esempio n. 24
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
Esempio n. 25
0
def bay_bridge_example(render=None,
                       use_inflows=False,
                       use_traffic_lights=False):
    """
    Perform a simulation of vehicles on the Oakland-San Francisco Bay Bridge.

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

    sumo_car_following_params = SumoCarFollowingParams(speedDev=0.2)
    sumo_lc_params = SumoLaneChangeParams(
        lc_assertive=20,
        lc_pushy=0.8,
        lc_speed_gain=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(inflows=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",
                                 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)
class TestInstantaneousFailsafe(unittest.TestCase):
    """
    Tests that the instantaneous failsafe of the base acceleration controller
    does not allow vehicles to crash under situations where they otherwise
    would. This is tested on two crash-prone controllers: OVM and LinearOVM
    """
    def setUp_failsafe(self, vehicles):
        additional_env_params = {
            "target_velocity": 8,
            "max-deacc": 3,
            "max-acc": 3
        }
        env_params = EnvParams(additional_params=additional_env_params,
                               longitudinal_fail_safe="instantaneous")

        additional_net_params = {
            "length": 100,
            "lanes": 1,
            "speed_limit": 30,
            "resolution": 40
        }
        net_params = NetParams(additional_params=additional_net_params)

        initial_config = InitialConfig(bunching=10)

        # create the environment and scenario classes for a ring road
        env, scenario = ring_road_exp_setup(vehicles=vehicles,
                                            env_params=env_params,
                                            net_params=net_params,
                                            initial_config=initial_config)

        # instantiate an experiment class
        self.exp = SumoExperiment(env, scenario)

    def tearDown_failsafe(self):
        # free data used by the class
        self.exp = None

    def test_no_crash_OVM(self):
        vehicles = Vehicles()
        vehicles.add_vehicles(veh_id="test",
                              acceleration_controller=(OVMController, {}),
                              routing_controller=(ContinuousRouter, {}),
                              num_vehicles=10)

        self.setUp_failsafe(vehicles=vehicles)

        # run the experiment, see if it fails
        self.exp.run(1, 200)

        self.tearDown_failsafe()

    def test_no_crash_LinearOVM(self):
        vehicles = Vehicles()
        vehicles.add_vehicles(veh_id="test",
                              acceleration_controller=(LinearOVM, {}),
                              routing_controller=(ContinuousRouter, {}),
                              num_vehicles=10)

        self.setUp_failsafe(vehicles=vehicles)

        # run the experiment, see if it fails
        self.exp.run(1, 200)

        self.tearDown_failsafe()
Esempio n. 27
0
def grid_example(render=None):
    """
    Perform a simulation of vehicles on a grid.

    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 and balanced traffic lights on a grid.
    """
    inner_length = 300
    long_length = 500
    short_length = 300
    n = 2
    m = 3
    num_cars_left = 0
    num_cars_right = 0
    num_cars_top = 0
    num_cars_bot = 0
    tot_cars = (num_cars_left + num_cars_right) * m \
        + (num_cars_top + num_cars_bot) * 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=0.1, render=True)

    if render is not None:
        sumo_params.render = render

    vehicles = Vehicles()
    vehicles.add(veh_id="human",
                 routing_controller=(GridRouter, {}),
                 num_vehicles=tot_cars)

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

    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"
    }]
    tl_logic.add("center0", phases=phases, programID=1)
    tl_logic.add("center1", phases=phases, programID=1)
    tl_logic.add("center2", tls_type="actuated", phases=phases, programID=1)
    """"""
    inflow = InFlows()

    inflow.add(veh_type="human",
               edge="bot1_0",
               probability=1,
               departLane="free",
               departSpeed=20)
    """
    inflow.add(
        veh_type="human",
        edge="bot0_0",
        probability=0.25,
        departLane="free",
        departSpeed=20)

    inflow.add(
        veh_type="human",
        edge="top1_3",
        probability=1,
        departLane="free",
        departSpeed=20)
    inflow.add(
        veh_type="human",
        edge="top0_3",
        probability=0.25,
        departLane="free",
        departSpeed=20)

    inflow.add(
        veh_type="human",
        edge="left2_0",
        probability=1,
        departLane="free",
        departSpeed=20)
    inflow.add(
        veh_type="human",
        edge="left2_1",
        probability=0.25,
        departLane="free",
        departSpeed=20)    
    inflow.add(
        veh_type="human",
        edge="left2_2",
        probability=1,
        departLane="free",
        departSpeed=20)

    inflow.add(
        veh_type="human",
        edge="right0_0",
        probability=1,
        departLane="free",
        departSpeed=20)
    inflow.add(
        veh_type="human",
        edge="right0_1",
        probability=0.25,
        departLane="free",
        departSpeed=20)   """
    inflow.add(veh_type="human",
               edge="right0_2",
               probability=1,
               departLane="free",
               departSpeed=20)

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

    initial_config = InitialConfig()

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

    env = AccelEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
Esempio n. 28
0
def grid1_baseline(num_runs, render=True):
    """Run script for the grid1 baseline.

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

    Returns
    -------
        SumoExperiment
            class needed to run simulations
    """
    exp_tag = flow_params['exp_tag']
    sumo_params = flow_params['sumo']
    vehicles = flow_params['veh']
    env_params = flow_params['env']
    net_params = flow_params['net']
    initial_config = flow_params.get('initial', InitialConfig())

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

    # modify the rendering to match what is requested
    sumo_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=tl_logic
    )

    # 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)
    total_delay = np.mean(results['returns'])

    return total_delay