def play_with_MCTS(game, black_strategy, white_strategy): game.restart() strategy = lambda who: black_strategy if who == utils.BLACK else white_strategy while game.player is not None: game_strategy = strategy(game.player) oppo_strategy = strategy(Othello.opponent(game.player)) if isinstance(game_strategy, MCTS): move = game_strategy.suggest_move(game) game_strategy.shift_root(move) else: move = Othello.get_move(game_strategy, game.player, game.board) if isinstance(oppo_strategy, MCTS): oppo_strategy.shift_root(move) game.make_valid_move(move) # print move # print Othello.print_board(game.board) # game.player = Othello.next_player(game.board, game.player) # raw_input() # Store the value for value network training # if isinstance(black_strategy, MCTS): # black_strategy.store_value() # if isinstance(white_strategy, MCTS): # white_strategy.store_value() return game.board, Othello.score(utils.BLACK, game.board)
def evaluate(self, game): # player = utils.PIECES.index(player) # black-1, white-0 data = np.empty((1, 10, 8, 8), dtype="int8") player = game.player board = game.board validmoves = Othello.legal_moves(player, board) arr = np.zeros((10, 8, 8), dtype="int8") arr[9] = np.ones((8, 8), dtype="int8") for i in xrange(8): for j in xrange(8): square = i + j * 10 + 11 if square in validmoves: arr[4][i][j] = 1 if board[square] == utils.EMPTY: arr[3][i][j] = 1 else: if board[square] == player: arr[1][i][j] = 1 elif board[square] == Othello.opponent(player): arr[2][i][j] = 1 if self.check_adjacent(square, board): if board[square] == player: arr[5][i][j] = 1 elif board[square] == Othello.opponent(player): arr[7][i][j] = 1 else: if board[square] == player: arr[6][i][j] = 1 elif board[square] == Othello.opponent(player): arr[8][i][j] = 1 data[0] = arr prob = self.model.predict(data, batch_size=1)[0] prob = np.reshape(prob, 64) # softmax temperature manipulate prob_sum = 0 for i in validmoves: prob[LABEL_MAP.index(i)] = math.exp(prob[LABEL_MAP.index(i)] / T) prob_sum += prob[LABEL_MAP.index(i)] move_probs = list( (i, prob[LABEL_MAP.index(i)] / prob_sum) for i in validmoves) return move_probs
def evaluate(self, game): player = game.player board = game.board validmoves = Othello.legal_moves(player, board) if len(validmoves) == 0: return [] prob = 1.0 / len(validmoves) random.shuffle(validmoves) move_probs = list((i, prob) for i in validmoves) return move_probs
def get_move(player, board): data = np.empty((1, 10, 8, 8), dtype="int8") validmoves = Othello.legal_moves(player, board) arr = np.zeros((10, 8, 8), dtype="int8") arr[9] = np.ones((8, 8), dtype="int8") for i in xrange(8): for j in xrange(8): square = i + j * 10 + 11 if square in validmoves: arr[4][i][j] = 1 if board[square] == utils.EMPTY: arr[3][i][j] = 1 else: if board[square] == player: arr[1][i][j] = 1 elif board[square] == Othello.opponent(player): arr[2][i][j] = 1 if check_adjacent(square, board): if board[square] == player: arr[5][i][j] = 1 elif board[square] == Othello.opponent(player): arr[7][i][j] = 1 else: if board[square] == player: arr[6][i][j] = 1 elif board[square] == Othello.opponent(player): arr[8][i][j] = 1 data[0] = arr # print utils.print_board(board) # print arr # raw_input() prob = model.predict(data, batch_size=1)[0] # prob = np.reshape(prob, 64) validpred = np.asarray( list([i, prob[LABEL_MAP.index(i)]] for i in validmoves)) move = validpred[np.argmax(validpred[:, 1])][0] return int(move)
def evaluate(self, game): # player = utils.PIECES.index(player) # black-1, white-0 data = np.empty((1, 10, 8, 8), dtype="int8") player = game.player board = game.board validmoves = Othello.legal_moves(player, board) arr = np.zeros((10, 8, 8), dtype="int8") arr[9] = np.ones((8, 8), dtype="int8") for i in xrange(8): for j in xrange(8): square = i + j*10 + 11 if square in validmoves: arr[4][i][j] = 1 if board[square] == utils.EMPTY: arr[3][i][j] = 1 else: if board[square] == player: arr[1][i][j] = 1 elif board[square] == Othello.opponent(player): arr[2][i][j] = 1 if self.check_adjacent(square, board): if board[square] == player: arr[5][i][j] = 1 elif board[square] == Othello.opponent(player): arr[7][i][j] = 1 else: if board[square] == player: arr[6][i][j] = 1 elif board[square] == Othello.opponent(player): arr[8][i][j] = 1 data[0] = arr score = self.model.predict(data, batch_size=1)[0] return score
def store_value(self, dest=DEST_DIR): filename = time.strftime('mcts_result_%Y%m%d%H%M%S.txt') try: with open(dest + filename, 'w') as f: expansion_list = [] for i in self.root.children: if self.root.children[i].is_expanded(): expansion_list.append(self.root.children[i]) while len(expansion_list) > 0: temp_node = expansion_list.pop(0) if temp_node.game.player is None: continue for i in temp_node.children: if temp_node.children[i].is_expanded(): expansion_list.append(temp_node.children[i]) f.write( Othello.board_to_str(temp_node.game.board, temp_node.game.player) + ' ' + str(temp_node.Q) + ' ' + str(temp_node.N) + '\n') except: print "Error! Cannot log file: " + filename
def get_generate_data(self): print("start") files = os.listdir(self.dirname) length = 0 for filename in files: length += sum(1 for line in open(self.dirname + "/" + filename, "r")) length *= 4 print length data = np.empty((length, 10, 8, 8), dtype="int8") label = np.empty(length, dtype="float32, int32") data_id = 0 for filename in files: with open(self.dirname + "/" + filename, "r") as f: for line in f: # Read the board, player and Q/N from the file board, player = Othello.str_to_board(line[0:102]) values = line[103:].split(' ') Q = float(values[0]) N = int(values[1]) # Process the board into training data validmoves = Othello.legal_moves(player, board) arr = np.zeros((10, 8, 8), dtype="int8") arr[9] = np.ones((8, 8), dtype="int8") for i in xrange(8): for j in xrange(8): square = i + j * 10 + 11 if square in validmoves: arr[4][i][j] = 1 if board[square] == utils.EMPTY: arr[3][i][j] = 1 else: if board[square] == player: arr[1][i][j] = 1 elif board[square] == Othello.opponent(player): arr[2][i][j] = 1 if check_adjacent(square, board): if board[square] == player: arr[5][i][j] = 1 elif board[square] == Othello.opponent( player): arr[7][i][j] = 1 else: if board[square] == player: arr[6][i][j] = 1 elif board[square] == Othello.opponent( player): arr[8][i][j] = 1 data[data_id, :, :, :] = arr data[data_id + 1, :, :, :] = np.array( list(m.transpose() for m in arr)) arr = arr[:, ::-1, ::-1] data[data_id + 2, :, :, :] = arr data[data_id + 3, :, :, :] = np.array( list(m.transpose() for m in arr)) label[data_id:data_id + 4] = [(Q, N)] * 4 # progress report percentage = int((float(data_id + 1) / (length)) * 30) progress_bar = '[' + '=' * percentage + '>' + '.' * ( 29 - percentage) + ']' sys.stdout.write(' ' + str(data_id + 1) + '/' + str(length) + ' ' + progress_bar + '\r') sys.stdout.flush() data_id += 4 #Map print("\nMap:") bin_board_dict = {} for i in xrange(length): key = int( ''.join( list( str(num) for own_stone_list in data[i][1].tolist() for num in own_stone_list)) + ''.join( list( str(num) for oppo_stone_list in data[i][2].tolist() for num in oppo_stone_list)), 2) if label[i][1] > 10: if key not in bin_board_dict: bin_board_dict[key] = [] bin_board_dict[key].append(i) # progress report percentage = int((float(i + 1) / (length)) * 30) progress_bar = '[' + '=' * percentage + '>' + '.' * ( 29 - percentage) + ']' sys.stdout.write(' ' + str(i + 1) + '/' + str(length) + ' ' + progress_bar + '\r') sys.stdout.flush() #Reduce print("\nReduce") uni_length = len(bin_board_dict) uni_data = np.empty((uni_length, 10, 8, 8), dtype="int8") uni_label = np.zeros(uni_length, dtype="float32") uni_data_id = 0 print(uni_length) for board in bin_board_dict: uni_data[uni_data_id] = data[bin_board_dict[board][0]] uni_label[uni_data_id] = sum( inner_product(label[i]) for i in bin_board_dict[board]) / sum( label[i][1] for i in bin_board_dict[board]) # progress report percentage = int((float(uni_data_id + 1) / (uni_length)) * 30) progress_bar = '[' + '=' * percentage + '>' + '.' * ( 29 - percentage) + ']' sys.stdout.write(' ' + str(uni_data_id + 1) + '/' + str(uni_length) + ' ' + progress_bar + '\r') sys.stdout.flush() uni_data_id += 1 print("\nOver!") print("Total sample count: " + str(len(uni_data))) return uni_data, uni_label
def __init__(self, heuristic): self.heuristic = heuristic self.engine = Othello()
def get_generate_data(self): print ("start") files = os.listdir(self.dirname) length = 0 for filename in files: # if sum(1 for line in open(self.dirname + "/" + filename, "r")) < self.omit_lines: # print "ERROR\n",filename length += sum(1 for line in open(self.dirname + "/" + filename, "r")) - self.omit_lines length*=4 print length data = np.empty((length, 10, 8, 8), dtype="int8") label = np.empty((length), dtype="int8") data_id = 0 for filename in files: with open(self.dirname + "/" + filename, "r") as f: self.game.restart() line_count = 0 for line in f: line_count += 1 currentplayer = utils.PIECES[int(line[0])] x = int(line[2]) y = int(line[4]) # score = float(line[6:]) if self.game.player == currentplayer: if line_count > self.omit_lines: player = self.game.player board = self.game.board validmoves = Othello.legal_moves(player, board) arr = np.zeros((10, 8, 8), dtype="int8") arr[9] = np.ones((8, 8), dtype="int8") for i in xrange(8): for j in xrange(8): square = i + j*10 + 11 if square in validmoves: arr[4][i][j] = 1 if board[square] == utils.EMPTY: arr[3][i][j] = 1 else: if board[square] == player: arr[1][i][j] = 1 elif board[square] == Othello.opponent(player): arr[2][i][j] = 1 if check_adjacent(square, board): if board[square] == player: arr[5][i][j] = 1 elif board[square] == Othello.opponent(player): arr[7][i][j] = 1 else: if board[square] == player: arr[6][i][j] = 1 elif board[square] == Othello.opponent(player): arr[8][i][j] = 1 data[data_id, :, :, :] = arr data[data_id+1, :, :, :] = np.array(list(m.transpose() for m in arr)) arr = arr[:,::-1,::-1] data[data_id+2, :, :, :] = arr data[data_id+3, :, :, :] = np.array(list(m.transpose() for m in arr)) label[data_id] = LABEL_MAP[x][y] label[data_id+1] = LABEL_MAP[y][x] label[data_id+2] = LABEL_MAP[7-x][7-y] label[data_id+3] = LABEL_MAP[7-y][7-x] # progress report percentage = int((float(data_id+1) / (length)) * 30) progress_bar = '[' + '='*percentage + '>' + '.'*(29-percentage) + ']' sys.stdout.write(' ' + str(data_id+1) + '/' + str(length) + ' ' + progress_bar + '\r') sys.stdout.flush() data_id += 4 pos = x + y*10 + 11 self.game.make_valid_move(pos) #Map print ("\nMap:") bin_board_dict = {} for i in xrange(length): key = int( ''.join(list(str(num) for own_stone_list in data[i][1].tolist() for num in own_stone_list)) + ''.join(list(str(num) for oppo_stone_list in data[i][2].tolist() for num in oppo_stone_list)) + bin(label[i])[2:], 2) if key not in bin_board_dict: bin_board_dict[key] = i # progress report percentage = int((float(i+1) / (length)) * 30) progress_bar = '[' + '='*percentage + '>' + '.'*(29-percentage) + ']' sys.stdout.write(' ' + str(i+1) + '/' + str(length) + ' ' + progress_bar + '\r') sys.stdout.flush() #Reduce print ("\nReduce") uni_length = len(bin_board_dict) uni_data = np.empty((uni_length, 10, 8, 8), dtype="int8") uni_label = np.zeros(uni_length, dtype="int8") uni_data_id = 0 print(uni_length) for board in bin_board_dict: uni_data[uni_data_id] = data[bin_board_dict[board]] uni_label[uni_data_id] = label[bin_board_dict[board]] # progress report percentage = int((float(uni_data_id+1) / (uni_length)) * 30) progress_bar = '[' + '='*percentage + '>' + '.'*(29-percentage) + ']' sys.stdout.write(' ' + str(uni_data_id+1) + '/' + str(uni_length) + ' ' + progress_bar + '\r') sys.stdout.flush() uni_data_id += 1 print("\nOver!") print("Total sample count: " + str(len(uni_data))) return uni_data, uni_label
def __init__(self, dirname, omit_lines=8): self.dirname = dirname self.game = Othello() self.omit_lines = omit_lines
def createGrid(self): def btn_restart_game(): if self.playing: self.packAndSend({ 'to': self.opponent['client_id'], 'action': 'restart', 'value': 'apply' }) else: QMessageBox.information(self, '系统消息', "没有加入任何游戏") self.restartGame() def btn_leave_game(): if self.playing: self.packAndSend({ 'to': self.opponent['client_id'], 'action': 'leave', 'value': '' }) self.restartGame() else: QMessageBox.information(self, '系统消息', "没有加入任何游戏") #设置主窗口的大小与位置 screen = QDesktopWidget().screenGeometry() self.setGeometry((screen.width() - UI_WIDTH) / 2, (screen.height() - UI_HEIGHT) / 2, UI_WIDTH, UI_HEIGHT) self.setFixedSize(UI_WIDTH, UI_HEIGHT) #创建布局 main = QWidget(self) main.setObjectName('main') self.setCentralWidget(main) self.othello = Othello() self.board = Board(main, self.othello) self.hall = Hall(self) #创建应该下子的图片标志,初始化为黑子 pic = QLabel(self) pic.setGeometry(UI_WIDTH * 0.212, UI_HEIGHT * 0.12, 50, 50) pic.setObjectName('label_for_turn_image') #创建功能按钮 buttons = QWidget(self) buttons.setGeometry(UI_WIDTH * 0.77, UI_HEIGHT * 0.4, UI_WIDTH * 0.2, UI_HEIGHT * 0.12) buttons.setObjectName('buttons') btn_restart = QPushButton('重新开始', self) btn_list = QPushButton('玩家列表', self) btn_leave = QPushButton('离开', self) btn_hall = QPushButton('大厅', self) grid = QGridLayout() grid.addWidget(btn_restart, 0, 0) grid.addWidget(btn_list, 0, 1) grid.addWidget(btn_leave, 1, 0) grid.addWidget(btn_hall, 1, 1) buttons.setLayout(grid) btn_restart.clicked.connect(btn_restart_game) btn_list.clicked.connect(lambda: self.packAndSend({ 'to': 'admin', 'action': 'list', 'value': '玩家列表' })) btn_hall.clicked.connect(lambda: self.packAndSend({ 'to': 'admin', 'action': 'games', 'value': '列出大厅' })) btn_leave.clicked.connect(btn_leave_game) #创建分数显示屏,包括黑棋得分,黑棋昵称,白棋得分,白棋昵称 self.score_black = QLabel(self) self.score_black.setObjectName('score_black') self.score_black.setGeometry(UI_WIDTH * 0.08, UI_HEIGHT * 0.25, 100, 60) self.nickname_black = QLabel("<p style='text-align:center'>黑棋</p>", self) self.nickname_black.setObjectName('nickname_black') self.nickname_black.setGeometry(UI_WIDTH * 0.03, UI_HEIGHT * 0.36, 120, 60) self.score_white = QLabel(self) self.score_white.setObjectName('score_white') self.score_white.setGeometry(UI_WIDTH * 0.08, UI_HEIGHT * 0.57, 100, 60) self.nickname_white = QLabel("<p style='text-align:center'>白棋</p>", self) self.nickname_white.setObjectName('nickname_white') self.nickname_white.setGeometry(UI_WIDTH * 0.03, UI_HEIGHT * 0.68, 120, 60) self.move_record = QTextEdit("当前下子情况", self) self.move_record.setReadOnly(True) #显示走棋的位置 self.move_record = QTextEdit("下子情况", self) self.move_record.setObjectName("move_record") self.move_record.setReadOnly(True) self.move_record.setGeometry(UI_WIDTH * 0.765, UI_HEIGHT * 0.24, UI_WIDTH * 0.215, UI_HEIGHT * 0.15) #创建输入框 chat = QWidget(self) chat.setObjectName('chat') chat.setGeometry(UI_WIDTH * 0.755, UI_HEIGHT * 0.54, UI_WIDTH * 0.235, UI_HEIGHT * 0.45) grid = QGridLayout() chat.setLayout(grid) # 聊天窗口 self.messages = QTextEdit("欢饮来到呆尐兔兔五子棋,撒花~~~", self) self.messages.setReadOnly(True) self.edit = QLineEdit(self) btn_send_msg = QPushButton("Enter/发送", self) btn_send_msg.clicked.connect(self.chat) grid.addWidget(self.messages, 0, 0, 10, 10) grid.addWidget(self.edit, 10, 0, 2, 8) grid.addWidget(btn_send_msg, 10, 8, 2, 2)
def use_random(self, game): game.make_valid_move( random.choice(Othello.legal_moves(game.player, game.board))) return game