4

I'm running a genetic algorithm program and can find the best individual at the end of the run (hof[0]), but i want to know which generation produced it. Is there any attributes of hof[0] that will help print the individual and the generation that created it. I tried looking at the manuals and Google for answers but could not find it anywhere. I also couldn't find a list of the attributes of individuals that I could print. Can someone point to the right link and documentation to that.

Thanks

2 Answers2

1

This deap post suggest tracking the logbook, or explicitly adding the generation to the individual along with fitness: https://groups.google.com/g/deap-users/c/r7fZbMwHg3I/m/BAzHh2ogBAAJ

For the latter: If you are working with the algo locally(recommended if working beyond a tutorial as something always comes up like adding plotting or this very questions) then you can modify the fitness update line to resemble:

fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
    ind.fitness.values = fit
    ind.generation = gen  # now we can: print(hof[0].gen)

if halloffame is not None:
    halloffame.update(population)
DMTishler
  • 501
  • 1
  • 6
  • 12
0

There is no built in way to do this (yet/to the best of my knowledge), and implementing this so would probably be quite a large task. The simplest of which (simplest in thought, not in implementation) would be to change the individual to be a tuple, where tup[0] is the individual and tup[1] is the generation it was produced in, or something similar.

If you're looking for a hacky way, you could maybe try writing the children of each generation to a text file and cross-checking your final solution with the text file; but other than that I'm not sure.

You could always try posting on their Google Group, though it can take a couple of days for a reply.

Good luck!

JakeCowton
  • 1,374
  • 5
  • 15
  • 35