-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
153 lines (143 loc) · 5.58 KB
/
utils.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import matplotlib.pyplot as plt
import numpy as np
def plot_fittest(gens,birds,subdirs):
results = []
for subdir in subdirs:
fitnesses = []
split = subdir.split('-')
for gen in gens:
birds.load_population(generation=gen,subdir=subdir)
fitnesses.append(birds.select_fittest(1).fitness)
plt.plot(gens,fitnesses)
plt.title(f'Fittest birds - population_size={split[1]}, mutation_rate={split[2]}%')
plt.xlabel('Generation')
plt.ylabel('Average Fitness of Fittest Bird')
plt.tight_layout()
# plt.savefig(f'plots/top_birds_{gens[0]}-{gens[-1]}_{split[2]}.png')
plt.close()
results.append(fitnesses)
return results
def plot_parents(gens,birds,subdirs):
results = []
for subdir in subdirs:
fitnesses = []
split = subdir.split('-')
for gen in gens:
birds.load_population(generation=gen,subdir=subdir)
fitnesses.append(np.mean([b.fitness for b in birds.select_fittest()]))
plt.plot(gens,fitnesses)
plt.title(f'Fitness of Parent Birds - population_size={split[1]}, mutation_rate={split[2]}%')
plt.xlabel('Generation')
plt.ylabel('Average Fitness of Parents')
plt.tight_layout()
# plt.savefig(f'plots/parents_fitness_{gens[0]}-{gens[-1]}_{split[2]}.png')
plt.close()
results.append(fitnesses)
return results
def plot_pop_fitness(gens,birds,subdirs):
results = []
for subdir in subdirs:
fitnesses = []
split = subdir.split('-')
for gen in gens:
birds.load_population(generation=gen,subdir=subdir)
fitnesses.append(np.mean([b.fitness for b in birds.population]))
plt.plot(gens,fitnesses)
plt.title(f'Population Fitness - population_size={split[1]}, mutation_rate={split[2]}%')
plt.xlabel('Generation')
plt.ylabel('Average Population Fitness')
plt.tight_layout()
# plt.savefig(f'plots/pop_fitness_{gens[0]}-{gens[-1]}_{split[2]}.png')
plt.close()
results.append(fitnesses)
return results
def plot_fitness_comparison(gens,top,mean,parents,pop_size,mr):
plt.plot(gens,top,label='Top Bird')
plt.plot(gens,parents,label='Top 10 Birds')
plt.plot(gens,mean,label='All Birds')
plt.title(f'Fitness comparison - population_size={pop_size}, mutation_rate={mr}%')
plt.xlabel('Generation')
plt.ylabel('Average Fitness')
plt.legend(loc=0)
plt.tight_layout()
plt.savefig(f'plots/comp_fitness_{gens[0]}-{gens[-1]}_{mr}.png')
plt.close()
def plot_highscores(gens,birds,subdirs):
results = []
for subdir in subdirs:
if 'scores' not in subdir:
continue
fitnesses = []
split = subdir.split('-')
for gen in gens:
birds.load_population(generation=gen,subdir=subdir)
fitnesses.append(birds.select_fittest(1).score)
plt.plot(gens,fitnesses)
plt.title(f'Fittest birds - population_size={split[1]}, mutation_rate={split[2]}%')
plt.xlabel('Generation')
plt.ylabel('Average Score of Fittest Bird')
plt.tight_layout()
# plt.savefig(f'plots/top_birds_score_{gens[0]}-{gens[-1]}_{split[2]}.png')
plt.close()
results.append(fitnesses)
return results
def plot_parents_score(gens,birds,subdirs):
results = []
for subdir in subdirs:
if 'scores' not in subdir:
continue
fitnesses = []
split = subdir.split('-')
for gen in gens:
birds.load_population(generation=gen,subdir=subdir)
fitnesses.append(np.mean([b.score for b in birds.select_fittest()]))
plt.plot(gens,fitnesses)
plt.title(f'Score of Parent Birds - population_size={split[1]}, mutation_rate={split[2]}%')
plt.xlabel('Generation')
plt.ylabel('Average Score of Parents')
plt.tight_layout()
# plt.savefig(f'plots/parents_score_{gens[0]}-{gens[-1]}_{split[2]}.png')
plt.close()
results.append(fitnesses)
return results
def plot_pop_score(gens,birds,subdirs):
results = []
for subdir in subdirs:
if 'scores' not in subdir:
continue
fitnesses = []
split = subdir.split('-')
for gen in gens:
birds.load_population(generation=gen,subdir=subdir)
fitnesses.append(np.mean([b.score for b in birds.population]))
plt.plot(gens,fitnesses)
plt.title(f'Population Score - population_size={split[1]}, mutation_rate={split[2]}%')
plt.xlabel('Generation')
plt.ylabel('Average Population Score')
plt.tight_layout()
# plt.savefig(f'plots/pop_score_{gens[0]}-{gens[-1]}_{split[2]}.png')
plt.close()
results.append(fitnesses)
return results
def plot_score_comparison(gens,top,mean,parents,pop_size,mr):
plt.plot(gens,top,label='Top Bird')
plt.plot(gens,parents,label='Top 10 Birds')
plt.plot(gens,mean,label='All Birds')
plt.title(f'Score comparison - population_size={pop_size}, mutation_rate={mr}%')
plt.xlabel('Generation')
plt.ylabel('Average Score')
plt.legend(loc=0)
plt.tight_layout()
plt.savefig(f'plots/comp_score_{gens[0]}-{gens[-1]}_{mr}.png')
plt.close()
def plot_comparison(gens,p1,p2,p3,title,ylabel,suffix):
plt.plot(gens,p1[0],label=p1[1])
plt.plot(gens,p2[0],label=p2[1])
plt.plot(gens,p3[0],label=p3[1])
plt.title(title)
plt.xlabel('Generation')
plt.ylabel(ylabel)
plt.legend(loc=0)
plt.tight_layout()
plt.savefig(f'plots/comp_{gens[0]}-{gens[-1]}_{suffix}.png')
plt.close()