Skip to content

Commit d23d330

Browse files
algorithms: accept optional logbook instance
1 parent 1509d0b commit d23d330

1 file changed

Lines changed: 26 additions & 8 deletions

File tree

deap/algorithms.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def varAnd(population, toolbox, cxpb, mutpb):
8383

8484

8585
def eaSimple(population, toolbox, cxpb, mutpb, ngen, stats=None,
86-
halloffame=None, verbose=__debug__):
86+
halloffame=None, logbook=None, verbose=__debug__):
8787
"""This algorithm reproduce the simplest evolutionary algorithm as
8888
presented in chapter 7 of [Back2000]_.
8989
@@ -97,6 +97,8 @@ def eaSimple(population, toolbox, cxpb, mutpb, ngen, stats=None,
9797
inplace, optional.
9898
:param halloffame: A :class:`~deap.tools.HallOfFame` object that will
9999
contain the best individuals, optional.
100+
:param logbook: A :class:`~deap.tools.Logbook` to use, optional.
101+
If None is given, a new logbook is created.
100102
:param verbose: Whether or not to log the statistics.
101103
:returns: The final population
102104
:returns: A class:`~deap.tools.Logbook` with the statistics of the
@@ -142,7 +144,9 @@ def eaSimple(population, toolbox, cxpb, mutpb, ngen, stats=None,
142144
.. [Back2000] Back, Fogel and Michalewicz, "Evolutionary Computation 1 :
143145
Basic Algorithms and Operators", 2000.
144146
"""
145-
logbook = tools.Logbook()
147+
if logbook is None:
148+
logbook = tools.Logbook()
149+
146150
logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])
147151

148152
# Evaluate the individuals with an invalid fitness
@@ -246,7 +250,8 @@ def varOr(population, toolbox, lambda_, cxpb, mutpb):
246250

247251

248252
def eaMuPlusLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
249-
stats=None, halloffame=None, verbose=__debug__):
253+
stats=None, halloffame=None, logbook=None,
254+
verbose=__debug__):
250255
r"""This is the :math:`(\mu + \lambda)` evolutionary algorithm.
251256
252257
:param population: A list of individuals.
@@ -261,6 +266,8 @@ def eaMuPlusLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
261266
inplace, optional.
262267
:param halloffame: A :class:`~deap.tools.HallOfFame` object that will
263268
contain the best individuals, optional.
269+
:param logbook: A :class:`~deap.tools.Logbook` to use, optional.
270+
If None is given, a new logbook is created.
264271
:param verbose: Whether or not to log the statistics.
265272
:returns: The final population
266273
:returns: A class:`~deap.tools.Logbook` with the statistics of the
@@ -293,7 +300,9 @@ def eaMuPlusLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
293300
registered in the toolbox. This algorithm uses the :func:`varOr`
294301
variation.
295302
"""
296-
logbook = tools.Logbook()
303+
if logbook is None:
304+
logbook = tools.Logbook()
305+
297306
logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])
298307

299308
# Evaluate the individuals with an invalid fitness
@@ -338,7 +347,8 @@ def eaMuPlusLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
338347

339348

340349
def eaMuCommaLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
341-
stats=None, halloffame=None, verbose=__debug__):
350+
stats=None, halloffame=None, logbook=None,
351+
verbose=__debug__):
342352
r"""This is the :math:`(\mu~,~\lambda)` evolutionary algorithm.
343353
344354
:param population: A list of individuals.
@@ -353,6 +363,8 @@ def eaMuCommaLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
353363
inplace, optional.
354364
:param halloffame: A :class:`~deap.tools.HallOfFame` object that will
355365
contain the best individuals, optional.
366+
:param logbook: A :class:`~deap.tools.Logbook` to use, optional.
367+
If None is given, a new logbook is created.
356368
:param verbose: Whether or not to log the statistics.
357369
:returns: The final population
358370
:returns: A class:`~deap.tools.Logbook` with the statistics of the
@@ -403,7 +415,9 @@ def eaMuCommaLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
403415
if halloffame is not None:
404416
halloffame.update(population)
405417

406-
logbook = tools.Logbook()
418+
if logbook is None:
419+
logbook = tools.Logbook()
420+
407421
logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])
408422

409423
record = stats.compile(population) if stats is not None else {}
@@ -437,7 +451,7 @@ def eaMuCommaLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen,
437451
return population, logbook
438452

439453

440-
def eaGenerateUpdate(toolbox, ngen, halloffame=None, stats=None,
454+
def eaGenerateUpdate(toolbox, ngen, halloffame=None, stats=None, logbook=None,
441455
verbose=__debug__):
442456
"""This is algorithm implements the ask-tell model proposed in
443457
[Colette2010]_, where ask is called `generate` and tell is called `update`.
@@ -449,6 +463,8 @@ def eaGenerateUpdate(toolbox, ngen, halloffame=None, stats=None,
449463
inplace, optional.
450464
:param halloffame: A :class:`~deap.tools.HallOfFame` object that will
451465
contain the best individuals, optional.
466+
:param logbook: A :class:`~deap.tools.Logbook` to use, optional.
467+
If None is given, a new logbook is created.
452468
:param verbose: Whether or not to log the statistics.
453469
:returns: The final population
454470
:returns: A class:`~deap.tools.Logbook` with the statistics of the
@@ -478,7 +494,9 @@ def eaGenerateUpdate(toolbox, ngen, halloffame=None, stats=None,
478494
Wiley, pp. 527-565;
479495
480496
"""
481-
logbook = tools.Logbook()
497+
if logbook is None:
498+
logbook = tools.Logbook()
499+
482500
logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])
483501

484502
for gen in range(ngen):

0 commit comments

Comments
 (0)