Genetic algorithm (GA) optimizer#

MOTEP also contains a Python implementation of a genetic algorithm (GA) for optimization problems. The GA is designed to find the optimal solution to a given problem by evolving a population of candidate solutions over multiple generations.

Algorithm#

class motep.optimizers.ga.GeneticAlgorithm(fitness_function: Callable, parameter: ndarray, lower_bound: ndarray, upper_bound: ndarray, population_size: int = 40, mutation_rate: float = 0.1, elitism_rate: float = 0.1, crossover_probability: float = 0.7, *, superhuman: bool = True)#

Bases: object

initialize_population() None#

Generate an initial population of individuals with random parameters.

generate_random_parameters() list[float]#

Generate a random parameter within specified bounds.

Return type:

list[float]

supermutation(elite_individuals: list[ndarray], steps: int = 20) list[ndarray]#

Optimize elites further using scipy.optimize.minimize.

Return type:

list[np.ndarray]

crossover(parent1: ndarray, parent2: ndarray) tuple[ndarray, ndarray]#

Perform crossover between two parents to generate offspring.

Return type:

tuple[np.ndarray, np.ndarray]

mutate(parameter: ndarray) ndarray#

Perform mutation on an individual’s parameter with a certain probability.

Return type:

np.ndarray

select_elite(fitness_scores: list[float]) list[ndarray]#

Select elite individuals based on their fitness scores.

Return type:

list[np.ndarray]

evolve_with_elites(fitness_function: Callable, generations: int, elite_callback: Callable | None = None) ndarray#

Perform evolution using strategies like elitism.

This method preserves a certain percentage of the fittest individuals from each generation. Elite individuals are directly copied to the next generation without undergoing crossover and mutation. It helps maintain the best solutions found so far and can accelerate convergence.

Return type:

np.ndarray

evolve_with_common(fitness_function: Callable, generations: int, elite_callback: Callable | None = None) ndarray#

Perform evolution using strategies like common.

In this method, selection for crossover and mutation is done from the entire population, not just the elite individuals. Offspring are generated by selecting parents randomly from the entire population. This method might introduce more diversity into the population, potentially exploring a wider range of solutions.

Return type:

np.ndarray

evolve_with_mix(fitness_function: Callable, generations: int, elite_callback: Callable | None = None) ndarray#

Perform evolution using strategies like mix.

This method combines elitism and common evolution strategies. Elite individuals are preserved directly, while the rest of the population undergoes evolution with crossover and mutation. The crossover operation is still performed between an elite individual and a randomly selected individual from the entire population. This method aims to strike a balance between preserving good solutions and exploring new ones.

Return type:

np.ndarray

evolve_with_steady(fitness_function: Callable, generations: int, elite_callback: Callable | None = None) ndarray#

Perform evolution using strategies like steady.

In this method, the population evolves one individual at a time, maintaining a constant population size throughout evolution. Elite individuals are selected and preserved, and the remaining individuals are replaced with offspring generated through crossover and mutation. The replacement strategy ensures that the population size remains constant. This method can be useful for problems where memory usage is a concern or when it’s desirable to maintain a stable population size.

Return type:

np.ndarray

Optimizer#

class motep.optimizers.ga.GeneticAlgorithmOptimizer(loss: ~motep.loss.LossFunctionBase, *, comm: ~motep.parallel.DummyMPIComm = <motep.parallel.DummyMPIComm object>, **kwargs: dict[str, ~typing.Any])#

Bases: ParallelOptimizerBase

Optimizer based on genetic algorithm (GA).

This function is a wrapper for using the GA to optimize a target function. It takes the target function, initial guess for parameters, and any additional arguments. It initializes the GA instance with provided parameters and then runs the GA evolution process using one of the evolution methods (by default evolve_with_elites).