How to display the best individual of every generation?

2,709 views
Skip to first unread message

kramer65

unread,
May 7, 2012, 2:37:04 PM5/7/12
to deap-...@googlegroups.com
Hello,


I am building an evolution based upon the basic evolution strategies example. What I now need to do is display the best individual for every generation and its corresponding fitness. So I took the example code and copy pasted the function algorithms.eaMuCommaLambda() into it (see here) so that I can find a spot there to display the generations best individual.

From here I am kind of lost though. I can simply print(population), but that doesn't tell me which one is the fittest. I guess I have to do something with the Fitness class (located in base.py) and specifically the method called getValues(). I am not sure what though.

If anyone could help me out here a little bit that would be great.


Kind regards,
Hielke

François-Michel De Rainville

unread,
May 7, 2012, 2:50:18 PM5/7/12
to deap-...@googlegroups.com
The Hall of Fame is a container that keeps a copy (deepcopy) of the best individuals seen so far. Once it is updated, the best individual ever will be at index 1 of the hall of fame. On the next update if a better individual is found it will replace the last best by either pushing the last best individual to second place or removing it from the container. Otherwise, if no better (or equal) individual are in the population than in the hall of fame the hall of fame will remain unchanged. On equality (same fitness exactly), the individual is inserted after (bisect_right) the already present individual.

To print the best individual :

print(hof[0])

wherever you need it.

kramer65

unread,
May 7, 2012, 3:20:40 PM5/7/12
to deap-...@googlegroups.com
Hi François-Michel,

Thank you for your answer.

I was aware of the hall of fame. However, there are two reasons that I can' t use that. Every generation I add new data to the dataset which makes it an evolving landscape. The very best individual from the whole evolution (hof[0]) could have been found many generations before and is therefore not valid anymore. In addition to that, I purposely allow degeneration in my evolution (by using muCommaLambda and not muPlusLambda) because non-robust optima can are more easily "forgotten".

For these reasons I want to get the best individual out of the current population. Would you know of a way to get it?

Kind regards,
Hielke



Op maandag 7 mei 2012 20:50:18 UTC+2 schreef François-Michel De Rainville het volgende:

François-Michel De Rainville

unread,
May 7, 2012, 3:27:40 PM5/7/12
to deap-...@googlegroups.com
You can actually sort your population with sorted and provide the key to the fitness.

sorted_pop = sorted(population, key=lambda ind: ind.fitness, reverse=True)

or using operator.attrgetter (that might be faster)

sorted_pop = sorted(population, key=attrgetter("fitness"), reverse=True)

This applies the key to each element of population (your individuals) and sort according to what is returned by the key. The best individual will be at index 0 of sorted_pop

Marc-André Gardner

unread,
May 7, 2012, 3:30:57 PM5/7/12
to deap-...@googlegroups.com
Just to complete François-Michel answer :

Well, if you do not need to keep track of the best individual of all times (like the hall of fame does), you may just clear() it at each generation...

Another way is to sort the population according to the fitness :
sortedPop = sorted(population, key=lambda ind:ind.fitness)

Yes, it involves a copy, but in general case, the overhead should be too high (compared to the sorting).

If you do not want to sort all the population, which is done in O(n log(n) ), you may also just check any individual, with a standard for loop over the population (there is no automatic mecanism in DEAP to do such a trivial thing), which will then be done in O(n).

François-Michel De Rainville

unread,
May 7, 2012, 3:52:32 PM5/7/12
to deap-...@googlegroups.com
To complement MAG's answer, you can also use max instead of sorted with the same arguments to get the best individual.

best = max(population, key=attrgetter("fitness"))

Cordially,
FM

kramer65

unread,
May 7, 2012, 4:00:41 PM5/7/12
to deap-...@googlegroups.com
Wow! Now that I call a complete set op options. Thanks a lot guys!

Since it is rather late over here (10pm) I am going to incorporate your good advice tomorrow.

Thanks again!



Op maandag 7 mei 2012 21:52:32 UTC+2 schreef François-Michel De Rainville het volgende:
Reply all
Reply to author
Forward
0 new messages