GeneticAlgorithm

A Julia package providing genetic algorithms for optimization. It provides diffrent implementation of mutation, recombination, crossover and selection functions. Documentation for GeneticAlgorithm.

Important

This package is build for the Julia Programming for Machine Learning course at TU Berlin and will will not be maintained afterwards.

What is a Genetic Algorithm?

A Genetic Algorithm is a technique based on natural evolution and genetics. It solves constrained and unconstrained optimization problems in which traditional algorithms and methods might struggle. It is part of the class of evolutionary algorithms.

The following chart shows the basic steps of a Genetic Algorithm:

How does a Genetic Algorithm work

Outline of a Genetic Algorithm:

  • Initialisation: creates the initial population of individuals with random chromosomes
  • Evaluation: evaluate the fitness of each individual using the fitness function
  • Selection: select the best individuals based on their fitness score to select parents for the new generation
  • Crossover: the selected parents are combined to produce offspring
  • Mutation: some chromosomes are mutated
  • Repeat: repeat this the Evaluation, Selection, Crossover, and Mutation until a solution is found or the termination condition is met

Getting started

The package can be installed with the Julia package manager. From the Julia REPL, type ] to enter the Pkg REPL mode and run:

] add https://github.com/MaxHTu/GeneticAlgorithm.jl

This is a simple example unsing a genetic algorithm to solve he Rosenbrock function.

using GeneticAlgorithm
genAlgo(50, true, 2, rosenbrock, 50, 0.25, 0.1)
GeneticAlgorithm.geneticAlgorithmFunction

geneticAlgorithm( popSize::Integer, unitValues::Type, unitShape::Integer, fitnessFunc::Function, genNum::Integer, selection::Function, crossover::Function, mutation::Function, crossRate::Real, mutRate::Real, nextGenAmt::Real, terminationNum::Real; initFunc::Function )

Evolve the population of units for a specified number of generations. The loop creates a new population every generation by selecting units with higher fitness, recombinating and mutating them until the old population can be replaced by a fitter one.

Arguments

  • fitnessFunc: Fitness function.
  • popSize: Size of population.
  • unitValues: Type of unit.
  • unitShape: Length of a unit vector.
  • genNum: Number of generations.
  • selection: Selection function.
  • crossover: Crossover function.
  • mutation: Mutation function.
  • crossRate: Crossover rate.
  • mutRate: Mutation rate.
  • nextGenAmt: amount of genes that are automatically copied to new generation
  • terminationNum: number of iteration after which the algorithm aborts if there is no change in fitness value from the best gene
  • initFunc: function to generate initial population
source
GeneticAlgorithm.genAlgoFunction

genAlgo( popSize::Integer, unitValues::Type, unitShape::Integer, fitnessFunc::Function, genNum::Integer, selection::Function, crossover::Function, mutation::Function, crossRate::Real, mutRate::Real )

TODO: description

Arguments

  • fitnessFunc: Fitness function.
  • popSize: Size of population.
  • unitValues: Type of unit.
  • unitShape: Length of a unit vector.
  • genNum: Number of generations.
  • selection: Selection function.
  • crossover: Crossover function.
  • mutation: Mutation function.
  • crossRate: Crossover rate.
  • mutRate: Mutation rate.
source
GeneticAlgorithm.binarystringMethod
binarystring(x::AbstractVector{Bool})

Compute the fitness of a binary string unit.

Arguments

  • x: Input vector.

Returns

  • The digit sum of input vector.
source
GeneticAlgorithm.crossoverSudokuMethod
crossoverSudoku(s_1::Matrix{Int64}, s_2::Matrix{Int64})

Crosses 2 given sudokus row wise at a randomly generated row index.

Arguments

  • s_1: first filled out sudoku
  • s_2: second filled out sudoku

Returns

  • 2 altered sudokus
source
GeneticAlgorithm.default_selectionMethod
default_selection(population::Union{AbstractVector,AbstractMatrix}, num::Integer)

Selects a specified number of individuals from a population.

Arguments

  • population: The population from which individuals will be selected.
  • num: The number of individuals to select.

Returns

An array of selected individuals.

source
GeneticAlgorithm.fitnessMethod
fitness(genome::Matrix{Int64}, sudoku::Matrix{Int64})

Evaluates the fitness of a given genome and the base sudoku. Starting from 0 a genome gets 10 points for each correctly filled column (reminder the rows are filled per definition by the initialization function) and each 3x3 box. 2 points are deducted for each value that cannot be in this column or box because of the values in the base sudoku.

Arguments

  • genome: filled out sudoku to be evaluated
  • s: Base sudoku

Returns

  • A fitness value
source
GeneticAlgorithm.genAlgoMethod

genAlgo( popSize::Integer, unitValues::Type, unitShape::Integer, fitnessFunc::Function, genNum::Integer, selection::Function, crossover::Function, mutation::Function, crossRate::Real, mutRate::Real )

TODO: description

Arguments

  • fitnessFunc: Fitness function.
  • popSize: Size of population.
  • unitValues: Type of unit.
  • unitShape: Length of a unit vector.
  • genNum: Number of generations.
  • selection: Selection function.
  • crossover: Crossover function.
  • mutation: Mutation function.
  • crossRate: Crossover rate.
  • mutRate: Mutation rate.
source
GeneticAlgorithm.generatePopulationMethod
generatePopulation(n::Int, s::Matrix{Int64})

Generate an array of n filled out sudokus

Arguments

  • n: size of the population
  • s: Given sudoku with zeros

Returns

  • Array of n possible Sudoku solutions without zeros
source
GeneticAlgorithm.geneticAlgorithmMethod

geneticAlgorithm( popSize::Integer, unitValues::Type, unitShape::Integer, fitnessFunc::Function, genNum::Integer, selection::Function, crossover::Function, mutation::Function, crossRate::Real, mutRate::Real, nextGenAmt::Real, terminationNum::Real; initFunc::Function )

Evolve the population of units for a specified number of generations. The loop creates a new population every generation by selecting units with higher fitness, recombinating and mutating them until the old population can be replaced by a fitter one.

Arguments

  • fitnessFunc: Fitness function.
  • popSize: Size of population.
  • unitValues: Type of unit.
  • unitShape: Length of a unit vector.
  • genNum: Number of generations.
  • selection: Selection function.
  • crossover: Crossover function.
  • mutation: Mutation function.
  • crossRate: Crossover rate.
  • mutRate: Mutation rate.
  • nextGenAmt: amount of genes that are automatically copied to new generation
  • terminationNum: number of iteration after which the algorithm aborts if there is no change in fitness value from the best gene
  • initFunc: function to generate initial population
source
GeneticAlgorithm.griewankMethod
griewank(x::AbstractVector{<:Number})

Compute the Griewank function value for a given input vector x.

The Griewank function is a multimodal function used as a performance test problem for optimization algorithms. It is defined as the difference between two terms: the sum of the squares of each element in x divided by 4000, and the product of the cosine of each element in x divided by the square root of its index.

Arguments

  • x: Input vector.

Returns

  • The Griewank function value.
source
GeneticAlgorithm.initialStateMethod
initialState(s::Matrix{Int64})

Generate a filled out sudoku (without zeros) from given base sudoku (with zeros). The sudoku is filled so that all the rows are already properly filled. If possible the function tries to also fill the columns and boxes properly.

Arguments

  • s: Given sudoku with zeros

Returns

  • Sudoku without zeros
source
GeneticAlgorithm.k_point_crossoverMethod
k_point_crossover(gene1::Union{AbstractVector, AbstractMatrix}, gene2::Union{AbstractVector, AbstractMatrix}, k::Integer)

Recombinate two units by exchanging their genes at k random points

Arguments

  • gene1: Parent unit 1.
  • gene2: Parent unit 2.
  • k: # of crossover points.

Returns

  • Two recombined child units.
source
GeneticAlgorithm.mutation!Method
mutation!(gene, mutation_prob, unitValues)

Mutates the given gene based on the mutation_prob and unitValues.

Arguments

  • gene: A matrix or vector containing real numbers or boolean values.
  • mutation_prob: The probability of mutation for each element in the gene.
  • unitValues: A type or vector representing the range of values for mutation.

Details

  • For each element in the gene, if a random number is less than mutation_prob, the element is mutated.
  • If unitValues is an abstract range of floating-point numbers, the mutated element is transformed to fit within the range.
  • If unitValues is a vector of boolean values, the mutated element is negated.
  • Otherwise, the mutated element is replaced with a random value from unitValues.

Returns

The mutated gene.

source
GeneticAlgorithm.quarticMethod
quartic(x::AbstractVector{<:Number})

Compute the quartic function value for a given input vector x.

The quartic function is defined as the sum of the product of each element in x raised to the power of 4 and its index, plus a random number.

Arguments

  • x: Input vector.

Returns

  • The quartic function value.
source
GeneticAlgorithm.rastriginMethod
rastrigin(x::AbstractVector{<:Number})

Compute the Rastrigin function value for a given input vector x.

The Rastrigin function is a multimodal function used as a performance test problem for optimization algorithms. It is defined as a sum of terms involving the square of each element in x, minus 10 times the cosine of 2π times each element in x.

Arguments

  • x: Input vector.

Returns

  • The Rastrigin function value.
source
GeneticAlgorithm.rosenbrockMethod
rosenbrock(x::Vector{<:Number}; a::Integer, b::Integer)

Compute the Rosenbrock function value for a given input vector x.

The Rosenbrock function is a non-convex function used as a performance test problem for optimization algorithms. It is defined as the sum of a series of terms involving the squares of differences between adjacent elements of x.

Arguments

  • x: Input vector.
  • a: value for the variable a
  • b: value for the variable b

Returns

  • The Rosenbrock function value.
source
GeneticAlgorithm.schwefelMethod
schwefel(x::AbstractVector{<:Number})

Compute the Schwefel function value for a given input vector x.

The Schwefel function is a multimodal function used as a performance test problem for optimization algorithms. It is defined as a sum of terms involving the sine function and the square root of the absolute value of each element in x.

Arguments

  • x: Input vector.

Returns

  • The Schwefel function value.
source
GeneticAlgorithm.single_point_crossoverMethod
single_point_crossover(gene1::Union{AbstractVector, AbstractMatrix}, gene2::Union{AbstractVector, AbstractMatrix})

Recombinate two units by exchanging their genes from a random index onward.

Arguments

  • gene1: Parent unit 1.
  • gene2: Parent unit 2.

Returns

  • Two recombined child units.
source
GeneticAlgorithm.solveRosenbrockMethod

solveRosenbrock(; a::Integer, b::Integer, popSize::Integer, fitnessFunc::Function, unitValues::Union{Type, AbstractVector{<:AbstractFloat}, AbstractRange{<:Real}}, unitShape::AbstractVector{<:Integer}, genNum::Integer, crossRate::Real, mutRate::Real, nextGenAmt::Number, selectionFunc::Function, crossoverFunc::Function, mutationFunc::Function , terminationNum::Real )

Runs the rosenbrock function with the geneticAlgorithm

Arguments

  • a: rosenbrock function variable
  • b: rosenbrock function variable
  • popSize: Size of population.
  • fitnessFunc: Fitness function.
  • unitValues: Type of unit.
  • unitShape: Length of a unit vector.
  • genNum: Number of generations.
  • crossRate: Crossover rate.
  • mutRate: Mutation rate.
  • nextGenAmt: amount of genes that are automatically copied to new generation
  • selectionFunc: Selection function.
  • crossoverFunc: Crossover function.
  • mutationFunc: Mutation function.
  • terminationNum: number of iteration after which the algorithm aborts if there is no change in fitness value from the best gene
source
GeneticAlgorithm.sphereMethod
sphere(x::Vector{<:Number})

Compute the sphere function value for a given input vector x.

The sphere function is defined as the sum of the squares of each element in x.

Arguments

  • x: Input vector.

Returns

  • The sphere function value.
source
GeneticAlgorithm.sudokuMutationFunction
sudokuMutation(o::Matrix{Int64}, sudoku::Matrix{Int64},  probability=0.2)

Go through all the rows of a sudoku and if a random value is larger than a probability, swap 2 values in that row

Arguments

  • o: sudoku to be mutated
  • sudoku: base sudoku
  • probability: probability with which we mutate a row

Returns

  • a mutated sudoku
source
GeneticAlgorithm.tournament_selectionMethod
tournament_selection(population::Vector, fitness::Vector, tournamentSize::Int, num::Integer)

Select one unit with higher fitness over several tournament rounds with randomly chosen groups of unit from the population .

Arguments

  • population: Population vector.
  • fitness: Vector of fitness values.
  • tournamentSize: Size of tournament groups.
  • num: Amount of genes to select

Returns

  • The unit with better fitness value.
source
GeneticAlgorithm.uniformCrossoverMethod
uniformCrossover(s_1::Matrix{Int64}, s_2::Matrix{Int64})

Crosses 2 given sudokus row wise by generating a random value that indicates if the current row of both sudokus should be changed between them

Arguments

  • s_1: first filled out sudoku
  • s_2: second filled out sudoku

Returns

  • 2 altered sudokus
source
GeneticAlgorithm.uniform_crossoverMethod
uniform_crossover(gene1::Union{AbstractVector, AbstractMatrix}, gene2::Union{AbstractVector, AbstractMatrix})

Recombinate two units by iterating a gene and swapping the values with a 50% chance

Arguments

  • gene1: Parent unit 1.
  • gene2: Parent unit 2.

Returns

  • Two recombined child units.
source
GeneticAlgorithm.weighted_selectionMethod

weighted_selection(population::AbstractVector, fitness::AbstractVector, num::Integer)

Select num random genes from population weight by the fitness of the genes

Arguments

  • population: Population vector.
  • fitness: Vector of fitness values.
  • num: Amount of genes to select

Returns

  • num selected genes
source