Example #1
0
    def test_given_same_state_should_return_correct_reward(self):

        curr_state = GridWorldStateBuilder.create_state_with_spec()
        next_state = GridWorldStateBuilder.create_state_with_spec()

        reward = GridWorldRewardFunction().calculate(curr_state, next_state)

        self.assertEqual(self.expected_reward.no_movement, reward)
Example #2
0
    def test_given_player_has_key_and_is_next_to_lock_when_unlock_should_unlock_lock_2(
            self):

        #  test case bug discovered while training

        lock_coords = (9, 1)
        player_start_coords = [(8, 1), (9, 2), (9, 0)]
        player_position_to_lock = ["North", "East", "West"]
        grid_shape = (10, 10)

        for i in range(len(player_position_to_lock)):
            with self.subTest(
                    player_position_to_lock=player_position_to_lock[i]):

                player_coords = player_start_coords[i]

                state = GridWorldStateBuilder.create_state_with_spec(
                    player_coords=player_coords,
                    lock_coords=lock_coords,
                    key_coords=None,
                    shape=grid_shape)
                actual_state = GridWorldDynamics(state).step(
                    GridWorldAction.UNLOCK_LOCK)

                expected_state = state.copy(lock=None)

                self.assertEqual(expected_state, actual_state)
                self.assertTrue(actual_state.lock_is_unlocked(),
                                "lock should be unlocked")
Example #3
0
    def test_given_player_is_next_to_key_when_pick_up_key_should_pick_up_key_2(
            self):

        # test generated from observed bug when training

        lock_coords = (1, 1)
        key_coords = (7, 5)
        pit_start = (4, 4)
        pit_end = (4, 6)
        player_start_coords = [(6, 5), (7, 6), (8, 5), (7, 4)]
        player_position_to_key = ["North", "East", "South", "West"]

        for i in range(len(player_position_to_key)):
            with self.subTest(
                    player_position_to_key=player_position_to_key[i]):
                player_coords = player_start_coords[i]

                state = GridWorldStateBuilder.create_state_with_spec(
                    player_coords=player_coords,
                    key_coords=key_coords,
                    lock_coords=lock_coords,
                    pit_start_coords=pit_start,
                    pit_end_coords=pit_end)
                action = 1.0 * GridWorldAction.PICK_UP_KEY  # not the same pointer ref as the action constant
                actual_state = GridWorldDynamics(state).step(action)

                expected_state = state.copy(key=None, has_key=1)
                self.assertEqual(expected_state, actual_state)
Example #4
0
    def test_given_player_has_key_and_is_next_to_lock_when_unlock_should_unlock_lock(
            self):

        lock_coords = (1, 1)
        player_start_coords = [(0, 1), (1, 2), (2, 1), (1, 0)]
        player_position_to_lock = ["North", "East", "South", "West"]

        for i in range(len(player_position_to_lock)):
            with self.subTest(
                    player_position_to_lock=player_position_to_lock[i]):

                player_coords = player_start_coords[i]

                state = GridWorldStateBuilder.create_state_with_spec(
                    player_coords=player_coords,
                    lock_coords=lock_coords,
                    key_coords=None)
                actual_state = GridWorldDynamics(state).step(
                    GridWorldAction.UNLOCK_LOCK)

                expected_state = state.copy(lock=None)

                self.assertEqual(expected_state, actual_state)
                self.assertTrue(actual_state.lock_is_unlocked(),
                                "lock should be unlocked")
Example #5
0
    def test_given_no_lock_should_correctly_create_state(self):

        lock_coords = None

        state = GridWorldStateBuilder.create_state_with_spec(lock_coords=lock_coords)

        self.assertEqual(lock_coords, state.lock, "lock coords not equal")
Example #6
0
    def test_when_player_moves_into_pit_should_move_player_into_pit(self):

        shape = (10, 10)
        pit_start_coords = (4, 2)
        pit_end_coords = (4, 7)
        player_start_coords = [(5, 4), (4, 1), (3, 2), (4, 8)]
        expected_player_coords_list = [(4, 4), (4, 2), (4, 2), (4, 7)]

        for i in range(len(self.directional_actions)):
            with self.subTest(action=self.action_names[i]):

                player_coords = player_start_coords[i]
                action = self.directional_actions[i]
                expected_player_coords = expected_player_coords_list[i]

                state = GridWorldStateBuilder.create_state_with_spec(
                    shape=shape,
                    player_coords=player_coords,
                    pit_start_coords=pit_start_coords,
                    pit_end_coords=pit_end_coords)

                new_state = GridWorldDynamics(state).step(action)

                expected_state = state.copy(player=expected_player_coords)
                self.assertEqual(expected_state, new_state)
                self.assertTrue(new_state.is_in_pit())
    def test_lock_coords(self):

        lock_coords = (5, 4)

        state = GridWorldStateBuilder.create_state_with_spec(lock_coords=lock_coords)
        grid = GridStateVisualizer(state).grid

        self.assertEqual(GridStateVisualizer.LOCK, grid[lock_coords])
    def test_key_coords(self):

        key_coords = (5, 4)

        state = GridWorldStateBuilder.create_state_with_spec(key_coords=key_coords)
        grid = GridStateVisualizer(state).grid

        self.assertEqual(GridStateVisualizer.KEY, grid[key_coords])
    def test_state_size(self):

        shape = (20, 25)

        state = GridWorldStateBuilder.create_state_with_spec(shape=shape)
        grid = GridStateVisualizer(state).grid

        self.assertEqual(shape, grid.shape)
    def test_no_key_coords(self):

        key_coords = None

        state = GridWorldStateBuilder.create_state_with_spec(key_coords=key_coords)
        grid = GridStateVisualizer(state).grid

        self.assertFalse(np.any(grid == GridStateVisualizer.KEY), "key should not be on grid")
    def test_no_lock_coords(self):

        lock_coords = None

        state = GridWorldStateBuilder.create_state_with_spec(lock_coords=lock_coords)
        grid = GridStateVisualizer(state).grid

        self.assertFalse(np.any(grid == GridStateVisualizer.LOCK), "lock should not be on grid")
Example #12
0
    def test_new_state_should_be_new_object(self):

        player_coords = (0, 0)
        state = GridWorldStateBuilder.create_state_with_spec(
            player_coords=player_coords)

        new_state = GridWorldDynamics(state).step(GridWorldAction.NORTH)

        self.assertFalse(state is new_state)
Example #13
0
    def test_given_no_key_should_correctly_create_state(self):

        key_coords = None

        has_key = 1

        state = GridWorldStateBuilder.create_state_with_spec(key_coords=key_coords)

        self.assertEqual(key_coords, state.key, "key coords not equal")
        self.assertEqual(has_key, state.has_key, "has key not equal")
    def test_no_pit_coords(self):

        pit_start_coords = None
        pit_end_coords = None

        state = GridWorldStateBuilder.create_state_with_spec(
            pit_start_coords=pit_start_coords, pit_end_coords=pit_end_coords)
        grid = GridStateVisualizer(state).grid

        self.assertFalse(np.any(grid == GridStateVisualizer.PIT), "pit should not be on grid")
    def test_pit_coords(self):

        pit_start = (4, 2)
        pit_end = (4, 7)

        state = GridWorldStateBuilder.create_state_with_spec(pit_start_coords=pit_start, pit_end_coords=pit_end)
        grid = GridStateVisualizer(state).grid

        pit_state = grid[pit_start[0]:pit_end[0]+1, pit_start[1]:pit_end[1]+1]

        self.assertTrue(np.all(pit_state == GridStateVisualizer.PIT))
    def test_player_coords(self):

        player_coords = (2, 3)

        state = GridWorldStateBuilder.create_state_with_spec(player_coords=player_coords)
        grid = GridStateVisualizer(state).grid

        self.assertEqual(GridStateVisualizer.PLAYER, grid[player_coords])

        # alternative notations
        self.assertEqual(GridStateVisualizer.PLAYER, grid[player_coords[0], player_coords[1]])
        self.assertEqual(GridStateVisualizer.PLAYER, grid[player_coords[0]][player_coords[1]])
Example #17
0
    def test_given_player_is_not_near_a_key_when_pick_up_key_should_remain_in_same_state(
            self):

        shape = (10, 10)
        player_coords = (0, 0)
        key_coords = (9, 9)

        state = GridWorldStateBuilder.create_state_with_spec(
            shape=shape, player_coords=player_coords, key_coords=key_coords)
        new_state = GridWorldDynamics(state).step(GridWorldAction.PICK_UP_KEY)

        self.assertEqual(state, new_state)
Example #18
0
    def test_given_player_picked_up_key_should_return_correct_reward(self):

        player_coords = (0, 0)
        key_coords = (1, 0)

        curr_state = GridWorldStateBuilder.create_state_with_spec(
            player_coords=player_coords, key_coords=key_coords)
        next_state = GridWorldDynamics(curr_state).step(
            GridWorldAction.PICK_UP_KEY)

        reward = GridWorldRewardFunction().calculate(curr_state, next_state)

        self.assertEqual(self.expected_reward.player_picked_up_key, reward)
    def test_beacons_for_pit_in_bound(self):

        # target task desc in Auto Sequencing..., Narverkar 2017

        pit_start = (4, 2)
        pit_end = (4, 7)

        state = GridWorldStateBuilder.create_state_with_spec(pit_start_coords=pit_start, pit_end_coords=pit_end)
        grid = GridStateVisualizer(state).grid

        self.assertEqual(GridStateVisualizer.BEACON, grid[3, 1])  # top left
        self.assertEqual(GridStateVisualizer.BEACON, grid[3, 8])  # bottom left
        self.assertEqual(GridStateVisualizer.BEACON, grid[5, 1])  # top right
        self.assertEqual(GridStateVisualizer.BEACON, grid[5, 8])  # bottom right
    def test_beacons_for_pit_on_top_right_state_bound(self):

        shape = (10, 10)
        pit_start = (0, 7)
        pit_end = (3, 9)

        state = GridWorldStateBuilder.create_state_with_spec(
            shape=shape, pit_start_coords=pit_start, pit_end_coords=pit_end)
        grid = GridStateVisualizer(state).grid

        self.assertEqual(GridStateVisualizer.BEACON, grid[4, 6])  # bottom left

        grid[4, 6] = GridStateVisualizer.EMPTY
        self.assertFalse(np.any(grid == GridStateVisualizer.BEACON), "additional beacons should not be on grid")
Example #21
0
    def test_given_player_has_key_and_if_not_next_to_lock_when_unlock_should_remain_in_same_state(
            self):

        shape = (10, 10)
        player_coords = (0, 0)
        lock_coords = (9, 9)

        state = GridWorldStateBuilder.create_state_with_spec(
            shape=shape,
            player_coords=player_coords,
            lock_coords=lock_coords,
            key_coords=None)
        new_state = GridWorldDynamics(state).step(GridWorldAction.UNLOCK_LOCK)

        self.assertEqual(state, new_state)
Example #22
0
    def test_given_pit_on_ne_bound_should_correctly_create_state(self):

        shape = (20, 20)
        pit_start_coords = (0, 17)
        pit_end_coords = (3, 19)

        sw_beacon = (4, 16)

        state = GridWorldStateBuilder.create_state_with_spec(
            shape=shape, pit_start_coords=pit_start_coords, pit_end_coords=pit_end_coords)

        self.assertEqual(None, state.nw_beacon, "nw beacon coords not equal")
        self.assertEqual(None, state.ne_beacon, "ne beacon coords not equal")
        self.assertEqual(sw_beacon, state.sw_beacon, "sw beacon coords not equal")
        self.assertEqual(None, state.se_beacon, "se beacon coords not equal")
Example #23
0
    def test_given_player_moved_into_empty_space_should_return_correct_reward(
            self):

        player_coords = (0, 0)

        curr_state = GridWorldStateBuilder.create_state_with_spec(
            player_coords=player_coords)
        next_state = GridWorldDynamics(curr_state).step(GridWorldAction.EAST)

        reward = GridWorldRewardFunction().calculate(curr_state, next_state)

        self.assertFalse(curr_state == next_state,
                         "states should not be equal")
        self.assertEqual(self.expected_reward.player_moved_into_empty_space,
                         reward)
Example #24
0
    def test_given_player_unlocked_lock_should_return_correct_reward(self):

        player_coords = (0, 0)
        key_coords = None
        lock_coords = (1, 0)

        curr_state = GridWorldStateBuilder.create_state_with_spec(
            player_coords=player_coords,
            key_coords=key_coords,
            lock_coords=lock_coords)
        next_state = GridWorldDynamics(curr_state).step(
            GridWorldAction.UNLOCK_LOCK)

        reward = GridWorldRewardFunction().calculate(curr_state, next_state)

        self.assertEqual(self.expected_reward.player_unlocked_lock, reward)
Example #25
0
    def test_given_pit_on_south_bound_should_correctly_create_state(self):

        shape = (20, 20)
        pit_start_coords = (17, 3)
        pit_end_coords = (19, 6)

        nw_beacon = (16, 2)
        ne_beacon = (16, 7)

        state = GridWorldStateBuilder.create_state_with_spec(
            shape=shape, pit_start_coords=pit_start_coords, pit_end_coords=pit_end_coords)

        self.assertEqual(nw_beacon, state.nw_beacon, "nw beacon coords not equal")
        self.assertEqual(ne_beacon, state.ne_beacon, "ne beacon coords not equal")
        self.assertEqual(None, state.sw_beacon, "sw beacon coords not equal")
        self.assertEqual(None, state.se_beacon, "se beacon coords not equal")
Example #26
0
    def test_when_player_moves_into_boundary_should_remain_in_same_state(self):

        grid_shape = (10, 10)
        player_start_coords = [(0, 0), (9, 9), (9, 9), (0, 0)]

        for i in range(len(self.directional_actions)):
            with self.subTest(action=self.action_names[i]):

                player_coords = player_start_coords[i]
                action = self.directional_actions[i]

                state = GridWorldStateBuilder.create_state_with_spec(
                    shape=grid_shape, player_coords=player_coords)
                new_state = GridWorldDynamics(state).step(action)

                self.assertEqual(state, new_state)
Example #27
0
    def test_given_no_key_and_no_pit_should_not_throw_error(self):
        
        shape = (10, 10)
        player_coords = (1, 4)
        key_coords = None
        pit_start_coords = None
        pit_end_coords = None

        state = GridWorldStateBuilder.create_state_with_spec(shape=shape, player_coords=player_coords, 
                                                             key_coords=key_coords, pit_start_coords=pit_start_coords, 
                                                             pit_end_coords=pit_end_coords)

        self.assertEqual(shape, state.grid_shape, "shape not equal")
        self.assertEqual(player_coords, state.player, "player coords not equal")
        self.assertEqual(key_coords, state.key, "key coords not equal")
        self.assertEqual(pit_start_coords, state.pit_start, "pit start key not equal")
        self.assertEqual(pit_end_coords, state.pit_end, "pit end key not equal")
Example #28
0
    def test_given_no_lock_and_no_pit_should_not_throw_error(self):
        
        shape = (10, 10)
        player_coords = (1, 4)
        lock_coords = None
        pit_start_coords = None
        pit_end_coords = None

        state = GridWorldStateBuilder.create_state_with_spec(shape=shape, player_coords=player_coords,
                                                             lock_coords=lock_coords, pit_start_coords=pit_start_coords,
                                                             pit_end_coords=pit_end_coords)

        self.assertEqual(shape, state.grid_shape, "shape not equal")
        self.assertEqual(player_coords, state.player, "player coords not equal")
        self.assertEqual(lock_coords, state.lock, "lock coords not equal")
        self.assertEqual(pit_start_coords, state.pit_start, "pit start lock not equal")
        self.assertEqual(pit_end_coords, state.pit_end, "pit end lock not equal")
Example #29
0
    def test_when_player_moves_into_lock_should_remain_in_same_state(self):

        player_start_coords = [(1, 0), (0, 0), (0, 0), (0, 1)]
        lock_start_coords = [(0, 0), (0, 1), (1, 0), (0, 0)]

        for i in range(len(self.directional_actions)):
            with self.subTest(action=self.action_names[i]):

                player_coords = player_start_coords[i]
                action = self.directional_actions[i]
                lock_coords = lock_start_coords[i]

                state = GridWorldStateBuilder.create_state_with_spec(
                    player_coords=player_coords, lock_coords=lock_coords)
                new_state = GridWorldDynamics(state).step(action)

                self.assertEqual(state, new_state)
    def test_beacons_for_pit_on_bottom_state_bound(self):

        shape = (10, 10)
        pit_start = (7, 3)
        pit_end = (9, 5)
        key_coords = (1, 2)

        state = GridWorldStateBuilder.create_state_with_spec(
            key_coords=key_coords, shape=shape, pit_start_coords=pit_start, pit_end_coords=pit_end)
        grid = GridStateVisualizer(state).grid

        self.assertEqual(GridStateVisualizer.BEACON, grid[6, 2])  # top left
        self.assertEqual(GridStateVisualizer.BEACON, grid[6, 6])  # top right

        grid[6, 2] = GridStateVisualizer.EMPTY
        grid[6, 6] = GridStateVisualizer.EMPTY
        self.assertFalse(np.any(grid == GridStateVisualizer.BEACON), "additional beacons should not be on grid")