/
gp_deap.py
466 lines (400 loc) · 19.6 KB
/
gp_deap.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# -*- coding: utf-8 -*-
"""This file is part of the TPOT library.
TPOT was primarily developed at the University of Pennsylvania by:
- Randal S. Olson (rso@randalolson.com)
- Weixuan Fu (weixuanf@upenn.edu)
- Daniel Angell (dpa34@drexel.edu)
- and many more generous open source contributors
TPOT 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.
TPOT 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 TPOT. If not, see <http://www.gnu.org/licenses/>.
"""
import numpy as np
from deap import tools, gp
from inspect import isclass
from .operator_utils import set_sample_weight
from sklearn.utils import indexable
from sklearn.metrics.scorer import check_scoring
from sklearn.model_selection._validation import _fit_and_score
from sklearn.model_selection._split import check_cv
from sklearn.base import clone, is_classifier
from collections import defaultdict
import warnings
from stopit import threading_timeoutable, TimeoutException
def pick_two_individuals_eligible_for_crossover(population):
"""Pick two individuals from the population which can do crossover, that is, they share a primitive.
Parameters
----------
population: array of individuals
Returns
----------
tuple: (individual, individual)
Two individuals which are not the same, but share at least one primitive.
Alternatively, if no such pair exists in the population, (None, None) is returned instead.
"""
primitives_by_ind = [set([node.name for node in ind if isinstance(node, gp.Primitive)])
for ind in population]
pop_as_str = [str(ind) for ind in population]
eligible_pairs = [(i, i+1+j) for i, ind1_prims in enumerate(primitives_by_ind)
for j, ind2_prims in enumerate(primitives_by_ind[i+1:])
if not ind1_prims.isdisjoint(ind2_prims) and
pop_as_str[i] != pop_as_str[i+1+j]]
# Pairs are eligible in both orders, this ensures that both orders are considered
eligible_pairs += [(j, i) for (i, j) in eligible_pairs]
if not eligible_pairs:
# If there are no eligible pairs, the caller should decide what to do
return None, None
pair = np.random.randint(0, len(eligible_pairs))
idx1, idx2 = eligible_pairs[pair]
return population[idx1], population[idx2]
def mutate_random_individual(population, toolbox):
"""Picks a random individual from the population, and performs mutation on a copy of it.
Parameters
----------
population: array of individuals
Returns
----------
individual: individual
An individual which is a mutated copy of one of the individuals in population,
the returned individual does not have fitness.values
"""
idx = np.random.randint(0,len(population))
ind = population[idx]
ind, = toolbox.mutate(ind)
del ind.fitness.values
return ind
def varOr(population, toolbox, lambda_, cxpb, mutpb):
"""Part of an evolutionary algorithm applying only the variation part
(crossover, mutation **or** reproduction). 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 lambda\_: The number of children to produce
:param cxpb: The probability of mating two individuals.
:param mutpb: The probability of mutating an individual.
:returns: The final population
:returns: A class:`~deap.tools.Logbook` with the statistics of the
evolution
The variation goes as follow. On each of the *lambda_* iteration, it
selects one of the three operations; crossover, mutation or reproduction.
In the case of a crossover, two individuals are selected at random from
the parental population :math:`P_\mathrm{p}`, those individuals are cloned
using the :meth:`toolbox.clone` method and then mated using the
:meth:`toolbox.mate` method. Only the first child is appended to the
offspring population :math:`P_\mathrm{o}`, the second child is discarded.
In the case of a mutation, one individual is selected at random from
:math:`P_\mathrm{p}`, it is cloned and then mutated using using the
:meth:`toolbox.mutate` method. The resulting mutant is appended to
:math:`P_\mathrm{o}`. In the case of a reproduction, one individual is
selected at random from :math:`P_\mathrm{p}`, cloned and appended to
:math:`P_\mathrm{o}`.
This variation is named *Or* beceause an offspring will never result from
both operations crossover and mutation. The sum of both probabilities
shall be in :math:`[0, 1]`, the reproduction probability is
1 - *cxpb* - *mutpb*.
"""
offspring = []
for _ in range(lambda_):
op_choice = np.random.random()
if op_choice < cxpb: # Apply crossover
ind1, ind2 = pick_two_individuals_eligible_for_crossover(population)
if ind1 is not None:
ind1, _ = toolbox.mate(ind1, ind2)
del ind1.fitness.values
else:
# If there is no pair eligible for crossover, we still want to
# create diversity in the population, and do so by mutation instead.
ind1 = mutate_random_individual(population, toolbox)
offspring.append(ind1)
elif op_choice < cxpb + mutpb: # Apply mutation
ind = mutate_random_individual(population, toolbox)
offspring.append(ind)
else: # Apply reproduction
idx = np.random.randint(0, len(population))
offspring.append(toolbox.clone(population[idx]))
return offspring
def initialize_stats_dict(individual):
'''
Initializes the stats dict for individual
The statistics initialized are:
'generation': generation in which the individual was evaluated. Initialized as: 0
'mutation_count': number of mutation operations applied to the individual and its predecessor cumulatively. Initialized as: 0
'crossover_count': number of crossover operations applied to the individual and its predecessor cumulatively. Initialized as: 0
'predecessor': string representation of the individual. Initialized as: ('ROOT',)
Parameters
----------
individual: deap individual
Returns
-------
object
'''
individual.statistics['generation'] = 0
individual.statistics['mutation_count'] = 0
individual.statistics['crossover_count'] = 0
individual.statistics['predecessor'] = 'ROOT',
def eaMuPlusLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen, pbar,
stats=None, halloffame=None, verbose=0, per_generation_function=None):
"""This is the :math:`(\mu + \lambda)` evolutionary algorithm.
:param population: A list of individuals.
:param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution
operators.
:param mu: The number of individuals to select for the next generation.
:param lambda\_: The number of children to produce at each generation.
:param cxpb: The probability that an offspring is produced by crossover.
:param mutpb: The probability that an offspring is produced by mutation.
:param ngen: The number of generation.
:param pbar: processing bar
: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.
:param per_generation_function: if supplied, call this function before each generation
used by tpot to save best pipeline before each new generation
:returns: The final population
:returns: A class:`~deap.tools.Logbook` with the statistics of the
evolution.
The algorithm takes in a population and evolves it in place using the
:func:`varOr` function. It returns the optimized population and a
:class:`~deap.tools.Logbook` with the statistics of the evolution. The
logbook will contain the generation number, the number of evalutions for
each generation and the statistics if a :class:`~deap.tools.Statistics` is
given as argument. The *cxpb* and *mutpb* arguments are passed to the
:func:`varOr` function. The pseudocode goes as follow ::
evaluate(population)
for g in range(ngen):
offspring = varOr(population, toolbox, lambda_, cxpb, mutpb)
evaluate(offspring)
population = select(population + offspring, mu)
First, the individuals having an invalid fitness are evaluated. Second,
the evolutionary loop begins by producing *lambda_* offspring from the
population, the offspring are generated by the :func:`varOr` function. The
offspring are then evaluated and the next generation population is
selected from both the offspring **and** the population. Finally, when
*ngen* generations are done, the algorithm returns a tuple with the final
population and a :class:`~deap.tools.Logbook` of the evolution.
This function expects :meth:`toolbox.mate`, :meth:`toolbox.mutate`,
:meth:`toolbox.select` and :meth:`toolbox.evaluate` aliases to be
registered in the toolbox. This algorithm uses the :func:`varOr`
variation.
"""
logbook = tools.Logbook()
logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])
# Initialize statistics dict for the individuals in the population, to keep track of mutation/crossover operations and predecessor relations
for ind in population:
initialize_stats_dict(ind)
population = toolbox.evaluate(population)
record = stats.compile(population) if stats is not None else {}
logbook.record(gen=0, nevals=len(population), **record)
# Begin the generational process
for gen in range(1, ngen + 1):
# after each population save a periodic pipeline
if per_generation_function is not None:
per_generation_function(gen)
# Vary the population
offspring = varOr(population, toolbox, lambda_, cxpb, mutpb)
# Update generation statistic for all individuals which have invalid 'generation' stats
# This hold for individuals that have been altered in the varOr function
for ind in population:
if ind.statistics['generation'] == 'INVALID':
ind.statistics['generation'] = gen
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
offspring = toolbox.evaluate(offspring)
# Select the next generation population
population[:] = toolbox.select(population + offspring, mu)
# pbar process
if not pbar.disable:
# Print only the best individual fitness
if verbose == 2:
high_score = max([halloffame.keys[x].wvalues[1] for x in range(len(halloffame.keys))])
pbar.write('Generation {0} - Current best internal CV score: {1}'.format(gen, high_score))
# Print the entire Pareto front
elif verbose == 3:
pbar.write('Generation {} - Current Pareto front scores:'.format(gen))
for pipeline, pipeline_scores in zip(halloffame.items, reversed(halloffame.keys)):
pbar.write('{}\t{}\t{}'.format(
int(pipeline_scores.wvalues[0]),
pipeline_scores.wvalues[1],
pipeline
)
)
pbar.write('')
# after each population save a periodic pipeline
if per_generation_function is not None:
per_generation_function(gen)
# Update the statistics with the new population
record = stats.compile(population) if stats is not None else {}
logbook.record(gen=gen, nevals=len(invalid_ind), **record)
return population, logbook
def cxOnePoint(ind1, ind2):
"""Randomly select in each individual and exchange each subtree with the
point as root between each individual.
:param ind1: First tree participating in the crossover.
:param ind2: Second tree participating in the crossover.
:returns: A tuple of two trees.
"""
# List all available primitive types in each individual
types1 = defaultdict(list)
types2 = defaultdict(list)
for idx, node in enumerate(ind1[1:], 1):
types1[node.ret].append(idx)
common_types = []
for idx, node in enumerate(ind2[1:], 1):
if node.ret in types1 and node.ret not in types2:
common_types.append(node.ret)
types2[node.ret].append(idx)
if len(common_types) > 0:
type_ = np.random.choice(common_types)
index1 = np.random.choice(types1[type_])
index2 = np.random.choice(types2[type_])
slice1 = ind1.searchSubtree(index1)
slice2 = ind2.searchSubtree(index2)
ind1[slice1], ind2[slice2] = ind2[slice2], ind1[slice1]
return ind1, ind2
# point mutation function
def mutNodeReplacement(individual, pset):
"""Replaces a randomly chosen primitive from *individual* by a randomly
chosen primitive no matter if it has the same number of arguments from the :attr:`pset`
attribute of the individual.
Parameters
----------
individual: DEAP individual
A list of pipeline operators and model parameters that can be
compiled by DEAP into a callable function
Returns
-------
individual: DEAP individual
Returns the individual with one of point mutation applied to it
"""
index = np.random.randint(0, len(individual))
node = individual[index]
slice_ = individual.searchSubtree(index)
if node.arity == 0: # Terminal
term = np.random.choice(pset.terminals[node.ret])
if isclass(term):
term = term()
individual[index] = term
else: # Primitive
# find next primitive if any
rindex = None
if index + 1 < len(individual):
for i, tmpnode in enumerate(individual[index + 1:], index + 1):
if isinstance(tmpnode, gp.Primitive) and tmpnode.ret in tmpnode.args:
rindex = i
break
# pset.primitives[node.ret] can get a list of the type of node
# for example: if op.root is True then the node.ret is Output_DF object
# based on the function _setup_pset. Then primitives is the list of classifor or regressor
primitives = pset.primitives[node.ret]
if len(primitives) != 0:
new_node = np.random.choice(primitives)
new_subtree = [None] * len(new_node.args)
if rindex:
rnode = individual[rindex]
rslice = individual.searchSubtree(rindex)
# find position for passing return values to next operator
position = np.random.choice([i for i, a in enumerate(new_node.args) if a == rnode.ret])
else:
position = None
for i, arg_type in enumerate(new_node.args):
if i != position:
term = np.random.choice(pset.terminals[arg_type])
if isclass(term):
term = term()
new_subtree[i] = term
# paste the subtree to new node
if rindex:
new_subtree[position:position + 1] = individual[rslice]
# combine with primitives
new_subtree.insert(0, new_node)
individual[slice_] = new_subtree
return individual,
@threading_timeoutable(default="Timeout")
def _wrapped_cross_val_score(sklearn_pipeline, features, target,
cv, scoring_function, sample_weight=None,
groups=None, use_dask=False):
"""Fit estimator and compute scores for a given dataset split.
Parameters
----------
sklearn_pipeline : pipeline object implementing 'fit'
The object to use to fit the data.
features : array-like of shape at least 2D
The data to fit.
target : array-like, optional, default: None
The target variable to try to predict in the case of
supervised learning.
cv: int or cross-validation generator
If CV is a number, then it is the number of folds to evaluate each
pipeline over in k-fold cross-validation during the TPOT optimization
process. If it is an object then it is an object to be used as a
cross-validation generator.
scoring_function : callable
A scorer callable object / function with signature
``scorer(estimator, X, y)``.
sample_weight : array-like, optional
List of sample weights to balance (or un-balanace) the dataset target as needed
groups: array-like {n_samples, }, optional
Group labels for the samples used while splitting the dataset into train/test set
use_dask : bool, default False
Whether to use dask
"""
sample_weight_dict = set_sample_weight(sklearn_pipeline.steps, sample_weight)
features, target, groups = indexable(features, target, groups)
cv = check_cv(cv, target, classifier=is_classifier(sklearn_pipeline))
cv_iter = list(cv.split(features, target, groups))
scorer = check_scoring(sklearn_pipeline, scoring=scoring_function)
if use_dask:
try:
import dask_ml.model_selection # noqa
import dask # noqa
from dask.delayed import Delayed
except ImportError:
msg = "'use_dask' requires the optional dask and dask-ml depedencies."
raise ImportError(msg)
dsk, keys, n_splits = dask_ml.model_selection._search.build_graph(
estimator=sklearn_pipeline,
cv=cv,
scorer=scorer,
candidate_params=[{}],
X=features,
y=target,
groups=groups,
fit_params=sample_weight_dict,
refit=False,
error_score=float('-inf'),
)
cv_results = Delayed(keys[0], dsk)
scores = [cv_results['split{}_test_score'.format(i)]
for i in range(n_splits)]
CV_score = dask.delayed(np.array)(scores)[:, 0]
return dask.delayed(np.nanmean)(CV_score)
else:
try:
with warnings.catch_warnings():
warnings.simplefilter('ignore')
scores = [_fit_and_score(estimator=clone(sklearn_pipeline),
X=features,
y=target,
scorer=scorer,
train=train,
test=test,
verbose=0,
parameters=None,
fit_params=sample_weight_dict)
for train, test in cv_iter]
CV_score = np.array(scores)[:, 0]
return np.nanmean(CV_score)
except TimeoutException:
return "Timeout"
except Exception as e:
return -float('inf')