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.
- class splendor.agents.our_agents.genetic_algorithm.argument_parsing.Arguments[source]
Bases:
TypedDict
TypedDict representing the command-line arguments.
- generations: Required[int]
- multiprocess: Required[bool]
- mutation_rate: Required[float]
- population_size: Required[int]
- quiet: Required[bool]
- seed: Required[int]
- working_dir: Required[Path]
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) list[GeneAlgoAgent] [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) list[GeneAlgoAgent] [source]
Creates agents with random genes.
- splendor.agents.our_agents.genetic_algorithm.evolve.main() None [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) None [source]
Mutates a single gene.
- splendor.agents.our_agents.genetic_algorithm.evolve.mutate_population(population: list[GeneAlgoAgent], progress: float, mutation_rate: float) None [source]
Mutates the genes of the population.
- splendor.agents.our_agents.genetic_algorithm.evolve.single_game(agents: list[GeneAlgoAgent]) 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 their 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: ndarray[Any, dtype[_ScalarType_co]]
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 dimensions 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) GeneTypeVar [source]
Initiate a gene with DNA from a saved file.
- mutate(mutate_rate: float, mutator: Callable[[float], float]) None [source]
Mutates a gene’s DNA (in place). Should be the only thing that edits the DNA of an existing gene.
- property raw_dna: ndarray[Any, dtype[_ScalarType_co]]
Return a direct access to the private _dna attribute. This is useful when implementing the crossover functionality.
- 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.
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: int, manager: ManagerGene | None = None, strategy1: StrategyGene | None = None, strategy2: StrategyGene | None = None, strategy3: StrategyGene | None = 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: list[CollectAction | ReserveAction | BuyAction], game_state: SplendorState, game_rule: SplendorGameRule) CollectAction | ReserveAction | BuyAction [source]
Method used by the game’s engine when running a game with this agent.
- evaluate_action(strategy: StrategyGene, action: CollectAction | ReserveAction | BuyAction, game_state: SplendorState, game_rule: SplendorGameRule) float [source]
Evaluates an action by the metrcis of the game’s state after the action. The strategy is used to evaluate the state.
- splendor.agents.our_agents.genetic_algorithm.genetic_algorithm_agent.myAgent
alias of
GeneAlgoAgent