Genetic Algorithm (GA)¶
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
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
In [2]:
Copied!
available = Function().available_functions
print(available)
available = Function().available_functions
print(available)
['Rastrigin', 'Ackley', 'Sphere', 'Rosenbrock', 'Beale', 'GoldsteinPrice', 'Booth', 'Bukin', 'Matyas', 'Levi_N13', 'Griewank', 'Himmelblau', 'ThreeHumpCamel', 'Easom', 'Cross_In_Tray', 'EggHolder', 'HolderTable', 'McCormick', 'Schaffer_N2', 'StyblinskiTang', 'Shekel']
In [3]:
Copied!
fitness_function = Function('Ackley', reverse=False)
fitness_function = Function('Ackley', reverse=False)
In [4]:
Copied!
fitness_function.bounds
fitness_function.bounds
Out[4]:
[(-5, 5), (-5, 5)]
In [5]:
Copied!
#fitness_function.plot(bounds=fitness_function.bounds[0], dim=2, num_points=100, mode='surface')
#fitness_function.plot(bounds=fitness_function.bounds[0], dim=2, num_points=100, mode='contour')
#fitness_function.plot(bounds=fitness_function.bounds[0], dim=2, num_points=100, mode='surface')
#fitness_function.plot(bounds=fitness_function.bounds[0], dim=2, num_points=100, mode='contour')
In [11]:
Copied!
type = 'binary'
if type == 'binary':
mutation_function = mutation.flip_bit
encoder = encoding.Binary(precision=3, bounds=fitness_function.bounds)
else:
mutation_function = mutation.gaussian
encoder = encoding.Real(bounds=fitness_function.bounds)
ga = GeneticAlgorithm(
fitness_function=fitness_function,
crossover_function=crossover.onepoint,
mutation_function=mutation_function,
selection_function=selection.tournament,
population_size=200,
mutation_rate=0.01,
crossover_rate=0.7,
encoder=encoder,
minimize=True,
)
#ga.summary()
type = 'binary'
if type == 'binary':
mutation_function = mutation.flip_bit
encoder = encoding.Binary(precision=3, bounds=fitness_function.bounds)
else:
mutation_function = mutation.gaussian
encoder = encoding.Real(bounds=fitness_function.bounds)
ga = GeneticAlgorithm(
fitness_function=fitness_function,
crossover_function=crossover.onepoint,
mutation_function=mutation_function,
selection_function=selection.tournament,
population_size=200,
mutation_rate=0.01,
crossover_rate=0.7,
encoder=encoder,
minimize=True,
)
#ga.summary()
In [12]:
Copied!
ga.run(generations=500)
ga.run(generations=500)
Output()
In [13]:
Copied!
ga.best_fitness
ga.best_fitness
Out[13]:
0.001225738155231415
In [14]:
Copied!
best = ga.best_individual.reshape(1, -1)
print(f'Best Individual: {best}')
best = ga.best_individual.reshape(1, -1)
print(f'Best Individual: {best}')
Best Individual: [[-0.00030519 0.00030519]]
In [15]:
Copied!
x = best[0]
result = fitness_function(x)
print(result)
x = best[0]
result = fitness_function(x)
print(result)
0.001225738155231415
In [16]:
Copied!
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 = 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)
In [17]:
Copied!
fig1.show()
fig2.show()
fig1.show()
fig2.show()
In [18]:
Copied!
ga.fitness_plot()
ga.fitness_plot()