Skip to content

Commit

Permalink
style: pre-commit fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Sep 16, 2024
1 parent e59103d commit d54b63c
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 28 deletions.
1 change: 0 additions & 1 deletion src/p2lab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
p2lab: a package for genetic optimisation of pokemon teams
"""


from __future__ import annotations

__version__ = "0.1.0"
Expand Down
1 change: 0 additions & 1 deletion src/p2lab/_compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
p2lab: a package for genetic optimisation of pokemon teams
"""


from __future__ import annotations
1 change: 0 additions & 1 deletion src/p2lab/_compat/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
p2lab: a package for genetic optimisation of pokemon teams
"""


from __future__ import annotations

from typing import Literal, Protocol, runtime_checkable
Expand Down
2 changes: 1 addition & 1 deletion src/p2lab/genetic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def crossover(
team2_pokemon = team2_old.pokemon

# Randomly pick locus
locus = random.sample(range(0, num_pokemon), k=1)[0]
locus = random.sample(range(num_pokemon), k=1)[0]

# Check that the sliciing here works but you get the idea
team1_new_pokemeon = team1_pokemon[0:locus] + team2_pokemon[locus:num_pokemon]
Expand Down
1 change: 1 addition & 0 deletions src/p2lab/pokemon/pokefactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
This is directly inspired by poke-env's diagostic_tools folder
"""

from __future__ import annotations

import numpy as np
Expand Down
58 changes: 38 additions & 20 deletions target_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ running genetic algorithms to optimize teams.
Thank you to github copilot for helping me write this document lmao

```python3

team = generate_team(format='gen8randombattle', seed=1234)
team = generate_team(format="gen8randombattle", seed=1234)

# a team object contains a list of 6 pokemon objects with their moves, items, etc.
# it's also tracking the (normalised) number of wins/losses/ties for this team.
Expand All @@ -30,12 +29,12 @@ team = generate_team(format='gen8randombattle', seed=1234)
# here's an idealised Bot class that uses these same env variables to run a showdown bot, but more cleanly:
bot_1 = Bot(
team,
strategy='safest',
websocket_uri='sim.psim.us:8000',
user='whimsicaldreams',
password='password',
bot_mode='CHALLENGE_USER',
format='gen3ou',
strategy="safest",
websocket_uri="sim.psim.us:8000",
user="whimsicaldreams",
password="password",
bot_mode="CHALLENGE_USER",
format="gen3ou",
num_battles=1,
save_replay=False,
)
Expand All @@ -47,25 +46,27 @@ bot_1 = Bot(
# now we can run a battle between two bots:
bot_2 = Bot(
team,
strategy='safest',
websocket_uri='sim.psim.us:8000',
user='melonchomper',
password='password',
bot_mode='CHALLENGE_USER', # this would need to be ACCEPT_CHALLENGE
format='gen3ou',
strategy="safest",
websocket_uri="sim.psim.us:8000",
user="melonchomper",
password="password",
bot_mode="CHALLENGE_USER", # this would need to be ACCEPT_CHALLENGE
format="gen3ou",
num_battles=1,
save_replay=False,
)

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


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


run_battle(bot_1, bot_2)

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

# here's a more generic example, where we specify a number of battles for each team, and then run them all:
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)}
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)
}

# 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

Expand All @@ -85,14 +99,18 @@ for bot_1, bot_2 in itertools.combinations(bots.values(), 2):
if bot_1 != bot_2:
try:
# make sure bot_1 is the challenger and bot_2 is the acceptor
bot_1.bot_mode = 'CHALLENGE_USER'
bot_2.bot_mode = 'ACCEPT_CHALLENGE'
bot_1.bot_mode = "CHALLENGE_USER"
bot_2.bot_mode = "ACCEPT_CHALLENGE"

# check if either bot is currently in a battle
if bot_1.in_battle or bot_2.in_battle:
print(f'Bot {bot_1} or {bot_2} is currently in a battle, waiting for them to finish...')
print(
f"Bot {bot_1} or {bot_2} is currently in a battle, waiting for them to finish..."
)
while bot_1.in_battle or bot_2.in_battle:
await asyncio.sleep(1) # hopefully this will wait for the battle to finish
await asyncio.sleep(
1
) # hopefully this will wait for the battle to finish

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

except Exception as e:
print(e)
print(f'Error running battle between {bot_1} and {bot_2}')
print(f"Error running battle between {bot_1} and {bot_2}")

# ensure we're only here once every bot has fought every other bot
while any(bot.in_battle for bot in bots.values()):
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from p2lab.pokemon import pokefactory, premade, teams


@pytest.fixture()
@pytest.fixture
def default_factory():
return pokefactory.PokeFactory()


@pytest.fixture()
@pytest.fixture
def gen_1_pool():
return teams.import_pool(premade.gen_1_pokemon())
3 changes: 1 addition & 2 deletions tests/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ def test_version():

@runtime_checkable
class HasQuack(Protocol):
def quack(self) -> str:
...
def quack(self) -> str: ...


class Duck:
Expand Down

0 comments on commit d54b63c

Please sign in to comment.