def test_pick_moves(self): player = initialize_basic_player() root = player.root root.child_N[coords.to_flat(utils_test.BOARD_SIZE, (2, 0))] = 10 root.child_N[coords.to_flat(utils_test.BOARD_SIZE, (1, 0))] = 5 root.child_N[coords.to_flat(utils_test.BOARD_SIZE, (3, 0))] = 1 # move 81, or 361, or... Endgame. root.position.n = utils_test.BOARD_SIZE ** 2 # Assert we're picking deterministically self.assertTrue(root.position.n > player.temp_threshold) move = player.pick_move() self.assertEqual(move, (2, 0)) # But if we're in the early part of the game, pick randomly root.position.n = 3 self.assertFalse(player.root.position.n > player.temp_threshold) with unittest.mock.patch('random.random', lambda: .5): move = player.pick_move() self.assertEqual(move, (2, 0)) with unittest.mock.patch('random.random', lambda: .99): move = player.pick_move() self.assertEqual(move, (3, 0))
def test_pick_moves(self): player = initialize_basic_player() root = player.root root.child_N[coords.to_flat((2, 0))] = 10 root.child_N[coords.to_flat((1, 0))] = 5 root.child_N[coords.to_flat((3, 0))] = 1 root.position.n = go.N ** 2 # move 81, or 361, or... Endgame. # Assert we're picking deterministically self.assertTrue(root.position.n > player.temp_threshold) move = player.pick_move() self.assertEqual(move, (2, 0)) # But if we're in the early part of the game, pick randomly root.position.n = 3 self.assertFalse(player.root.position.n > player.temp_threshold) with mock.patch('random.random', lambda: .5): move = player.pick_move() self.assertEqual(move, (2, 0)) with mock.patch('random.random', lambda: .99): move = player.pick_move() self.assertEqual(move, (3, 0))
def test_make_dataset_from_sgf(self): with tempfile.NamedTemporaryFile() as sgf_file, \ tempfile.NamedTemporaryFile() as record_file: sgf_file.write(TEST_SGF.encode('utf8')) sgf_file.seek(0) preprocessing.make_dataset_from_sgf( utils_test.BOARD_SIZE, sgf_file.name, record_file.name) recovered_data = self.extract_data(record_file.name) start_pos = go.Position(utils_test.BOARD_SIZE) first_move = coords.from_sgf('fd') next_pos = start_pos.play_move(first_move) second_move = coords.from_sgf('cf') expected_data = [ ( features.extract_features(utils_test.BOARD_SIZE, start_pos), preprocessing._one_hot(utils_test.BOARD_SIZE, coords.to_flat( utils_test.BOARD_SIZE, first_move)), -1 ), ( features.extract_features(utils_test.BOARD_SIZE, next_pos), preprocessing._one_hot(utils_test.BOARD_SIZE, coords.to_flat( utils_test.BOARD_SIZE, second_move)), -1 ) ] self.assertEqualData(expected_data, recovered_data)
def test_flatten(self): self.assertEqual(coords.to_flat((0, 0)), 0) self.assertEqual(coords.to_flat((0, 3)), 3) self.assertEqual(coords.to_flat((3, 0)), 27) self.assertEqual(coords.from_flat(27), (3, 0)) self.assertEqual(coords.from_flat(10), (1, 1)) self.assertEqual(coords.from_flat(80), (8, 8)) self.assertEqual(coords.to_flat(coords.from_flat(10)), 10) self.assertEqual(coords.from_flat(coords.to_flat((5, 4))), (5, 4))
def test_flatten(self): self.assertEqual(0, coords.to_flat((0, 0))) self.assertEqual(3, coords.to_flat((0, 3))) self.assertEqual(27, coords.to_flat((3, 0))) self.assertEqual((3, 0), coords.from_flat(27)) self.assertEqual((1, 1), coords.from_flat(10)) self.assertEqual((8, 8), coords.from_flat(80)) self.assertEqual(10, coords.to_flat(coords.from_flat(10))) self.assertEqual((5, 4), coords.from_flat(coords.to_flat((5, 4))))
def test_flatten(self): self.assertEqual(coords.to_flat((0, 0)), 0) self.assertEqual(coords.to_flat((0, 3)), 3) self.assertEqual(coords.to_flat((3, 0)), 27) self.assertEqual(coords.from_flat(27), (3, 0)) self.assertEqual(coords.from_flat(10), (1, 1)) self.assertEqual(coords.from_flat(80), (8, 8)) self.assertEqual(coords.to_flat( coords.from_flat(10)), 10) self.assertEqual(coords.from_flat( coords.to_flat((5, 4))), (5, 4))
def test_flatten(self): self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 0)), 0) self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 3)), 3) self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (3, 0)), 27) self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 27), (3, 0)) self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 10), (1, 1)) self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 80), (8, 8)) self.assertEqual(coords.to_flat( utils_test.BOARD_SIZE, coords.from_flat(utils_test.BOARD_SIZE, 10)), 10) self.assertEqual(coords.from_flat( utils_test.BOARD_SIZE, coords.to_flat( utils_test.BOARD_SIZE, (5, 4))), (5, 4))
def test_do_not_explore_past_finish(self): probs = np.array([0.02] * (go.N * go.N + 1), dtype=np.float32) root = MCTSNode(go.Position()) root.select_leaf().incorporate_results(probs, 0, root) first_pass = root.maybe_add_child(coords.to_flat(None)) first_pass.incorporate_results(probs, 0, root) second_pass = first_pass.maybe_add_child(coords.to_flat(None)) with self.assertRaises(AssertionError): second_pass.incorporate_results(probs, 0, root) node_to_explore = second_pass.select_leaf() # should just stop exploring at the end position. self.assertEqual(node_to_explore, second_pass)
def test_do_not_explore_past_finish(self): probs = np.array([0.02] * (go.N * go.N + 1), dtype=np.float32) root = mcts.MCTSNode(go.Position()) root.select_leaf().incorporate_results(probs, 0, root) first_pass = root.maybe_add_child(coords.to_flat(None)) first_pass.incorporate_results(probs, 0, root) second_pass = first_pass.maybe_add_child(coords.to_flat(None)) with self.assertRaises(AssertionError): second_pass.incorporate_results(probs, 0, root) node_to_explore = second_pass.select_leaf() # should just stop exploring at the end position. self.assertEqual(second_pass, node_to_explore)
def test_flatten(self): self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 0)), 0) self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 3)), 3) self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (3, 0)), 27) self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 27), (3, 0)) self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 10), (1, 1)) self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 80), (8, 8)) self.assertEqual( coords.to_flat(utils_test.BOARD_SIZE, coords.from_flat(utils_test.BOARD_SIZE, 10)), 10) self.assertEqual( coords.from_flat(utils_test.BOARD_SIZE, coords.to_flat(utils_test.BOARD_SIZE, (5, 4))), (5, 4))
def _make_tf_example_from_pwc(board_size, position_w_context): features = features_lib.extract_features( board_size, position_w_context.position) pi = _one_hot(board_size, coords.to_flat( board_size, position_w_context.next_move)) value = position_w_context.result return make_tf_example(features, pi, value)
def play_move(self, c): ''' Notable side effects: - finalizes the probability distribution according to this roots visit counts into the class' running tally, `searches_pi` - Makes the node associated with this move the root, for future `inject_noise` calls. ''' if not self.two_player_mode: self.searches_pi.append( self.root.children_as_pi( self.root.position.n < self.temp_threshold)) self.comments.append(self.root.describe()) try: self.root = self.root.maybe_add_child(coords.to_flat(c)) except go.IllegalMove: dbg("Illegal move") if not self.two_player_mode: self.searches_pi.pop() self.comments.pop() raise self.position = self.root.position # for showboard del self.root.parent.children return True # GTP requires positive result.
def test_parallel_tree_search(self): player = initialize_almost_done_player() # check -- white is losing. self.assertEqual(player.root.position.score(), -0.5) # initialize the tree so that the root node has populated children. player.tree_search(num_parallel=1) # virtual losses should enable multiple searches to happen simultaneously # without throwing an error... for i in range(5): player.tree_search(num_parallel=4) # uncomment to debug this test # print(player.root.describe()) # Search should converge on D9 as only winning move. flattened = coords.to_flat( utils_test.BOARD_SIZE, coords.from_kgs(utils_test.BOARD_SIZE, 'D9')) best_move = np.argmax(player.root.child_N) self.assertEqual(best_move, flattened) # D9 should have a positive value self.assertGreater(player.root.children[flattened].Q, 0) self.assertGreaterEqual(player.root.N, 20) # passing should be ineffective. self.assertLess(player.root.child_Q[-1], 0) # no virtual losses should be pending self.assertNoPendingVirtualLosses(player.root)
def test_parallel_tree_search(self): player = initialize_almost_done_player() # check -- white is losing. self.assertEqual(player.root.position.score(), -0.5) # initialize the tree so that the root node has populated children. player.tree_search(num_parallel=1) # virtual losses should enable multiple searches to happen simultaneously # without throwing an error... for i in range(5): player.tree_search(num_parallel=4) # uncomment to debug this test # print(player.root.describe()) # Search should converge on D9 as only winning move. flattened = coords.to_flat(utils_test.BOARD_SIZE, coords.from_kgs( utils_test.BOARD_SIZE, 'D9')) best_move = np.argmax(player.root.child_N) self.assertEqual(best_move, flattened) # D9 should have a positive value self.assertGreater(player.root.children[flattened].Q, 0) self.assertGreaterEqual(player.root.N, 20) # passing should be ineffective. self.assertLess(player.root.child_Q[-1], 0) # no virtual losses should be pending self.assertNoPendingVirtualLosses(player.root)
def parse_comment_node(comment): # Example of a comment node. The resign threshold line appears only # for the first move in the game; it gets preprocessed by extract_game_data """ Resign Threshold: -0.88 -0.0662 D4 (100) ==> D16 (14) ==> Q16 (3) ==> Q4 (1) ==> Q: -0.07149 move: action Q U P P-Dir N soft-N p-delta p-rel D4 : -0.028, -0.048, 0.020, 0.048, 0.064, 100 0.1096 0.06127 1.27 D16 : -0.024, -0.043, 0.019, 0.044, 0.059, 96 0.1053 0.06135 1.40 """ lines = comment.split('\n') if lines[0].startswith('Resign'): lines = lines[1:] post_Q = float(lines[0]) debug_rows = [] comment_splitter = re.compile(r'[ :,]') for line in lines[3:]: if not line: continue columns = comment_splitter.split(line) columns = list(filter(bool, columns)) coord, *other_columns = columns coord = coords.to_flat(coords.from_gtp(coord)) debug_rows.append(DebugRow(coord, *map(float, other_columns))) if FLAGS.only_top_move: break return post_Q, debug_rows
def play_move(self, c): ''' Notable side effects: - finalizes the probability distribution according to this roots visit counts into the class' running tally, `searches_pi` - Makes the node associated with this move the root, for future `inject_noise` calls. ''' if not self.two_player_mode: self.searches_pi.append( self.root.children_as_pi(self.root.position.n < self.temp_threshold)) self.qs.append(self.root.Q) # Save our resulting Q. self.comments.append(self.root.describe()) try: self.root = self.root.maybe_add_child(coords.to_flat(c)) except go.IllegalMove: print("Illegal move") if not self.two_player_mode: self.searches_pi.pop() self.qs.pop() self.comments.pop() return False self.position = self.root.position # for showboard del self.root.parent.children return True # GTP requires positive result.
def test_pass(self): self.assertEqual(None, coords.from_sgf('')) self.assertEqual(None, coords.from_flat(81)) self.assertEqual(None, coords.from_kgs('pass')) self.assertEqual('', coords.to_sgf(None)) self.assertEqual(81, coords.to_flat(None)) self.assertEqual('pass', coords.to_kgs(None))
def test_upperleft(self): self.assertEqual((0, 0), coords.from_sgf('aa')) self.assertEqual((0, 0), coords.from_flat(0)) self.assertEqual((0, 0), coords.from_kgs('A9')) self.assertEqual('aa', coords.to_sgf((0, 0))) self.assertEqual(0, coords.to_flat((0, 0))) self.assertEqual('A9', coords.to_kgs((0, 0)))
def test_topleft(self): self.assertEqual((0, 8), coords.from_sgf('ia')) self.assertEqual((0, 8), coords.from_flat(8)) self.assertEqual((0, 8), coords.from_kgs('J9')) self.assertEqual('ia', coords.to_sgf((0, 8))) self.assertEqual(8, coords.to_flat((0, 8))) self.assertEqual('J9', coords.to_kgs((0, 8)))
def test_topleft(self): self.assertEqual(coords.from_sgf('ia'), (0, 8)) self.assertEqual(coords.from_flat(8), (0, 8)) self.assertEqual(coords.from_kgs('J9'), (0, 8)) self.assertEqual(coords.to_sgf((0, 8)), 'ia') self.assertEqual(coords.to_flat((0, 8)), 8) self.assertEqual(coords.to_kgs((0, 8)), 'J9')
def test_upperleft(self): self.assertEqual(coords.from_sgf('aa'), (0, 0)) self.assertEqual(coords.from_flat(0), (0, 0)) self.assertEqual(coords.from_kgs('A9'), (0, 0)) self.assertEqual(coords.to_sgf((0, 0)), 'aa') self.assertEqual(coords.to_flat((0, 0)), 0) self.assertEqual(coords.to_kgs((0, 0)), 'A9')
def test_pass(self): self.assertEqual(coords.from_sgf(''), None) self.assertEqual(coords.from_flat(81), None) self.assertEqual(coords.from_kgs('pass'), None) self.assertEqual(coords.to_sgf(None), '') self.assertEqual(coords.to_flat(None), 81) self.assertEqual(coords.to_kgs(None), 'pass')
def test_select_leaf(self): flattened = coords.to_flat(coords.from_kgs('D9')) probs = np.array([.02] * (go.N * go.N + 1)) probs[flattened] = 0.4 root = MCTSNode(SEND_TWO_RETURN_ONE) root.select_leaf().incorporate_results(probs, 0, root) self.assertEqual(root.position.to_play, go.WHITE) self.assertEqual(root.select_leaf(), root.children[flattened])
def test_select_leaf(self): flattened = coords.to_flat(coords.from_kgs('D9')) probs = np.array([.02] * (go.N * go.N + 1)) probs[flattened] = 0.4 root = mcts.MCTSNode(SEND_TWO_RETURN_ONE) root.select_leaf().incorporate_results(probs, 0, root) self.assertEqual(root.position.to_play, go.WHITE) self.assertEqual(root.select_leaf(), root.children[flattened])
def test_topleft(self): self.assertEqual(coords.from_sgf('ia'), (0, 8)) self.assertEqual(coords.from_flat(8), (0, 8)) self.assertEqual(coords.from_kgs('J9'), (0, 8)) self.assertEqual(coords.from_pygtp((9, 9)), (0, 8)) self.assertEqual(coords.to_sgf((0, 8)), 'ia') self.assertEqual(coords.to_flat((0, 8)), 8) self.assertEqual(coords.to_kgs((0, 8)), 'J9') self.assertEqual(coords.to_pygtp((0, 8)), (9, 9))
def test_upperleft(self): self.assertEqual(coords.from_sgf('aa'), (0, 0)) self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 0), (0, 0)) self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'A9'), (0, 0)) self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (1, 9)), (0, 0)) self.assertEqual(coords.to_sgf((0, 0)), 'aa') self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 0)), 0) self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, (0, 0)), 'A9') self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, (0, 0)), (1, 9))
def test_pass(self): self.assertEqual(None, coords.from_sgf('')) self.assertEqual(None, coords.from_sgf('tt')) self.assertEqual(None, coords.from_flat(81)) self.assertEqual(None, coords.from_gtp('pass')) self.assertEqual(None, coords.from_gtp('PASS')) self.assertEqual('', coords.to_sgf(None)) self.assertEqual(81, coords.to_flat(None)) self.assertEqual('pass', coords.to_gtp(None))
def test_pass(self): self.assertEqual(coords.from_sgf(''), None) self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 81), None) self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'pass'), None) self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (0, 0)), None) self.assertEqual(coords.to_sgf(None), '') self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, None), 81) self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, None), 'pass') self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, None), (0, 0))
def test_upperleft(self): self.assertEqual(coords.from_sgf('aa'), (0, 0)) self.assertEqual(coords.from_flat(0), (0, 0)) self.assertEqual(coords.from_kgs('A9'), (0, 0)) self.assertEqual(coords.from_pygtp((1, 9)), (0, 0)) self.assertEqual(coords.to_sgf((0, 0)), 'aa') self.assertEqual(coords.to_flat((0, 0)), 0) self.assertEqual(coords.to_kgs((0, 0)), 'A9') self.assertEqual(coords.to_pygtp((0, 0)), (1, 9))
def test_topleft(self): self.assertEqual(coords.from_sgf('ia'), (0, 8)) self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 8), (0, 8)) self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'J9'), (0, 8)) self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (9, 9)), (0, 8)) self.assertEqual(coords.to_sgf((0, 8)), 'ia') self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 8)), 8) self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, (0, 8)), 'J9') self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, (0, 8)), (9, 9))
def test_pass(self): self.assertEqual(coords.from_sgf(''), None) self.assertEqual(coords.from_flat(81), None) self.assertEqual(coords.from_kgs('pass'), None) self.assertEqual(coords.from_pygtp((0, 0)), None) self.assertEqual(coords.to_sgf(None), '') self.assertEqual(coords.to_flat(None), 81) self.assertEqual(coords.to_kgs(None), 'pass') self.assertEqual(coords.to_pygtp(None), (0, 0))
def test_make_dataset_from_sgf(self): with tempfile.NamedTemporaryFile() as sgf_file, \ tempfile.NamedTemporaryFile() as record_file: sgf_file.write(TEST_SGF.encode('utf8')) sgf_file.seek(0) preprocessing.make_dataset_from_sgf(sgf_file.name, record_file.name) recovered_data = self.extract_data(record_file.name) start_pos = go.Position() first_move = coords.from_sgf('fd') next_pos = start_pos.play_move(first_move) second_move = coords.from_sgf('cf') expected_data = [ (features.extract_features(start_pos), preprocessing._one_hot(coords.to_flat(first_move)), -1), (features.extract_features(next_pos), preprocessing._one_hot(coords.to_flat(second_move)), -1) ] self.assertEqualData(expected_data, recovered_data)
def test_pass(self): self.assertEqual(coords.from_sgf(''), None) self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 81), None) self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'pass'), None) self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (0, 0)), None) self.assertEqual(coords.to_sgf(None), '') self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, None), 81) self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, None), 'pass') self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, None), (0, 0))
def test_select_leaf(self): flattened = coords.to_flat( utils_test.BOARD_SIZE, coords.from_kgs(utils_test.BOARD_SIZE, 'D9')) probs = np.array([.02] * (utils_test.BOARD_SIZE * utils_test.BOARD_SIZE + 1)) probs[flattened] = 0.4 root = MCTSNode(utils_test.BOARD_SIZE, SEND_TWO_RETURN_ONE) root.select_leaf().incorporate_results(probs, 0, root) self.assertEqual(root.position.to_play, go.WHITE) self.assertEqual(root.select_leaf(), root.children[flattened])
def test_topleft(self): self.assertEqual(coords.from_sgf('ia'), (0, 8)) self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 8), (0, 8)) self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'J9'), (0, 8)) self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (9, 9)), (0, 8)) self.assertEqual(coords.to_sgf((0, 8)), 'ia') self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 8)), 8) self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, (0, 8)), 'J9') self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, (0, 8)), (9, 9))
def test_upperleft(self): self.assertEqual(coords.from_sgf('aa'), (0, 0)) self.assertEqual(coords.from_flat(utils_test.BOARD_SIZE, 0), (0, 0)) self.assertEqual(coords.from_kgs(utils_test.BOARD_SIZE, 'A9'), (0, 0)) self.assertEqual(coords.from_pygtp(utils_test.BOARD_SIZE, (1, 9)), (0, 0)) self.assertEqual(coords.to_sgf((0, 0)), 'aa') self.assertEqual(coords.to_flat(utils_test.BOARD_SIZE, (0, 0)), 0) self.assertEqual(coords.to_kgs(utils_test.BOARD_SIZE, (0, 0)), 'A9') self.assertEqual(coords.to_pygtp(utils_test.BOARD_SIZE, (0, 0)), (1, 9))
def play_move(self, c): """Play a move.""" # Notable side effects: # - finalizes the probability distribution according to # this roots visit counts into the class' running tally, `searches_pi` # - Makes the node associated with this move the root, for future # `inject_noise` calls. if not self.two_player_mode: self.searches_pi.append( self.root.children_as_pi(self.root.position.n < self.temp_threshold)) self.qs.append(self.root.Q) # Save our resulting Q. self.comments.append(self.root.describe()) self.root = self.root.maybe_add_child(coords.to_flat(self.board_size, c)) self.position = self.root.position # for showboard del self.root.parent.children return True # GTP requires positive result.
def play_move(self, c): """Play a move.""" # Notable side effects: # - finalizes the probability distribution according to # this roots visit counts into the class' running tally, `searches_pi` # - Makes the node associated with this move the root, for future # `inject_noise` calls. if not self.two_player_mode: self.searches_pi.append( self.root.children_as_pi(self.root.position.n < self.temp_threshold)) self.qs.append(self.root.Q) # Save our resulting Q. self.comments.append(self.root.describe()) self.root = self.root.maybe_add_child(coords.to_flat(self.board_size, c)) self.position = self.root.position # for showboard del self.root.parent.children return True # GTP requires positive result.
def extract_move_data(root_node, worker_id, completed_time, board_size): current_node = root_node.next move_data = [] move_num = 1 while current_node is not None: props = current_node.properties if 'B' in props: to_play = 1 move_played = props['B'][0] elif 'W' in props: to_play = -1 move_played = props['W'][0] else: import pdb; pdb.set_trace() move_played = coords.to_flat(coords.from_sgf(move_played)) post_Q, debug_rows = parse_comment_node(props['C'][0]) policy_prior = [0] * (board_size * board_size + 1) policy_prior_orig = policy_prior[:] mcts_visit_counts = policy_prior[:] mcts_visit_counts_norm = policy_prior[:] for debug_row in debug_rows: move = debug_row.move policy_prior[move] = debug_row.prior policy_prior_orig[move] = debug_row.orig_prior mcts_visit_counts[move] = debug_row.N mcts_visit_counts_norm[move] = debug_row.soft_N move_data.append({ 'worker_id': worker_id, 'completed_time': completed_time, 'move_num': move_num, 'turn_to_play': to_play, 'move': move_played, 'move_kgs': coords.to_kgs(coords.from_flat(move_played)), 'prior_Q': None, 'post_Q': post_Q, 'policy_prior': policy_prior, 'policy_prior_orig': policy_prior_orig, 'mcts_visit_counts': mcts_visit_counts, 'mcts_visit_counts_norm': mcts_visit_counts_norm, }) move_num += 1 current_node = current_node.next return move_data
def test_dont_pass_if_losing(self): player = initialize_almost_done_player() # check -- white is losing. self.assertEqual(player.root.position.score(), -0.5) for i in range(20): player.tree_search() # uncomment to debug this test # print(player.root.describe()) # Search should converge on D9 as only winning move. flattened = coords.to_flat(coords.from_kgs('D9')) best_move = np.argmax(player.root.child_N) self.assertEqual(best_move, flattened) # D9 should have a positive value self.assertGreater(player.root.children[flattened].Q, 0) self.assertGreaterEqual(player.root.N, 20) # passing should be ineffective. self.assertLess(player.root.child_Q[-1], 0) # no virtual losses should be pending self.assertNoPendingVirtualLosses(player.root)
def test_dont_pass_if_losing(self): player = initialize_almost_done_player() # check -- white is losing. self.assertEqual(player.root.position.score(), -0.5) for i in range(20): player.tree_search() # uncomment to debug this test # print(player.root.describe()) # Search should converge on D9 as only winning move. flattened = coords.to_flat(utils_test.BOARD_SIZE, coords.from_kgs( utils_test.BOARD_SIZE, 'D9')) best_move = np.argmax(player.root.child_N) self.assertEqual(best_move, flattened) # D9 should have a positive value self.assertGreater(player.root.children[flattened].Q, 0) self.assertGreaterEqual(player.root.N, 20) # passing should be ineffective. self.assertLess(player.root.child_Q[-1], 0) # no virtual losses should be pending self.assertNoPendingVirtualLosses(player.root)
def _make_tf_example_from_pwc(position_w_context): f = dual_net.get_features() features = features_lib.extract_features(position_w_context.position, f) pi = _one_hot(coords.to_flat(position_w_context.next_move)) value = position_w_context.result return make_tf_example(features, pi, value)
def _make_tf_example_from_pwc(position_w_context): features = features_lib.extract_features(position_w_context.position) pi = _one_hot(coords.to_flat(position_w_context.next_move)) value = position_w_context.result return make_tf_example(features, pi, value)
def extract_move_data(root_node, worker_id, completed_time, board_size): current_node = root_node.next move_data = [] move_num = 1 while current_node is not None: props = current_node.properties if 'B' in props: to_play = 1 move_played = props['B'][0] elif 'W' in props: to_play = -1 move_played = props['W'][0] else: import pdb pdb.set_trace() move_played = coords.to_flat(coords.from_sgf(move_played)) post_Q, debug_rows = parse_comment_node(props['C'][0]) def get_row_data(debug_row): column_names = ["prior", "orig_prior", "N", "soft_N"] return [getattr(debug_row, field) for field in column_names] if FLAGS.only_top_move: assert len(debug_rows) <= 1 row_data = list(map(get_row_data, debug_rows)) else: row_data = [[0] * 4 for _ in range(board_size * board_size + 1)] for debug_row in debug_rows: move = debug_row.move row_data[move] = get_row_data(debug_row) policy_prior, policy_prior_orig, mcts_visits, mcts_visits_norm = \ zip(*row_data) move_data.append({ 'worker_id': worker_id, 'completed_time': completed_time, 'move_num': move_num, 'turn_to_play': to_play, 'move': move_played, 'move_kgs': coords.to_gtp(coords.from_flat(move_played)), 'prior_Q': None, 'post_Q': post_Q, 'policy_prior': policy_prior, 'policy_prior_orig': policy_prior_orig, 'mcts_visit_counts': mcts_visits, 'mcts_visit_counts_norm': mcts_visits_norm, }) move_num += 1 current_node = current_node.next return move_data