A Python implementation of a genetic algorithm for numerical optimization of real functions
The algorithm is encapsulated in the NumericalOptimizationGA
object,
which consists basically of a list of Individual
objects and some methods
to interact with it. Suppose you want to optimize the function of
from num_opt_ga import NumericalOptimizationGA
import numpy as np
from numpy.typing import NDArray
from numpy.linalg import norm
def func_to_optimize(pos: NDArray) -> float:
r = norm(pos)
sigma = 0.4
return np.cos(9 * np.pi * r) * np.exp(-((r / sigma) ** 2))
ga = NumericalOptimizationGA(
search_region=((-1, 1), (-1, 1)),
function=func_to_optimize,
pop_size=100,
)
Now the object ga
contains a list ga.population
of Individual
objects.
Each in,dividual is on a random position of the given search space. This
position is stored on the pos
atribute as an NumPy one dimensional NDArray.
The next generation of individuals is computed by a call to the evolve
method,
which will replace all the individuals in the list. So we can evolve the
population for 100 generation with
for _ in range(100):
ga.evolve()
Now we can create a scatter plot with the position of the best individual
import matplotlib.pyplot as plt
best_pos = [individual.pos for individual in ga.best(5)]
best_x = [pos[0] for pos in best_pos]
best_y = [pos[1] for pos in best_pos]
fig, ax = plt.subplots()
ax.set(
xlabel="x",
ylabel="y",
xlim=(-1, 1),
ylim=(-1, 1),
)
ax.scatter(best_x, best_y, color="red")
plt.show()
yielding
as expected, since the global maximum of this particular function is located at