The function described below mutates offspring from the selected individuals that fit the best
based on the predefined condition(aim/objective).
e.g.: To optimize the function \(f(x) = x^2 - 4x + 4\)
to find the value of \(x\) that minimizes the function.
\(x\): represents a possible value the an individual from the population can have.
Arguments
- offspring
The list of offspring.
- mutation_rate
The probably of a single offspring to be modified/mutated.
Details
The mutation is needed to increase the diversity in the population and help the next generation close to the fitness.
Examples
# example of usage
population <- c(1, 3, 0)
# Evaluate fitness.
fitness <- genetic.algo.optimizeR::evaluate_fitness(population)
print("Evaluation:")
#> [1] "Evaluation:"
print(fitness)
#> [1] 1 1 4
# Selection
selected_parents <- genetic.algo.optimizeR::selection(population, fitness, num_parents = 2)
print("Selection:")
#> [1] "Selection:"
print(selected_parents)
#> [1] 1 3
# Crossover
offspring <- genetic.algo.optimizeR::crossover(selected_parents, offspring_size = 2)
print("Crossover:")
#> [1] "Crossover:"
print(offspring)
#> [1] 2 2
# Mutation
mutated_offspring <- genetic.algo.optimizeR::mutation(offspring, mutation_rate = 0)
# (no mutation in this example)
print(mutated_offspring)
#> [1] 2 2