Skip to content

Instantly share code, notes, and snippets.

@fmder
Last active August 29, 2015 13:57
Show Gist options
  • Save fmder/9767038 to your computer and use it in GitHub Desktop.
Save fmder/9767038 to your computer and use it in GitHub Desktop.
Generate update function with individual logging
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