Skip to content

Commit

Permalink
Merge pull request #214 from pmariglia/parallel-mcts
Browse files Browse the repository at this point in the history
Parallel mcts
  • Loading branch information
pmariglia authored Jan 2, 2025
2 parents fe10a0f + f3609e9 commit 500f217
Show file tree
Hide file tree
Showing 17 changed files with 29,183 additions and 5,100 deletions.
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class _FoulPlayConfig:
bot_mode: str
pokemon_mode: str = ""
search_time_ms: int
parallelism: int
run_count: int
team: str
user_to_challenge: str
Expand All @@ -83,6 +84,7 @@ def configure(self):
self.pokemon_mode = env("POKEMON_MODE")

self.search_time_ms = env.int("SEARCH_TIME_MS", 100)
self.parallelism = env.int("MCTS_PARALLELISM", 1)

self.run_count = env.int("RUN_COUNT", 1)
self.team = env("TEAM_NAME", None)
Expand Down
28 changes: 28 additions & 0 deletions data/mods/apply_mods.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import constants
from data import all_move_json
from data import pokedex
from fp.helpers import (
DAMAGE_MULTIPICATION_ARRAY,
POKEMON_TYPE_INDICES,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -32,6 +36,15 @@
}


def _steel_resists_dark_and_ghost():
DAMAGE_MULTIPICATION_ARRAY[POKEMON_TYPE_INDICES["ghost"]][
POKEMON_TYPE_INDICES["steel"]
] = 0.5
DAMAGE_MULTIPICATION_ARRAY[POKEMON_TYPE_INDICES["dark"]][
POKEMON_TYPE_INDICES["steel"]
] = 0.5


def apply_move_mods(gen_number):
logger.debug("Applying move mod for gen {}".format(gen_number))
for gen_number in reversed(range(gen_number, CURRENT_GEN)):
Expand All @@ -57,6 +70,7 @@ def apply_gen_3_mods():
apply_move_mods(3)
apply_pokedex_mods(4) # no pokedex mods in gen3 so use gen4
undo_physical_special_split()
_steel_resists_dark_and_ghost()


# these are the same as gen3
Expand All @@ -70,6 +84,18 @@ def apply_gen_1_mods():
pokedex_mods = json.load(f)
for pokemon, modifications in pokedex_mods.items():
pokedex[pokemon].update(modifications)
DAMAGE_MULTIPICATION_ARRAY[POKEMON_TYPE_INDICES["ice"]][
POKEMON_TYPE_INDICES["fire"]
] = 1
DAMAGE_MULTIPICATION_ARRAY[POKEMON_TYPE_INDICES["ghost"]][
POKEMON_TYPE_INDICES["psychic"]
] = 0
DAMAGE_MULTIPICATION_ARRAY[POKEMON_TYPE_INDICES["poison"]][
POKEMON_TYPE_INDICES["bug"]
] = 2
DAMAGE_MULTIPICATION_ARRAY[POKEMON_TYPE_INDICES["bug"]][
POKEMON_TYPE_INDICES["poison"]
] = 2


def apply_gen_4_mods():
Expand All @@ -78,6 +104,7 @@ def apply_gen_4_mods():
constants.REQUEST_DICT_ABILITY = "baseAbility"
apply_move_mods(4)
apply_pokedex_mods(4)
_steel_resists_dark_and_ghost()


def apply_gen_5_mods():
Expand All @@ -86,6 +113,7 @@ def apply_gen_5_mods():
constants.REQUEST_DICT_ABILITY = "baseAbility"
apply_move_mods(5)
apply_pokedex_mods(5)
_steel_resists_dark_and_ghost()


def apply_gen_6_mods():
Expand Down
4 changes: 2 additions & 2 deletions data/mods/gen1_pokedex_mods.json
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@
},
"magnemite": {
"types": [
"Electric"
"electric"
],
"baseStats": {
"hp": 25,
Expand All @@ -814,7 +814,7 @@
},
"magneton": {
"types": [
"Electric"
"electric"
],
"baseStats": {
"hp": 50,
Expand Down
34 changes: 25 additions & 9 deletions data/pkmn_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class PokemonSet:
nature: str
evs: Tuple[int, int, int, int, int, int]
count: int
level: Optional[int] = 100
tera_type: Optional[str] = None

def set_makes_sense(self, pkmn: Pokemon, match_traits):
Expand Down Expand Up @@ -254,12 +255,13 @@ def _initialize_pkmn_sets(self):
self.pkmn_sets[pkmn] = []
for set_, count in sets.items():
set_split = set_.split(",")
item = set_split[0]
ability = set_split[1]
moves = set_split[2:6]
level = int(set_split[0])
item = set_split[1]
ability = set_split[2]
moves = set_split[3:7]
tera_type = None
if len(set_split) > 6:
tera_type = set_split[6]
if len(set_split) > 7:
tera_type = set_split[7]
self.pkmn_sets[pkmn].append(
PredictedPokemonSet(
pkmn_set=PokemonSet(
Expand All @@ -269,6 +271,7 @@ def _initialize_pkmn_sets(self):
evs=(85, 85, 85, 85, 85, 85),
count=count,
tera_type=tera_type,
level=level,
),
pkmn_moveset=PokemonMoveset(moves=moves),
)
Expand Down Expand Up @@ -313,11 +316,24 @@ def predict_set(

return None

def pokemon_can_have_move(self, pkmn: Pokemon, move: str) -> bool:
def get_all_remaining_sets(
self, pkmn: Pokemon, match_traits=True
) -> list[PredictedPokemonSet]:
if not self.pkmn_sets:
logger.warning("Called `predict_set` when pkmn_sets was empty")
return []

remaining_sets = []
for pkmn_set in self.get_pkmn_sets_from_pkmn_name(pkmn.name, pkmn.base_name):
if move in pkmn_set.pkmn_moveset.moves:
return True
return False
if pkmn_set.full_set_pkmn_can_have_set(
pkmn,
match_ability=match_traits,
match_item=match_traits,
speed_check=False, # speed check never makes sense for randombattles because we know the nature/evs
):
remaining_sets.append(pkmn_set)

return remaining_sets


class _TeamDatasets(PokemonSets):
Expand Down
Loading

0 comments on commit 500f217

Please sign in to comment.