예제 #1
0
 def test_reverse(self):
     board = Board.init_board()
     actual = put_and_reverse((4, 5), board)
     expected = Board.init_board().board
     expected[4][4] = 1
     expected[4][5] = 1
     np.testing.assert_array_equal(expected, actual.board)
예제 #2
0
 def test_multi_size_reverse(self):
     board = Board.init_board()
     board.board[4][5] = -1
     actual = put_and_reverse((4, 6), board)
     expected = Board.init_board().board
     expected[4][4] = 1
     expected[4][5] = 1
     expected[4][6] = 1
     np.testing.assert_array_equal(expected, actual.board)
예제 #3
0
 def test_select_node(self):
     node = Node(Board.init_board())
     nodes = [Node(Board.init_board()) for _ in range(4)]
     node.children = nodes
     for i in range(3):
         nodes[i].w = 2
         nodes[i].n = 4
     actual = select_node_ucb(node, 1.0)
     expected = nodes[-1]
     self.assertEqual(actual, expected)
예제 #4
0
def play(network, ai_param=None):
    if ai_param is None:
        ai_param = dict(play_count=100)
    board = Board.init_board()

    ai1 = AlphaZero(network, history=True, **ai_param)
    ai2 = AlphaZero(network, history=True, **ai_param)
    hands = []
    while True:
        if not extract_valid_hand(board):
            board.side ^= True
            hands.append(Hand.pass_hand())
        if not extract_valid_hand(board):
            break
        if board.side:
            hand = ai1.put(board, hands)
        else:
            hand = ai2.put(board, hands)
        hands.append(hand)
        board = put_and_reverse(hand, board)

    result = judge_simple(board)
    history1 = ai1.history if isinstance(ai1, AlphaZero) else []
    history2 = ai2.history if isinstance(ai1, AlphaZero) else []
    for x in history1:
        x[-1] = result
    for x in history2:
        x[0] = x[0] * -1
        x[-1] = -result
    return history1 + history2
예제 #5
0
파일: boss2.py 프로젝트: pytran3/Othello
def main(sente=True):
    board = Board.init_board()
    board.side = sente
    ai = MonteCarloAI()
    print(view_board(board))
    while True:
        if not extract_valid_hand(board):
            board.side ^= True
        if not extract_valid_hand(board):
            break
        if board.side:
            hand = input_hand(board)
        else:
            if (board.board == 0).sum() < 8:
                # 計算時間に余裕があるので全探索
                hand = ai.put_exhaustive_search(board)
            else:
                hand = ai.put(board)
            print("AI put: {}".format(hand))

        board = put_and_reverse(hand, board)
        print(view_board(board))

    print("=" * 10 + "  GAME OVER  " + "=" * 10)
    x_count = (board.board == 1).sum()
    o_count = (board.board == -1).sum()
    print("x: {}, o: {}".format(x_count, o_count))
예제 #6
0
def evaluate(ai1, ai2, sente=True, is_view=False):
    board = Board.init_board()
    if is_view:
        print(view_board(board))
    hands = []
    while True:
        if not extract_valid_hand(board):
            board.side ^= True
            hands.append(Hand.pass_hand())
            # print("pass hand!!")
        if not extract_valid_hand(board):
            break
        if board.side is sente:
            hand = ai1.put(board, hands)
        else:
            hand = ai2.put(board, hands)
        hands.append(hand)
        board = put_and_reverse(hand, board)

        if is_view:
            print(view_board(board))
    # print(ai2.history)
    if is_view:
        print("=" * 10 + "  GAME OVER  " + "=" * 10)
        x_count = (board.board == 1).sum()
        o_count = (board.board == -1).sum()
        print("x: {}, o: {}".format(x_count, o_count))
    return judge_simple(board) * (1 if sente else -1)
예제 #7
0
def parse(s: str) -> Tuple[int, int]:
    x, y = s[0], s[1]
    x = ord(x) - ord("a")
    y = int(y) - 1  # to 0-indexed
    # check array index out of bound
    Hand((y, x), Board.init_board())
    return y, x
예제 #8
0
def main(sente=True):
    board = Board.init_board()
    ai = MiniMaxAI(5)
    print(view_board(board))
    hands = []
    while True:
        if not extract_valid_hand(board):
            board.side ^= True
            hands.append(Hand.pass_hand())
        if not extract_valid_hand(board):
            break
        if board.side ^ sente:
            hand = input_hand(board)
        else:
            hand = ai.put(board, hands)
            hands.append(hand)
            print("AI put: {}".format(hand))
        if (board.board == 0).sum() < 12:
            # 計算時間に余裕があるのでdeepに読む
            ai.depth = 8
        board = put_and_reverse(hand, board)
        print(view_board(board))

    print("=" * 10 + "  GAME OVER  " + "=" * 10)
    x_count = (board.board == 1).sum()
    o_count = (board.board == -1).sum()
    print("x: {}, o: {}".format(x_count, o_count))
예제 #9
0
    def test_score(self):
        def test(board: Board):
            def score(b: Board):
                return float(b.board[4][5] * 100)

            actual_hands, actual_score = self.searcher.search_mini_max(board, score, 1)
            actual_hands = [hand.hand for hand in actual_hands]
            self.assertEqual(actual_score, 100)
            self.assertEqual(actual_hands, [(4, 5)])

        board = Board.init_board()
        self._test(board, test)
예제 #10
0
    def test_cant_put(self):
        def test(board):
            def score(_: Board):
                return 0

            actual_hands, actual_score = self.searcher.search_mini_max(board, score, 3)
            actual_hands = [hand.hand for hand in actual_hands]
            self.assertEqual(actual_score, WIN_SCORE)
            self.assertIn(actual_hands, [[(4, 5)], [(5, 4)], [(5, 5)]])

        board = Board.init_board()
        board.board[3][3] = 1
        self._test(board, test)
예제 #11
0
    def test_random(self):
        def test(board):
            for i in range(5):
                _sb = np.random.normal(size=(8, 8))

                def score(b: Board):
                    return float((b.board * _sb).sum())

                expected_hands, expected_score = self.searcher.search_mini_max(board, score, 4)
                actual_hands, actual_score = self.searcher.search_alpha_beta(board, score, 4)
                actual_hands = [hand.hand for hand in actual_hands]
                expected_hands = [hand.hand for hand in expected_hands]
                self.assertEqual(expected_score, actual_score)
                self.assertEqual(expected_hands, actual_hands)

        board = Board.init_board()
        self._test(board, test)
예제 #12
0
    def test_depth2(self):
        def test(board):
            def score(b: Board):
                sb = np.zeros((8, 8))
                sb[3][2] = 500
                sb[4][5] = 1000
                sb[5][5] = 50
                sb[6][5] = 10
                sb[3][5] = 45
                sb[2][6] = 1
                return (b.board * sb).sum()

            actual_hands, actual_score = self.searcher.search_mini_max(board, score, 2)
            actual_hands = [hand.hand for hand in actual_hands]
            self.assertEqual(actual_score, 950)
            self.assertEqual(actual_hands, [(4, 5), (5, 5)])

        board = Board.init_board()
        self._test(board, test)
예제 #13
0
 def test_valid(self):
     board = Board.init_board()
     actual = is_valid_hand((4, 5), board)
     self.assertTrue(actual)
예제 #14
0
 def test_invalid_shape(self):
     with self.assertRaises(IllegalIndexException):
         Hand((0, -1), Board.init_board())
예제 #15
0
파일: network.py 프로젝트: pytran3/Othello
        x = self.conv(x)
        x = self.resnet(x)
        x = F.max_pool2d(x, kernel_size=x.size()[2:])
        x = torch.squeeze(x, -1)
        x = torch.squeeze(x, -1)
        p = self.p_clf(x)
        v = self.v_clf(x)
        p = F.softmax(p, dim=-1)
        v = torch.tanh(v)
        return p, v


if __name__ == '__main__':
    # test
    from othello.model import Board
    from othello.helper import put_and_reverse

    input = Board.init_board()
    # input = put_and_reverse((2, 3), input)
    x = np.array([[input.board == 1, input.board == -1]], dtype=np.float32)
    network = Network()
    network.load_state_dict(torch.load("model/latest.pth"))
    p, v = network(x)
    p = p.to("cpu").detach().numpy().copy()
    v = v.to("cpu").detach().numpy().copy()
    print(sorted(p[-1])[::-1])
    p = p[:, :-1].reshape((8, 8))
    np.set_printoptions(precision=1)
    [print(p[i, :]) for i in range(8)]
    print(v)
예제 #16
0
 def test_false_side(self):
     board = Board.init_board(False)
     actual = extract_valid_hand(board)
     expected = [(2, 4), (4, 2), (3, 5), (5, 3)]
     self.assertEquals(set(expected), set(i.hand for i in actual))
예제 #17
0
    def test(self):
        def test(board):
            self.searcher.search_monte_carlo(board, 20)

        board = Board.init_board()
        self._test(board, test)
예제 #18
0
 def test_jump(self):
     board = Board.init_board()
     board.board[4][5] = 1
     actual = is_valid_hand((4, 6), board)
     self.assertFalse(actual)
예제 #19
0
 def test_diagonal(self):
     board = Board.init_board()
     board.board[3][3] = 1
     actual = is_valid_hand((5, 5), board)
     self.assertTrue(actual)
예제 #20
0
 def test_invalid(self):
     board = Board.init_board(False)
     actual = is_valid_hand((4, 5), board)
     self.assertFalse(actual)