Lecture 1 - Genetic Algorithms (GAs) for function optimization"

knitr::opts_chunk$set(echo = TRUE)

Let's get started

Introduction to GAs

Overview: Genetic algorithms (GAs) are optimization methods inspired by natural selection and genetics. They are used to solve optimization problems by evolving a population of candidate solutions.

Key concepts:

Problem to solve

Objective: We aim to optimize the quadratic function $f(x) = x^2 - 4x + 4$, minimizing the value of $f(x)$.

Function: $$ f(x) = x^2 - 4x + 4 $$

Initialize population

We begin by creating an initial population of three individuals: $x_1 = 1$, $x_2 = 3$, and $x_3 = 0$.

population <- c(1, 3, 0)
population

Evaluate fitness

Fitness function: The fitness of an individual is how well it minimizes the function $f(x)$.

For each individual, calculate $f(x)$: $$ f(x) = x^2 - 4x + 4 $$

f <- function(x) {
    x^2 - 4*x + 4
}

# we calculate fitness
fitness <- f(population)
fitness

Visualizing the fitness function

Let's plot the quadratic function $f(x)$ to visualize the optimization landscape.

library(ggplot2)

x <- seq(from = 0, to = 4, length.out = 100)
y <- f(x)

ggplot(data.frame(x, y), aes(x, y)) +
  geom_line(color = "blue") +
  labs(title = "Quadratic function: f(x)", x = "x", y = "f(x)") +
  theme_minimal()

Selection

We select individuals for crossover based on fitness. Lower fitness values are better in this case, so we select $x_1 = 1$ and $x_2 = 3$.

# w select the parents with the best fitness (lowest values)
selected_parents <- population[order(fitness)][1:2]
selected_parents

Crossover and mutation

Crossover: Combines the genes (values) of two parents to create offspring.

Mutation: Introduces random changes to avoid local minima.

# perform crossover (simple example: no change in this step)
offspring <- selected_parents

# introduce mutation with a small probability (no mutation for this example)
mutated_offspring <- offspring
mutated_offspring

Replacement

We replace one of the individuals in the population with the offspring to form a new population.

# replace the least fit individual in the population with one of the offspring
new_population <- c(selected_parents, population[which.min(fitness)])
new_population

Iteration and convergence

The process is repeated over multiple generations. The population evolves over time, approaching the optimal solution, to eventually reach it.

Finding the optimal solution

Theoretical optimal point:

The minimum of the quadratic function occurs at: $$ x = \frac{-b}{2a} = 2 $$ The corresponding function value is $f(2) = 0$.

# calculate the optimal point
optimal_x <- -(-4) / (2 * 1)
optimal_y <- f(optimal_x)
c(optimal_x, optimal_y)

Conclusion

Key Takeaways:

Further Applications:


Thank You!

Any questions?



Try the genetic.algo.optimizeR package in your browser

Any scripts or data that you put into this service are public.

genetic.algo.optimizeR documentation built on Oct. 10, 2024, 5:07 p.m.