Esempio n. 1
0
def figure_eight_example(sumo_binary=None):
    sumo_params = SumoParams(sumo_binary="sumo-gui")

    if sumo_binary is not None:
        sumo_params.sumo_binary = sumo_binary

    vehicles = Vehicles()
    vehicles.add(veh_id="idm",
                 acceleration_controller=(IDMController, {}),
                 lane_change_controller=(StaticLaneChanger, {}),
                 routing_controller=(ContinuousRouter, {}),
                 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",
                               generator_class=Figure8Generator,
                               vehicles=vehicles,
                               net_params=net_params)

    env = AccelEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
Esempio n. 2
0
def run_task(*_):
    sumo_params = SumoParams(sim_step=0.1,
                             sumo_binary="sumo")

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

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

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

    initial_config = InitialConfig(spacing="uniform")

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

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

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

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

    baseline = LinearFeatureBaseline(env_spec=env.spec)

    algo = TRPO(
        env=env,
        policy=policy,
        baseline=baseline,
        batch_size=15000,
        max_path_length=horizon,
        n_itr=500,
        # whole_paths=True,
        discount=0.999,
        # step_size=v["step_size"],
    )
    algo.train(),
Esempio 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",
                               generator_class=Figure8Generator,
                               vehicles=vehicles,
                               net_params=net_params)

    env = AccelEnv(env_params, sumo_params, scenario)

    return SumoExperiment(env, scenario)
sumo_params = SumoParams(sumo_binary="sumo-gui")

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

additional_env_params = {"target_velocity": 8, "num_steps": 500}
env_params = EnvParams(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)

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

env = SimpleAccelerationEnvironment(env_params, sumo_params, scenario)

exp = SumoExperiment(env, scenario)

logging.info("Experiment Set Up complete")

exp.run(1, 1500)

exp.env.terminate()
Esempio n. 5
0
    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",
                           generator_class=Figure8Generator,
                           vehicles=vehicles,
                           net_params=net_params,
                           initial_config=initial_config)

env = AccelEnv(env_params, sumo_params, scenario)

exp = SumoExperiment(env, scenario)

num_runs = 2
results = exp.run(num_runs, HORIZON)
avg_speed = np.mean(results["mean_returns"])
print('The average speed across {} runs is {}'.format(num_runs, avg_speed))
Esempio n. 6
0
def figure_eight_exp_setup(sumo_params=None,
                           vehicles=None,
                           env_params=None,
                           net_params=None,
                           initial_config=None,
                           traffic_lights=None):
    """
    Creates an environment and scenario pair for figure eight test experiments.

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

    if sumo_params is None:
        # set default sumo_params configuration
        sumo_params = SumoParams(sim_step=0.1, sumo_binary="sumo")

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

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

    if net_params is None:
        # set default net_params configuration
        additional_net_params = {
            "radius_ring": 30,
            "lanes": 1,
            "speed_limit": 30,
            "resolution": 40
        }
        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(lanes_distribution=1)

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

    # create the scenario
    scenario = Figure8Scenario(
        name="RingRoadTest",
        generator_class=Figure8Generator,
        vehicles=vehicles,
        net_params=net_params,
        initial_config=initial_config,
        traffic_lights=traffic_lights)

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

    return env, scenario