Пример #1
0
def num_threads_and_time(class_under_test, options: MctsPlayerOptions):
  # pylint: disable=too-many-locals,cell-var-from-loop
  data = []
  for seed in range(NUM_SEEDS):
    game_state = GameState.new(random_seed=seed)
    for num_threads in [1, 2, 4, 6, 8]:
      options.num_processes = num_threads
      mcts = class_under_test(game_state.next_player, cheater=False,
                              options=options)
      timer = timeit.Timer(
        lambda: mcts.request_next_action(game_state.next_player_view()))
      number, time_taken = timer.autorange()
      duration_sec = time_taken / number
      logging.info("Mcts took %.5f seconds using %d threads (seed=%s)",
                   duration_sec, num_threads, seed)
      data.append((seed, num_threads, duration_sec))
      mcts.cleanup()

  # Save the dataframe with the timing info.
  dataframe = DataFrame(data, columns=["seed", "num_threads", "duration_sec"])
  folder = os.path.join(os.path.dirname(__file__), "data")
  csv_path = os.path.join(folder, "num_threads_and_time.csv")
  # noinspection PyTypeChecker
  dataframe.to_csv(csv_path, index=False)

  # Plot the timing data obtained.
  for seed in sorted(dataframe.seed.drop_duplicates()):
    filtered_dataframe = dataframe[dataframe["seed"].eq(seed)]
    plt.plot(filtered_dataframe.num_threads, filtered_dataframe.duration_sec,
             label=None, alpha=0.5)
    plt.scatter(filtered_dataframe.num_threads, filtered_dataframe.duration_sec,
                s=10)
  mean = dataframe.groupby("num_threads").mean().sort_index()
  plt.plot(mean.index, mean.duration_sec, label="Average", color="r",
           linewidth=3)
  plt.grid(which="both", linestyle="--")
  plt.legend(loc=0)
  plt.xlabel("Number of threads")
  plt.ylabel("Duration (seconds)")
  plt.title(f"{class_under_test.__name__}: " +
            f"{options.max_permutations} permutations x " +
            f"{options.max_iterations} iterations on\n" +
            cpuinfo.get_cpu_info()["brand_raw"])
  plt.savefig(os.path.join(folder, "num_threads_and_time.png"))
 def _play_against_another_mcts_player_until_the_end(
         self, game_state) -> GameState:
     player_two: Optional[MctsPlayer] = None
     try:
         options = MctsPlayerOptions(max_iterations=None)
         player_class = self._mcts_player.__class__
         if player_class == CythonMctsPlayer:
             options.num_processes = 1
         player_two = player_class(PlayerId.TWO, options=options)
         players = PlayerPair(self._mcts_player, player_two)
         while not game_state.is_game_over:
             player = players[game_state.next_player]
             action = player.request_next_action(
                 game_state.next_player_view())
             print(f"{game_state.next_player}: {action}")
             game_state = action.execute(game_state)
     finally:
         if player_two is not None:
             player_two.cleanup()
     return game_state