def test_constant_teambuilder_yields_showdown(showdown_format_teams, packed_format_teams): for format_ in packed_format_teams: for showdown_team, packed_team in zip(showdown_format_teams[format_], packed_format_teams[format_]): tb = ConstantTeambuilder(showdown_team) for _ in range(10): assert tb.yield_team() == packed_team
def test_showdown_team_parsing_works_without_items(): team = """ Flareon Ability: Flash Fire EVs: 252 HP / 126 Def / 126 SpD / 4 Spe Hardy Nature - Flare Blitz - Superpower - Double-Edge - Iron Tail Ninetales Ability: Flash Fire EVs: 252 HP / 126 Def / 126 SpD / 4 Spe IVs: 0 Atk - Flamethrower - Extrasensory - Calm Mind - Dark Pulse Arcanine Ability: Flash Fire EVs: 252 HP / 126 Def / 126 SpD / 4 Spe - Flare Blitz - Wild Charge - Facade - Crunch Heatmor Ability: Flash Fire EVs: 252 HP / 126 Def / 126 SpD / 4 Spe - Flare Blitz - Body Slam - Night Slash - Stomping Tantrum Typhlosion Ability: Flash Fire EVs: 252 HP / 126 Def / 126 SpD / 4 Spe - Flamethrower - Extrasensory - Flare Blitz - Earthquake Rapidash Ability: Flash Fire EVs: 252 HP / 126 Def / 126 SpD / 4 Spe - Flare Blitz - Wild Charge - Drill Run - Poison Jab """ packed_team = "Flareon|||flashfire|flareblitz,superpower,doubleedge,irontail|Hardy|252,,126,,126,4|||||]Ninetales|||flashfire|flamethrower,extrasensory,calmmind,darkpulse||252,,126,,126,4||,0,,,,|||]Arcanine|||flashfire|flareblitz,wildcharge,facade,crunch||252,,126,,126,4|||||]Heatmor|||flashfire|flareblitz,bodyslam,nightslash,stompingtantrum||252,,126,,126,4|||||]Typhlosion|||flashfire|flamethrower,extrasensory,flareblitz,earthquake||252,,126,,126,4|||||]Rapidash|||flashfire|flareblitz,wildcharge,drillrun,poisonjab||252,,126,,126,4|||||" # noqa: E501 print(ConstantTeambuilder(team).yield_team()) assert ConstantTeambuilder(team).yield_team() == packed_team
def get_teambuilder_from_string(s): list_of_teams = s.split(",") #stronger_starters,starters new_teams = [teams[team_name] for team_name in list_of_teams] if len(new_teams) == 1: return ConstantTeambuilder(new_teams[0]) else: return RandomTeamFromPool(new_teams)
def __init__( self, player_configuration: PlayerConfiguration, *, avatar: Optional[int] = None, battle_format: str, log_level: Optional[int] = None, max_concurrent_battles: int = 1, server_configuration: ServerConfiguration, start_listening: bool = True, team: Optional[Union[str, Teambuilder]] = None, ) -> None: """ :param player_configuration: Player configuration. :type player_configuration: PlayerConfiguration :param avatar: Player avatar id. Optional. :type avatar: int, optional :param battle_format: Name of the battle format this player plays. :type battle_format: str :param log_level: The player's logger level. :type log_level: int. Defaults to logging's default level. :param max_concurrent_battles: Maximum number of battles this player will play concurrently. If 0, no limit will be applied. Defaults to 1. :type max_concurrent_battles: int :param server_configuration: Server configuration. :type server_configuration: ServerConfiguration :param start_listening: Wheter to start listening to the server. Defaults to True. :type start_listening: bool :param team: The team to use for formats requiring a team. Can be a showdown team string, a showdown packed team string, of a ShowdownTeam object. Defaults to None. :type team: str or Teambuilder, optional """ super(Player, self).__init__( player_configuration=player_configuration, avatar=avatar, log_level=log_level, server_configuration=server_configuration, start_listening=start_listening, ) self._format: str = battle_format self._max_concurrent_battles: int = max_concurrent_battles self._battles: Dict[str, Battle] = {} self._battle_semaphore: Semaphore = Semaphore(0) self._battle_start_condition: Condition = Condition() self._battle_count_queue: Queue = Queue(max_concurrent_battles) self._challenge_queue: Queue = Queue() if isinstance(team, Teambuilder): self._team = team elif isinstance(team, str): self._team = ConstantTeambuilder(team) else: self._team = None self.logger.debug("Player initialisation finished")
def __init__( self, player_configuration: Optional[PlayerConfiguration] = None, *, avatar: Optional[int] = None, battle_format: str = "gen8randombattle", log_level: Optional[int] = None, max_concurrent_battles: int = 1, save_replays: Union[bool, str] = False, server_configuration: Optional[ServerConfiguration] = None, start_timer_on_battle_start: bool = False, start_listening: bool = True, team: Optional[Union[str, Teambuilder]] = None, ) -> None: """ :param player_configuration: Player configuration. If empty, defaults to an automatically generated username with no password. This option must be set if the server configuration requires authentication. :type player_configuration: PlayerConfiguration, optional :param avatar: Player avatar id. Optional. :type avatar: int, optional :param battle_format: Name of the battle format this player plays. Defaults to gen8randombattle. :type battle_format: str :param log_level: The player's logger level. :type log_level: int. Defaults to logging's default level. :param max_concurrent_battles: Maximum number of battles this player will play concurrently. If 0, no limit will be applied. Defaults to 1. :type max_concurrent_battles: int :param save_replays: Whether to save battle replays. Can be a boolean, where True will lead to replays being saved in a potentially new /replay folder, or a string representing a folder where replays will be saved. :type save_replays: bool or str :param server_configuration: Server configuration. Defaults to Localhost Server Configuration. :type server_configuration: ServerConfiguration, optional :param start_listening: Whether to start listening to the server. Defaults to True. :type start_listening: bool :param start_timer_on_battle_start: Whether to automatically start the battle timer on battle start. Defaults to False. :type start_timer_on_battle_start: bool :param team: The team to use for formats requiring a team. Can be a showdown team string, a showdown packed team string, of a ShowdownTeam object. Defaults to None. :type team: str or Teambuilder, optional """ if player_configuration is None: player_configuration = _create_player_configuration_from_player(self) if server_configuration is None: server_configuration = LocalhostServerConfiguration super(Player, self).__init__( player_configuration=player_configuration, avatar=avatar, log_level=log_level, server_configuration=server_configuration, start_listening=start_listening, ) self._format: str = battle_format self._max_concurrent_battles: int = max_concurrent_battles self._save_replays = save_replays self._start_timer_on_battle_start: bool = start_timer_on_battle_start self._battles: Dict[str, AbstractBattle] = {} self._battle_semaphore: Semaphore = Semaphore(0) self._battle_start_condition: Condition = Condition() self._battle_count_queue: Queue = Queue(max_concurrent_battles) self._battle_end_condition: Condition = Condition() self._challenge_queue: Queue = Queue() if isinstance(team, Teambuilder): self._team = team elif isinstance(team, str): self._team = ConstantTeambuilder(team) else: self._team = None self.logger.debug("Player initialisation finished")
def main(args): global rb global p1 global p2 global test_scores global loop global buffer_size global memory_size global rb_beta memory_size = args.memory_size rb_beta = args.rb_beta buffer_size = 0 test_scores = {} test_score_max = -1e7 with open("/users/atraylor/current_server.txt") as rf: server_name = rf.readlines()[0].split(".")[0] server_configuration = manual_server(server_name)#LocalhostServerConfiguration# wandb.init() if wandb.run.name is not None: run_name = wandb.run.name else: run_name = "unlabeled-run-1" writepath = os.path.join("results/",run_name) replaypath = os.path.join(writepath, "replays") if not os.path.exists(writepath): os.makedirs(writepath) if not os.path.exists(replaypath): os.makedirs(replaypath) env_dict = {"obs": {"shape": (440, 1)}, "act": {}, "rew": {}, "next_obs": {"shape": (440, 1)}, "done": {} } if args.nstep_returns > 1: nstepdict = {"size": args.nstep_returns, "gamma": args.gamma, "rew": "rew", "next": "next_obs"} rb = cpprb.PrioritizedReplayBuffer(args.memory_size, env_dict, Nstep=nstepdict)#ReplayMemory(config.memory_size) else: rb = cpprb.PrioritizedReplayBuffer(args.memory_size, env_dict) p1_teambuilder = get_teambuilder_from_string(args.player_teams) p2_teambuilder = get_teambuilder_from_string(args.opponent_teams) battle_format = "gen8ou" # Pick player AI based on string p1_class = str_to_player_class.get(args.player_ai, RandomEnvPlayer) if p1_class == RandomEnvPlayer: print("Using random env player for p1") player_name = "p1 " + args.player_ai + " " + run_name p1 = p1_class(name=player_name, shortname="p1", team=p1_teambuilder, battle_format=battle_format, server_configuration=server_configuration, args = args) if args.saved_model is not None: p1.policy_net_theta.load_state_dict(torch.load(args.saved_model)) if args.voltron == True: print("TODO: edit player names or it will get really mad") substrategies = [] if args.selfplay == True: substrategies.append(MediumQPlayer(name="p2", team=p2_teambuilder, server_configuration=server_configuration)) substrategies.append(RandomEnvPlayer(name="p2", team=p2_teambuilder, server_configuration=server_configuration)) substrategies.append(MaxDamagePlayer(name="p2", team=p2_teambuilder, server_configuration=server_configuration)) p2 = VoltronPlayer(name="p2", team=p2_teambuilder, server_configuration=server_configuration, substrategies = substrategies) else: if args.selfplay == True: p2_class = p1_class else: p2_class = str_to_player_class.get(args.opponent_ai, RandomEnvPlayer) if p2_class == RandomEnvPlayer: print("Using random env player for p2") p2_name = "p2 " + args.opponent_ai + " " + run_name p2 = p2_class(name=p2_name, battle_format=battle_format, shortname="p2", team=p2_teambuilder, server_configuration=server_configuration) p1._start_new_battle = True p2._start_new_battle = True loop = asyncio.get_event_loop() if args.train: #TODO: this might be differnt in some situations! inloop_test_teambuilder = get_teambuilder_from_string(args.opponent_teams) inloop_test_algorithms = ["random", "max", "shpp"] inloop_test_players = [] for inloop_test_alg in inloop_test_algorithms: inloop_test_class = str_to_player_class.get(inloop_test_alg, RandomEnvPlayer) test_class_name = inloop_test_alg + " " + run_name inloop_test_player = inloop_test_class( name=test_class_name, shortname=inloop_test_alg, battle_format=battle_format, team=inloop_test_teambuilder, server_configuration=server_configuration) inloop_test_player._start_new_battle = True inloop_test_players.append(inloop_test_player) if args.selfplay == True: if args.voltron == True: inloop_test_players += [p2.substrategies[0]] else: inloop_test_players += [p2] FIT_INTERVAL = args.fit_interval N_EPOCHS = np.floor(args.n_battles_train * 1.0 / FIT_INTERVAL) for epoch in range(int(N_EPOCHS)): p1.policy_net_theta.train() #TRAIN env_algorithm_kwargs = {"n_battles": FIT_INTERVAL, "selfplay": args.selfplay, "battles_so_far": epoch * FIT_INTERVAL, "fit_interval" : FIT_INTERVAL, "target_update": args.target_update} t1 = Thread(target=lambda: env_algorithm_wrapper(p1, env_algorithm_kwargs)) t1.start() t2 = Thread(target=lambda: env_algorithm_wrapper(p2, env_algorithm_kwargs)) t2.start() p1._start_new_battle = True while p1._start_new_battle: loop.run_until_complete(launch_battles(p1, p2)) t1.join() t2.join() #TEST '''p1.policy_net_theta.eval() for test_player in inloop_test_players: test_env_algorithm_kwargs = { "n_battles": args.n_battles_eval, "selfplay": args.selfplay, "test": True, "player2_name": test_player.shortname } t3 = Thread(target=lambda: env_algorithm_wrapper(p1, test_env_algorithm_kwargs)) t3.start() t4 = Thread(target=lambda: env_algorithm_wrapper(test_player, test_env_algorithm_kwargs)) t4.start() test_player._start_new_battle = True while test_player._start_new_battle: loop.run_until_complete(launch_battles(p1, test_player)) t3.join() t4.join() score = test_scores.get("test_UBR_{}".format(args.opponent_ai), None) if score is None or score > test_score_max: print("saving model...") test_score_max = score torch.save(p1.policy_net_theta.state_dict(), os.path.join(writepath, "saved_model_theta.torch"))''' if args.test: p1.policy_net_theta.eval() print("Testing now") end_test_teambuilder = get_teambuilder_from_string(args.opponent_teams) end_test_algorithms= ["shpp", "random", "max"] end_test_players = [] for end_test_alg in end_test_algorithms: end_test_class = str_to_player_class.get(end_test_alg, RandomEnvPlayer) if type(end_test_teambuilder) == RandomTeamFromPool: for team_name, team in zip(args.opponent_teams.split(","),end_test_teambuilder.teams): constant_teambuilder = ConstantTeambuilder(team) player_name = end_test_alg + " " + team_name + " " + run_name alg_shortname = end_test_alg + " " + team_name end_test_player = end_test_class(name=player_name, shortname = alg_shortname, battle_format=battle_format, team=constant_teambuilder, server_configuration=server_configuration, save_replays=replaypath) end_test_player._start_new_battle = True end_test_players.append(end_test_player) else: player_name = end_test_alg + " " + run_name end_test_player = end_test_class(name=player_name, shortname = end_test_alg, battle_format=battle_format, team=end_test_teambuilder, server_configuration=server_configuration, save_replays=replaypath) end_test_player._start_new_battle = True end_test_players.append(end_test_player) if p2 is not None and args.selfplay == True: if args.voltron == True: end_test_players += [p2.substrategies[0]] else: end_test_players += [p2] for test_player in end_test_players: test_env_algorithm_kwargs = { "n_battles": args.n_battles_test - 1, "selfplay": args.selfplay, "test": True, "player2_name": test_player.shortname } t3 = Thread(target=lambda: env_algorithm_wrapper(p1, test_env_algorithm_kwargs)) t3.start() t4 = Thread(target=lambda: env_algorithm_wrapper(test_player, test_env_algorithm_kwargs)) t4.start() test_player._start_new_battle = True while test_player._start_new_battle: loop.run_until_complete(launch_battles(p1, test_player)) t3.join() t4.join() print("saving model...") torch.save(p1.policy_net_theta.state_dict(), os.path.join(writepath, "saved_model_theta.torch")) #transitions_path = os.path.join(writepath, "transitions") #if not os.path.exists(transitions_path): # os.makedirs(transitions_path) '''fconfig = open("results/"+run_name+"/config.txt","w+")
def test_constant_teambuilder_yields_packed(packed_format_teams): for format_, teams in packed_format_teams.items(): for team in teams: tb = ConstantTeambuilder(team) for _ in range(10): assert tb.yield_team() == team