Last active
August 29, 2015 13:57
-
-
Save fmder/9767038 to your computer and use it in GitHub Desktop.
Generate update function with individual logging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from operator import attrgetter | |
logbook = tools.Logbook() | |
logbook.header = ['gen', 'nevals'] + (stats.fields if stats else []) | |
for gen in xrange(ngen): | |
# Generate a new population | |
population = toolbox.generate() | |
# Evaluate the individuals | |
fitnesses = toolbox.map(toolbox.evaluate, population) | |
for ind, fit in zip(population, fitnesses): | |
ind.fitness.values = fit | |
if halloffame is not None: | |
halloffame.update(population) | |
# Update the strategy with the evaluated individuals | |
toolbox.update(population) | |
# Clone to ensure it won't be modified by any side effect | |
best_ind = toolbox.clone(max(individuals, key=attrgetter("fitness"))) | |
record = stats.compile(population) if stats is not None else {} | |
logbook.record(gen=gen, nevals=len(population), best=best_ind, **record) | |
if verbose: | |
print logbook.stream | |
return population, logbook |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment