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 test_wall_properties(): """ Basic property tests """ wall = Wall() assert not wall.transparent assert wall.blocks assert wall.color == Color.NONE assert not wall.can_be_picked_up assert wall.state_index == 0 assert wall.can_be_represented_in_state() assert wall.render_as_char() == '#' assert wall.num_states() == 1
def str_wall_row(grid_width: int) -> str: """Returns string representation of a row of wall Args: grid_width (`int`): width of grid """ return str_render_object(Wall()) * (grid_width + 2)
def creeping_walls( state: State, action: Action, # pylint: disable=unused-argument *, rng: Optional[rnd.Generator] = None, ) -> None: """randomly chooses a Floor tile and turns it into a Wall tile""" rng = get_gv_rng_if_none(rng) # necessary to use rng object! # all positions associated with a Floor object floor_positions = [ position for position in state.grid.positions() if isinstance(state.grid[position], Floor) ] try: # floor_positions could be an empty list position = rng.choice(floor_positions) except ValueError: # there are no floor positions pass else: # if we were able to sample a position, change the corresponding Floor # into a Wall state.grid[position] = Wall()
def test_minigrid_observation_partially_observable( agent: Agent, expected_objects: Sequence[Sequence[GridObject]] ): grid = Grid.from_objects( [ [Floor(), Floor(), Floor()], [Wall(), Wall(), Wall()], [Floor(), Floor(), Floor()], ] ) state = State(grid, agent) observation_space = ObservationSpace(Shape(6, 5), [], []) observation = minigrid_observation( state, observation_space=observation_space ) expected = Grid.from_objects(expected_objects) assert observation.grid == expected
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)
def test_state_hash(): wall_position = (0, 0) agent_position = (0, 1) agent_orientation = Orientation.N agent_object = None grid = Grid(2, 2) grid[wall_position] = Wall() agent = Agent(agent_position, agent_orientation, agent_object) state = State(grid, agent) hash(state)
def test_observation_hash(): wall_position = (0, 0) agent_position = (0, 1) agent_orientation = Orientation.N agent_object = None grid = Grid(2, 2) grid[wall_position] = Wall() agent = Agent(agent_position, agent_orientation, agent_object) observation = Observation(grid, agent) hash(observation)
def test_minigrid_observation(agent: Agent): grid = Grid(10, 10) grid[5, 5] = Wall() state = State(grid, agent) observation_space = ObservationSpace(Shape(6, 5), [], []) observation = minigrid_observation( state, observation_space=observation_space ) assert observation.agent.position == (5, 2) assert observation.agent.orientation == Orientation.N assert observation.agent.obj == state.agent.obj assert observation.grid.shape == Shape(6, 5) assert isinstance(observation.grid[3, 0], Wall)
def test_grid_subgrid(area: Area, expected_objects: Sequence[Sequence[GridObject]]): # checkerboard pattern grid = Grid.from_objects([ [Wall(), Floor(), Wall(), Floor()], [Floor(), Wall(), Floor(), Wall()], [Wall(), Floor(), Wall(), Floor()], ]) expected = Grid.from_objects(expected_objects) assert grid.subgrid(area) == expected
def test_grid_change_orientation( orientation: Orientation, expected_objects: Sequence[Sequence[GridObject]]): # checkerboard pattern grid = Grid.from_objects([ [Wall(), Floor(), Wall(), Floor()], [Floor(), Wall(), Floor(), Wall()], [Wall(), Floor(), Wall(), Floor()], ]) expected = Grid.from_objects(expected_objects) assert grid.change_orientation(orientation) == expected
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
def _change_grid(observation: Observation): """changes one object in the grid""" observation.grid[0, 0] = ( Wall() if isinstance(observation.grid[0, 0], Floor) else Floor() )
@pytest.mark.parametrize( 'area,expected_objects', [ ( Area((-1, 3), (-1, 4)), [ [Hidden(), Hidden(), Hidden(), Hidden(), Hidden(), Hidden()], [Hidden(), Wall(), Floor(), Wall(), Floor(), Hidden()], [Hidden(), Floor(), Wall(), Floor(), Wall(), Hidden()], [Hidden(), Wall(), Floor(), Wall(), Floor(), Hidden()], [Hidden(), Hidden(), Hidden(), Hidden(), Hidden(),
assert observation.agent.obj == state.agent.obj assert observation.grid.shape == Shape(6, 5) assert isinstance(observation.grid[3, 0], Wall) @pytest.mark.parametrize( 'agent,expected_objects', [ ( Agent((2, 1), Orientation.N), [ [Hidden(), Hidden(), Hidden(), Hidden(), Hidden()], [Hidden(), Hidden(), Hidden(), Hidden(), Hidden()], [Hidden(), Hidden(), Hidden(), Hidden(), Hidden()], [Hidden(), Hidden(), Hidden(), Hidden(), Hidden()], [Hidden(), Wall(), Wall(), Wall(), Hidden()], [Hidden(), Floor(), Floor(), Floor(), Hidden()], ], ), ( Agent((0, 1), Orientation.S), [ [Hidden(), Hidden(), Hidden(), Hidden(), Hidden()], [Hidden(), Hidden(), Hidden(), Hidden(), Hidden()], [Hidden(), Hidden(), Hidden(), Hidden(), Hidden()], [Hidden(), Hidden(), Hidden(), Hidden(), Hidden()], [Hidden(), Wall(), Wall(), Wall(), Hidden()], [Hidden(), Floor(), Floor(), Floor(), Hidden()], ], ), (
def make_wall_state(orientation: Orientation = Orientation.N) -> State: """makes a simple state with goal object and agent on or off the goal""" grid = Grid(2, 1) grid[0, 0] = Wall() agent = Agent((1, 0), orientation) return State(grid, agent)
) from gym_gridverse.geometry import Position from gym_gridverse.grid import Grid from gym_gridverse.grid_object import Floor, GridObject, Wall @pytest.mark.parametrize( 'objects', [ [ [Floor(), Floor(), Floor()], [Floor(), Floor(), Floor()], [Floor(), Floor(), Floor()], ], [ [Wall(), Wall(), Wall()], [Wall(), Wall(), Wall()], [Wall(), Wall(), Wall()], ], ], ) def test_full_visibility(objects: Sequence[Sequence[GridObject]]): grid = Grid.from_objects(objects) for position in grid.positions(): visibility = full_visibility(grid, position) assert visibility.dtype == bool assert visibility.all() @pytest.mark.parametrize(
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)
def _change_grid(state: State): """changes one object in the grid""" state.grid[0, 0] = (Wall() if isinstance(state.grid[0, 0], Floor) else Floor())