Genetic Algorithm (GA)¶
Implementar el Algoritmo Genético simple con las siguientes características:
- Codificación binaria
- Selección: ruleta
- Cruza en un punto
- Mutación aleatoria
- La probabilidad de cruza, probabilidad de mutación, tamaño de la población y número de generación quedan a su elección.
- Precisión de 3 dígitos decimales.
Se deben usar las siguientes funciones de optimización de un solo objetivo para minimización:
Esfera (n=2 y n=5) ; -10≤x≤10
Bukin (x, y) ; -15≤x≤-5 ; -3≤y≤-3
Himmenblau (x, y); -5≤x,y≤5
Eggholder (x, y); -512≤x,y≤512
[Opcional] Easom (x, y); -100≤x,y≤100
In [1]:
Copied!
from metazoo.bio.evolutionary import GeneticAlgorithm
from metazoo.bio.evolutionary.operators import selection, mutation, crossover
from metazoo.gym.mono import Function
from metazoo.bio.utils import encoding
# Set offline plotly
import plotly.io as pio
pio.renderers.default = "svg"
from metazoo.bio.evolutionary import GeneticAlgorithm
from metazoo.bio.evolutionary.operators import selection, mutation, crossover
from metazoo.gym.mono import Function
from metazoo.bio.utils import encoding
# Set offline plotly
import plotly.io as pio
pio.renderers.default = "svg"
In [2]:
Copied!
targets = ['Sphere', 'Bukin', 'Himmelblau', 'EggHolder', 'Easom']
targets = ['Sphere', 'Bukin', 'Himmelblau', 'EggHolder', 'Easom']
In [3]:
Copied!
mutation_function = mutation.flip_bit
mutation_function = mutation.flip_bit
In [4]:
Copied!
fitness_function = Function(targets[0], reverse=False)
encoder = encoding.Binary(precision=3, bounds=fitness_function.bounds)
ga = GeneticAlgorithm(
fitness_function=fitness_function,
crossover_function=crossover.onepoint,
mutation_function=mutation_function,
selection_function=selection.roulette,
population_size=100,
mutation_rate=0.01,
crossover_rate=0.7,
encoder=encoder,
elitism=False,
minimize=True,
)
ga.run(generations=100)
print(f'Best Individual: {ga.best_individual}')
print(f'Best Fitness: {ga.best_fitness}')
best = ga.best_individual.reshape(1, -1)
fig1 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='surface', population=best)
fig2 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='contour', population=best)
fig1.show()
fig2.show()
fitness_function = Function(targets[0], reverse=False)
encoder = encoding.Binary(precision=3, bounds=fitness_function.bounds)
ga = GeneticAlgorithm(
fitness_function=fitness_function,
crossover_function=crossover.onepoint,
mutation_function=mutation_function,
selection_function=selection.roulette,
population_size=100,
mutation_rate=0.01,
crossover_rate=0.7,
encoder=encoder,
elitism=False,
minimize=True,
)
ga.run(generations=100)
print(f'Best Individual: {ga.best_individual}')
print(f'Best Fitness: {ga.best_fitness}')
best = ga.best_individual.reshape(1, -1)
fig1 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='surface', population=best)
fig2 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='contour', population=best)
fig1.show()
fig2.show()
Output()
Best Individual: [-0.00531282 0.01906366] Best Fitness: 0.00039164936980193234
In [5]:
Copied!
fitness_function = Function(targets[0], reverse=True)
encoder = encoding.Binary(precision=3, bounds=[(-5.12, 5.12), (-5.12, 5.12), (-5.12, 5.12), (-5.12, 5.12), (-5.12, 5.12)])
ga = GeneticAlgorithm(
fitness_function=fitness_function,
crossover_function=crossover.onepoint,
mutation_function=mutation_function,
selection_function=selection.roulette,
population_size=100,
mutation_rate=0.01,
crossover_rate=0.7,
encoder=encoder,
elitism=False,
minimize=True,
)
ga.run(generations=100)
print(f"Shape: {ga.best_individual.shape}")
print(f'Best Individual: {ga.best_individual}')
print(f'Best Fitness: {ga.best_fitness}')
best = ga.best_individual.reshape(1, -1)
fitness_function = Function(targets[0], reverse=True)
encoder = encoding.Binary(precision=3, bounds=[(-5.12, 5.12), (-5.12, 5.12), (-5.12, 5.12), (-5.12, 5.12), (-5.12, 5.12)])
ga = GeneticAlgorithm(
fitness_function=fitness_function,
crossover_function=crossover.onepoint,
mutation_function=mutation_function,
selection_function=selection.roulette,
population_size=100,
mutation_rate=0.01,
crossover_rate=0.7,
encoder=encoder,
elitism=False,
minimize=True,
)
ga.run(generations=100)
print(f"Shape: {ga.best_individual.shape}")
print(f'Best Individual: {ga.best_individual}')
print(f'Best Fitness: {ga.best_fitness}')
best = ga.best_individual.reshape(1, -1)
Output()
Shape: (5,) Best Individual: [ 5.11812489 5.10312397 -4.98749191 5.10874931 -5.10437405] Best Fitness: -129.2661061231976
In [6]:
Copied!
fitness_function = Function(targets[1], reverse=False)
encoder = encoding.Binary(precision=3, bounds=fitness_function.bounds)
ga = GeneticAlgorithm(
fitness_function=fitness_function,
crossover_function=crossover.onepoint,
mutation_function=mutation_function,
selection_function=selection.roulette,
population_size=100,
mutation_rate=0.01,
crossover_rate=0.7,
encoder=encoder,
elitism=False,
minimize=True,
)
ga.run(generations=100)
print(f'Best Individual: {ga.best_individual}')
print(f'Best Fitness: {ga.best_fitness}')
best = ga.best_individual.reshape(1, -1)
fig1 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='surface', population=best)
fig2 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='contour', population=best)
fig1.show()
fig2.show()
fitness_function = Function(targets[1], reverse=False)
encoder = encoding.Binary(precision=3, bounds=fitness_function.bounds)
ga = GeneticAlgorithm(
fitness_function=fitness_function,
crossover_function=crossover.onepoint,
mutation_function=mutation_function,
selection_function=selection.roulette,
population_size=100,
mutation_rate=0.01,
crossover_rate=0.7,
encoder=encoder,
elitism=False,
minimize=True,
)
ga.run(generations=100)
print(f'Best Individual: {ga.best_individual}')
print(f'Best Fitness: {ga.best_fitness}')
best = ga.best_individual.reshape(1, -1)
fig1 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='surface', population=best)
fig2 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='contour', population=best)
fig1.show()
fig2.show()
Output()
Best Individual: [-13.95013123 1.94598059] Best Fitness: 0.9396385788307542
In [7]:
Copied!
fitness_function = Function(targets[2], reverse=False)
encoder = encoding.Binary(precision=3, bounds=fitness_function.bounds)
ga = GeneticAlgorithm(
fitness_function=fitness_function,
crossover_function=crossover.onepoint,
mutation_function=mutation_function,
selection_function=selection.roulette,
population_size=100,
mutation_rate=0.01,
crossover_rate=0.7,
encoder=encoder,
elitism=False,
minimize=True,
)
ga.run(generations=100)
print(f'Best Individual: {ga.best_individual}')
print(f'Best Fitness: {ga.best_fitness}')
best = ga.best_individual.reshape(1, -1)
fig1 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='surface', population=best)
fig2 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='contour', population=best)
fig1.show()
fig2.show()
fitness_function = Function(targets[2], reverse=False)
encoder = encoding.Binary(precision=3, bounds=fitness_function.bounds)
ga = GeneticAlgorithm(
fitness_function=fitness_function,
crossover_function=crossover.onepoint,
mutation_function=mutation_function,
selection_function=selection.roulette,
population_size=100,
mutation_rate=0.01,
crossover_rate=0.7,
encoder=encoder,
elitism=False,
minimize=True,
)
ga.run(generations=100)
print(f'Best Individual: {ga.best_individual}')
print(f'Best Fitness: {ga.best_fitness}')
best = ga.best_individual.reshape(1, -1)
fig1 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='surface', population=best)
fig2 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='contour', population=best)
fig1.show()
fig2.show()
Output()
Best Individual: [-2.81297687 3.13037905] Best Fitness: 0.0020555828097402243
In [8]:
Copied!
fitness_function = Function(targets[3], reverse=False)
encoder = encoding.Binary(precision=3, bounds=fitness_function.bounds)
ga = GeneticAlgorithm(
fitness_function=fitness_function,
crossover_function=crossover.onepoint,
mutation_function=mutation_function,
selection_function=selection.roulette,
population_size=100,
mutation_rate=0.01,
crossover_rate=0.7,
encoder=encoder,
elitism=False,
minimize=True,
)
ga.run(generations=100)
print(f'Best Individual: {ga.best_individual}')
print(f'Best Fitness: {ga.best_fitness}')
best = ga.best_individual.reshape(1, -1)
fig1 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='surface', population=best)
fig2 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='contour', population=best)
fig1.show()
fig2.show()
fitness_function = Function(targets[3], reverse=False)
encoder = encoding.Binary(precision=3, bounds=fitness_function.bounds)
ga = GeneticAlgorithm(
fitness_function=fitness_function,
crossover_function=crossover.onepoint,
mutation_function=mutation_function,
selection_function=selection.roulette,
population_size=100,
mutation_rate=0.01,
crossover_rate=0.7,
encoder=encoder,
elitism=False,
minimize=True,
)
ga.run(generations=100)
print(f'Best Individual: {ga.best_individual}')
print(f'Best Fitness: {ga.best_fitness}')
best = ga.best_individual.reshape(1, -1)
fig1 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='surface', population=best)
fig2 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=100, mode='contour', population=best)
fig1.show()
fig2.show()
Output()
Best Individual: [510.93163961 403.51747467] Best Fitness: -955.7702119015352
In [9]:
Copied!
fitness_function = Function(targets[4], reverse=False)
encoder = encoding.Binary(precision=3, bounds=fitness_function.bounds)
ga = GeneticAlgorithm(
fitness_function=fitness_function,
crossover_function=crossover.onepoint,
mutation_function=mutation_function,
selection_function=selection.roulette,
population_size=100,
mutation_rate=0.01,
crossover_rate=0.7,
encoder=encoder,
elitism=False,
minimize=True,
)
ga.run(generations=200)
print(f'Best Individual: {ga.best_individual}')
print(f'Best Fitness: {ga.best_fitness}')
best = ga.best_individual.reshape(1, -1)
fig1 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=300, mode='surface', population=best)
fig2 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=300, mode='contour', population=best)
fig1.show()
fig2.show()
fitness_function = Function(targets[4], reverse=False)
encoder = encoding.Binary(precision=3, bounds=fitness_function.bounds)
ga = GeneticAlgorithm(
fitness_function=fitness_function,
crossover_function=crossover.onepoint,
mutation_function=mutation_function,
selection_function=selection.roulette,
population_size=100,
mutation_rate=0.01,
crossover_rate=0.7,
encoder=encoder,
elitism=False,
minimize=True,
)
ga.run(generations=200)
print(f'Best Individual: {ga.best_individual}')
print(f'Best Fitness: {ga.best_fitness}')
best = ga.best_individual.reshape(1, -1)
fig1 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=300, mode='surface', population=best)
fig2 = fitness_function.plot(bounds=fitness_function.bounds, dim=2, num_points=300, mode='contour', population=best)
fig1.show()
fig2.show()
Output()
Best Individual: [ 2.96212373 -3.37029789] Best Fitness: -3.5594754561549357e-19