def play_game(): tree = MCTS() board = new_domineering_board() board.to_pretty_string() while True: row_col = input("enter row,col: ") row, col = map(int, row_col.split(",")) stdout.write('You choose ({}, {})'.format(row, col)) index = conf.BOARD_Y_SIZE * (row - 1) + (col - 1) if (board.tup[index] is not None) and ( board.is_valid_move(index + conf.BOARD_Y_SIZE)): raise RuntimeError("Invalid move") board = board.make_move(index) board.to_pretty_string() if board.terminal: stdout.write("\nWinner is {}".format( conf.PLAYERS_NAME[board.winner])) break # You can train as you go, or only at the beginning. # Here, we train as we go, doing fifty rollouts each turn. for _ in range(conf.TRAINING_EPOCHS): tree.do_rollout(board) board = tree.choose(board) board.to_pretty_string() if board.terminal: stdout.write("\nWinner is {}".format( conf.PLAYERS_NAME[board.winner])) break
def play_game(): tree = MCTS() board = new_tic_tac_toe_board() print(board.to_pretty_string()) while True: row_col = input("enter row,col: ") row, col = map(int, row_col.split(",")) index = 3 * (row - 1) + (col - 1) if board.tup[index] is not None: raise RuntimeError("Invalid move") board = board.make_move(index) print(board.to_pretty_string()) if board.terminal: break # You can train as you go, or only at the beginning. # Here, we train as we go, doing fifty rollouts each turn. for _ in range(2): tree.do_rollout(board) print(tree.children) print(len(tree.children[board])) for b in tree.children[board]: print(colored(b.to_pretty_string(), 'green')) board = tree.choose(board) print(board.to_pretty_string()) if board.terminal: break
def play_game_ocba(budget=1000, optimum=0, n0=5, sigma_0=1): mcts = MCTS(policy='ocba', budget=budget, optimum=optimum, n0=n0, sigma_0=sigma_0) tree = new_tree() for _ in range(budget): mcts.do_rollout(tree) next_tree = mcts.choose(tree) return (mcts, tree, next_tree)
class myPlayer(AdvancePlayer): def __init__(self, _id): super().__init__(_id) self.opponent_id = None self.tree = MCTS() def SelectMove(self, moves, game_state): self.opponent_id = next( filter(lambda ps: ps.id != self.id, game_state.players)).id self.board = new_azul_board(game_state, self.id, self.opponent_id) for _ in range(30): self.tree.do_rollout(self.board) best_board = self.tree.choose(self.board) return best_board.game_state.lastmove
def play_game_uct(budget=1000, exploration_weight=1, optimum=0, n0=2, sigma_0=1): mcts = MCTS(policy='uct', exploration_weight=exploration_weight, budget=budget, n0=n0, sigma_0=sigma_0) tree = new_tree() for _ in range(budget): mcts.do_rollout(tree) next_tree = mcts.choose(tree) return (mcts, tree, next_tree)