def generate_games(predictor, n_simulations, iter_max, with_pool, n_processes, n_episodes): model_saver = ModelSaver() while True: if with_pool: begin_time = time.time() with mp.Pool(n_processes) as pool: episodes = pool.map(run_episode_raw, [(predictor, n_simulations, iter_max) for _ in range(n_episodes)]) model_saver.save_episodes_raw(episodes) print("Total time:", time.time() - begin_time) else: output_queue = mp.Queue() processes = [] saving_process = mp.Process(target=save_queue_process, args=(output_queue, n_episodes)) saving_process.start() for _ in range(n_processes): processes.append( mp.Process(target=run_episode_raw_loop, args=(predictor, n_simulations, iter_max, output_queue))) for process in processes: process.start() for process in processes: process.join()
def save_queue_process(queue, n_episodes): model_saver = ModelSaver() begin_time = time.time() while True: if queue.qsize() < n_episodes: time.sleep(1) continue episodes = [] for _ in range(n_episodes): episodes.append(queue.get()) model_saver.save_episodes_raw(episodes) print("Total time:", time.time() - begin_time) begin_time = time.time()
with open("arena_temp.json") as f: res = json.load(f) else: print("Nothing to load.") res = dict() return res if __name__ == "__main__": result = load() while True: players = ["random_mcts"] model_saver = ModelSaver() for i in range(model_saver.get_last_weight_index() + 1): players.append(i) if len(players) == 1: print("Not enough players") continue player_one_name, player_two_name = random.sample(players, k=2) player_one = get_player(player_one_name, Color.BLUE, model_saver) player_two = get_player(player_two_name, Color.RED, model_saver) player_one_name = str(player_one_name) player_two_name = str(player_two_name) if player_one_name not in result: result[player_one_name] = dict() if player_two_name not in result[player_one_name]:
def load_latest_model(): model_saver_temp = ModelSaver() model_saver_temp.load_latest_model(model)
def load_latest_model(): model_saver = ModelSaver() model_saver.load_latest_model(model)
def __call__(self, features): current_index = send_tensor(self.shr_name, features, self.lock) result = read_result(self.shr_name, current_index, self.lock) return result if __name__ == "__main__": if current_process().name == "MainProcess": mp.set_start_method("spawn", force=True) while True: with mp.Manager() as manager: print("Creating shared block") shr = create_shared_block() lock = manager.Lock() predictor = ProcessPredictor(shr.name, lock) model_saver = ModelSaver(BASE_DIR) print("Start Predictor Process") predictor_process = Process(target=predictor_loop, args=(shr.name, lock)) predictor_process.start() time.sleep(5) begin_time = time.time() with mp.Pool(N_POOLS) as pool: episodes = pool.map(run_episode_raw, [(predictor, N_SIMULATIONS, ITER_MAX) for _ in range(N_EPISODES)]) model_saver.save_episodes_raw(episodes) print("Total time:", time.time() - begin_time)