def create_scenario():
    with temp_scenario(name="cycles",
                       map="maps/6lane.net.xml") as scenario_root:
        actors = [
            SocialAgentActor(
                name=f"non-interactive-agent-{speed}-v0",
                agent_locator="zoo.policies:non-interactive-agent-v0",
                policy_kwargs={"speed": speed},
            ) for speed in [10, 30]
        ]

        for name, (edge_start, edge_end) in [
            ("group-1", ("edge-north-NS", "edge-south-NS")),
            ("group-2", ("edge-west-WE", "edge-east-WE")),
            ("group-3", ("edge-east-EW", "edge-west-EW")),
            ("group-4", ("edge-south-SN", "edge-north-SN")),
        ]:
            route = Route(begin=("edge-north-NS", 1, 0),
                          end=("edge-south-NS", 1, "max"))
            missions = [Mission(route=route)] * 2
            gen_social_agent_missions(
                scenario_root,
                social_agent_actor=actors,
                name=name,
                missions=missions,
            )

        yield scenario_root
示例#2
0
def build_social_agent_actors(original_data):
    agent_names, actors = [], []
    agent_groups = {}
    for group_name, agents in original_data.items():
        agent_groups[group_name] = []
        for agent in agents:
            if "locator" in agent:
                # local file-based agent
                agent_locator = agent["locator"]
                if "name" in agent:
                    agent_name = agent["name"]
                else:
                    agent_name = agent_locator.split(":")[-1]

                actor = SocialAgentActor(
                    name=agent_name,
                    agent_locator=agent_locator,
                    policy_kwargs=agent["params"] if "params" in agent else {},
                )
            else:
                raise ValueError(
                    f"Agent {agent} configuration does not match the expected schema"
                )

            agent_names.append(agent_name)
            actors.append(actor)
            agent_groups[group_name].append(agent_name)

    return agent_names, actors, agent_groups
示例#3
0
def bubble():
    """
    |(-7)  |(-5)     (0)     (5)|  (7)|
    """
    return Bubble(
        zone=PositionalZone(pos=(0, 0), size=(10, 10)),
        margin=2,
        actor=SocialAgentActor(name="zoo-car", agent_locator="come.find.me"),
    )
示例#4
0
def build_social_agent_actors(original_data):
    agent_names, actors = [], []
    agent_groups = {}
    for group_name, agents in original_data.items():
        agent_groups[group_name] = []
        for agent in agents:
            if "locator" in agent:
                # local file-based agent
                agent_locator = agent["locator"]
                if "name" in agent:
                    agent_name = agent["name"]
                else:
                    agent_name = agent_locator.split(":")[-1]

                actor = SocialAgentActor(
                    name=agent_name,
                    agent_locator=agent_locator,
                    policy_kwargs=agent["params"] if "params" in agent else {},
                )
            elif "url" in agent:
                # packaged agent, installed via pip
                agent_name = f"{agent['package']}-{agent['version']}"
                actor = SocialAgentActor.from_zoo(
                    name=agent_name,
                    url=agent["url"],
                    package=agent["package"],
                    version=agent["version"],
                )
            else:
                raise ValueError(
                    f"Agent {agent} configuration does not match the expected schema"
                )

            agent_names.append(agent_name)
            actors.append(actor)
            agent_groups[group_name].append(agent_name)

    return agent_names, actors, agent_groups
示例#5
0
def scenario_root(scenario_parent_path):
    # TODO: We may want to consider referencing to concrete scenarios in our tests
    #       rather than generating them. The benefit of generting however is that
    #       we can change the test criteria and scenario code in unison.
    scenario = Path(scenario_parent_path) / "cycles"
    scenario.mkdir()

    shutil.copyfile(
        Path(__file__).parent / "maps/6lane.net.xml", scenario / "map.net.xml")
    generate_glb_from_sumo_network(str(scenario / "map.net.xml"),
                                   str(scenario / "map.glb"))

    actors = [
        SocialAgentActor(
            name=f"non-interactive-agent-{speed}-v0",
            agent_locator="zoo.policies:non-interactive-agent-v0",
            policy_kwargs={"speed": speed},
        ) for speed in [10, 30, 80]
    ]

    for name, (edge_start, edge_end) in [
        ("group-1", ("edge-north-NS", "edge-south-NS")),
        ("group-2", ("edge-west-WE", "edge-east-WE")),
        ("group-3", ("edge-east-EW", "edge-west-EW")),
        ("group-4", ("edge-south-SN", "edge-north-SN")),
    ]:
        route = Route(begin=("edge-north-NS", 1, 0),
                      end=("edge-south-NS", 1, "max"))
        missions = [Mission(route=route)] * 2  # double up
        gen_social_agent_missions(
            scenario,
            social_agent_actor=actors,
            name=name,
            missions=missions,
        )

    gen_missions(
        scenario,
        missions=[
            Mission(
                Route(begin=("edge-west-WE", 0, 0),
                      end=("edge-east-WE", 0, "max")))
        ],
    )

    return scenario
示例#6
0
def scenario_root():
    # TODO: We may want to consider referencing to concrete scenarios in our tests
    #       rather than generating them. The benefit of generting however is that
    #       we can change the test criteria and scenario code in unison.
    with temp_scenario(name="cycles",
                       map="maps/6lane.net.xml") as scenario_root:
        actors = [
            SocialAgentActor(
                name=f"non-interactive-agent-{speed}-v0",
                agent_locator="zoo.policies:non-interactive-agent-v0",
                policy_kwargs={"speed": speed},
            ) for speed in [10, 30, 80]
        ]

        for name, (edge_start, edge_end) in [
            ("group-1", ("edge-north-NS", "edge-south-NS")),
            ("group-2", ("edge-west-WE", "edge-east-WE")),
            ("group-3", ("edge-east-EW", "edge-west-EW")),
            ("group-4", ("edge-south-SN", "edge-north-SN")),
        ]:
            route = Route(begin=("edge-north-NS", 1, 0),
                          end=("edge-south-NS", 1, "max"))
            missions = [Mission(route=route)] * 2  # double up
            gen_social_agent_missions(
                scenario_root,
                social_agent_actor=actors,
                name=name,
                missions=missions,
            )

        gen_missions(
            scenario_root,
            missions=[
                Mission(
                    Route(begin=("edge-west-WE", 0, 0),
                          end=("edge-east-WE", 0, "max")))
            ],
        )
        yield scenario_root
示例#7
0
# select it by sumo NETEDIT
starting_edges = ["-156328679", "gneE9", "179968249", "138566998"]


#########################################
# generate social agents with predefined policy
#########################################
#
# N.B. You need to have the agent_locator in a location where the left side can be resolved
#   as a module in form:
#       "this.resolved.module:attribute"
#   In your own project you would place the prefabs script where python can reach it

social_agent1 = SocialAgentActor(
    name="zoo-car1", agent_locator="agent_prefabs:zoo-agent1-v0",
)

social_agent2 = SocialAgentActor(
    name="zoo-car2", agent_locator="agent_prefabs:zoo-agent2-v0",
)

social_agent3 = SocialAgentActor(
    name="zoo-car3", agent_locator="agent_prefabs:zoo-agent2-v0",
)

# here define social agent type and numbers
social_agents = [social_agent2, social_agent3, social_agent1]

# generate social agent missisons and store social agents' information under social_agents/
for scenario, starting_edge in zip(scenario_paths, starting_edges):
示例#8
0
import os

from smarts.sstudio import gen_social_agent_missions
from smarts.sstudio.types import Mission, Route, SocialAgentActor

scenario = os.path.dirname(os.path.realpath(__file__))

laner_agent = SocialAgentActor(
    name="laner-agent",
    agent_locator=
    "scenarios.intersections.2lane_circle.agent_prefabs:laner-agent-v0",
)
buddha_agent = SocialAgentActor(
    name="buddha-agent",
    agent_locator=
    "scenarios.intersections.2lane_circle.agent_prefabs:buddha-agent-v0",
)

gen_social_agent_missions(
    scenario,
    social_agent_actor=laner_agent,
    name=f"s-agent-{laner_agent.name}",
    missions=[
        Mission(Route(begin=("edge-east-EW", 0, 5),
                      end=("edge-west-EW", 0, 5)))
    ],
)

gen_social_agent_missions(
    scenario,
    social_agent_actor=buddha_agent,
示例#9
0
import os
from pathlib import Path

from smarts.sstudio import gen_scenario
from smarts.sstudio.types import Mission, Route, Scenario, SocialAgentActor

actors = [
    SocialAgentActor(
        name=f"non-interactive-agent-{speed}-v0",
        agent_locator="zoo.policies:non-interactive-agent-v0",
        policy_kwargs={"speed": speed},
    )
    for speed in [10, 30, 80]
]


def to_mission(start_edge, end_edge):
    route = Route(begin=(start_edge, 1, 0), end=(end_edge, 1, "max"))
    return Mission(route=route)


gen_scenario(
    Scenario(
        social_agent_missions={
            "group-1": (actors, [to_mission("edge-north-NS", "edge-south-NS")]),
            "group-2": (actors, [to_mission("edge-west-WE", "edge-east-WE")]),
            "group-3": (actors, [to_mission("edge-east-EW", "edge-west-EW")]),
            "group-4": (actors, [to_mission("edge-south-SN", "edge-north-SN")]),
        }
    ),
    output_dir=Path(__file__).parent,
示例#10
0
def bubble_config_to_bubble_object(
    scenario: str, bubble_config: Dict[str, Any], vehicles_to_not_hijack: Sequence[str]
) -> Bubble:
    """Converts a bubble config to a bubble object.

    Args:
        scenario:
            A string representing the path to this scenario.
        bubble_config:
            A dictionary with 'location', 'actor_name', 'agent_locator', and
            'agent_params' keys that is used to initialize the bubble.
        vehicles_to_not_hijack:
            A tuple of vehicle IDs that are passed to the bubble. The bubble will not
            capture those vehicles that have an ID in this tuple.

    Returns:
        Bubble: The bubble object created from the bubble config.
    """
    BUBBLE_MARGIN = 2
    map_file = sumolib.net.readNet(f"{scenario}/map.net.xml")

    location_name = bubble_config["location"][0]
    location_data = bubble_config["location"][1:]
    actor_name = bubble_config["actor_name"]
    agent_locator = bubble_config["agent_locator"]
    agent_params = bubble_config["agent_params"]

    if location_name == "intersection":
        # Create a bubble centered at the intersection.
        assert len(location_data) == 2
        bubble_length, bubble_width = location_data
        bubble_coordinates = map_file.getNode("junction-intersection").getCoord()
        zone = PositionalZone(
            pos=bubble_coordinates, size=(bubble_length, bubble_width)
        )
    else:
        # Create a bubble on one of the lanes.
        assert len(location_data) == 4
        lane_index, lane_offset, bubble_length, num_lanes_spanned = location_data
        zone = MapZone(
            start=("edge-" + location_name, lane_index, lane_offset),
            length=bubble_length,
            n_lanes=num_lanes_spanned,
        )

    bubble = Bubble(
        zone=zone,
        actor=SocialAgentActor(
            name=actor_name,
            agent_locator=agent_locator,
            policy_kwargs=agent_params,
            initial_speed=None,
        ),
        margin=BUBBLE_MARGIN,
        limit=None,
        exclusion_prefixes=vehicles_to_not_hijack,
        follow_actor_id=None,
        follow_offset=None,
        keep_alive=False,
    )
    return bubble
示例#11
0
        gen_traffic(
            scenario,
            traffic,
            name=f"{name}-{seed}",
            seed=seed,
        )

# Social Agents
#
# N.B. You need to have the agent_locator in a location where the left side can be resolved
#   as a module in form:
#       "this.resolved.module:attribute"
#   In your own project you would place the prefabs script where python can reach it
social_agent1 = SocialAgentActor(
    name="zoo-car1",
    agent_locator="scenarios.zoo_intersection.agent_prefabs:zoo-agent2-v0",
    initial_speed=20,
)
social_agent2 = SocialAgentActor(
    name="zoo-car2",
    agent_locator="scenarios.zoo_intersection.agent_prefabs:zoo-agent2-v0",
    initial_speed=20,
)

gen_social_agent_missions(
    scenario,
    social_agent_actor=social_agent2,
    name=f"s-agent-{social_agent2.name}",
    missions=[Mission(RandomRoute())],
)