View source: R/breed_next_gen.R
| breed_next_gen | R Documentation |
The purpose of this function is to take a vector of new parents, their corresponding fitness score vector and their corresponding genes to output the next generation in the genetic algorithm. It does this via ' several crossover methods including uniform, k-point, fitness-uniform along with their corresponding parameters. After Crossover is complete mutation is applied in two different methods either adaptive which can increase mutation when diversity becomes too low and fixed which maintains a steady mutation rate. Lastly, there is an option to minimize inbreeding prior to crossover and mutation.
breed_next_gen( generation_matrix, new_parents, score_vec, number_of_parents, mutation = "fixed", minimize_inbreeding = FALSE, crossover = "uniform", mutation_rate = 0.01, ad_max_mutate = 0.1, ad_min_mutate = 0.01, ad_inflection = 0.3, ad_curve = 15, required_pop, number_of_crossovers = 1 )
generation_matrix |
matrix with ncol = gene_length and nrow = total number of creatures, consists only of 1s and 0s, no all-zero rows. |
new_parents |
a numeric vector with elements indexing the next generation of parents, length is total population x number_of_parents. |
score_vec |
a numeric vector with length equal to total population. |
number_of_parents |
is an integer defining the number of parents per offspring |
mutation |
a character either 'fixed' or 'adaptive' indicating the type of mutation |
minimize_inbreeding |
a logical indicating whether or not we minimize inbreeding. |
crossover |
a character that can be 'uniform', 'fitness', or ’k_point’ indicating how to carry out crossover. |
mutation_rate |
a numeric between 0 and 1 indicating how often a creature is selected for mutation. |
ad_max_mutate |
max mutation rate for adaptive mutation, numeric between 0 and 1 and more than ad_min |
ad_min_mutate |
min mutation rate for adaptive mutation, numeric between 0 and 1 and less than ad_max |
ad_inflection |
percentage of diversity in population where adaptive mutation begins to increase rapidly |
ad_curve |
rate that influence how intensely adaptive mutation changes |
required_pop |
the number of offspring required |
number_of_crossovers |
number of k_point crossovers, needs to be less than gene_length |
crossover Methods Uniform - each gene is randomly selected from 2 or more parents from a PMF proportional to the number of parents.
Fitness - each gene is randomly selected from 2 or more parents from a PMF proportional to the parent's fitness.
K-Point - parents genes are broken into k+1 segments, then the offspring inherits portions randomly from the parents. Takes the parameter number_of_crossovers which must be less than 1/2 gene_length.
For all methods candidate offspring are accepted / rejected so that they don't have completely 0-vector genes.
mutation Fixed - Each offspring has a mutation_rate chance of being selected for mutation. Once selected one gene is switched form one to zero or zero to one.
Adaptive - the overall population is measured for diversity. As diversity becomes lower, the mutation rate increases. Once selected for mutation a single gene is switched from one to zero or zero to one. The adaptive function is controlled by a simple logistic function with parameters ad_min and ad_max describing the minimum and maximum mutation rates. ad_inflection controls where the logistics point pivots, and ad_curve controls how rapidly the logistics curve increase.
For all mutation methods, candidate offspring are accepted / rejected so that they don't have completely 0-vector genes.
Minimize inbreeding. his option reduces (though does not remove) the chance of similar creatures creating offspring together. Each parent is assigned new partner(s) randomly from a PMF proportional to how different their genes are. Parents are drawn without replacement so that if they were selected to become parents, they will still remain parents.
returns matrix of 1s and 0s with no all-zero rows representing the genes of the next generation after crossover and mutation
generation_matrix <- matrix(rbinom(10*10,1,.5),nc=10) new_parents <- sample(1:10,20,replace = TRUE) mutation <- 'fixed' crossover <- 'uniform' number_of_parents <- 2 required_pop <- 10 score_vec <-abs(rnorm(10)) next_generation_matrix <- breed_next_gen(generation_matrix = generation_matrix, new_parents = new_parents, mutation = mutation, crossover = crossover, number_of_parents = number_of_parents,required_pop = required_pop, score_vec = score_vec)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.