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
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))
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)
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))
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)
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)
def play_out(board: Board) -> float: root_board = board while not is_finished(board): valid_hands = extract_valid_hand(board) if len(valid_hands) == 0: board = Board(board.board, not board.side) continue hand = np.random.choice(valid_hands) board = put_and_reverse(hand, board) return judge_simple(board) * (1 if root_board.side else -1)
def test_index_array_of_bound(self): board = Board(np.zeros((8, 8))) board.board[0][0] = -1 board.board[0][2] = -1 board.board[0][3] = 1 actual = put_and_reverse((0, 1), board) expected = np.zeros_like(board.board) expected[0][0] = -1 expected[0][1] = 1 expected[0][2] = 1 expected[0][3] = 1 np.testing.assert_array_equal(expected, actual.board)
def _put_and_reverse(self, hand: Union[Hand, Tuple[int, int]], board: Board) -> Board: return put_and_reverse(hand, board)