Optimizing Genetic Algorithms

To optimize solutions using a genetic algorithm, the struct GeneticAlgorithm is used to configure key parameters like initialization, selection, crossover, mutation and more. By passing it to the optimize method, the algorithm iterates through generations until a termination condition is met (currently only max_generations is implemented).

GeneticAlgorithms.GeneticAlgorithmType
GeneticAlgorithm(
    initialization_strategy::P,
    fitness_function::Function,
    selection_strategy::S,
    crossover_method::C,
    mutation_method::M;
    elitism::Bool=true,
    verbose::Bool=false,
    max_generations::Int=5,
    mutation_rate::Float64=0.1,
    save_best::Bool=false,
) where {P<:PopulationInitializationMethod,S<:SelectionMethod,C<:CrossoverMethod,M<:MutationMethod}

Defines a genetic algorithm with the specified parameters for selection, mutation, crossover and fitness evaluation.

Arguments

  • initialization_strategy::P: Strategy to initialize the population.
  • fitness_function::Function: Function to calculate the fitness of each chromosome.
  • selection_strategy::S: Strategy to select parents for crossover.
  • crossover_method::C: Method to generate offspring from selected parents.
  • mutation_method::M: Method to mutate the offspring.
  • elitism::Bool=true: If true, the best individual from the previous generation is carried over to the next generation.
  • verbose::Bool=false: If true, additional information is printed during the execution.
  • max_generations::Int64=5: Number of generations the algorithm runs for.
  • mutation_rate::Float64=0.1: Probability of mutation for each offspring in a generation.
  • save_best::Bool=false: If true, the best chromosomes and their fitness scores are saved for visualization.

Fields

  • initialization_strategy::P: Strategy to initialize the population.
  • fitness_function::Function: Function to calculate the fitness of each chromosome.
  • max_generations::Int64: Number of generations the algorithm runs for.
  • selection_strategy::S: Strategy to select parents for crossover.
  • crossover_method::C: Method to generate offspring from selected parents.
  • mutation_method::M: Method to mutate the offspring.
  • mutation_rate::Float64: Probability of mutation.
  • elitism::Bool: If true, the best individual from the previous generation is carried over to the next generation.
  • verbose::Bool: If true, additional information is printed during the execution.
  • save_best::Bool: If true, the best chromosomes and their fitness scores are saved for visualization.
  • best_chromosomes::Vector{Chromosome}: Stores the best chromosomes for each generation.
  • best_fitness::Vector{Float64}: Stores the fitness scores of the best chromosomes for each generation.

Note that the best_chromosomes and best_fitness fields store max_generations + 1 values, including the initial population.

source
GeneticAlgorithms.optimizeMethod
optimize(genetic_algorithm::GeneticAlgorithm)::Chromosome

Runs the genetic algorithm optimization loop and returns the best Chromosome.

genetic_algorithm specifies the configuration of the genetic algorithm that is optimized. If the field genetic_algorithm.save_best is true the field genetic_algorithm.best_chromosomes and genetic_algorithm.best_fitness are filled during the optimization process.

source