class LearnerTestCase(ut.TestCase): def setUp(self): self.board = Board() self.learner = Learner() def tearDown(self): self.board = None self.learner = None def testMinimax(self): # self.board= Board(new_grid = RED_EASY_LOOKAHEAD) # print(self.learner.getNextMove(self.board)) # self.board= Board(new_grid = START) self.board = Board(new_grid = RED_EASY_LOOKAHEAD_2) best = self.learner.getNextMove(self.board) def testNearestNeighbor(self): weights = [0] * len(self.board.getMoveList(AI_COLOR)) weights[0] = 1 self.learner = Learner(data_points = [(self.board.getArray().tolist(), weights)]) # self.board.getMoveList(AI_COLOR)[0][1].printMove() # self.learner.getNextMove(self.board).printBoard() self.assertEqual(self.learner.getNextMove(self.board), self.board.getMoveList(AI_COLOR)[0][1], \ 'predicted best move does not match') def testUpdateWeights(self): pass
def _getNearestNeighbors(self, current_board): #dist, ind = self._tree.query(current_board.getArray(), k=3) if self._tree is None: return None ind = self._tree.query_radius(current_board.getArray(), r = self._threshold).tolist() ind = ind[0].tolist() if len(ind) > 0: pass # print "neighbors found" #cur_moves = current_board.getMoveList(AI_COLOR) moves = [] weights = [] # print ind for i in ind: _board = Board(new_array = self.state_list[i]) assert(len(_board.getMoveList(AI_COLOR)) == len(self.weights_list[i])) for j, (board, move) in enumerate(_board.getMoveList(AI_COLOR)): # move.printMove() # current_board.printBoard() if current_board.verifyMove(AI_COLOR, move = move): # print "move found" # move.printMove() if move not in moves: moves.append(move) weights.append(self.weights_list[i][j]) else: weights[moves.index(move)] *= self.weights_list[i][j] if len(moves) == 0: # raise Exception() # print "aborted neighbors" return None else: assert(len(moves) == len(weights)) zipped = zip(moves, weights) moves = [mv[0] for mv in zipped if mv[1] >= 1] weights = [mv[1] for mv in zipped if mv[1] >= 1] if len(moves) < 1: return None return np.random.choice(moves, 1, weights)[0]
class BoardTestCase(ut.TestCase): def setUp(self): self.board = Board() def tearDown(self): self.board = None def testGetMovesList(self): def testWithGrid(grid = None, red_c = 7, black_c = 7): self.board = Board(new_grid = grid) self.board.getMoveList(RED) self.board.getMoveList(BLACK) # self.board.printBoard() # print "red moves:", len(self.board.getMoveList(RED)) # print "black moves:", len(self.board.getMoveList(BLACK)) self.assertEqual(len(self.board.getMoveList(RED)), red_c, \ 'incorrect number of RED moves available') self.assertEqual(len(self.board.getMoveList(BLACK)), black_c, \ 'incorrect number of BLACK moves available') # for board, move in self.board.getMoveList(BLACK) + self.board.getMoveList(RED): # print # move.printMove() # board.printBoard() # ---------- My Testing ----------------- # self.board = Board(new_grid = RED_EASY_LOOKAHEAD) # for bd in self.board.getMoveList(RED): # bd[0].printBoard() testWithGrid() testWithGrid(KINGS, 6, 1) testWithGrid(CORNER, 6, 2) self.board = Board(new_grid = NEW_KING) test_board = Board(new_grid = NEW_KING_RESULT) self.assertTrue(any(bd[0] == test_board for bd in self.board.getMoveList(RED))) def testApplyMove(self): for board, move in self.board.getMoveList(BLACK) + self.board.getMoveList(RED): self.assertEqual(board, self.board.applyMove(move), \ 'move_board does not match move applied to self.board') self.board = Board(new_grid = NEW_KING) for board, move in self.board.getMoveList(BLACK) + self.board.getMoveList(RED): self.assertEqual(board, self.board.applyMove(move), \ 'move_board does not match move applied to self.board') self.board = Board(new_grid = BLK_DOUBLE) for board, move in self.board.getMoveList(BLACK) + self.board.getMoveList(RED): self.assertEqual(board, self.board.applyMove(move), \ 'move_board does not match move applied to self.board') def testVerifyMove(self): ''' For board verify that a move is valid and in the set of moves ''' # self.board = Board(new_grid = START_MOVE_B_9_13).getInverse() self.board.getMoveList(RED) self.board.getMoveList(BLACK) self.assertTrue(self.board.verifyMove(BLACK, next_board = Board(new_grid = START_MOVE_B_9_13))) self.assertTrue(self.board.verifyMove(RED, next_board = Board(new_grid = START_MOVE_R_21_17))) def testGetInverse(self): self.assertEqual(self.board.getInverse().getInverse(), self.board, \ 'inverse of inverse of board is not original board')