Пример #1
0
def test_only_agents_reset_if_scenario_does_not_change(smarts, scenarios):
    """Upon reset, if the scenario remains the same then the agent should be reset
    ("teleported" somewhere else) but the social vehicles should continue as if
    nothing happened.
    """

    def ego_vehicle_pose(agent_obs):
        state = agent_obs.ego_vehicle_state
        start = tuple(state.position) + (state.heading,)
        return np.array(start)

    def neighborhood_vehicle_poses(agent_obs):
        neighborhood_vehicles = agent_obs.neighborhood_vehicle_states
        states = sorted(neighborhood_vehicles, key=lambda s: s.id)
        starts = [tuple(s.position) + (s.heading,) for s in states]
        if not starts:
            return np.array([])

        return np.stack(starts)

    scenario = next(scenarios)

    seed(42)
    obs = smarts.reset(scenario)

    agent_obs = obs["Agent-007"]
    ego_start_pose = ego_vehicle_pose(agent_obs)
    sv_start_poses = neighborhood_vehicle_poses(agent_obs)

    for _ in range(50):
        smarts.step({"Agent-007": "keep_lane"})

    seed(42)
    obs = smarts.reset(scenario)

    agent_obs = obs["Agent-007"]
    agent_pose_reset_to_initial = np.all(
        np.isclose(ego_start_pose, ego_vehicle_pose(agent_obs))
    )

    sv_poses = neighborhood_vehicle_poses(agent_obs)
    sv_poses_reset_to_initial = sv_poses.shape == sv_start_poses.shape
    if sv_poses_reset_to_initial:
        sv_poses_reset_to_initial = np.all(np.isclose(sv_poses, sv_start_poses))

    assert (
        agent_pose_reset_to_initial
    ), "Upon reset the agent goes back to the starting position"
    assert sv_poses_reset_to_initial, "Upon reset social vehicles are unaffected"
Пример #2
0
def test_boids(smarts, scenarios, bubble):
    # TODO: this is a hack to specify a seed to make this test pass
    seed(int(os.getenv("PYTHONHASHSEED", 42)))

    scenario = next(scenarios)
    smarts.reset(scenario)

    index = smarts.vehicle_index
    geometry = bubble_geometry(bubble, smarts.road_network)

    triggered_multiple_vehicles_in_bubble = False
    triggered_multiple_vehicles_airlocked = False

    # vehicle: steps per zone
    steps_driven_in_zones = defaultdict(lambda: ZoneSteps())
    # TODO: It's possible that multiple vehicles get spawned within the 500 steps but
    #       not all of them make it through the bubble completely causing the test to
    #       fail.
    for _ in range(500):
        smarts.step({})

        hijacked_actor_ids = []
        shadowed_actor_ids = []

        for vehicle in index.vehicles:
            position = Point(vehicle.position)
            in_bubble = position.within(geometry.bubble)
            is_shadowing = index.shadow_actor_id_from_vehicle_id(
                vehicle.id) is not None
            is_agent_controlled = vehicle.id in index.agent_vehicle_ids

            zone_steps = steps_driven_in_zones[vehicle.id]
            if position.within(geometry.bubble):
                zone_steps.in_bubble += 1
                hijacked_actor_ids.append(
                    index.actor_id_from_vehicle_id(vehicle.id))
                assert in_bubble and not is_shadowing and is_agent_controlled
            elif position.within(geometry.airlock_entry):
                zone_steps.airlock_entry += 1
                shadowed_actor_ids.append(
                    index.shadow_actor_id_from_vehicle_id(vehicle.id))
                assert not in_bubble and is_shadowing and not is_agent_controlled
            elif position.within(geometry.airlock_exit):
                zone_steps.airlock_exit += 1
                # TODO: Presently not implemented, but `is_shadowing` should be True
                assert not in_bubble and not is_shadowing and is_agent_controlled
            else:
                zone_steps.outside_bubble += 1
                assert not in_bubble and not is_shadowing and not is_agent_controlled

        if len(hijacked_actor_ids) > 1:
            triggered_multiple_vehicles_in_bubble = True

        if len(shadowed_actor_ids) > 1:
            triggered_multiple_vehicles_airlocked = True

        assert (len(set(hijacked_actor_ids)) <=
                1), "Boid vehicles must be controlled by the same actor"
        assert (len(set(shadowed_actor_ids)) <=
                1), "Boid vehicles must be shadowed by the same actor"

    # Just to have some padding, we want to be in each region at least 5 steps
    min_steps = 5
    for vehicle_id, zone in steps_driven_in_zones.items():
        assert all([
            zone.in_bubble > min_steps,
            zone.outside_bubble > min_steps,
            zone.airlock_entry > min_steps,
            zone.airlock_exit > min_steps,
        ]), (f"vehicle_id={vehicle_id}, zone={zone} doesn't meet "
             f"min_steps={min_steps} requirement")

    assert (triggered_multiple_vehicles_in_bubble
            ), "Multiple vehicles did not enter the bubble simultaneously"
    assert (triggered_multiple_vehicles_airlocked
            ), "Multiple vehicles were not airlocked simultaneously"
Пример #3
0
from pathlib import Path

from smarts.core import seed
from smarts.sstudio import gen_scenario
from smarts.sstudio import types as t

seed(42)

traffic = t.Traffic(flows=[
    t.Flow(
        route=t.RandomRoute(),
        rate=60 * 60,
        actors={t.TrafficActor(name="car", vehicle_type=vehicle_type): 1},
    ) for vehicle_type in [
        "passenger",
        "bus",
        "coach",
        "truck",
        "trailer",
    ]
])

laner_actor = t.SocialAgentActor(
    name="keep-lane-agent",
    agent_locator="zoo.policies:keep-lane-agent-v0",
)

gen_scenario(
    t.Scenario(
        traffic={"basic": traffic},
        social_agent_missions={
from smarts.core import seed
from smarts.sstudio import gen_traffic, gen_missions
from smarts.sstudio.types import (
    Traffic,
    Flow,
    Route,
    RandomRoute,
    TrafficActor,
    Mission,
    Distribution,
    LaneChangingModel,
    JunctionModel,
)

seed_ = 3
seed(seed_)

scenario_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                             "../dataset_public/3lane")

traffic_actor = TrafficActor(
    name="car",
    speed=Distribution(sigma=0.2, mean=0.8),
)

# add 10 social vehicles with random routes.
traffic = Traffic(flows=[
    # generate flows last for 10 hours
    Flow(
        route=RandomRoute(),
        begin=0,