Ejemplo n.º 1
0
def figure_eight_example(render=None):
    """Perform a simulation of vehicles on a figure eight.

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

    Returns
    -------
    exp: flow.core.experiment.Experiment
        A non-rl experiment demonstrating the performance of human-driven
        vehicles on a figure eight.
    """
    sim_params = AimsunParams(sim_step=0.5, render=False, emission_path='data')

    if render is not None:
        sim_params.render = render

    vehicles = VehicleParams()
    vehicles.add(veh_id="human",
                 acceleration_controller=(IDMController, {}),
                 num_vehicles=14)

    env_params = EnvParams()

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

    scenario = Figure8Scenario(name="figure8",
                               vehicles=vehicles,
                               net_params=net_params)

    env = TestEnv(env_params, sim_params, scenario, simulator='aimsun')

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

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

    Returns
    -------
    exp: flow.core.experiment.Experiment
        A non-rl experiment demonstrating the performance of human-driven
        vehicles on a figure eight.
    """
    sim_params = SumoParams(render=True)

    if render is not None:
        sim_params.render = render

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

    env_params = EnvParams(additional_params=ADDITIONAL_ENV_PARAMS)

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

    scenario = Figure8Scenario(
        name="figure8",
        vehicles=vehicles,
        net_params=net_params)

    env = AccelEnv(env_params, sim_params, scenario)

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

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

    Returns
    -------
    exp: flow.core.SumoExperiment type
        A non-rl experiment demonstrating the performance of human-driven
        vehicles on a figure eight.
    """
    sumo_params = SumoParams(render=True)

    if render is not None:
        sumo_params.render = render

    vehicles = Vehicles()
    vehicles.add(veh_id="idm",
                 acceleration_controller=(IDMController, {}),
                 lane_change_controller=(StaticLaneChanger, {}),
                 routing_controller=(ContinuousRouter, {}),
                 speed_mode="no_collide",
                 initial_speed=0,
                 num_vehicles=14)

    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)

    scenario = Figure8Scenario(name="figure8",
                               vehicles=vehicles,
                               net_params=net_params)

    env = AccelEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
Ejemplo n.º 4
0
def figure_eight_exp_setup(sim_params=None,
                           vehicles=None,
                           env_params=None,
                           net_params=None,
                           initial_config=None,
                           traffic_lights=None):
    """
    Create an environment and scenario pair for figure eight test experiments.

    Parameters
    ----------
    sim_params : flow.core.params.SumoParams
        sumo-related configuration parameters, defaults to a time step of 0.1s
        and no sumo-imposed failsafe on human or rl vehicles
    vehicles : Vehicles type
        vehicles to be placed in the network, default is one vehicles with an
        IDM acceleration controller and ContinuousRouter routing controller.
    env_params : flow.core.params.EnvParams
        environment-specific parameters, defaults to a environment with no
        failsafes, where other parameters do not matter for non-rl runs
    net_params : flow.core.params.NetParams
        network-specific configuration parameters, defaults to a figure eight
        with a 30 m radius
    initial_config : flow.core.params.InitialConfig
        specifies starting positions of vehicles, defaults to evenly
        distributed vehicles across the length of the network
    traffic_lights: flow.core.params.TrafficLightParams
        traffic light signals, defaults to no traffic lights in the network
    """
    logging.basicConfig(level=logging.WARNING)

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

    if vehicles is None:
        # set default vehicles configuration
        vehicles = VehicleParams()
        vehicles.add(veh_id="idm",
                     acceleration_controller=(IDMController, {}),
                     car_following_params=SumoCarFollowingParams(
                         speed_mode="aggressive", ),
                     routing_controller=(ContinuousRouter, {}),
                     num_vehicles=1)

    if env_params is None:
        # set default env_params configuration
        additional_env_params = {
            "target_velocity": 8,
            "max_accel": 1,
            "max_decel": 1,
            "sort_vehicles": False
        }
        env_params = EnvParams(additional_params=additional_env_params)

    if net_params is None:
        # set default net_params configuration
        additional_net_params = {
            "radius_ring": 30,
            "lanes": 1,
            "speed_limit": 30,
            "resolution": 40
        }
        net_params = NetParams(additional_params=additional_net_params)

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

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

    # create the scenario
    scenario = Figure8Scenario(name="FigureEightTest",
                               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
Ejemplo n.º 5
0
def run_task(*_):
    """Implement the run_task method needed to run experiments with rllab."""
    sumo_params = SumoParams(sim_step=0.1, render=True)

    vehicles = Vehicles()
    vehicles.add(veh_id="rl",
                 acceleration_controller=(RLController, {}),
                 routing_controller=(ContinuousRouter, {}),
                 sumo_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, {}),
                 sumo_car_following_params=SumoCarFollowingParams(
                     speed_mode="no_collide", ),
                 num_vehicles=13)

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

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

    initial_config = InitialConfig(spacing="uniform")

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

    env_name = "AccelEnv"
    pass_params = (env_name, sumo_params, vehicles, env_params, net_params,
                   initial_config, scenario)

    env = GymEnv(env_name, record_video=False, register_params=pass_params)
    horizon = env.horizon
    env = normalize(env)

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

    baseline = LinearFeatureBaseline(env_spec=env.spec)

    algo = TRPO(
        env=env,
        policy=policy,
        baseline=baseline,
        batch_size=15000,
        max_path_length=horizon,
        n_itr=500,
        # whole_paths=True,
        discount=0.999,
        # step_size=v["step_size"],
    )
    algo.train(),
Ejemplo n.º 6
0
def figure_eight_baseline(num_runs, render=True):
    """Run script for all figure eight baselines.

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

    Returns
    -------
        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