splendor.agents.our_agents.genetic_algorithm package

Submodules

splendor.agents.our_agents.genetic_algorithm.argument_parsing module

All things related to command-line arguments for evolving the genetic algorithm agent.

splendor.agents.our_agents.genetic_algorithm.argument_parsing.parse_args()[source]

parse the command-line arguments.

splendor.agents.our_agents.genetic_algorithm.constants module

Genetic algorithm evolution constants.

splendor.agents.our_agents.genetic_algorithm.evolve module

Genetic algorithm based agent evolution program.

splendor.agents.our_agents.genetic_algorithm.evolve.crossover(mom: Gene, dad: Gene) tuple[Gene, Gene][source]

Executes crossover between 2 genes, which produces 2 children.

splendor.agents.our_agents.genetic_algorithm.evolve.evaluate(population: list[GeneAlgoAgent], quiet: bool, multiprocess: bool) Tuple[List[float], List[List[int | float | str]]][source]

Measures the fitness of each individual by having them play against each other. Each individual plays in 3 games with 1,2 and 3 rivals.

splendor.agents.our_agents.genetic_algorithm.evolve.evolve(population_size: int = 24, generations: int = 100, mutation_rate: float = 0.2, working_dir: Path = PosixPath('/home/eyal/Desktop/projects/Splendor-AI'), seed: int | None = None, quiet: bool = False, multiprocess: bool = False)[source]

Genetic algorithm evolution process. In each generation selection_size are kept and used for mating. Returns the top return_size individuals of the last generation.

splendor.agents.our_agents.genetic_algorithm.evolve.generate_initial_population(population_size: int)[source]

Creates agents with random genes.

splendor.agents.our_agents.genetic_algorithm.evolve.main()[source]

entry-point for the evolve console script.

splendor.agents.our_agents.genetic_algorithm.evolve.mate(parents: list[GeneAlgoAgent], population_size: int) list[GeneAlgoAgent][source]

Creates new individual by randomly choosing 2 parents and mating them till we got enough individuals.

splendor.agents.our_agents.genetic_algorithm.evolve.mutate(gene: Gene, progress: float, mutate_rate: float)[source]

Mutates a single gene.

splendor.agents.our_agents.genetic_algorithm.evolve.mutate_population(population: list[GeneAlgoAgent], progress: float, mutation_rate: float)[source]

Mutates the genes of the population.

splendor.agents.our_agents.genetic_algorithm.evolve.single_game(agents) tuple[Game, dict][source]

Runs a single game of Splendor (with the Engine) using the given agents.

splendor.agents.our_agents.genetic_algorithm.evolve.sort_by_fitness(population: List[GeneAlgoAgent], folder: Path, message: str, quiet: bool, multiprocess: bool) List[List[int | float | str]][source]

Sort the individuals of the population based on thier fitness score.

Parameters:
  • population – list of all the individuals comprizing the entire population.

  • folder – where to store the fittest individual of the population.

  • message – a message to print.

  • quiet – should print the given message or stay silent.

  • multiprocess – should the games simulations uses multi-processing or a single-process.

Returns:

the games statistics.

splendor.agents.our_agents.genetic_algorithm.genes module

Defining how to represent agents as genes (i.e. vectors) so they could be optimized with a genetic algorithm.

class splendor.agents.our_agents.genetic_algorithm.genes.Gene(dna: ndarray[Any, dtype[_ScalarType_co]])[source]

Bases: object

A generic gene representation class.

LOWER_BOUND = -20
SHAPE: Tuple[int, ...] | None = None
UPPER_BOUND = 20
property dna

We want some metrics to have the same weight (in places the is no meaning to the order). This methods returns a dna which matches in dimentions to the metrics we get in practice by repeating some value multiple time (according to the instructions of METRICS_SHAPE).

classmethod load(path_or_file: Path | str)[source]

Initiate a gene with DNA from a saved file.

mutate(mutate_rate: float, mutator)[source]

Mutates a gene’s DNA (in place). Should be the only thing that edits the DNA of an existing gene.

classmethod random()[source]

Initiate a gene with random DNA.

property raw_dna

Return a direct access to the private _dna attribute. This is useful when implementing the crossover functionality.

save(path_or_file: Path | str)[source]

Saves a gene’s DNA to a file.

class splendor.agents.our_agents.genetic_algorithm.genes.ManagerGene(dna: ndarray[Any, dtype[_ScalarType_co]])[source]

Bases: Gene

Represent a gene that helps to choose a strategy to follow (from 3 options)

SHAPE: Tuple[int, ...] | None = (24, 3)
select_strategy(state_metrics: ndarray[Any, dtype[_ScalarType_co]], strategies: Tuple[StrategyGene, ...]) StrategyGene[source]

Select which strategy should be used based on given state.

Parameters:
  • state_metrics – the feature vector extracted from the state.

  • strategies – all the available strategies.

Returns:

the selected strategy.

class splendor.agents.our_agents.genetic_algorithm.genes.StrategyGene(dna: ndarray[Any, dtype[_ScalarType_co]])[source]

Bases: Gene

Represent a gene that helps to choose an action each turn.

SHAPE: Tuple[int, ...] | None = (24,)
evaluate_state(state_metrics: ndarray[Any, dtype[_ScalarType_co]])[source]

Evaluates a state’s value according to its metrics

splendor.agents.our_agents.genetic_algorithm.genetic_algorithm_agent module

Genetic Algorithm based agent.

class splendor.agents.our_agents.genetic_algorithm.genetic_algorithm_agent.GeneAlgoAgent(_id, manager=None, strategy1=None, strategy2=None, strategy3=None)[source]

Bases: Agent

Agent which plays according to a “plan” reached by genetic algorithm. This agent is also used to represent an individual in the evolution process. We decided to describe an agents plan using 4 genes: 3 of them are used to evaluate actions and choose one from an available actions list and the forth used to assess the current situation and choose one of the 3 strategies.

INVALID_POPULATION_ID = -1
MANAGER_PATH = PosixPath('/home/eyal/Desktop/projects/Splendor-AI/src/splendor/agents/our_agents/genetic_algorithm/manager.npy')
STRATEGY_1_PATH = PosixPath('/home/eyal/Desktop/projects/Splendor-AI/src/splendor/agents/our_agents/genetic_algorithm/strategy1.npy')
STRATEGY_2_PATH = PosixPath('/home/eyal/Desktop/projects/Splendor-AI/src/splendor/agents/our_agents/genetic_algorithm/strategy2.npy')
STRATEGY_3_PATH = PosixPath('/home/eyal/Desktop/projects/Splendor-AI/src/splendor/agents/our_agents/genetic_algorithm/strategy3.npy')
SelectAction(actions, game_state, game_rule)[source]

Method used by the game’s engine when running a game with this agent.

evaluate_action(strategy, action, game_state, game_rule)[source]

Evaluates an action by the metrcis of the game’s state after the action. The strategy is used to evaluate the state.

save(folder: Path)[source]

Saves the genes of the given agent to the provided folder. Used for evolution.

splendor.agents.our_agents.genetic_algorithm.genetic_algorithm_agent.myAgent

alias of GeneAlgoAgent

Module contents