def get_user_move(em, color): userInput = input("What's your move? (Type it like a2a3)") lastInput = "xxxxx" if userInput[0] == "a": lastInput = lastInput[0] + "0" + lastInput[2:] elif userInput[0] == "b": lastInput = lastInput[0] + "1" + lastInput[2:] elif userInput[0] == "c": lastInput = lastInput[0] + "2" + lastInput[2:] lastInput = str(6 - int(userInput[1])) + lastInput[1:] lastInput = lastInput[:2] + str(6 - int(userInput[3])) + lastInput[4:] if userInput[2] == "a": lastInput = lastInput[:3] + "0" + lastInput[4:] elif userInput[2] == "b": lastInput = lastInput[:3] + "1" + lastInput[4:] elif userInput[2] == "c": lastInput = lastInput[:3] + "2" + lastInput[4:] pieceNotationBeforeMove = em.board[int(lastInput[0])][int(lastInput[1])] #No promotion if pieceNotationBeforeMove[1] != "P" or ( pieceNotationBeforeMove == "+P" and lastInput[0] != "1") or (pieceNotationBeforeMove == "-P" and lastInput[0] != "4"): lastInput = lastInput[:4] + pieceNotationBeforeMove[1] #Promotion else: lastInput = lastInput[:4] + "=R" incoming = mcts.State(em, color) outgoing = incoming.next_state(0, False, lastInput) return outgoing.BoardObject
def execute_generate_play(nnet_path, multiplikator=configs.SIMS_FAKTOR, exponent=configs.SIMS_EXPONENT): gc.collect() os.environ["CUDA_VISIBLE_DEVICES"] = "-1" nnet = Network.get_net(configs.FILTERS, configs.HIDDEN_SIZE, configs.OUT_FILTERS, configs.NUM_ACTIONS, configs.INPUT_SIZE, None, configs.NUM_RESIDUAL) nnet.load_weights(nnet_path) env = MillEnv.MillEnv() mcts_ = mcts.MonteCarloTreeSearch( mcts.State(zeros((1, 24)), 0, -env.isPlaying, env)) stmem = mcts_.generatePlay(nnet, multiplikator, exponent) del mcts_ del env keras.backend.clear_session() tf.compat.v1.reset_default_graph() del nnet gc.collect() return stmem
def real_game(modelname, time_limit, recommendation_count): mode = ask_question("What gamemode are you playing?", ["ap", "cm"]) side = ask_question("Which side are you playing on?", ["radiant", "dire"]) first = ask_question("Do you have first pick / ban?", ["y", "n"]) if mode == "ap": util.pick_ban_order = util.allpick_order else: util.pick_ban_order = util.cm_order radiant_goes_first = (side == "radiant" and first == "y") or (side == "dire" and first == "n") node = mcts_transpositions.Node(mcts.State(radiant_goes_first)) transpositions = dict() model = util.load_model(modelname) players_turn = (side == "radiant" and node.state.radiant_moves_next) or ( side == "dire" and not node.state.radiant_moves_next) while not node.state.is_terminal(): print_state(node.state) choices = node.state.get_actions() choices_sets = [set(i) for i in choices] (pick_ban, count) = util.pick_ban_order[node.state.pick_ban_position] subject = 'pick' if pick_ban == util.pick else 'ban' print("The next action is a", subject, "of", count, "heroes.") if players_turn: print("It is your turn. MCTS recommends the following heroes: ...") (_, root_node, transpositions) = mcts_transpositions.uct_search( model, initial_node=node, time_limit=time_limit, transpositions=transpositions) node = root_node def to_transpo(n): return transpositions[mcts_transpositions.state_to_key( n.state)] children = sorted( root_node.children, key=lambda n: to_transpo( n).total_simulated_reward / to_transpo(n).visit_count, reverse=True) for c in children[:recommendation_count]: print([ util.simple_heroes.ordered_to_name(i) for i in c.incoming_action ], to_transpo(c).total_simulated_reward / to_transpo(c).visit_count, to_transpo(c).visit_count) else: print("It is the other team's turn. What did they do?") players_turn = not players_turn choice = get_pick(node.state, pick_ban, count) print() assert (set(choice) in choices_sets) found = False for n in node.children: if n.incoming_action == choice: node = n node.parent = None node.incoming_action = None found = True if not found: node = mcts.Node(node.state.get_next_state(choice)) print('Done!') print_state(node.state) print('Predicting Radiant win probability with all models:') for model_name in util.all_models: model = util.load_model(model_name) print( model_name, ':', util.predict_radiant_win_probability( util.state_to_feature(node.state), model))
def setUp(self) -> None: self.env = MillEnv.MillEnv() import Network self.nnet = Network.get_net(96, 3, 256, 4, 1, 24, (8, 3, 4)) self.mcts = mcts.MonteCarloTreeSearch(mcts.State(np.zeros((1, 24)), 0, -self.env.isPlaying, self.env))
def test_dirichlet(self): self.env.makeMove(1) self.state = mcts.State(np.zeros((1, 24)), 0, -self.env.isPlaying, self.env) self.state.add_noise(np.random.default_rng()) self.assertNotEqual(self.state.priors[0, 0], 0)
def setUp(self) -> None: import Network self.env = MillEnv.MillEnv() self.state = mcts.State(np.zeros((1, 24)), 0, -self.env.isPlaying, self.env) self.nnet = Network.get_net(24, 3, 64, 4, 1, 24, (8, 3, 4))