def debug_game(): mind = PExpMind_v3(size=9, init=False, search_params=None) # mind.load_net('../../models/voldmaster_' + str(0)) mind.load_net('../../models/v3_0') cache = BitBoardCache("../cache/9-magics", size=9, win_chain_length=5, force_build_win_checks=False) board = BitBoard(cache, size=9, win_chain_length=5) moves = [49, 40, 50, 29, 51, 52, 47, 48, 41, 31, 32, 23, 39, 68, 42, 58, 38, 33, 65, 56, 57, 73, 67, 37, 64, 66, 43, 59, 21, 22, 13, 25, 24, 44, 30, 16, 3, 12, 2, 1, 5, 6, 14, 17, 35, 7, 71, 28, 19, 34, 60, 78, 77, 62, 46, 54, 55, 36, 20, 45, 63, 18, 27, 53, 15, 9, 0, 69, 10, 75, 74, 8, 79, 72, 80] for move in moves: board.move(move) print(board) print(board) searcher = PEvenSearch(board, mind.policy_est, mind.value_est, search_params={ 'max_iterations': 10, 'min_child_p': -6, 'p_batch_size': 1 << 10, 'q_fraction': 3 }) searcher.run()
def test_move_order_search(self): mind = PExpMind_v3(size=9, init=False, search_params=None) mind.load_net('../../models/voldmaster_' + str(0)) cache = BitBoardCache("../cache/9-magics", size=9, win_chain_length=5, force_build_win_checks=False) board = BitBoard(cache, size=9, win_chain_length=5) moves = [(7, 1), (0, 1), (7, 3), (1, 7), (7, 4), (3, 1), (3, 2), (4, 1), (3, 4), (6, 1), (6, 3), (6, 2), (5, 2), (8, 7), (7, 7), (6, 8), (5, 8), (1, 6), (2, 7)] for move in moves: board.move_coord(*move) print(board) searcher = PEvenSearch(board, mind.policy_est, mind.value_est, search_params={ 'max_iterations': 10, 'min_child_p': -7, 'p_batch_size': 1 << 10, 'q_fraction': 1 }) searcher.run(3) self.assertEqual(searcher.get_pv().calculate_pv_order(), [19, 46, 10])
def test_game_closure(self): mind = PExpMind_v3(size=9, init=False) mind.load_net('../../models/voldmaster_' + str(0)) cache = BitBoardCache("../cache/9-magics", size=9, win_chain_length=5, force_build_win_checks=False) board = BitBoard(cache, size=9, win_chain_length=5) board.move_coord(1, 0) board.move_coord(1, 1) board.move_coord(2, 0) board.move_coord(2, 1) board.move_coord(3, 0) board.move_coord(3, 1) board.move_coord(4, 0) board.move_coord(4, 1) searcher = PEvenSearch(board, mind.policy_est, mind.value_est, max_iterations=4, p_batch_size=1024, verbose=True, validations=True) searcher.run(4) pv = searcher.get_pv() self.assertEqual(len(pv.get_move_chain()), 1) self.assertEqual(pv.get_q(), PExpNodeV3.MAX_Q)
def test_search(self): mind = PExpMind_v3(size=9, init=False, verbose=True, search_params={ 'max_iterations': 10, 'min_child_p': -7, 'p_batch_size': 1 << 12, 'q_fraction': 1 }) mind.load_net('../../models/voldmaster_' + str(0)) cache = BitBoardCache("../cache/9-magics", size=9, win_chain_length=5, force_build_win_checks=False) board = BitBoard(cache, size=9, win_chain_length=5) moves = [(6, 0), (7, 0), (3, 1), (2, 4), (0, 3), (3, 4), (2, 6), (4, 4), (3, 5), (2, 5), (4, 5), (2, 7), (5, 5)] for move in moves: board.move_coord(*move) print(board) searcher = PEvenSearch(board, mind.policy_est, mind.value_est) searcher.run(num_iterations=5) pv = searcher.get_pv() self.assertEqual(len(pv.get_move_chain()), 3) self.assertEqual(pv.get_q(), PExpNodeV3.MIN_Q)
def play_oldmaster(): mind = PExpMind_v3(size=9, init=False, search_params={ 'max_iterations': 3, 'min_child_p': -7, 'p_batch_size': 1 << 10, 'q_fraction': 1 }) mind.load_net('../../models/voldmaster_' + str(0)) return mind
def make_v3(min_child_p=-7, size=9): mind = PExpMind_v3(size=size, init=False, search_params={ 'max_iterations': 10, 'min_child_p': min_child_p, 'p_batch_size': 1 << 10, 'num_pv_expand': 25, 'q_fraction': 1 }) mind.load_net('../../models/v3_0_1') return mind
def test_even_search(self): mind = PExpMind_v3(size=9, init=False, search_params=None) mind.load_net('../../models/voldmaster_' + str(0)) board = Board(size=9, win_chain_length=5) searcher = PEvenSearch(board, mind.policy_est, mind.value_est, max_iterations=2, p_batch_size=1024, verbose=True, validations=True) searcher.p_expand() self.assertGreater(len(searcher.expandable_nodes), 0) for node in searcher.expandable_nodes: self.assertEqual(list(node.get_parents())[0], searcher.root_node) self.assertLess(node.log_total_p, 0) searcher.q_eval() self.assertNotEqual(searcher.root_node.get_principal_variation(), None)
def test_able_to_draw(self): mind = PExpMind_v3(size=9, init=False, search_params=None) mind.load_net('../../models/voldmaster_' + str(0)) cache = BitBoardCache("../cache/9-magics", size=9, win_chain_length=5, force_build_win_checks=False) board = BitBoard(cache, size=9, win_chain_length=5) # uninteresting moves for P moves = [ 37, 45, 74, 50, 47, 29, 31, 39, 49, 58, 48, 41, 40, 32, 22, 13, 23, 59, 68, 21, 60, 36, 5, 42, 34, 20, 67, 57, 56, 65, 52, 44, 38, 12, 30, 14, 11, 15, 16, 43, 46, 54, 66, 18, 27, 72, 63, 28, 4, 69, 76, 55, 75, 77, 25, 7, 24, 26, 53, 61, 78, 73, 19, 8, 64, 35, 62, 79, 51, 6, 17, 3 ] self.just_no_crash_from_position(mind, board, moves, num_iterations=2) # last move on the board moves = [ 70, 69, 10, 42, 43, 25, 50, 40, 49, 48, 32, 41, 39, 58, 38, 31, 51, 22, 13, 52, 21, 29, 67, 59, 56, 37, 61, 30, 28, 47, 12, 14, 11, 9, 33, 3, 23, 53, 27, 24, 6, 46, 57, 19, 55, 2, 44, 34, 4, 74, 7, 36, 5, 8, 54, 62, 16, 65, 66, 78, 75, 68, 1, 15, 76, 77, 26, 35, 45, 63, 18, 60, 79, 20, 0, 64, 80, 72, 73, 17 ] self.just_no_crash_from_position(mind, board, moves, num_iterations=2) # instant win moves = [23, 66, 44, 80, 35, 12, 33, 30, 31, 39, 40, 48, 75] self.just_no_crash_from_position(mind, board, moves, num_iterations=2) # no leaf nodes moves = [41, 8, 2, 29, 73, 31, 49, 40, 33, 50, 30, 39, 57, 22] self.just_no_crash_from_position(mind, board, moves, num_iterations=2)
def test_pnet(self): mind = PExpMind_v3(size=9, init=False, search_params=None) mind.load_net('../../models/v3_' + str(0)) cache = BitBoardCache("../cache/9-magics", size=9, win_chain_length=5, force_build_win_checks=False) board = BitBoard(cache, size=9, win_chain_length=5) fboard = FeatureBoard_v1_1(board) for move in [76, 65, 58, 27, 40]: board.move(move) fboard.move(move) print(board) mind.make_move(board) predictions = mind.policy_est.predict( [np.array([fboard.get_q_features()])])[0] self.assertEqual(np.argmax(predictions), 49) print(sorted(enumerate(predictions), key=lambda x: x[1], reverse=True))
def play(): SIZE = 9 mind = PExpMind_v3(size=9, init=False, search_params={ 'max_iterations': 10, 'min_child_p': -7, 'p_batch_size': 1 << 10, 'num_pv_expand': 25, 'q_fraction': 1 }) mind.load_net('../../models/v3_' + str(0)) cache = BitBoardCache("../cache/9-magics", size=9, win_chain_length=5, force_build_win_checks=False) board = BitBoard(cache, size=9, win_chain_length=5) # randomize the board a bit for j in range(random.randint(0, int(SIZE * 2))): board.make_random_move() # board.move_coord(4, 4) print(board.pprint()) while True: if board.get_player_to_move() == Player.FIRST: inp = input("Input your move (i.e. \"3 5\"): ") if len(inp.split(' ')) != 2: print('Incorrect number of coordinates, please try again!') continue x, y = inp.split(' ') try: x = int(x) y = int(y) except: print('Please input Numbers!') continue if x < 0 or x >= SIZE or y < 0 or y >= SIZE: print('Out of bounds!') continue index = board._transformer.coordinate_to_index(x, y) if not board.is_move_available(index): print('Invalid Move!') continue result = board.move_coord(x, y) print(board) else: print('Computer is thinking...') print(board._ops) move, current_q, best_q = mind.make_move(board) print(" ") print(move, 'Q:', best_q) # if best_q > PExpNode.MAX_MODEL_Q : # print('Computer Resigns!') # break board.move(move) print(board) if board.game_over(): if board.get_winning_player() == Player.FIRST: print('COMPUTER WINS!') elif board.get_winning_player() == Player.SECOND: print('YOU WIN!') print("DRAW!") break