예제 #1
0
def test_pickup_mechanics_swap():
    grid = Grid(height=3, width=4)
    agent = Agent(position=(1, 2), orientation=Orientation.S)
    item_pos = (2, 2)

    agent.obj = Key(Color.BLUE)
    grid[item_pos] = Key(Color.GREEN)
    state = State(grid, agent)

    next_state = step_with_copy(state, Action.PICK_N_DROP)
    assert state.grid[item_pos] == next_state.agent.obj
    assert state.agent.obj == next_state.grid[item_pos]
예제 #2
0
def _change_agent_object(observation: Observation):
    """changes agent object"""
    observation.agent.obj = (
        Key(Color.RED)
        if isinstance(observation.agent.obj, NoneGridObject)
        else NoneGridObject()
    )
예제 #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)
예제 #4
0
def reset_keydoor(height: int,
                  width: int,
                  *,
                  rng: Optional[rnd.Generator] = None) -> State:
    """An environment with a key and a door

    Creates a height x width (including outer walls) grid with a random column
    of walls. The agent and a yellow key are randomly dropped left of the
    column, while the goal is placed in the bottom right. For example::

        #########
        # @#    #
        #  D    #
        #K #   G#
        #########

    Args:
        height (`int`):
        width (`int`):
        rng: (`Generator, optional`)

    Returns:
        State:
    """
    if height < 3 or width < 5 or (height, width) == (3, 5):
        raise ValueError(
            f'Shape must larger than (3, 5), given {(height, width)}')

    rng = get_gv_rng_if_none(rng)

    state = reset_empty(height, width)
    assert isinstance(state.grid[height - 2, width - 2], Goal)

    # Generate vertical splitting wall
    x_wall = rng.integers(2, width - 3, endpoint=True)
    line_wall = draw_line_vertical(state.grid, range(1, height - 1), x_wall,
                                   Wall)

    # Place yellow, locked door
    pos_wall = rng.choice(line_wall)
    state.grid[pos_wall] = Door(Door.Status.LOCKED, Color.YELLOW)

    # Place yellow key left of wall
    # XXX: potential general function
    y_key = rng.integers(1, height - 2, endpoint=True)
    x_key = rng.integers(1, x_wall - 1, endpoint=True)
    state.grid[y_key, x_key] = Key(Color.YELLOW)

    # Place agent left of wall
    # XXX: potential general function
    y_agent = rng.integers(1, height - 2, endpoint=True)
    x_agent = rng.integers(1, x_wall - 1, endpoint=True)
    state.agent.position = (y_agent, x_agent)  # type: ignore
    state.agent.orientation = rng.choice(list(Orientation))

    return state
예제 #5
0
def test_move_action_can_go_on_non_block_objects():
    grid = Grid(height=3, width=2)
    agent = Agent(position=(2, 1), orientation=Orientation.N)

    grid[2, 0] = Door(Door.Status.OPEN, Color.YELLOW)
    move_agent(agent, grid, action=Action.MOVE_LEFT)
    assert agent.position == (2, 0)

    grid[2, 1] = Key(Color.BLUE)
    move_agent(agent, grid, action=Action.MOVE_RIGHT)
    assert agent.position == (2, 1)
예제 #6
0
def test_grid_subgrid_references():
    key = Key(Color.RED)
    box = Box(key)

    # weird scenario where the key is both in the box and outside the box,
    # only created to test references
    grid = Grid.from_objects([[key, box]])

    subgrid = grid.subgrid(Area((0, 0), (0, 1)))
    key = subgrid[0, 0]
    box = subgrid[0, 1]
    assert box.content is key
예제 #7
0
def test_key_properties():
    """ Basic property tests """

    color = Color.YELLOW
    key = Key(color)

    assert key.transparent
    assert not key.blocks
    assert key.color == color
    assert key.can_be_picked_up
    assert key.state_index == 0

    assert key.can_be_represented_in_state()
    assert key.num_states() == 1
예제 #8
0
def test_actuate_door(
    door_state: Door.Status,
    door_color: Color,
    key_color: Color,
    action: Action,
    expected_state: Door.Status,
):
    # agent facing door
    grid = Grid(2, 1)
    grid[0, 0] = door = Door(door_state, door_color)
    agent = Agent((1, 0), Orientation.N, Key(key_color))
    state = State(grid, agent)

    actuate_door(state, action)
    assert door.state == expected_state

    # agent facing away
    grid = Grid(2, 1)
    grid[0, 0] = door = Door(door_state, door_color)
    agent = Agent((1, 0), Orientation.S, Key(key_color))
    state = State(grid, agent)

    actuate_door(state, action)
    assert door.state == door_state
예제 #9
0
def test_pickup_mechanics_pickup():
    grid = Grid(height=3, width=4)
    agent = Agent(position=(1, 2), orientation=Orientation.S)
    item_pos = (2, 2)

    grid[item_pos] = Key(Color.GREEN)
    state = State(grid, agent)

    # Pick up works
    next_state = step_with_copy(state, Action.PICK_N_DROP)
    assert grid[item_pos] == next_state.agent.obj
    assert isinstance(next_state.grid[item_pos], Floor)

    # Pick up only works with correct action
    next_state = step_with_copy(state, Action.MOVE_LEFT)
    assert grid[item_pos] != next_state.agent.obj
    assert next_state.grid[item_pos] == grid[item_pos]
예제 #10
0
def test_pickup_mechanics_drop():
    grid = Grid(height=3, width=4)
    agent = Agent(position=(1, 2), orientation=Orientation.S)
    item_pos = (2, 2)

    agent.obj = Key(Color.BLUE)
    state = State(grid, agent)

    # Can drop:
    next_state = step_with_copy(state, Action.PICK_N_DROP)
    assert isinstance(next_state.agent.obj, NoneGridObject)
    assert agent.obj == next_state.grid[item_pos]

    # Cannot drop:
    state.grid[item_pos] = Wall()

    next_state = step_with_copy(state, Action.PICK_N_DROP)
    assert isinstance(next_state.grid[item_pos], Wall)
    assert agent.obj == next_state.agent.obj
예제 #11
0

def _change_agent_object(observation: Observation):
    """changes agent object"""
    observation.agent.obj = (
        Key(Color.RED)
        if isinstance(observation.agent.obj, NoneGridObject)
        else NoneGridObject()
    )


@pytest.mark.parametrize(
    'observation',
    [
        Observation(Grid(2, 3), Agent((0, 0), Orientation.N)),
        Observation(Grid(3, 2), Agent((1, 1), Orientation.S, Key(Color.RED))),
    ],
)
def test_observation_eq(observation: Observation):
    other_observation = deepcopy(observation)
    assert observation == other_observation

    other_observation = deepcopy(observation)
    _change_grid(other_observation)
    assert observation != other_observation

    other_observation = deepcopy(observation)
    _change_agent_position(other_observation)
    assert observation != other_observation

    other_observation = deepcopy(observation)
예제 #12
0
def make_key_state(has_key: bool) -> State:
    """makes a simple state with a door"""
    grid = Grid(1, 1)
    obj = Key(Color.RED) if has_key else None
    agent = Agent((0, 0), Orientation.N, obj)
    return State(grid, agent)
예제 #13
0
def _change_agent_object(state: State):
    """changes agent object"""
    state.agent.obj = (Key(Color.RED) if isinstance(
        state.agent.obj, NoneGridObject) else NoneGridObject())
예제 #14
0
    state = State(grid, agent)

    actuate_door(state, action)
    assert door.state == door_state


@pytest.mark.parametrize(
    'content,orientation,action,expected',
    [
        # empty box
        (Floor(), Orientation.N, Action.ACTUATE, True),
        (Floor(), Orientation.S, Action.ACTUATE, False),
        (Floor(), Orientation.N, Action.PICK_N_DROP, False),
        (Floor(), Orientation.S, Action.PICK_N_DROP, False),
        # content is key
        (Key(Color.RED), Orientation.N, Action.ACTUATE, True),
        (Key(Color.RED), Orientation.S, Action.ACTUATE, False),
        (Key(Color.RED), Orientation.N, Action.PICK_N_DROP, False),
        (Key(Color.RED), Orientation.S, Action.PICK_N_DROP, False),
    ],
)
def test_actuate_box(
    content: GridObject,
    orientation: Orientation,
    action: Action,
    expected: bool,
):
    grid = Grid(2, 1)
    grid[0, 0] = box = Box(content)
    agent = Agent((1, 0), orientation)
    state = State(grid, agent)