Skip to content

Commit

Permalink
update pseudocode
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Simpson committed Jun 14, 2023
1 parent 7160e87 commit 4e5983c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/p2lab/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ def main():
teams = [builder.yield_team() for n in range(N_teams)]
evaluator = PokeEnv()
# Main expected loop
while curr_gen < N_generations:
team_fitness = evaluator.evaluate_teams(teams)
# while curr_gen < N_generations:
# team_fitness = evaluator.evaluate_teams(teams)

poke_pool = [] # List of Pokemon
teams = [] # list of teams (of Pokemon)
curr_gen += 1
# poke_pool = [] # List of Pokemon
# teams = [] # list of teams (of Pokemon)
# curr_gen += 1


if __name__ == "__main__":
Expand Down
65 changes: 52 additions & 13 deletions src/p2lab/genetic/genetic.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
from __future__ import annotations

import random
from typing import Callable

import numpy as np

from p2lab.genetic.fitness import BTmodel
from p2lab.genetic.matching import dense
from p2lab.genetic.operations import fitness_mutate, mutate
from p2lab.pokemon.battle import run_battles
from p2lab.pokemon.team import Team


# Psuedocode for the genetic algorithm, some placeholder functions below to
# be deleted
def genetic_team(
pokemon_population: list[str], # list of all valid pokemon names
generate_teams: Callable,
generate_matches: Callable,
run_battles: Callable,
crossover_fn: Callable,
fitness_func: Callable[[list[Team], np.ndarray, list[int]], list[float]] = BTmodel,
num_pokemon: int = 6,
num_teams: int = 100,
max_evolutions: int = 500,
num_pokemon: int,
num_teams: int,
fitness_fn: Callable,
crossover_fn: Callable = None,
crossover_prob: float = 0.95,
mutate_prob: float = 0.01,
mutate_with_fitness: bool = False,
mutate_k=None,
allow_all=False,
num_evolutions: int = 500,
fitness_kwargs: dict | None = None,
) -> None:
"""
Expand Down Expand Up @@ -78,7 +83,9 @@ def genetic_team(
results = run_battles(matches)

# Compute fitness
fitness = fitness_fn(teams, matches, results, **kwargs)
if fitness_kwargs is None:
fitness_kwargs = {}
fitness = fitness_fn(teams, matches, results, **fitness_kwargs)

# Genetic Loop
for _iter in range(num_evolutions):
Expand Down Expand Up @@ -122,6 +129,38 @@ def genetic_team(
results = run_battles(matches)

# Compute fitness
if fitness_kwargs is None:
fitness_kwargs = {}
fitness = fitness_func(teams, matches, results, **kwargs)
fitness = fitness_fn(teams, matches, results, **fitness_kwargs)


# Consider seeding this?
def generate_teams(
pokemon_population: list[str],
num_pokemon: int,
num_teams: int,
) -> list[Team]:
# TODO Use pre-made teambuilder
teams = []
for _i in range(num_teams):
pokemon = random.sample(population=pokemon_population, k=num_pokemon)
teams.append(Team(pokemon=pokemon))

return teams


def generate_matches(teams: list[Team]) -> np.ndarray:
"""
First column should be team IDs for player 1
Second column should be team IDs for player 2
Team IDs can be generated fresh each round, but we'll need to
track them each round
Actually, team IDs can just index the current team list?
Outputs:
np.ndarray shape (N_matches, 2) The columns are team ids.
"""
return dense(teams)

0 comments on commit 4e5983c

Please sign in to comment.