def test_capture_multiple_groups(self): board = test_utils.load_board(''' .OX...... OXX...... XX....... ''' + EMPTY_ROW * 6) lib_tracker = LibertyTracker.from_board(board) captured = lib_tracker.add_stone(BLACK, coords.from_gtp('A9')) self.assertEqual(len(lib_tracker.groups), 2) self.assertEqual(captured, coords_from_kgs_set('B9 A8')) corner_stone = lib_tracker.groups[lib_tracker.group_index[ coords.from_gtp('A9')]] self.assertEqual(corner_stone.stones, coords_from_kgs_set('A9')) self.assertEqual(corner_stone.liberties, coords_from_kgs_set('B9 A8')) surrounding_stones = lib_tracker.groups[lib_tracker.group_index[ coords.from_gtp('C9')]] self.assertEqual(surrounding_stones.stones, coords_from_kgs_set('C9 B8 C8 A7 B7')) self.assertEqual(surrounding_stones.liberties, coords_from_kgs_set('B9 D9 A8 D8 C7 A6 B6')) liberty_cache = lib_tracker.liberty_cache for stone in corner_stone.stones: self.assertEqual(liberty_cache[stone], 2, str(stone)) for stone in surrounding_stones.stones: self.assertEqual(liberty_cache[stone], 7, str(stone))
def test_move_with_capture(self): start_board = test_utils.load_board(EMPTY_ROW * 5 + ''' XXXX..... XOOX..... O.OX..... OOXX..... ''') start_position = Position( board=start_board, n=0, komi=6.5, caps=(1, 2), ko=None, recent=tuple(), to_play=BLACK, ) expected_board = test_utils.load_board(EMPTY_ROW * 5 + ''' XXXX..... X..X..... .X.X..... ..XX..... ''') expected_position = Position( board=expected_board, n=1, komi=6.5, caps=(7, 2), ko=None, recent=(PlayerMove(BLACK, coords.from_gtp('B2')), ), to_play=WHITE, ) actual_position = start_position.play_move(coords.from_gtp('B2')) self.assertEqualPositions(actual_position, expected_position)
def test_neighbors(self): corner = coords.from_gtp('A1') neighbors = [go.EMPTY_BOARD[c] for c in go.NEIGHBORS[corner]] self.assertEqual(len(neighbors), 2) side = coords.from_gtp('A2') side_neighbors = [go.EMPTY_BOARD[c] for c in go.NEIGHBORS[side]] self.assertEqual(len(side_neighbors), 3)
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_capture_stone(self): board = test_utils.load_board(''' .X....... XO....... .X....... ''' + EMPTY_ROW * 6) lib_tracker = LibertyTracker.from_board(board) captured = lib_tracker.add_stone(BLACK, coords.from_gtp('C8')) self.assertEqual(len(lib_tracker.groups), 4) self.assertEqual(lib_tracker.group_index[coords.from_gtp('B8')], go.MISSING_GROUP_ID) self.assertEqual(captured, coords_from_kgs_set('B8'))
def test_lib_tracker_init(self): board = test_utils.load_board('X........' + EMPTY_ROW * 8) lib_tracker = LibertyTracker.from_board(board) self.assertEqual(len(lib_tracker.groups), 1) self.assertNotEqual(lib_tracker.group_index[coords.from_gtp('A9')], go.MISSING_GROUP_ID) self.assertEqual(lib_tracker.liberty_cache[coords.from_gtp('A9')], 2) sole_group = lib_tracker.groups[lib_tracker.group_index[ coords.from_gtp('A9')]] self.assertEqual(sole_group.stones, coords_from_kgs_set('A9')) self.assertEqual(sole_group.liberties, coords_from_kgs_set('B9 A8')) self.assertEqual(sole_group.color, BLACK)
def test_chinese_handicap_handling(self): intermediate_board = test_utils.load_board(''' ......... ......... ......X.. ......... ......... ......... ......... ......... ......... ''') intermediate_position = go.Position( intermediate_board, n=1, komi=5.5, caps=(0, 0), recent=(go.PlayerMove(go.BLACK, coords.from_gtp('G7')), ), to_play=go.BLACK, ) final_board = test_utils.load_board(''' ....OX... .O.OOX... O.O.X.X.. .OXXX.... OX...XX.. .X.XXO... X.XOOXXX. XXXO.OOX. .XOOX.O.. ''') final_position = go.Position(final_board, n=50, komi=5.5, caps=(7, 2), ko=None, recent=( go.PlayerMove(go.WHITE, coords.from_gtp('E9')), go.PlayerMove(go.BLACK, coords.from_gtp('F9')), ), to_play=go.WHITE) positions_w_context = list(replay_sgf(CHINESE_HANDICAP_SGF)) self.assertEqualPositions(intermediate_position, positions_w_context[1].position) self.assertEqual(positions_w_context[1].next_move, coords.from_gtp('C3')) final_replayed_position = positions_w_context[-1].position.play_move( positions_w_context[-1].next_move) self.assertEqualPositions(final_position, final_replayed_position)
def test_same_friendly_group_neighboring_twice(self): board = test_utils.load_board(''' XX....... X........ ''' + EMPTY_ROW * 7) lib_tracker = LibertyTracker.from_board(board) captured = lib_tracker.add_stone(BLACK, coords.from_gtp('B8')) self.assertEqual(len(lib_tracker.groups), 1) sole_group_id = lib_tracker.group_index[coords.from_gtp('A9')] sole_group = lib_tracker.groups[sole_group_id] self.assertEqual(sole_group.stones, coords_from_kgs_set('A9 B9 A8 B8')) self.assertEqual(sole_group.liberties, coords_from_kgs_set('C9 C8 A7 B7')) self.assertEqual(captured, set())
def test_japanese_handicap_handling(self): intermediate_board = test_utils.load_board(''' ......... ......... ......X.. ......... ....O.... ......... ..X...... ......... ......... ''') intermediate_position = go.Position( intermediate_board, n=1, komi=5.5, caps=(0, 0), recent=(go.PlayerMove(go.WHITE, coords.from_gtp('E5')), ), to_play=go.BLACK, ) final_board = test_utils.load_board(''' ......... ......... ......X.. ......... ....O.... ......... ..XX..... ......... ......... ''') final_position = go.Position( final_board, n=2, komi=5.5, caps=(0, 0), recent=( go.PlayerMove(go.WHITE, coords.from_gtp('E5')), go.PlayerMove(go.BLACK, coords.from_gtp('D3')), ), to_play=go.WHITE, ) positions_w_context = list(replay_sgf(JAPANESE_HANDICAP_SGF)) self.assertEqualPositions(intermediate_position, positions_w_context[1].position) final_replayed_position = positions_w_context[-1].position.play_move( positions_w_context[-1].next_move) self.assertEqualPositions(final_position, final_replayed_position)
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 test_parsing_9x9(self): self.assertEqual((0, 0), coords.from_sgf('aa')) self.assertEqual((2, 0), coords.from_sgf('ac')) self.assertEqual((0, 2), coords.from_sgf('ca')) self.assertEqual(None, coords.from_sgf('')) self.assertEqual('', coords.to_sgf(None)) self.assertEqual('aa', coords.to_sgf(coords.from_sgf('aa'))) self.assertEqual('sa', coords.to_sgf(coords.from_sgf('sa'))) self.assertEqual((1, 17), coords.from_sgf(coords.to_sgf((1, 17)))) self.assertEqual((8, 0), coords.from_gtp('A1')) self.assertEqual((0, 0), coords.from_gtp('A9')) self.assertEqual((7, 2), coords.from_gtp('C2')) self.assertEqual((7, 8), coords.from_gtp('J2')) self.assertEqual('J9', coords.to_gtp((0, 8))) self.assertEqual('A1', coords.to_gtp((8, 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_gtp('J9')) self.assertEqual('ia', coords.to_sgf((0, 8))) self.assertEqual(8, coords.to_flat((0, 8))) self.assertEqual('J9', coords.to_gtp((0, 8)))
def cmd_play(self, arg0: str, arg1=None): if arg1 is None: move = arg0 else: # let's assume this never happens for now. # self._accomodate_out_of_turn(translate_gtp_color(arg0)) move = arg1 return self._player.play_move(coords.from_gtp(move))
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_gtp('A9')) self.assertEqual('aa', coords.to_sgf((0, 0))) self.assertEqual(0, coords.to_flat((0, 0))) self.assertEqual('A9', coords.to_gtp((0, 0)))
def test_select_leaf(self): flattened = coords.to_flat(coords.from_gtp('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_move(self): start_position = Position( board=TEST_BOARD, n=0, komi=6.5, caps=(1, 2), ko=None, recent=tuple(), to_play=BLACK, ) expected_board = test_utils.load_board(''' .XX....OO X........ ''' + EMPTY_ROW * 7) expected_position = Position( board=expected_board, n=1, komi=6.5, caps=(1, 2), ko=None, recent=(PlayerMove(BLACK, coords.from_gtp('C9')), ), to_play=WHITE, ) actual_position = start_position.play_move(coords.from_gtp('C9')) self.assertEqualPositions(actual_position, expected_position) expected_board2 = test_utils.load_board(''' .XX....OO X.......O ''' + EMPTY_ROW * 7) expected_position2 = Position( board=expected_board2, n=2, komi=6.5, caps=(1, 2), ko=None, recent=(PlayerMove(BLACK, coords.from_gtp('C9')), PlayerMove(WHITE, coords.from_gtp('J8'))), to_play=BLACK, ) actual_position2 = actual_position.play_move(coords.from_gtp('J8')) self.assertEqualPositions(actual_position2, expected_position2)
def test_same_opponent_group_neighboring_twice(self): board = test_utils.load_board(''' XX....... X........ ''' + EMPTY_ROW * 7) lib_tracker = LibertyTracker.from_board(board) captured = lib_tracker.add_stone(WHITE, coords.from_gtp('B8')) self.assertEqual(len(lib_tracker.groups), 2) black_group = lib_tracker.groups[lib_tracker.group_index[ coords.from_gtp('A9')]] self.assertEqual(black_group.stones, coords_from_kgs_set('A9 B9 A8')) self.assertEqual(black_group.liberties, coords_from_kgs_set('C9 A7')) white_group = lib_tracker.groups[lib_tracker.group_index[ coords.from_gtp('B8')]] self.assertEqual(white_group.stones, coords_from_kgs_set('B8')) self.assertEqual(white_group.liberties, coords_from_kgs_set('C8 B7')) self.assertEqual(captured, set())
def main(unused_argv): examples = read_examples(FLAGS.path) avg_value = sum([x.value for x in examples]) / len(examples) print('\nAverage value: %f' % avg_value) i = 0 while i < len(examples): example = examples[i] print_example(examples, i) sys.stdout.write('>> ') sys.stdout.flush() try: cmd = sys.stdin.readline().split() except KeyboardInterrupt: print() break if not cmd or cmd[0] == 'n': if len(cmd) == 2: try: i += int(cmd[1]) except: print('ERROR: "%s" isn\'t an int' % cmd[1]) continue else: i += 1 i = min(i, len(examples) - 1) elif cmd[0] == 'p': if len(cmd) == 2: try: i -= int(cmd[1]) except: print('ERROR: "%s" isn\'t an int' % cmd[1]) continue else: i -= 1 i = max(i, 0) elif cmd[0] == 'q': break else: try: c = coords.from_gtp(cmd[0].upper()) f = example.features[c[0], c[1]] except: print('ERROR: "%s" isn\'t a valid GTP coord' % cmd[0]) continue print(' plane:', ' '.join(['%2d' % i for i in range(len(f))])) print('feature:', ' '.join(['%2d' % x for x in f])) print('Bye!')
def test_merge_multiple_groups(self): board = test_utils.load_board(''' .X....... X.X...... .X....... ''' + EMPTY_ROW * 6) lib_tracker = LibertyTracker.from_board(board) lib_tracker.add_stone(BLACK, coords.from_gtp('B8')) self.assertEqual(len(lib_tracker.groups), 1) self.assertNotEqual(lib_tracker.group_index[coords.from_gtp('B8')], go.MISSING_GROUP_ID) sole_group = lib_tracker.groups[lib_tracker.group_index[ coords.from_gtp('B8')]] self.assertEqual(sole_group.stones, coords_from_kgs_set('B9 A8 B8 C8 B7')) self.assertEqual(sole_group.liberties, coords_from_kgs_set('A9 C9 D8 A7 C7 B6')) self.assertEqual(sole_group.color, BLACK) liberty_cache = lib_tracker.liberty_cache for stone in sole_group.stones: self.assertEqual(liberty_cache[stone], 6, str(stone))
def test_ko_move(self): start_board = test_utils.load_board(''' .OX...... OX....... ''' + EMPTY_ROW * 7) start_position = Position( board=start_board, n=0, komi=6.5, caps=(1, 2), ko=None, recent=tuple(), to_play=BLACK, ) expected_board = test_utils.load_board(''' X.X...... OX....... ''' + EMPTY_ROW * 7) expected_position = Position( board=expected_board, n=1, komi=6.5, caps=(2, 2), ko=coords.from_gtp('B9'), recent=(PlayerMove(BLACK, coords.from_gtp('A9')), ), to_play=WHITE, ) actual_position = start_position.play_move(coords.from_gtp('A9')) self.assertEqualPositions(actual_position, expected_position) # Check that retaking ko is illegal until two intervening moves with self.assertRaises(go.IllegalMove): actual_position.play_move(coords.from_gtp('B9')) pass_twice = actual_position.pass_move().pass_move() ko_delayed_retake = pass_twice.play_move(coords.from_gtp('B9')) expected_position = Position( board=start_board, n=4, komi=6.5, caps=(2, 3), ko=coords.from_gtp('A9'), recent=(PlayerMove(BLACK, coords.from_gtp('A9')), PlayerMove(WHITE, None), PlayerMove(BLACK, None), PlayerMove(WHITE, coords.from_gtp('B9'))), to_play=BLACK, ) self.assertEqualPositions(ko_delayed_retake, expected_position)
def test_passing(self): start_position = Position( board=TEST_BOARD, n=0, komi=6.5, caps=(1, 2), ko=coords.from_gtp('A1'), recent=tuple(), to_play=BLACK, ) expected_position = Position( board=TEST_BOARD, n=1, komi=6.5, caps=(1, 2), ko=None, recent=(PlayerMove(BLACK, None), ), to_play=WHITE, ) pass_position = start_position.pass_move() self.assertEqualPositions(pass_position, expected_position)
def test_flipturn(self): start_position = Position( board=TEST_BOARD, n=0, komi=6.5, caps=(1, 2), ko=coords.from_gtp('A1'), recent=tuple(), to_play=BLACK, ) expected_position = Position( board=TEST_BOARD, n=0, komi=6.5, caps=(1, 2), ko=None, recent=tuple(), to_play=WHITE, ) flip_position = start_position.flip_playerturn() self.assertEqualPositions(flip_position, expected_position)
def test_capture_many(self): board = test_utils.load_board(''' .XX...... XOO...... .XX...... ''' + EMPTY_ROW * 6) lib_tracker = LibertyTracker.from_board(board) captured = lib_tracker.add_stone(BLACK, coords.from_gtp('D8')) self.assertEqual(len(lib_tracker.groups), 4) self.assertEqual(lib_tracker.group_index[coords.from_gtp('B8')], go.MISSING_GROUP_ID) self.assertEqual(captured, coords_from_kgs_set('B8 C8')) left_group = lib_tracker.groups[lib_tracker.group_index[ coords.from_gtp('A8')]] self.assertEqual(left_group.stones, coords_from_kgs_set('A8')) self.assertEqual(left_group.liberties, coords_from_kgs_set('A9 B8 A7')) right_group = lib_tracker.groups[lib_tracker.group_index[ coords.from_gtp('D8')]] self.assertEqual(right_group.stones, coords_from_kgs_set('D8')) self.assertEqual(right_group.liberties, coords_from_kgs_set('D9 C8 E8 D7')) top_group = lib_tracker.groups[lib_tracker.group_index[coords.from_gtp( 'B9')]] self.assertEqual(top_group.stones, coords_from_kgs_set('B9 C9')) self.assertEqual(top_group.liberties, coords_from_kgs_set('A9 D9 B8 C8')) bottom_group = lib_tracker.groups[lib_tracker.group_index[ coords.from_gtp('B7')]] self.assertEqual(bottom_group.stones, coords_from_kgs_set('B7 C7')) self.assertEqual(bottom_group.liberties, coords_from_kgs_set('B8 C8 A7 D7 B6 C6')) liberty_cache = lib_tracker.liberty_cache for stone in top_group.stones: self.assertEqual(liberty_cache[stone], 4, str(stone)) for stone in left_group.stones: self.assertEqual(liberty_cache[stone], 3, str(stone)) for stone in right_group.stones: self.assertEqual(liberty_cache[stone], 4, str(stone)) for stone in bottom_group.stones: self.assertEqual(liberty_cache[stone], 6, str(stone)) for stone in captured: self.assertEqual(liberty_cache[stone], 0, str(stone))
def test_dont_pass_if_losing(self): player = initialize_almost_done_player() # check -- white is losing. self.assertEqual(-0.5, player.root.position.score()) for _ 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_gtp('D9')) best_move = np.argmax(player.root.child_N) self.assertEqual(flattened, best_move) # 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_place_stone_opposite_color(self): board = test_utils.load_board('X........' + EMPTY_ROW * 8) lib_tracker = LibertyTracker.from_board(board) lib_tracker.add_stone(WHITE, coords.from_gtp('B9')) self.assertEqual(len(lib_tracker.groups), 2) self.assertNotEqual(lib_tracker.group_index[coords.from_gtp('A9')], go.MISSING_GROUP_ID) self.assertNotEqual(lib_tracker.group_index[coords.from_gtp('B9')], go.MISSING_GROUP_ID) self.assertEqual(lib_tracker.liberty_cache[coords.from_gtp('A9')], 1) self.assertEqual(lib_tracker.liberty_cache[coords.from_gtp('B9')], 2) black_group = lib_tracker.groups[lib_tracker.group_index[ coords.from_gtp('A9')]] white_group = lib_tracker.groups[lib_tracker.group_index[ coords.from_gtp('B9')]] self.assertEqual(black_group.stones, coords_from_kgs_set('A9')) self.assertEqual(black_group.liberties, coords_from_kgs_set('A8')) self.assertEqual(black_group.color, BLACK) self.assertEqual(white_group.stones, coords_from_kgs_set('B9')) self.assertEqual(white_group.liberties, coords_from_kgs_set('C9 B8')) self.assertEqual(white_group.color, WHITE)
def test_parallel_tree_search(self): player = initialize_almost_done_player() # check -- white is losing. self.assertEqual(-0.5, player.root.position.score()) # initialize the tree so that the root node has populated children. player.tree_search(parallel_readouts=1) # virtual losses should enable multiple searches to happen simultaneously # without throwing an error... for _ in range(5): player.tree_search(parallel_readouts=4) # uncomment to debug this test # print(player.root.describe()) # Search should converge on D9 as only winning move. flattened = coords.to_flat(coords.from_gtp('D9')) best_move = np.argmax(player.root.child_N) self.assertEqual(flattened, best_move) # 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_is_koish(self): self.assertEqual(go.is_koish(TEST_BOARD, coords.from_gtp('A9')), BLACK) self.assertEqual(go.is_koish(TEST_BOARD, coords.from_gtp('B8')), None) self.assertEqual(go.is_koish(TEST_BOARD, coords.from_gtp('B9')), None) self.assertEqual(go.is_koish(TEST_BOARD, coords.from_gtp('E5')), None)