grape-bds 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
grape/GP/__init__.py ADDED
File without changes
grape/__init__.py ADDED
File without changes
grape/algorithms.py ADDED
@@ -0,0 +1,334 @@
1
+ # This file is part of DEAP.
2
+ #
3
+ # DEAP is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU Lesser General Public License as
5
+ # published by the Free Software Foundation, either version 3 of
6
+ # the License, or (at your option) any later version.
7
+ #
8
+ # DEAP is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with DEAP. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ import random
17
+ import math
18
+ import numpy as np
19
+ import time
20
+ import warnings
21
+
22
+ from deap import tools
23
+
24
+ def varAnd(population, toolbox, cxpb, mutpb,
25
+ bnf_grammar, codon_size, max_tree_depth, codon_consumption,
26
+ genome_representation, max_genome_length):
27
+ """Part of an evolutionary algorithm applying only the variation part
28
+ (crossover **and** mutation). The modified individuals have their
29
+ fitness invalidated. The individuals are cloned so returned population is
30
+ independent of the input population.
31
+
32
+ :param population: A list of individuals to vary.
33
+ :param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution
34
+ operators.
35
+ :param cxpb: The probability of mating two individuals.
36
+ :param mutpb: The probability of mutating an individual.
37
+ :returns: A list of varied individuals that are independent of their
38
+ parents.
39
+
40
+ """
41
+ offspring = [toolbox.clone(ind) for ind in population]
42
+
43
+ # Apply crossover and mutation on the offspring
44
+ for i in range(1, len(offspring), 2):
45
+ if random.random() < cxpb:
46
+ offspring[i - 1], offspring[i] = toolbox.mate(offspring[i - 1],
47
+ offspring[i],
48
+ bnf_grammar,
49
+ max_tree_depth,
50
+ codon_consumption,
51
+ genome_representation,
52
+ max_genome_length)
53
+
54
+ for i in range(len(offspring)):
55
+ offspring[i], = toolbox.mutate(offspring[i], mutpb,
56
+ codon_size, bnf_grammar,
57
+ max_tree_depth, codon_consumption,
58
+ max_genome_length)
59
+
60
+ return offspring
61
+
62
+ class hofWarning(UserWarning):
63
+ pass
64
+
65
+ def ge_eaSimpleWithElitism(population, toolbox, cxpb, mutpb, ngen, elite_size,
66
+ bnf_grammar, codon_size, max_tree_depth,
67
+ max_genome_length=None,
68
+ points_train=None, points_test=None, codon_consumption='eager',
69
+ report_items=None,
70
+ genome_representation='list',
71
+ stats=None, halloffame=None,
72
+ verbose=__debug__):
73
+ """This algorithm reproduce the simplest evolutionary algorithm as
74
+ presented in chapter 7 of [Back2000]_, with some adaptations to run GE
75
+ on GRAPE.
76
+ :param population: A list of individuals.
77
+ :param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution
78
+ operators.
79
+ :param cxpb: The probability of mating two individuals.
80
+ :param mutpb: The probability of mutating an individual.
81
+ :param ngen: The number of generation.
82
+ :param elite_size: The number of best individuals to be copied to the
83
+ next generation.
84
+ :params bnf_grammar, codon_size, max_tree_depth: Parameters
85
+ used to mapper the individuals after crossover and
86
+ mutation in order to check if they are valid.
87
+ :param stats: A :class:`~deap.tools.Statistics` object that is updated
88
+ inplace, optional.
89
+ :param halloffame: A :class:`~deap.tools.HallOfFame` object that will
90
+ contain the best individuals, optional.
91
+ :param verbose: Whether or not to log the statistics.
92
+ :returns: The final population
93
+ :returns: A class:`~deap.tools.Logbook` with the statistics of the
94
+ evolution
95
+ """
96
+
97
+ logbook = tools.Logbook()
98
+
99
+ if halloffame is None:
100
+ if elite_size != 0:
101
+ raise ValueError("You should add a hof object to use elitism.")
102
+ else:
103
+ warnings.warn('You will not register results of the best individual while not using a hof object.', hofWarning)
104
+ 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']
105
+ else:
106
+ if halloffame.maxsize < 1:
107
+ raise ValueError("HALLOFFAME_SIZE should be greater or equal to 1")
108
+ if elite_size > halloffame.maxsize:
109
+ raise ValueError("HALLOFFAME_SIZE should be greater or equal to ELITE_SIZE")
110
+ if points_test:
111
+ 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']
112
+ else:
113
+ 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']
114
+
115
+ start_gen = time.time()
116
+ # Evaluate the individuals with an invalid fitness
117
+ for ind in population:
118
+ if not ind.fitness.valid:
119
+ ind.fitness.values = toolbox.evaluate(ind, points_train)
120
+
121
+ valid0 = [ind for ind in population if not ind.invalid]
122
+ valid = [ind for ind in valid0 if not math.isnan(ind.fitness.values[0])]
123
+ if len(valid0) != len(valid):
124
+ warnings.warn("Warning: There are valid individuals with fitness = NaN in the population. We will avoid them.")
125
+ 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
126
+
127
+ list_structures = []
128
+ if 'fitness_diversity' in report_items:
129
+ list_fitnesses = []
130
+ if 'behavioural_diversity' in report_items:
131
+ behaviours = np.zeros([len(valid), len(valid[0].fitness_each_sample)], dtype=float)
132
+
133
+ #for ind in offspring:
134
+ for idx, ind in enumerate(valid):
135
+ list_structures.append(str(ind.structure))
136
+ if 'fitness_diversity' in report_items:
137
+ list_fitnesses.append(str(ind.fitness.values[0]))
138
+ if 'behavioural_diversity' in report_items:
139
+ behaviours[idx, :] = ind.fitness_each_sample
140
+
141
+ unique_structures = np.unique(list_structures, return_counts=False)
142
+ if 'fitness_diversity' in report_items:
143
+ unique_fitnesses = np.unique(list_fitnesses, return_counts=False)
144
+ if 'behavioural_diversity' in report_items:
145
+ unique_behaviours = np.unique(behaviours, axis=0)
146
+
147
+ structural_diversity = len(unique_structures)/len(population)
148
+ 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
149
+ behavioural_diversity = len(unique_behaviours)/len(population) if 'behavioural_diversity' in report_items else 0
150
+
151
+ # Update the hall of fame with the generated individuals
152
+ if halloffame is not None:
153
+ halloffame.update(valid)
154
+ best_ind_length = len(halloffame.items[0].genome)
155
+ best_ind_nodes = halloffame.items[0].nodes
156
+ best_ind_depth = halloffame.items[0].depth
157
+ best_ind_used_codons = halloffame.items[0].used_codons
158
+ if not verbose:
159
+ print("gen =", 0, ", Best fitness =", halloffame.items[0].fitness.values)
160
+
161
+ length = [len(ind.genome) for ind in valid]
162
+ avg_length = sum(length)/len(length)
163
+
164
+ nodes = [ind.nodes for ind in valid]
165
+ avg_nodes = sum(nodes)/len(nodes)
166
+
167
+ depth = [ind.depth for ind in valid]
168
+ avg_depth = sum(depth)/len(depth)
169
+
170
+ used_codons = [ind.used_codons for ind in valid]
171
+ avg_used_codons = sum(used_codons)/len(used_codons)
172
+
173
+ end_gen = time.time()
174
+ generation_time = end_gen-start_gen
175
+
176
+ selection_time = 0
177
+
178
+ if points_test:
179
+ fitness_test = np.NaN
180
+
181
+ record = stats.compile(population) if stats else {}
182
+ if points_test:
183
+ logbook.record(gen=0, invalid=invalid, **record,
184
+ fitness_test=fitness_test,
185
+ best_ind_length=best_ind_length, avg_length=avg_length,
186
+ best_ind_nodes=best_ind_nodes,
187
+ avg_nodes=avg_nodes,
188
+ best_ind_depth=best_ind_depth,
189
+ avg_depth=avg_depth,
190
+ avg_used_codons=avg_used_codons,
191
+ best_ind_used_codons=best_ind_used_codons,
192
+ behavioural_diversity=behavioural_diversity,
193
+ structural_diversity=structural_diversity,
194
+ fitness_diversity=fitness_diversity,
195
+ selection_time=selection_time,
196
+ generation_time=generation_time)
197
+ else:
198
+ logbook.record(gen=0, invalid=invalid, **record,
199
+ best_ind_length=best_ind_length, avg_length=avg_length,
200
+ best_ind_nodes=best_ind_nodes,
201
+ avg_nodes=avg_nodes,
202
+ best_ind_depth=best_ind_depth,
203
+ avg_depth=avg_depth,
204
+ avg_used_codons=avg_used_codons,
205
+ best_ind_used_codons=best_ind_used_codons,
206
+ behavioural_diversity=behavioural_diversity,
207
+ structural_diversity=structural_diversity,
208
+ fitness_diversity=fitness_diversity,
209
+ selection_time=selection_time,
210
+ generation_time=generation_time)
211
+ if verbose:
212
+ print(logbook.stream)
213
+
214
+ # Begin the generational process
215
+ for gen in range(logbook.select("gen")[-1]+1, ngen + 1):
216
+ start_gen = time.time()
217
+
218
+ # Select the next generation individuals
219
+ start = time.time()
220
+ offspring = toolbox.select(valid, len(population)-elite_size)
221
+ end = time.time()
222
+ selection_time = end-start
223
+ # Vary the pool of individuals
224
+ offspring = varAnd(offspring, toolbox, cxpb, mutpb,
225
+ bnf_grammar, codon_size, max_tree_depth,
226
+ codon_consumption, genome_representation,
227
+ max_genome_length)
228
+
229
+ # Evaluate the individuals with an invalid fitness
230
+ for ind in offspring:
231
+ if not ind.fitness.valid:
232
+ ind.fitness.values = toolbox.evaluate(ind, points_train)
233
+
234
+ #Update population for next generation
235
+ population[:] = offspring
236
+ #Include in the population the elitist individuals
237
+ for i in range(elite_size):
238
+ population.append(halloffame.items[i])
239
+
240
+ valid0 = [ind for ind in population if not ind.invalid]
241
+ valid = [ind for ind in valid0 if not math.isnan(ind.fitness.values[0])]
242
+ if len(valid0) != len(valid):
243
+ warnings.warn("Warning: There are valid individuals with fitness = NaN in the population. We will avoid in the statistics.")
244
+ 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
245
+
246
+ list_structures = []
247
+ if 'fitness_diversity' in report_items:
248
+ list_fitnesses = []
249
+ if 'behavioural_diversity' in report_items:
250
+ behaviours = np.zeros([len(valid), len(valid[0].fitness_each_sample)], dtype=float)
251
+
252
+ for idx, ind in enumerate(valid):
253
+ list_structures.append(str(ind.structure))
254
+ if 'fitness_diversity' in report_items:
255
+ list_fitnesses.append(str(ind.fitness.values[0]))
256
+ if 'behavioural_diversity' in report_items:
257
+ behaviours[idx, :] = ind.fitness_each_sample
258
+
259
+ unique_structures = np.unique(list_structures, return_counts=False)
260
+ if 'fitness_diversity' in report_items:
261
+ unique_fitnesses = np.unique(list_fitnesses, return_counts=False)
262
+ if 'behavioural_diversity' in report_items:
263
+ unique_behaviours = np.unique(behaviours, axis=0)
264
+
265
+ structural_diversity = len(unique_structures)/len(population)
266
+ 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
267
+ behavioural_diversity = len(unique_behaviours)/len(population) if 'behavioural_diversity' in report_items else 0
268
+
269
+ # Update the hall of fame with the generated individuals
270
+ if halloffame is not None:
271
+ halloffame.update(valid)
272
+ best_ind_length = len(halloffame.items[0].genome)
273
+ best_ind_nodes = halloffame.items[0].nodes
274
+ best_ind_depth = halloffame.items[0].depth
275
+ best_ind_used_codons = halloffame.items[0].used_codons
276
+ if not verbose:
277
+ print("gen =", gen, ", Best fitness =", halloffame.items[0].fitness.values, ", Number of invalids =", invalid)
278
+ if points_test:
279
+ if gen < ngen:
280
+ fitness_test = np.NaN
281
+ else:
282
+ fitness_test = toolbox.evaluate(halloffame.items[0], points_test)[0]
283
+
284
+ length = [len(ind.genome) for ind in valid]
285
+ avg_length = sum(length)/len(length)
286
+
287
+ nodes = [ind.nodes for ind in valid]
288
+ avg_nodes = sum(nodes)/len(nodes)
289
+
290
+ depth = [ind.depth for ind in valid]
291
+ avg_depth = sum(depth)/len(depth)
292
+
293
+ used_codons = [ind.used_codons for ind in valid]
294
+ avg_used_codons = sum(used_codons)/len(used_codons)
295
+
296
+ end_gen = time.time()
297
+ generation_time = end_gen-start_gen
298
+
299
+ # Append the current generation statistics to the logbook
300
+ record = stats.compile(population) if stats else {}
301
+ if points_test:
302
+ logbook.record(gen=gen, invalid=invalid, **record,
303
+ fitness_test=fitness_test,
304
+ best_ind_length=best_ind_length, avg_length=avg_length,
305
+ best_ind_nodes=best_ind_nodes,
306
+ avg_nodes=avg_nodes,
307
+ best_ind_depth=best_ind_depth,
308
+ avg_depth=avg_depth,
309
+ avg_used_codons=avg_used_codons,
310
+ best_ind_used_codons=best_ind_used_codons,
311
+ behavioural_diversity=behavioural_diversity,
312
+ structural_diversity=structural_diversity,
313
+ fitness_diversity=fitness_diversity,
314
+ selection_time=selection_time,
315
+ generation_time=generation_time)
316
+ else:
317
+ logbook.record(gen=gen, invalid=invalid, **record,
318
+ best_ind_length=best_ind_length, avg_length=avg_length,
319
+ best_ind_nodes=best_ind_nodes,
320
+ avg_nodes=avg_nodes,
321
+ best_ind_depth=best_ind_depth,
322
+ avg_depth=avg_depth,
323
+ avg_used_codons=avg_used_codons,
324
+ best_ind_used_codons=best_ind_used_codons,
325
+ behavioural_diversity=behavioural_diversity,
326
+ structural_diversity=structural_diversity,
327
+ fitness_diversity=fitness_diversity,
328
+ selection_time=selection_time,
329
+ generation_time=generation_time)
330
+
331
+ if verbose:
332
+ print(logbook.stream)
333
+
334
+ return population, logbook
File without changes