forked from bdsul/grape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithms.py
334 lines (289 loc) · 16.3 KB
/
algorithms.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# This file is part of DEAP.
#
# DEAP is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# DEAP is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with DEAP. If not, see <http://www.gnu.org/licenses/>.
import random
import math
import numpy as np
import time
import warnings
from deap import tools
def varAnd(population, toolbox, cxpb, mutpb,
bnf_grammar, codon_size, max_tree_depth, codon_consumption,
genome_representation, max_genome_length):
"""Part of an evolutionary algorithm applying only the variation part
(crossover **and** mutation). The modified individuals have their
fitness invalidated. The individuals are cloned so returned population is
independent of the input population.
:param population: A list of individuals to vary.
:param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution
operators.
:param cxpb: The probability of mating two individuals.
:param mutpb: The probability of mutating an individual.
:returns: A list of varied individuals that are independent of their
parents.
"""
offspring = [toolbox.clone(ind) for ind in population]
# Apply crossover and mutation on the offspring
for i in range(1, len(offspring), 2):
if random.random() < cxpb:
offspring[i - 1], offspring[i] = toolbox.mate(offspring[i - 1],
offspring[i],
bnf_grammar,
max_tree_depth,
codon_consumption,
genome_representation,
max_genome_length)
for i in range(len(offspring)):
offspring[i], = toolbox.mutate(offspring[i], mutpb,
codon_size, bnf_grammar,
max_tree_depth, codon_consumption,
max_genome_length)
return offspring
class hofWarning(UserWarning):
pass
def ge_eaSimpleWithElitism(population, toolbox, cxpb, mutpb, ngen, elite_size,
bnf_grammar, codon_size, max_tree_depth,
max_genome_length=None,
points_train=None, points_test=None, codon_consumption='eager',
report_items=None,
genome_representation='list',
stats=None, halloffame=None,
verbose=__debug__):
"""This algorithm reproduce the simplest evolutionary algorithm as
presented in chapter 7 of [Back2000]_, with some adaptations to run GE
on GRAPE.
:param population: A list of individuals.
:param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution
operators.
:param cxpb: The probability of mating two individuals.
:param mutpb: The probability of mutating an individual.
:param ngen: The number of generation.
:param elite_size: The number of best individuals to be copied to the
next generation.
:params bnf_grammar, codon_size, max_tree_depth: Parameters
used to mapper the individuals after crossover and
mutation in order to check if they are valid.
:param stats: A :class:`~deap.tools.Statistics` object that is updated
inplace, optional.
:param halloffame: A :class:`~deap.tools.HallOfFame` object that will
contain the best individuals, optional.
:param verbose: Whether or not to log the statistics.
:returns: The final population
:returns: A class:`~deap.tools.Logbook` with the statistics of the
evolution
"""
logbook = tools.Logbook()
if halloffame is None:
if elite_size != 0:
raise ValueError("You should add a hof object to use elitism.")
else:
warnings.warn('You will not register results of the best individual while not using a hof object.', hofWarning)
logbook.header = ['gen', 'invalid'] + (stats.fields if stats else []) + ['avg_length', 'avg_nodes', 'avg_depth', 'avg_used_codons', 'behavioural_diversity', 'structural_diversity', 'fitness_diversity', 'selection_time', 'generation_time']
else:
if halloffame.maxsize < 1:
raise ValueError("HALLOFFAME_SIZE should be greater or equal to 1")
if elite_size > halloffame.maxsize:
raise ValueError("HALLOFFAME_SIZE should be greater or equal to ELITE_SIZE")
if points_test:
logbook.header = ['gen', 'invalid'] + (stats.fields if stats else []) + ['fitness_test', 'best_ind_length', 'avg_length', 'best_ind_nodes', 'avg_nodes', 'best_ind_depth', 'avg_depth', 'avg_used_codons', 'best_ind_used_codons', 'behavioural_diversity', 'structural_diversity', 'fitness_diversity', 'selection_time', 'generation_time']
else:
logbook.header = ['gen', 'invalid'] + (stats.fields if stats else []) + ['best_ind_length', 'avg_length', 'best_ind_nodes', 'avg_nodes', 'best_ind_depth', 'avg_depth', 'avg_used_codons', 'best_ind_used_codons', 'behavioural_diversity', 'structural_diversity', 'fitness_diversity', 'selection_time', 'generation_time']
start_gen = time.time()
# Evaluate the individuals with an invalid fitness
for ind in population:
if not ind.fitness.valid:
ind.fitness.values = toolbox.evaluate(ind, points_train)
valid0 = [ind for ind in population if not ind.invalid]
valid = [ind for ind in valid0 if not math.isnan(ind.fitness.values[0])]
if len(valid0) != len(valid):
warnings.warn("Warning: There are valid individuals with fitness = NaN in the population. We will avoid them.")
invalid = len(population) - len(valid0) #We use the original number of invalids in this case, because we just want to count the completely mapped individuals
list_structures = []
if 'fitness_diversity' in report_items:
list_fitnesses = []
if 'behavioural_diversity' in report_items:
behaviours = np.zeros([len(valid), len(valid[0].fitness_each_sample)], dtype=float)
#for ind in offspring:
for idx, ind in enumerate(valid):
list_structures.append(str(ind.structure))
if 'fitness_diversity' in report_items:
list_fitnesses.append(str(ind.fitness.values[0]))
if 'behavioural_diversity' in report_items:
behaviours[idx, :] = ind.fitness_each_sample
unique_structures = np.unique(list_structures, return_counts=False)
if 'fitness_diversity' in report_items:
unique_fitnesses = np.unique(list_fitnesses, return_counts=False)
if 'behavioural_diversity' in report_items:
unique_behaviours = np.unique(behaviours, axis=0)
structural_diversity = len(unique_structures)/len(population)
fitness_diversity = len(unique_fitnesses)/(len(points_train[1])+1) if 'fitness_diversity' in report_items else 0 #TODO generalise for other problems, because it only works if the fitness is proportional to the number of testcases correctly predicted
behavioural_diversity = len(unique_behaviours)/len(population) if 'behavioural_diversity' in report_items else 0
# Update the hall of fame with the generated individuals
if halloffame is not None:
halloffame.update(valid)
best_ind_length = len(halloffame.items[0].genome)
best_ind_nodes = halloffame.items[0].nodes
best_ind_depth = halloffame.items[0].depth
best_ind_used_codons = halloffame.items[0].used_codons
if not verbose:
print("gen =", 0, ", Best fitness =", halloffame.items[0].fitness.values)
length = [len(ind.genome) for ind in valid]
avg_length = sum(length)/len(length)
nodes = [ind.nodes for ind in valid]
avg_nodes = sum(nodes)/len(nodes)
depth = [ind.depth for ind in valid]
avg_depth = sum(depth)/len(depth)
used_codons = [ind.used_codons for ind in valid]
avg_used_codons = sum(used_codons)/len(used_codons)
end_gen = time.time()
generation_time = end_gen-start_gen
selection_time = 0
if points_test:
fitness_test = np.NaN
record = stats.compile(population) if stats else {}
if points_test:
logbook.record(gen=0, invalid=invalid, **record,
fitness_test=fitness_test,
best_ind_length=best_ind_length, avg_length=avg_length,
best_ind_nodes=best_ind_nodes,
avg_nodes=avg_nodes,
best_ind_depth=best_ind_depth,
avg_depth=avg_depth,
avg_used_codons=avg_used_codons,
best_ind_used_codons=best_ind_used_codons,
behavioural_diversity=behavioural_diversity,
structural_diversity=structural_diversity,
fitness_diversity=fitness_diversity,
selection_time=selection_time,
generation_time=generation_time)
else:
logbook.record(gen=0, invalid=invalid, **record,
best_ind_length=best_ind_length, avg_length=avg_length,
best_ind_nodes=best_ind_nodes,
avg_nodes=avg_nodes,
best_ind_depth=best_ind_depth,
avg_depth=avg_depth,
avg_used_codons=avg_used_codons,
best_ind_used_codons=best_ind_used_codons,
behavioural_diversity=behavioural_diversity,
structural_diversity=structural_diversity,
fitness_diversity=fitness_diversity,
selection_time=selection_time,
generation_time=generation_time)
if verbose:
print(logbook.stream)
# Begin the generational process
for gen in range(logbook.select("gen")[-1]+1, ngen + 1):
start_gen = time.time()
# Select the next generation individuals
start = time.time()
offspring = toolbox.select(valid, len(population)-elite_size)
end = time.time()
selection_time = end-start
# Vary the pool of individuals
offspring = varAnd(offspring, toolbox, cxpb, mutpb,
bnf_grammar, codon_size, max_tree_depth,
codon_consumption, genome_representation,
max_genome_length)
# Evaluate the individuals with an invalid fitness
for ind in offspring:
if not ind.fitness.valid:
ind.fitness.values = toolbox.evaluate(ind, points_train)
#Update population for next generation
population[:] = offspring
#Include in the population the elitist individuals
for i in range(elite_size):
population.append(halloffame.items[i])
valid0 = [ind for ind in population if not ind.invalid]
valid = [ind for ind in valid0 if not math.isnan(ind.fitness.values[0])]
if len(valid0) != len(valid):
warnings.warn("Warning: There are valid individuals with fitness = NaN in the population. We will avoid in the statistics.")
invalid = len(population) - len(valid0) #We use the original number of invalids in this case, because we just want to count the completely mapped individuals
list_structures = []
if 'fitness_diversity' in report_items:
list_fitnesses = []
if 'behavioural_diversity' in report_items:
behaviours = np.zeros([len(valid), len(valid[0].fitness_each_sample)], dtype=float)
for idx, ind in enumerate(valid):
list_structures.append(str(ind.structure))
if 'fitness_diversity' in report_items:
list_fitnesses.append(str(ind.fitness.values[0]))
if 'behavioural_diversity' in report_items:
behaviours[idx, :] = ind.fitness_each_sample
unique_structures = np.unique(list_structures, return_counts=False)
if 'fitness_diversity' in report_items:
unique_fitnesses = np.unique(list_fitnesses, return_counts=False)
if 'behavioural_diversity' in report_items:
unique_behaviours = np.unique(behaviours, axis=0)
structural_diversity = len(unique_structures)/len(population)
fitness_diversity = len(unique_fitnesses)/(len(points_train[1])+1) if 'fitness_diversity' in report_items else 0 #TODO generalise for other problems, because it only works if the fitness is proportional to the number of testcases correctly predicted
behavioural_diversity = len(unique_behaviours)/len(population) if 'behavioural_diversity' in report_items else 0
# Update the hall of fame with the generated individuals
if halloffame is not None:
halloffame.update(valid)
best_ind_length = len(halloffame.items[0].genome)
best_ind_nodes = halloffame.items[0].nodes
best_ind_depth = halloffame.items[0].depth
best_ind_used_codons = halloffame.items[0].used_codons
if not verbose:
print("gen =", gen, ", Best fitness =", halloffame.items[0].fitness.values, ", Number of invalids =", invalid)
if points_test:
if gen < ngen:
fitness_test = np.NaN
else:
fitness_test = toolbox.evaluate(halloffame.items[0], points_test)[0]
length = [len(ind.genome) for ind in valid]
avg_length = sum(length)/len(length)
nodes = [ind.nodes for ind in valid]
avg_nodes = sum(nodes)/len(nodes)
depth = [ind.depth for ind in valid]
avg_depth = sum(depth)/len(depth)
used_codons = [ind.used_codons for ind in valid]
avg_used_codons = sum(used_codons)/len(used_codons)
end_gen = time.time()
generation_time = end_gen-start_gen
# Append the current generation statistics to the logbook
record = stats.compile(population) if stats else {}
if points_test:
logbook.record(gen=gen, invalid=invalid, **record,
fitness_test=fitness_test,
best_ind_length=best_ind_length, avg_length=avg_length,
best_ind_nodes=best_ind_nodes,
avg_nodes=avg_nodes,
best_ind_depth=best_ind_depth,
avg_depth=avg_depth,
avg_used_codons=avg_used_codons,
best_ind_used_codons=best_ind_used_codons,
behavioural_diversity=behavioural_diversity,
structural_diversity=structural_diversity,
fitness_diversity=fitness_diversity,
selection_time=selection_time,
generation_time=generation_time)
else:
logbook.record(gen=gen, invalid=invalid, **record,
best_ind_length=best_ind_length, avg_length=avg_length,
best_ind_nodes=best_ind_nodes,
avg_nodes=avg_nodes,
best_ind_depth=best_ind_depth,
avg_depth=avg_depth,
avg_used_codons=avg_used_codons,
best_ind_used_codons=best_ind_used_codons,
behavioural_diversity=behavioural_diversity,
structural_diversity=structural_diversity,
fitness_diversity=fitness_diversity,
selection_time=selection_time,
generation_time=generation_time)
if verbose:
print(logbook.stream)
return population, logbook