Introduction to genetic.algo.optimizeR
Source:vignettes/intro_to_genetic.algo.optimizeR.Rmd
intro_to_genetic.algo.optimizeR.Rmd
Overview
This vignette demonstrates how to use the
genetic.algo.optimizeR
package to optimize the function
using a genetic algorithm.
Method
Initial Population
We start with a population of three individuals: , , and .
# devtools::install_github("danymukesha/genetic.algo.optimizeR", upgrade = c("never"),)
library(genetic.algo.optimizeR)
# Initialize population
population <- initialize_population(population_size = 3, min = 0, max = 3)
population
#> [1] 2 1 3
Evaluation
We evaluate the fitness of each individual by calculating for each value:
# Evaluate fitness
fitness <- evaluate_fitness(population)
fitness
#> [1] 0 1 1
Selection
We select individuals and as parents for crossover because they have higher fitness.
# Perform selection
selected_parents <- selection(population, fitness, num_parents = 2)
selected_parents
#> [1] 2 1
Crossover and Mutation
We perform crossover and mutation on the selected parents to generate offspring: , .
Replacement
We replace individual with offspring , maintaining the population size.
# Replace individuals in the population
new_population <- replacement(population, mutated_offspring, num_to_replace = 1)
new_population
#> [1] 2 1 1
# Termination
# Repeat the above steps(from Evaluation) for multiple generations or until a termination condition is met.