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_door_locked_door_properties(): """ Basic property tests """ color = Color.NONE locked_door = Door(Door.Status.LOCKED, color) assert not locked_door.transparent assert locked_door.color == color assert not locked_door.can_be_picked_up assert locked_door.state_index == Door.Status.LOCKED.value assert not locked_door.is_open assert locked_door.locked assert locked_door.blocks assert locked_door.can_be_represented_in_state() assert locked_door.render_as_char() == 'D'
def test_door_open_door_properties(): """ Basic property tests """ color = Color.GREEN open_door = Door(Door.Status.OPEN, color) assert open_door.transparent assert open_door.color == color assert not open_door.can_be_picked_up assert open_door.state_index == Door.Status.OPEN.value assert open_door.is_open assert not open_door.locked assert not open_door.blocks assert open_door.can_be_represented_in_state() assert open_door.render_as_char() == '_' assert open_door.num_states() == 3
def test_move_action_blocked_by_grid_object(): """ Puts an object on (2,0) and try to move there""" grid = Grid(height=3, width=2) agent = Agent(position=(2, 1), orientation=Orientation.N) grid[2, 0] = Door(Door.Status.CLOSED, Color.YELLOW) move_agent(agent, grid, action=Action.MOVE_LEFT) assert agent.position == (2, 1)
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
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)
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
def test_pickup_mechanics_nothing_to_pickup(): grid = Grid(height=3, width=4) agent = Agent(position=(1, 2), orientation=Orientation.S) item_pos = (2, 2) state = State(grid, agent) # Cannot pickup floor next_state = step_with_copy(state, Action.PICK_N_DROP) assert state == next_state # Cannot pickup door grid[item_pos] = Door(Door.Status.CLOSED, Color.GREEN) next_state = step_with_copy(state, Action.PICK_N_DROP) assert state == next_state assert isinstance(next_state.grid[item_pos], Door)
def make_door_state(door_status: Door.Status) -> State: """makes a simple state with a door""" grid = Grid(2, 1) grid[0, 0] = Door(door_status, Color.RED) agent = Agent((1, 0), Orientation.N) return State(grid, agent)