Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch/poke env - fixes the NoneType error when battling #41

Merged
merged 4 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/p2lab/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import argparse
import asyncio

import numpy as np
Expand Down Expand Up @@ -44,13 +45,24 @@ async def main_loop(num_teams, team_size, num_generations, unique):
print(f"Fitness: {fitness}")


def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-g", help="Number of generations", type=int, default=10)
parser.add_argument("-t", help="Team size", type=int, default=2)
parser.add_argument("-n", help="Number of teams", type=int, default=30)
parser.add_argument("-s", help="Random seed", type=int, default=None)
parser.add_argument("-u", help="Unique teams", default=True)
return vars(parser.parse_args())


def main():
num_teams = 30
team_size = 2
num_generations = 10
unique = True
args = parse_args()

if args["s"] is not None:
np.random.seed(args["s"])

asyncio.get_event_loop().run_until_complete(
main_loop(num_teams, team_size, num_generations, unique)
main_loop(args["n"], args["t"], args["g"], args["u"])
)


Expand Down
6 changes: 4 additions & 2 deletions src/p2lab/pokemon/teams.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

from copy import deepcopy

__all__ = (
"Team",
"generate_pool",
Expand All @@ -18,7 +20,7 @@

class Team:
def __init__(self, pokemon) -> None:
self.pokemon = np.array(pokemon)
self.pokemon = np.array(deepcopy(pokemon))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was not deep-copying causing some hidden state mutation or something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, basically when a team was being created it had a situation where 2 Pokemon in a team (or even against each other in the same match) could be affecting each other's state!

self.first_name = self.pokemon[0].formatted.split("|")[0]

def to_packed_str(self) -> str:
Expand Down Expand Up @@ -106,7 +108,7 @@ def generate_teams(pool, num_teams, team_size=6, unique=False):
msg = f"Cannot generate {num_teams} teams of size {team_size} from pool of size {len(pool)}"
raise Exception(msg)
indicies = np.random.choice(
len(pool), size=num_teams * team_size, replace=False
len(pool), size=(num_teams, team_size), replace=False
)
teams = np.array(pool)[indicies].reshape(num_teams, team_size)
return [Team(team) for team in teams]
Expand Down