Basic genetic algorithm

class GA(domain: Domain, fitness_function: Callable[[Solution], float], population_size: int = 20, max_iterations: int = 50, mutation_rate: float = 0.1, distributed: bool = False, log_dir: str = 'logs/GA')

Bases: Metaheuristic

Genetic Algorithm (GA) class for optimization problems.

Parameters:
  • domain (Domain) – The domain representing the problem space.

  • fitness_func (Callable[[Solution], float]) – The fitness function used to evaluate solutions.

  • population_size (int, optional) – The size of the population (default is 10).

  • mutation_rate (float, optional) – The probability of mutation for each solution (default is 0.1).

  • n_generations (int, optional) – The number of generations to run the algorithm (default is 50).

Variables:
  • population_size (int) – The size of the population.

  • mutation_rate (float) – The probability of mutation for each solution.

  • n_generations (int) – The number of generations to run the algorithm.

  • domain (Domain) – The domain representing the problem space.

  • fitness_func (Callable[[Solution], float]) – The fitness function used to evaluate solutions.

initialize(num_solutions=10) Tuple[List[Solution], Solution]

Initialize the population

iterate(solutions: List[Solution]) Tuple[List[Solution], Solution]

Execute one generation of the genetic algorithm

stopping_criterion() bool

Check if the algorithm should stop. Override this method to implement custom stopping criteria.

Returns:

True if the algorithm should stop, False otherwise.

Return type:

bool

Steady-state genetic algorithm

class SSGA(domain: Domain, fitness_function: Callable[[Solution], float], population_size: int = 10, max_iterations: int = 50, mutation_rate: float = 0.1, distributed: bool = False, log_dir: str = 'logs/SSGA')

Bases: Metaheuristic

Steady State Genetic Algorithm (SSGA) class for optimization problems which is a variant of the Genetic Algorithm (GA) with population replacement.

Parameters:
  • domain (Domain) – The domain representing the problem space.

  • fitness_func (Callable[[Solution], float]) – The fitness function used to evaluate solutions.

  • population_size (int, optional) – The size of the population (default is 10).

  • mutation_rate (float, optional) – The probability of mutation for each solution (default is 0.1).

  • n_iterations (int, optional) – The number of generations to run the algorithm (default is 50).

Variables:
  • population_size (int) – The size of the population.

  • mutation_rate (float) – The probability of mutation for each solution.

  • n_iterations (int) – The number of generations to run the algorithm.

  • domain (Domain) – The domain representing the problem space.

  • fitness_func (Callable[[Solution], float]) – The fitness function used to evaluate solutions.

initialize(num_solutions=10) Tuple[List[Solution], Solution]

Initialize the population/solutions for the metaheuristic. Must set self.current_solutions and self.best_solution.

Parameters:

num_solutions (int) – The number of solutions to initialize.

Returns:

A tuple containing the population and the best individual.

Return type:

Tuple[List[Solution], Solution]

iterate(solutions: List[Solution]) Tuple[List[Solution], Solution]

Iterate the algorithm for one generation.

stopping_criterion() bool

Check if the algorithm should stop. Override this method to implement custom stopping criteria.

Returns:

True if the algorithm should stop, False otherwise.

Return type:

bool