def test_writer_basic(): """Basic test for LogWriter class.""" lw1 = LogWriter(header=HEADER, prefix=TEST_ID) dict_to_write = {} dict_to_write["X"] = 10 dict_to_write["pew"] = "pew" dict_to_write["Y"] = 20 lw1.write_line(dict_to_write) lw1.write_line(dict_to_write)
def create_invalid_file(): """ Create invalid file to test reader logic. This file will have different headers, so it should cause LogReader to generate a warning message. """ sleep(0.001) lw_invalid = LogWriter(header=HEADER + ["pew2"], prefix=TEST_ID) dict_to_write = {} dict_to_write["X"] = 10 dict_to_write["pew"] = "pew" dict_to_write["pew2"] = "pew2" dict_to_write["Y"] = 20 lw_invalid.write_line(dict_to_write) lw_invalid.write_line(dict_to_write)
class PokemonSimulation(BaseLoggingSimulation): """Class for Pokemon Simulation.""" def __init__(self, **kwargs): """ Initialize this simulation. Args: config (str): Filename for the population configs. data_delay (int): Number of matches between gathering type data. multithread (bool): Whether or not to run this simulation multithreaded. """ pkmn_kwargs = kwargs pkmn_kwargs["game"] = PokemonEngine() pkmn_kwargs["prefix"] = "PKMN" self.config = load_config(kwargs["config"]) self.type_log_writer = None self.data_delay = kwargs["data_delay"] self.multithread = kwargs.get("multithread", False) super().__init__(pkmn_kwargs) def add_agents(self): """Add the agents to this model.""" for conf in self.config: conf_tr = TeamReader(prefix=conf["team_file"]) conf_tr.process_files() conf_team = conf_tr.teams[0] for _ in range(int(self.num_players * conf["proportion"])): pkmn_agent = None if conf["agent_class"] == "basic": pkmn_agent = PokemonAgent(team=conf_team) pkmn_agent.type = conf["agent_type"] elif conf["agent_class"] == "basicplanning": pkmn_agent = BasicPlanningPokemonAgent( tier=conf["agent_tier"], team=conf_team) pkmn_agent.type = conf["agent_type"] else: raise RuntimeError("Invalid agent_class: {}".format( conf["agent_class"])) self.ladder.add_player(pkmn_agent) def init_type_log_writer(self): """Initialize Type Average Elo LogWriter.""" header = [] for conf in self.config: header.append(conf["agent_type"]) self.type_log_writer = LogWriter(header, prefix="PKMNTypes") def run(self): """Run this simulation.""" if not self.multithread: super().run() return battle_queue = Queue() battle_results_queue = Queue() type_results_queue = Queue() for num in range(self.num_games): battle_queue.put(num) start_time = time() # Threads to run the battles for _ in range(4): battle_thread = Thread(target=battle, args=(self, battle_queue, battle_results_queue, type_results_queue, start_time)) battle_thread.start() battle_queue.join() while not battle_results_queue.empty(): output, player1, player2 = battle_results_queue.get() self.write_player_log(output, player1, player2) battle_results_queue.task_done() while not type_results_queue.empty(): data_line = type_results_queue.get() self.type_log_writer.write_line(data_line) type_results_queue.task_done()