Skip to content

Commit 6b89ac1

Browse files
style: pre-commit fixes
1 parent 3e2a7ab commit 6b89ac1

4 files changed

Lines changed: 49 additions & 31 deletions

File tree

src/p2lab/genetic/operations.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def locus_swap(
146146
team2: list[str],
147147
num_pokemon: int,
148148
allow_all: bool,
149-
locus: int = None,
149+
locus: int | None = None,
150150
) -> tuple(list[str], list[str]):
151151
"""
152152
A method of performing the crossover. Pick a 'locus' point: this
@@ -194,7 +194,7 @@ def slot_swap(
194194
team2: list[str],
195195
num_pokemon: int,
196196
allow_all: bool,
197-
k: int = None,
197+
k: int | None = None,
198198
) -> tuple(list[str], list[str]):
199199
"""
200200
A method of performing the crossover. This method randomly
@@ -294,7 +294,7 @@ def mutate(
294294
mutate_prob: float | np.ndarray,
295295
pokemon_population: list[str],
296296
allow_all: bool,
297-
k: int = None,
297+
k: int | None = None,
298298
):
299299
"""
300300
A mutation operation. At random, k members of a team are swapped with k
@@ -362,7 +362,7 @@ def fitness_mutate(
362362
fitness: np.array,
363363
pokemon_population: list[str],
364364
allow_all: bool,
365-
k: int = None,
365+
k: int | None = None,
366366
):
367367
"""
368368
A mutation operation. Does the same as regular mutation, except that

src/p2lab/genetic/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def crossover(
6161
team2_pokemon = team2_old.pokemon
6262

6363
# Randomly pick locus
64-
locus = random.sample(range(0, num_pokemon), k=1)[0]
64+
locus = random.sample(range(num_pokemon), k=1)[0]
6565

6666
# Check that the sliciing here works but you get the idea
6767
team1_new_pokemeon = team1_pokemon[0:locus] + team2_pokemon[locus:num_pokemon]

src/p2lab/pokemon/pokefactory.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def __init__(self, gen=1, drop_forms=True):
2121
self.dex2mon = {
2222
int(self.dex.pokedex[m]["num"]): m
2323
for m in self.dex.pokedex
24-
if "forme" not in self.dex.pokedex[m].keys()
24+
if "forme" not in self.dex.pokedex[m]
2525
}
2626
else:
2727
self.dex2mon = {
@@ -68,24 +68,24 @@ def make_pokemon(self, dexnum=None, generate_moveset=False, **kwargs):
6868
raise ValueError(msg)
6969
if dexnum is None:
7070
dexnum = np.random.choice(list(self.dex2mon.keys()))
71-
if generate_moveset or "moves" not in kwargs.keys():
71+
if generate_moveset or "moves" not in kwargs:
7272
poss_moves = self.get_allowed_moves(dexnum)
7373
moves = (
7474
np.random.choice(poss_moves, 4, replace=False)
7575
if len(poss_moves) > 3
7676
else poss_moves
7777
)
7878
kwargs["moves"] = moves
79-
if "ivs" not in kwargs.keys():
79+
if "ivs" not in kwargs:
8080
ivs = [31] * 6
8181
kwargs["ivs"] = ivs
82-
if "evs" not in kwargs.keys():
82+
if "evs" not in kwargs:
8383
# TODO: implement EV generation better
8484
evs = [510 // 6] * 6
8585
kwargs["evs"] = evs
86-
if "level" not in kwargs.keys():
86+
if "level" not in kwargs:
8787
kwargs["level"] = 100
88-
if "ability" not in kwargs.keys():
88+
if "ability" not in kwargs:
8989
kwargs["ability"] = np.random.choice(
9090
list(self.get_allowed_abilities(dexnum).values())
9191
)

target_api.md

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ running genetic algorithms to optimize teams.
77
Thank you to github copilot for helping me write this document lmao
88

99
```python3
10-
11-
team = generate_team(format='gen8randombattle', seed=1234)
10+
team = generate_team(format="gen8randombattle", seed=1234)
1211

1312
# a team object contains a list of 6 pokemon objects with their moves, items, etc.
1413
# it's also tracking the (normalised) number of wins/losses/ties for this team.
@@ -30,12 +29,12 @@ team = generate_team(format='gen8randombattle', seed=1234)
3029
# here's an idealised Bot class that uses these same env variables to run a showdown bot, but more cleanly:
3130
bot_1 = Bot(
3231
team,
33-
strategy='safest',
34-
websocket_uri='sim.psim.us:8000',
35-
user='whimsicaldreams',
36-
password='password',
37-
bot_mode='CHALLENGE_USER',
38-
format='gen3ou',
32+
strategy="safest",
33+
websocket_uri="sim.psim.us:8000",
34+
user="whimsicaldreams",
35+
password="password",
36+
bot_mode="CHALLENGE_USER",
37+
format="gen3ou",
3938
num_battles=1,
4039
save_replay=False,
4140
)
@@ -47,25 +46,27 @@ bot_1 = Bot(
4746
# now we can run a battle between two bots:
4847
bot_2 = Bot(
4948
team,
50-
strategy='safest',
51-
websocket_uri='sim.psim.us:8000',
52-
user='melonchomper',
53-
password='password',
54-
bot_mode='CHALLENGE_USER', # this would need to be ACCEPT_CHALLENGE
55-
format='gen3ou',
49+
strategy="safest",
50+
websocket_uri="sim.psim.us:8000",
51+
user="melonchomper",
52+
password="password",
53+
bot_mode="CHALLENGE_USER", # this would need to be ACCEPT_CHALLENGE
54+
format="gen3ou",
5655
num_battles=1,
5756
save_replay=False,
5857
)
5958

6059
# from a little google searching, it looks like we can run these bots in parallel using asyncio:
6160

61+
6262
async def run_battle(bot_1, bot_2):
6363
# individually run the showdown bots at the same time, and wait for the results
6464
await asyncio.gather(bot_1.challenge(bot_2), bot_2.accept(bot_1))
6565
# parse the results
6666
# update the win/loss/tie counts for each team
6767
...
6868

69+
6970
run_battle(bot_1, bot_2)
7071

7172
# within this function, we'll need to:
@@ -74,7 +75,20 @@ run_battle(bot_1, bot_2)
7475
# - update the win/loss/tie counts for each team
7576

7677
# here's a more generic example, where we specify a number of battles for each team, and then run them all:
77-
bots = {f'bot{i}':Bot(team, strategy='safest', websocket_uri='sim.psim.us:8000', user=f'bot{i}', password='password', bot_mode='CHALLENGE_USER' if i % 2 == 0 else 'ACCEPT_CHALLENGE', format='gen3ou', num_battles=10, save_replay=False) for i in range(10)}
78+
bots = {
79+
f"bot{i}": Bot(
80+
team,
81+
strategy="safest",
82+
websocket_uri="sim.psim.us:8000",
83+
user=f"bot{i}",
84+
password="password",
85+
bot_mode="CHALLENGE_USER" if i % 2 == 0 else "ACCEPT_CHALLENGE",
86+
format="gen3ou",
87+
num_battles=10,
88+
save_replay=False,
89+
)
90+
for i in range(10)
91+
}
7892

7993
# num_battles = 10 would mean that bot would challenge the same bot 10 times, so we can make every bot fight every other bot 10 times
8094

@@ -85,14 +99,18 @@ for bot_1, bot_2 in itertools.combinations(bots.values(), 2):
8599
if bot_1 != bot_2:
86100
try:
87101
# make sure bot_1 is the challenger and bot_2 is the acceptor
88-
bot_1.bot_mode = 'CHALLENGE_USER'
89-
bot_2.bot_mode = 'ACCEPT_CHALLENGE'
102+
bot_1.bot_mode = "CHALLENGE_USER"
103+
bot_2.bot_mode = "ACCEPT_CHALLENGE"
90104

91105
# check if either bot is currently in a battle
92106
if bot_1.in_battle or bot_2.in_battle:
93-
print(f'Bot {bot_1} or {bot_2} is currently in a battle, waiting for them to finish...')
107+
print(
108+
f"Bot {bot_1} or {bot_2} is currently in a battle, waiting for them to finish..."
109+
)
94110
while bot_1.in_battle or bot_2.in_battle:
95-
await asyncio.sleep(1) # hopefully this will wait for the battle to finish
111+
await asyncio.sleep(
112+
1
113+
) # hopefully this will wait for the battle to finish
96114

97115
# run the battle
98116
bot_1.in_battle = True
@@ -105,7 +123,7 @@ for bot_1, bot_2 in itertools.combinations(bots.values(), 2):
105123

106124
except Exception as e:
107125
print(e)
108-
print(f'Error running battle between {bot_1} and {bot_2}')
126+
print(f"Error running battle between {bot_1} and {bot_2}")
109127

110128
# ensure we're only here once every bot has fought every other bot
111129
while any(bot.in_battle for bot in bots.values()):

0 commit comments

Comments
 (0)