def analyze_position(self, board): """Given a game board, analyze the position by evaluating the network on the legal moves Returns a list of (score, move) tuples, sorted from highest to lowest score, where the sum of scores is 1. """ # Get the encoded boards reachable via legal moves from this position next_board_tensors = [] next_extra_tensors = [] moves = list(board.legal_moves) for move in moves: board.push(move) nbt, net = Preprocessor.board_to_tensor(board) board.pop() next_board_tensors.append(nbt) next_extra_tensors.append(net) next_board_tensors = np.array(next_board_tensors) next_extra_tensors = np.array(next_extra_tensors) # Evaluate the network on the reachable moves scores = self.board_score.predict( [next_board_tensors, next_extra_tensors])[:, 0] # Convert scores to a probability distribution scores_dist = np.exp(scores / self.move_temp) scores_dist /= np.sum(scores_dist) return sorted(zip(scores_dist, moves), reverse=True)
def get_board_score(self, board): """Evaluate the network on the current board and return the score""" nbt, net = Preprocessor.board_to_tensor(board) return self.board_score.predict([nbt[np.newaxis], net[np.newaxis]])[0, 0]