def test_step_moving_obstacles( objects: Sequence[Sequence[GridObject]], expected_objects: Sequence[Sequence[GridObject]], ): state = State(Grid.from_objects(objects), MagicMock()) expected_state = State(Grid.from_objects(expected_objects), MagicMock()) action = MagicMock() step_moving_obstacles(state, action) assert state.grid == expected_state.grid
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_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()
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 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 test_raytracing_visibility( objects: Sequence[Sequence[GridObject]], position: Position, expected_int: Sequence[Sequence[int]], ): grid = Grid.from_objects(objects) visibility = raytracing_visibility(grid, position) assert visibility.dtype == bool assert (visibility == expected_int).all()
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
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)