Ejemplo n.º 1
0
def test_goal_properties():
    """ Basic property tests """

    goal = Goal()

    assert goal.transparent
    assert not goal.blocks
    assert goal.color == Color.NONE
    assert not goal.can_be_picked_up
    assert goal.state_index == 0

    assert goal.can_be_represented_in_state()
    assert goal.render_as_char() == 'G'
    assert goal.num_states() == 1
Ejemplo n.º 2
0
def make_goal_state(agent_on_goal: bool) -> State:
    """makes a simple state with a wall in front of the agent"""
    grid = Grid(2, 1)
    grid[0, 0] = Goal()
    agent_position = (0, 0) if agent_on_goal else (1, 0)
    agent = Agent(agent_position, Orientation.N)
    return State(grid, agent)
Ejemplo n.º 3
0
def match_key_color(
        *,
        rng: Optional[rnd.Generator] = None,  # pylint: disable=unused-argument
) -> State:
    """the agent has to pick the correct key to open a randomly colored door"""

    rng = get_gv_rng_if_none(rng)  # necessary to use rng object!

    # only consider these colors
    colors = [Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW]
    # randomly choose location of keys
    key1, key2, key3, key4 = rng.permute([Key(color) for color in colors])
    # randomly choose color of door
    door = Door(Door.Status.LOCKED, rng.choice(colors))

    # grids can be constructed directly from objects
    grid = Grid.from_objects([
        [Wall(), Wall(), Wall(), Wall(),
         Wall()],
        [Wall(), Wall(), Goal(), Wall(),
         Wall()],
        [Wall(), Wall(), door, Wall(), Wall()],
        [Wall(), key1, Floor(), key2, Wall()],
        [Wall(), key3, Floor(), key4, Wall()],
        [Wall(), Wall(), Wall(), Wall(),
         Wall()],
    ])

    # positioning the agent in the above grid
    agent = Agent((4, 2), Orientation.N)

    return State(grid, agent)
Ejemplo n.º 4
0
def test_grid_object_types():
    grid = Grid(3, 4)

    assert grid.object_types() == set([Floor])

    grid[0, 0] = Wall()
    assert grid.object_types() == set([Floor, Wall])

    grid[0, 0] = Goal()
    assert grid.object_types() == set([Floor, Goal])

    grid[1, 1] = Wall()
    assert grid.object_types() == set([Floor, Goal, Wall])
def simplest_reset(
        *,
        rng: Optional[rnd.Generator] = None,  # pylint: disable=unused-argument
) -> State:
    """smallest possible room with goal right in front of agent"""

    # constructed the grid directly from objects
    grid = Grid.from_objects([
        [Wall(), Wall(), Wall()],
        [Wall(), Goal(), Wall()],
        [Wall(), Floor(), Wall()],
        [Wall(), Wall(), Wall()],
    ])

    # positioning the agent in the above grid
    agent = Agent((2, 1), Orientation.N)

    return State(grid, agent)
Ejemplo n.º 6
0
def reset_empty(
    height: int,
    width: int,
    random_agent: bool = False,
    random_goal: bool = False,
    *,
    rng: Optional[rnd.Generator] = None,
) -> State:
    """An empty environment"""

    if height < 4 or width < 4:
        raise ValueError('height and width need to be at least 4')

    rng = get_gv_rng_if_none(rng)

    # TODO test creation (e.g. count number of walls, goals, check held item)

    grid = Grid(height, width)
    draw_wall_boundary(grid)

    if random_goal:
        goal_y = rng.integers(1, height - 2, endpoint=True)
        goal_x = rng.integers(1, width - 2, endpoint=True)
    else:
        goal_y = height - 2
        goal_x = width - 2

    grid[goal_y, goal_x] = Goal()

    if random_agent:
        agent_position = rng.choice([
            position for position in grid.positions()
            if isinstance(grid[position], Floor)
        ])
        agent_orientation = rng.choice(list(Orientation))
    else:
        agent_position = (1, 1)
        agent_orientation = Orientation.E

    agent = Agent(agent_position, agent_orientation)
    return State(grid, agent)
Ejemplo n.º 7
0
def reset_rooms(  # pylint: disable=too-many-locals
    height: int,
    width: int,
    layout: Tuple[int, int],
    *,
    rng: Optional[rnd.Generator] = None,
) -> State:

    rng = get_gv_rng_if_none(rng)

    # TODO test creation (e.g. count number of walls, goals, check held item)

    layout_height, layout_width = layout

    y_splits = np.linspace(
        0,
        height - 1,
        num=layout_height + 1,
        dtype=int,
    )

    if len(y_splits) != len(set(y_splits)):
        raise ValueError(
            f'insufficient height ({height}) for layout ({layout})')

    x_splits = np.linspace(
        0,
        width - 1,
        num=layout_width + 1,
        dtype=int,
    )

    if len(x_splits) != len(set(x_splits)):
        raise ValueError(
            f'insufficient width ({height}) for layout ({layout})')

    grid = Grid(height, width)
    draw_room_grid(grid, y_splits, x_splits, Wall)

    # passages in horizontal walls
    for y in y_splits[1:-1]:
        for x_from, x_to in mitt.pairwise(x_splits):
            x = rng.integers(x_from + 1, x_to)
            grid[y, x] = Floor()

    # passages in vertical walls
    for y_from, y_to in mitt.pairwise(y_splits):
        for x in x_splits[1:-1]:
            y = rng.integers(y_from + 1, y_to)
            grid[y, x] = Floor()

    # sample agent and goal positions
    agent_position, goal_position = rng.choice(
        [
            position for position in grid.positions()
            if isinstance(grid[position], Floor)
        ],
        size=2,
        replace=False,
    )
    agent_orientation = rng.choice(list(Orientation))

    grid[goal_position] = Goal()
    agent = Agent(agent_position, agent_orientation)
    return State(grid, agent)
Ejemplo n.º 8
0
def make_5x5_goal_state() -> State:
    """makes a simple 5x5 state with goal object in the middle"""
    grid = Grid(5, 5)
    grid[2, 2] = Goal()
    agent = Agent((0, 0), Orientation.N)
    return State(grid, agent)