def test_simple_heuristics_player_stat_estimation(): player = SimpleHeuristicsPlayer(start_listening=False) mon = Pokemon(species="charizard") assert player._stat_estimation(mon, "spe") == 236 mon._boost("spe", 2) assert player._stat_estimation(mon, "spe") == 472 mon._boost("atk", -1) assert player._stat_estimation(mon, "atk") == 136
async def cross_evaluation(n_battles, format_, teams): if len(teams) > 1: players = [ SimpleHeuristicsPlayer(battle_format=format_, max_concurrent_battles=n_battles, team=team) for i, team in enumerate(teams) ] else: assert len(teams) == 1 players = [ SimpleHeuristicsPlayer(battle_format=format_, max_concurrent_battles=n_battles, team=teams[0]) for _ in range(2) ] await cross_evaluate(players, n_challenges=n_battles) for player in players: await player.stop_listening()
async def simple_cross_evaluation(n_battles, n_concurrent_battles): players = [ SimpleHeuristicsPlayer(max_concurrent_battles=n_concurrent_battles), MoveCallTrackingRandomPlayer( max_concurrent_battles=n_concurrent_battles), ] await cross_evaluate(players, n_challenges=n_battles) for player in players: await player.stop_listening() return players[-1].move_history
def test_simple_heuristics_player_should_dynamax(): PseudoBattle = namedtuple( "PseudoBattle", ["active_pokemon", "opponent_active_pokemon", "team", "can_dynamax"], ) player = SimpleHeuristicsPlayer(start_listening=False) battle = PseudoBattle(Pokemon(species="charmander"), Pokemon(species="charmander"), {}, False) assert player._should_dynamax(battle, 4) is False battle = PseudoBattle(Pokemon(species="charmander"), Pokemon(species="charmander"), {}, True) assert player._should_dynamax(battle, 1) is True battle.active_pokemon._set_hp("100/100") battle.team["charmander"] = battle.active_pokemon assert player._should_dynamax(battle, 4) is True battle = PseudoBattle( Pokemon(species="squirtle"), Pokemon(species="charmander"), { "kakuna": Pokemon(species="kakuna"), "venusaur": Pokemon(species="venusaur"), "charmander": Pokemon(species="charmander"), }, True, ) for mon in battle.team.values(): mon._set_hp("100/100") battle.active_pokemon._set_hp("100/100") battle.opponent_active_pokemon._set_hp("100/100") assert player._should_dynamax(battle, 4) is True
def test_simple_heuristics_player(): player = SimpleHeuristicsPlayer(start_listening=False) PseudoBattle = namedtuple( "PseudoBattle", ( "active_pokemon", "opponent_active_pokemon", "available_moves", "available_switches", "team", "can_dynamax", "side_conditions", "opponent_side_conditions", ), ) battle = PseudoBattle( Pokemon(species="dragapult"), Pokemon(species="gengar"), [], [Pokemon(species="togekiss")], {}, True, set(), set(), ) battle.active_pokemon._last_request["stats"] = { stat: 100 for stat in battle.active_pokemon.base_stats } battle.available_switches[0]._set_hp("100/100") assert player.choose_move(battle).message == "/choose switch togekiss" battle.available_moves.append(Move("quickattack")) assert player.choose_move(battle).message == "/choose move quickattack" battle.available_moves.append(Move("flamethrower")) assert player.choose_move(battle).message == "/choose move flamethrower" battle.available_moves.append(Move("dracometeor")) assert player.choose_move(battle).message == "/choose move dracometeor" battle.active_pokemon._boost("atk", -3) battle.active_pokemon._boost("spa", -3) battle.available_switches.append(Pokemon(species="sneasel")) battle.available_switches[1]._set_hp("100/100") assert player.choose_move(battle).message == "/choose switch sneasel"
def test_simple_heuristics_player_should_switch_out(): PseudoBattle = namedtuple( "PseudoBattle", ["active_pokemon", "opponent_active_pokemon", "available_switches"], ) player = SimpleHeuristicsPlayer(start_listening=False) battle = PseudoBattle(Pokemon(species="charmander"), Pokemon(species="charmander"), []) battle.active_pokemon._last_request["stats"] = { stat: 0 for stat in battle.active_pokemon.base_stats } assert player._should_switch_out(battle) is False battle.available_switches.append(Pokemon(species="venusaur")) assert player._should_switch_out(battle) is False battle.available_switches.append(Pokemon(species="gyarados")) assert player._should_switch_out(battle) is False battle.active_pokemon._boost("spa", -3) battle.active_pokemon._last_request["stats"].update({"atk": 10, "spa": 20}) assert player._should_switch_out(battle) is True battle.active_pokemon._last_request["stats"].update({"atk": 30, "spa": 20}) assert player._should_switch_out(battle) is False battle.active_pokemon._boost("atk", -3) assert player._should_switch_out(battle) is True battle = PseudoBattle( Pokemon(species="gible"), Pokemon(species="mamoswine"), [Pokemon(species="charizard")], ) assert player._should_switch_out(battle) is True
def test_simple_heuristics_player_estimate_matchup(): player = SimpleHeuristicsPlayer(start_listening=False) dragapult = Pokemon(species="dragapult") assert player._estimate_matchup(dragapult, dragapult) == 0 gengar = Pokemon(species="gengar") assert player._estimate_matchup( dragapult, gengar) == -player._estimate_matchup(gengar, dragapult) assert player._estimate_matchup(dragapult, gengar) == player.SPEED_TIER_COEFICIENT mamoswine = Pokemon(species="mamoswine") assert (player._estimate_matchup(dragapult, mamoswine) == -1 + player.SPEED_TIER_COEFICIENT) dragapult._set_hp("100/100") mamoswine._set_hp("50/100") assert (player._estimate_matchup(dragapult, mamoswine) == -1 + player.SPEED_TIER_COEFICIENT + player.HP_FRACTION_COEFICIENT / 2)
async def train(hparams, fp): wandb.config.update(hparams) p_dict = hparams.policy a_dict = hparams.agent SEngine = importlib.import_module('pokebot.bots.state_engine') se_clazz = getattr(SEngine, hparams.state_engine) se_dict = hparams.se_params if hparams.se_params else {} player = BotPlayer( player_configuration=PlayerConfiguration("test", None), state_engine=se_clazz(**se_dict) ) m_lib = importlib.import_module('pokebot.models') m_clazz = getattr(m_lib, hparams.model) m_dict = hparams.model_params if hparams.model_params else {} model = m_clazz(player, **m_dict) train_lib = importlib.import_module('pokebot.trainers.trainer') t_class = getattr(train_lib, hparams.trainer) t_dict = hparams.trainer_params if hparams.trainer_params else {} trainer = t_class(player, model, p_dict, a_dict, **t_dict) await trainer.train() opponents = [RandomPlayer(battle_format="gen8randombattle"), MaxBasePowerPlayer(battle_format="gen8randombattle"), SimpleHeuristicsPlayer(battle_format="gen8randombattle")] print("Beginning Eval") await trainer.evaluate(opponents) trainer.agent.save_weights(fp)
battle_format="gen8ou", team=custom_builder2, server_configuration=LocalhostServerConfiguration, ) second_opponent = MaxDamagePlayer( player_configuration=PlayerConfiguration("Max damage player", None), battle_format="gen8ou", team=custom_builder2, server_configuration=LocalhostServerConfiguration, ) third_opponent = SimpleHeuristicsPlayer( player_configuration=PlayerConfiguration("Simple heuristic player", None), battle_format="gen8ou", team=custom_builder2, server_configuration=LocalhostServerConfiguration, ) n_actions = len(env_player.action_space) if "ou" in config.our_team_name and not "starter" in config.our_team_name: model_type = SinglelineMediumBoy_DQN else: model_type = TeenyBoy_DQN policy_net = model_type(config) target_net = model_type(config) policy_net.load_state_dict(torch.load(config.saved_model)) target_net.load_state_dict(policy_net.state_dict()) target_net.eval()
player_configuration=PlayerConfiguration(rand_name, None), battle_format="gen8ou", team=custom_builder2, server_configuration=LocalhostServerConfiguration, ) max_dmg_agent = MaxDamagePlayer( player_configuration=PlayerConfiguration(max_name, None), battle_format="gen8ou", team=custom_builder2, server_configuration=LocalhostServerConfiguration, ) shp_agent = SimpleHeuristicsPlayer( player_configuration=PlayerConfiguration(shp_name, None), battle_format="gen8ou", team=custom_builder2, server_configuration=LocalhostServerConfiguration, ) eps_shp_agent = EpsilonRandomSimpleHeuristicsPlayer( player_configuration=PlayerConfiguration(eps_shp_name, None), battle_format="gen8ou", team=custom_builder2, server_configuration=LocalhostServerConfiguration, epsilon=config.shp_epsilon, ) opponent_names = ["random", "max_dmg", "shp", "eps_shp"] opponent_teams = [] if config.opponent_team_name == "two_teams":
return ai_config.get("Opponent", "Opponent").lower() == "ladder" from src.geniusect.neural_net.dqn_history import DQNHistory from src.geniusect.player.max_damage_player import MaxDamagePlayer from src.geniusect.player.default_player import DefaultPlayer from poke_env.player.baselines import SimpleHeuristicsPlayer if get_train_against_ladder(): opponents = {} else: opponents = { "default": DefaultPlayer(battle_format="gen8randombattle"), "random": RandomPlayer(battle_format="gen8randombattle"), "max": MaxDamagePlayer(battle_format="gen8randombattle"), "heuristics": SimpleHeuristicsPlayer(battle_format="gen8randombattle") } def get_opponent(battle_format="gen8randombattle", cycle_count=0) -> Player: opponent_string = ai_config.get("Opponent", "Opponent").lower() opponent_num = cycle_count % len(opponents) if opponent_string == "ladder": return None elif opponent_string == "random" or (opponent_string == "cycle" and opponent_num == 0): return opponents["random"] elif opponent_string == "default" or (opponent_string == "cycle" and opponent_num == 1): return opponents["default"]