forked from carlosrabazo/AlgoritmoGenetico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problema_sencillo.py
83 lines (73 loc) · 2.62 KB
/
problema_sencillo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import random
import math
import numpy as np
from deap import base
from deap import creator
from deap import tools
from deap import algorithms
import matplotlib.pyplot as plt
# Creamos los objetos para definir el problema y el tipo de individuo
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
def funcion_objetivo(x):
"""
Función objetivo de nuestro problema
"""
for i in range(len(x)):
if x[i] > 100 or x[i] < -100:
return -1,
res = math.sqrt(x[0]**2 + x[1]**2)
return res,
toolbox = base.Toolbox()
# Generación de genes
toolbox.register("attr_uniform", random.uniform, -100, 100)
# Generación de inviduos y población
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.attr_uniform, 2)
toolbox.register("population", tools.initRepeat, list,
toolbox.individual, 30)
# Registro de operaciones genéticas
toolbox.register("evaluate", funcion_objetivo)
toolbox.register("mate", tools.cxOnePoint)
toolbox.register("mutate", tools.mutGaussian, mu=0,
sigma= 5, indpb=0.1)
toolbox.register("select", tools.selTournament, tournsize=3)
def plot_evolucion(log):
"""
Representa la evolución del mejor individuo en cada generación
"""
gen = log.select("gen")
fit_mins = log.select("min")
fit_maxs = log.select("max")
fit_ave = log.select("avg")
fig, ax1 = plt.subplots()
ax1.plot(gen, fit_mins, "b")
ax1.plot(gen, fit_maxs, "r")
ax1.plot(gen, fit_ave, "--k")
ax1.fill_between(gen, fit_mins, fit_maxs, where=fit_maxs >= fit_mins, facecolor='g', alpha = 0.2)
ax1.set_xlabel("Generation")
ax1.set_ylabel("Fitness")
ax1.set_ylim([-10, 160])
ax1.legend(["Min", "Max", "Avg"], loc="lower center")
plt.grid(True)
plt.savefig("Convergencia.eps", dpi = 300)
def main():
random.seed(42)
CXPB, MUTPB, NGEN = 0.5, 0.2, 20
pop = toolbox.population()
hof = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean)
stats.register("std", np.std)
stats.register("min", np.min)
stats.register("max", np.max)
logbook = tools.Logbook()
pop, logbook = algorithms.eaSimple(pop, toolbox, cxpb=CXPB,
mutpb=MUTPB, ngen=NGEN, stats=stats,
halloffame=hof, verbose=True)
return hof, logbook
if __name__ == "__main__":
best, log = main()
print("Mejor fitness: %f" %best[0].fitness.values)
print("Mejor individuo %s" %best[0])
plot_evolucion(log)