Skip to content

Ga Core

src.ga.ga_core

View source code

Genetic algorithm core: selection, crossover, mutation, and run artifacts.

GeneticAlgorithm

Manage one population and persist its training run.

crossover_rate = 0.4 class-attribute instance-attribute

elite_factor = 0.1 class-attribute instance-attribute

fps = fps instance-attribute

generation = 0 instance-attribute

metric_fields = ('generation', 'best_fitness', 'mean_fitness', 'median_fitness', 'player_score', 'cpu_score', 'player_hits', 'cpu_hits') class-attribute instance-attribute

population = population instance-attribute

population_size = len(population) instance-attribute

render = render instance-attribute

run_dir = Path(output_dir) / timestamp instance-attribute

seed = seed instance-attribute

sound_enabled = render and sound_enabled instance-attribute

speed = speed instance-attribute

steps_per_frame = steps_per_frame instance-attribute

timeout = timeout instance-attribute

validation_achievement = None instance-attribute

validation_reached = False instance-attribute

validation_status = None instance-attribute

validator = EliteValidator(seed=seed if seed is not None else random.randrange(2 ** 32), games=validation_games, fps=fps, speed=speed, steps_per_frame=steps_per_frame) instance-attribute

__init__(population: list[IndividualPlayer], *, output_dir: str | Path = 'runs', render: bool = True, seed: int | None = None, timeout: float = VARIABLES['TIME_OUT'], fps: float = VARIABLES['FPS'], speed: float = VARIABLES['SPEED'], steps_per_frame: int = VARIABLES['STEPS_PER_FRAME'], validation_games: int = 2, sound_enabled: bool = True)

calculate_fitness() -> None

Calculate, rank, record, and checkpoint the current generation.

crossover() -> list[IndividualPlayer]

Create paired offspring from the top-ranked mating pool.

cull_population() -> None

Keep elite copies and retain fitter members for reproduction.

epoch() -> None

Play one generation, with or without an Arcade window.

mutate_and_append_to_population(offsprings: list[IndividualPlayer]) -> None

Reset and mutate offspring, replacing the weakest population members.

print_scores() -> None

Print the best individual's fitness breakdown.

repopulate() -> None

Restore the population to its configured size.

save_generation_samples(top_n: int = 2) -> None

Save the generation's top checkpoints and its current elite.

selection() -> None

Run one epoch, calculate fitness, and prepare the next population.

start(runs: int | None = None) -> None

Run generations until interrupted or the optional limit is reached.