Genex v0.2.1 Genex behaviour View Source
Genetic Algorithms in Elixir!
Genex is a simple library for creating Genetic Algorithms in Elixir. A Genetic Algorithm is a search-based optimization technique based on the principles of Genetics and Natural Selection.
The basic life-cycle of a Genetic Algorithm is as follows:
1) Initialize the Population 2) Loop until goal is reached a) Select Parents b) Perform Crossover c) Mutate some of population d) Select Survivors
Genex uses a structure similar to the one above, using callbacks to give you full control over your genetic algorithm.
Implementation
Genex requires an implementation module:
defmodule OneMax do
use Genex
def encoding do
for _ <- 1..15, do: Enum.random(0..1)
end
def fitness_function(chromosome), do: Enum.sum(chromosome.genes)
def terminate?(population), do: population.max_fitness == 15
end
Genex requires 3 function definitions: encoding/0
, fitness_function/1
, and terminate?/1
. Let's take a closer look at each of these:
Encoding
def encoding do
for _ <- 1..15, do: Enum.random(0..1)
end
encoding/0
defines your encoding of the chromosome's genes for your use-case. In the example above, we define a Binary Gene Set of length 15. Genex uses this function to generate an initial population of Chromosomes matching your encoding.
Fitness Function
def fitness_function(chromosome) do
Enum.sum(chromosome.genes)
end
fitness_function/1
defines how the algorithm evaluates the fitness of a chromosome. It takes in a chromosome struct and returns a number. Fitness is how your algorithm determines which chromosomes should be persisted to the next generation as well as which chromosomes should be selected to crossover. In this case, we want to maximize 1's in our set of genes, so we define fitness as the sum of genes.
Termination Criteria
def terminate?(population) do
population.max_fitness == 15
end
terminate?/1
defines the termination criteria for your algorithm. This tells Genex when your algorithm should stop running. In this case we use a Max Fitness; however, you can also tell the algorithm to stop after a certain number of generations.
Running
Once you have defined an implementation module. Utilize the run/0
function to run the algorithm. The function will return the solution population for analysis. You can display a summary of the solution with the: Genex.Visualizers.Text.display_summary/1
function.
soln = OneMax.run()
Genex.Visualizers.Text.display_summary(soln)
Configuration
Genex offers a number of settings to adjust the algorithm to your liking. You can adjust: strategies, rates, and more. See the configuration guide for a full list.
Customization
You can customize every step of your genetic algorithm utilizing one or more of the many callbacks Genex provides. See the customization guide for more.
Link to this section Summary
Callbacks
Crossover a number of individuals to create a new population.
Crossover rate as a function of the population.
Generates a random gene set.
Evaluates a population's fitness.
Calculates a Chromosome's fitness.
Mutate a number of individuals, depending on the mutation_rate
.
Mutation rate as a function of the population.
Radiation level (aggressiveness) of mutation as a function of the population.
Seeds a population.
Selects a number of individuals for crossover, depending on the crossover_rate
.
Select a number of individuals to survive to the next generation, depending on the crossover_rate
.s
Specifies the statistics to collect on the population.
Tests the population for some termination criteria.
Link to this section Functions
Link to this section Callbacks
crossover(population)
View Sourcecrossover(population :: Genex.Population.t()) :: {:ok, Genex.Population.t()} | {:error, any()}
Crossover a number of individuals to create a new population.
crossover_rate(arg1)
View Sourcecrossover_rate(Genex.Population.t()) :: number()
Crossover rate as a function of the population.
Generates a random gene set.
evaluate(population)
View Sourceevaluate(population :: Genex.Population.t()) :: number()
Evaluates a population's fitness.
fitness_function(chromosome)
View Sourcefitness_function(chromosome :: Genex.Chromosome.t()) :: number()
Calculates a Chromosome's fitness.
mutate(population)
View Sourcemutate(population :: Genex.Population.t()) :: {:ok, Genex.Population.t()} | {:error, any()}
Mutate a number of individuals, depending on the mutation_rate
.
mutation_rate(arg1)
View Sourcemutation_rate(Genex.Population.t()) :: number()
Mutation rate as a function of the population.
Radiation level (aggressiveness) of mutation as a function of the population.
Seeds a population.
select_parents(population)
View Sourceselect_parents(population :: Genex.Population.t()) :: {:ok, Genex.Population.t()} | {:error, any()}
Selects a number of individuals for crossover, depending on the crossover_rate
.
select_survivors(population)
View Sourceselect_survivors(population :: Genex.Population.t()) :: {:ok, Genex.Population.t()} | {:error, any()}
Select a number of individuals to survive to the next generation, depending on the crossover_rate
.s
Specifies the statistics to collect on the population.
terminate?(population)
View Sourceterminate?(population :: Genex.Population.t()) :: boolean()
Tests the population for some termination criteria.