def play_game(): tree = monte_carlo_tree_searchV2.MCTS(save_data=False, C=math.sqrt(2), alpha=.5, player=1, file1='v2_sim50_heur2_children.txt', file2='v2_sim50_heur2_num_visit.txt', file3='v2_sim50_heur2_rewards.txt', file4='v2_sim50_heur2_heur.txt', heur_num=2, sim_num=50) board = STATE.State() print(board) for i in range(100000): if i % 1000 == 0: tree.save_data() tree.do_iteration(board) if i % 100 == 0: print("----------------------------------iteration running now", end=' ') print(i) print(tree.find_best_child) print("done ya lucky bastard")
def play_game(): tree = monte_carlo_tree_searchV2.MCTS(save_data=False, alpha=.5, player=1) board = STATE.State() print(board) for i in range(1): board = STATE.State() while True: for i in range(50): tree.do_iteration(board) board = tree.find_best_child(board) print(board) if board.isTerminal(): break _tuple = tuple(board.getActions()) board = board.takeAction(random.choice(_tuple)) if board.isTerminal(): break tree.save_data() print("done ya lucky bastard")
def play_game(your_player_num): pool = mp.Pool(processes=mp.cpu_count() - 1) tree = monte_carlo_tree_searchV2.MCTS( save_data=True, C=.5, alpha=.5, player=your_player_num % 2 + 1, file1='pkl_sim50_heur1_children.marshal', file2='pkl_sim50_heur1_num_visit.marshal', file3='pkl_sim50_heur1_rewards.marshal', file4='pkl_sim50_heur1_heur.marshal', sim_num=1) board = STATE.State() print(board) seen = True path_given = [] path_given.append(board) if your_player_num == 2: while True: if seen == False: best_move = float("-inf") best_action = None results = [ pool.apply_async(MiniMaxV2.payoff, args=(board.takeAction(i), 0, board.turn)) for i in board.getActions() ] output = [p.get() for p in results] new_board = max(output, key=lambda x: x[0])[1] action = new_board.getTilePostions( board.turn) - board.getTilePostions(board.turn) board = board.takeAction(action.pop()) seen = True else: for i in range(50): tree.do_iteration(board, path_given) board = tree.find_best_child(board) path_given.append(board) print(board) if board.terminal: tree.save_data_pickle() break print("your move --------- length tree->", end=" ") print(len(tree.Children), end=" - ") print("num actions", end=" ") print(len(board.getActions())) x_y_z = input("enter x,y,z:\n") if x_y_z == "e": tree.save_data_pickle() break x, y, z = map(int, x_y_z.split(",")) if (x, y, z) not in board.getActions(): print("action not in possible actions") continue new_board = board.takeAction((x, y, z)) path_given.append(new_board) if new_board in tree.Children: print("already seen -- resistance is futile") else: print("this is new, let me think") seen = False tree.expand(new_board) board = new_board else: while True: print("your move --------- length tree->", end=" ") print(len(tree.Children), end=" - ") print("num actions", end=" ") print(len(board.getActions())) x_y_z = input("enter x,y,z:\n") if x_y_z == "e": tree.save_data_pickle() break x, y, z = map(int, x_y_z.split(",")) if (x, y, z) not in board.getActions(): print("action not in possible actions") continue new_board = board.takeAction((x, y, z)) path_given.append(new_board) if new_board in tree.Children: print("already seen -- resistance is futile") else: print("this is new, let me think") seen = False tree.expand(new_board) board = new_board if seen == False: best_move = float("-inf") best_action = None results = [ pool.apply_async(MiniMaxV2.payoff, args=(board.takeAction(i), 0, board.turn)) for i in board.getActions() ] output = [p.get() for p in results] new_board = max(output, key=lambda x: x[0])[1] action = new_board.getTilePostions( board.turn) - board.getTilePostions(board.turn) board = board.takeAction(action.pop()) seen = True else: for i in range(50): tree.do_iteration(board, path_given) board = tree.find_best_child(board) path_given.append(board) print(board) if board.terminal: tree.save_data_pickle() break
def play_game(log_file_p1, log_file_p2, game_log): # num_visit and rewards were flipped during da pickle tree2 = monte_carlo_tree_searchV2.MCTS(save_data=True, C=math.sqrt(2), alpha=.5, player=2, file1='pkl_sim50_heur2_children.marshal', file2='pkl_sim50_heur2_num_visit.marshal', file3='pkl_sim50_heur2_rewards.marshal', file4='pkl_sim50_heur2_heur.marshal', sim_num=1) tree1 = monte_carlo_tree_searchV2.MCTS(save_data=True, C=math.sqrt(2), alpha=.5, player=1, file1='pkl_sim50_heur1_children.marshal', file2='pkl_sim50_heur1_num_visit.marshal', file3='pkl_sim50_heur1_rewards.marshal', file4='pkl_sim50_heur1_heur.marshal', sim_num=1) board = STATE.State() print(board) start_time = time.time() with open(game_log, 'w') as gl: gl.write("log start: ") gl.write(str(time.time() - start_time)) gl.write("\n") with open(log_file_p1, 'w') as pl: pl.write("log start: ") pl.write(str(time.time() - start_time)) pl.write("\n") with open(log_file_p2, 'w') as p2: p2.write("log start: ") p2.write(str(time.time() - start_time)) p2.write("\n") while True: board = STATE.State() tree1.save_data_pickle() tree2.save_data_pickle() num_moves = 0 path_given = [] path_given.append(board) while True: for i in range(25): tree1.do_iteration(board, path_given) board = tree1.find_best_child(board) path_given.append(board) num_moves += 1 with open(game_log, 'a') as gl: gl.write(board.__str__()) gl.write("\n") if board in tree2.Children: print("-------------------------------->SEEN") print("tree2 valuation-->", end='') print(tree2.Rewards[board], end=' ') print("#visit:", end=' ') print(tree2.VisitCount[board]) print("tree1 valuation-->", end='') print(tree1.Rewards[board], end=' ') print("#visit:", end=' ') print(tree1.VisitCount[board]) else: print("--------------------------------->new") print(board) if board.terminal: if board.get_reward() == 1: with open(log_file_p1, 'a') as p1: p1.write(str(1)) p1.write(" ") p1.write(str(board.p1Score)) p1.write(" ") p1.write(str(num_moves)) p1.write("\n") with open(log_file_p2, 'a') as p2: p2.write(str(0)) p2.write(" ") p2.write(str(board.p2Score)) p2.write(" ") p2.write(str(num_moves)) p2.write("\n") if board.get_reward_adversarial() == 1: print("player 2 has crushed") with open(log_file_p1, 'a') as p1: p1.write(str(0)) p1.write(" ") p1.write(str(board.p1Score)) p1.write(" ") p1.write(str(num_moves)) p1.write("\n") with open(log_file_p2, 'a') as p2: p2.write(str(1)) p2.write(" ") p2.write(str(board.p2Score)) p2.write(" ") p2.write(str(num_moves)) p2.write("\n") break for i in range(25): tree2.do_iteration(board, path_given) board = tree2.find_best_child(board) path_given.append(board) num_moves += 1 with open(game_log, 'a') as gl: gl.write(board.__str__()) gl.write("\n") if board in tree1.Children: print("-------------------------------->SEEN") print("tree2 valuation-->", end='') print(tree2.Rewards[board], end=' ') print("#visit:", end=' ') print(tree2.VisitCount[board]) print("tree1 valuation-->", end='') print(tree1.Rewards[board], end=' ') print("#visit:", end=' ') print(tree1.VisitCount[board]) else: print("--------------------------------->new") print(board) if board.terminal: if board.get_reward() == 1: with open(log_file_p1, 'a') as p1: p1.write(str(1)) p1.write(" ") p1.write(str(board.p1Score)) p1.write(" ") p1.write(str(num_moves)) p1.write("\n") with open(log_file_p2, 'a') as p2: p2.write(str(0)) p2.write(" ") p2.write(str(board.p2Score)) p2.write(" ") p2.write(str(num_moves)) p2.write("\n") if board.get_reward_adversarial() == 1: print("player 2 has crushed") with open(log_file_p1, 'a') as p1: p1.write(str(0)) p1.write(" ") p1.write(str(board.p1Score)) p1.write(" ") p1.write(str(num_moves)) p1.write("\n") with open(log_file_p2, 'a') as p2: p2.write(str(1)) p2.write(" ") p2.write(str(board.p2Score)) p2.write(" ") p2.write(str(num_moves)) p2.write("\n") break if time.time() - start_time >= 39600: with open(game_log, 'a') as gl: gl.write(str(time.time() - start_time)) gl.write(str(num_moves)) gl.write("\n") break