Elitism in GP

828 views
Skip to first unread message

Jeannie Fitzgerald

unread,
Sep 6, 2014, 9:47:42 AM9/6/14
to deap-...@googlegroups.com
Hello,

I have looked at other posts on the topic of elitism - but I am still not clear on how to implement it with GP in DEAP. How do I specify that I would like to keep say the best 10% of the population for the next generation, and then to populate the rest of the new population with offspring of individuals selected using tournament selection. Previously in open beagle this could be done with parameter ec.elite.keepsize.

thank you,

Jeannie Fitzgerald

Marc-André Gardner

unread,
Sep 6, 2014, 10:50:30 AM9/6/14
to deap-...@googlegroups.com
Hi,

There is no such thing as an explicit elitism operator in DEAP. However, this is intended, since it can be very easily achieved because of the use of the Python language (unlike OpenBeagle). Say your population is in 'pop' :

from deap import tools
pop = tools.selBest(pop, int(0.1*len(pop))) + tools.selTournament(pop, len(pop)-int(0.1*len(pop)), tournsize=3)

will do the job. Note that this method keep the best individual for the tournament selection.

If you want to simplify your code, you can create an operator which will do just that :
def selElitistAndTournament(individuals, k_elitist, k_tournament, tournsize):
return tools.selBest(individuals, k_elitist) + tools.selTournament(individuals, k_tournament, tournsize=3)

and then register it as your selection operator :
toolbox.register("select", selElitistAndTournament, k_elitist=int(0.1*POP_SIZE), k_tournament=POP_SIZE - int(0.1*POP_SIZE), tournsize=3)

And then it will work with any boxed algorithm.

Have fun with DEAP,

Marc-André

Jeannie Fitzgerald

unread,
Sep 6, 2014, 11:38:04 AM9/6/14
to deap-...@googlegroups.com
Hi Marc-André,

That is very nice indeed!

thank you,

Jeannie

Jeannie Fitzgerald

unread,
Sep 6, 2014, 2:06:30 PM9/6/14
to deap-...@googlegroups.com
Hi again Marc-André,

I have tried the first approach and have received the following error:

Toolbox object has no attribute 'select'

Perhaps I have missed some aspect of the solution.

thanks,

j

On Saturday, September 6, 2014 2:47:42 PM UTC+1, Jeannie Fitzgerald wrote:

Marc-André Gardner

unread,
Sep 6, 2014, 2:13:15 PM9/6/14
to deap-...@googlegroups.com
Hi,

Hum I'm not sure to understand exactly what you did : did you use a line like "pop = tools.selBest(pop, int(0.1*len(pop))) + tools.selTournament(pop, len(pop)-int(0.1*len(pop)), tournsize=3)" in your code?

The canned algorithms (like eaSimple, eaMuPlusLambda, etc.) assume that you have registered some objects in the toolbox. For instance, eaSimple expects toolbox.mate(), toolbox.mutate(), toolbox.select() and toolbox.evaluate(). If you did not define a "select" attribute, it will produce the error you see.

If you indeed defined a "select" attribute, I do not see from where the error may come from. Could you post a complete traceback (and maybe some code snippets)?

Have a good day,

Marc-André

Jeannie Fitzgerald

unread,
Sep 6, 2014, 2:47:14 PM9/6/14
to deap-...@googlegroups.com
Marc-André,

As I suspected! - I had missed a point. Mistakenly, I thought that as the new code contained selBest and selTournament that i did not need to configure the select.
So, just to be clear: I can configure that by setting the selection algorithm to either method but then it gets overridden by the pop=....selBest(...)....+ selTournament(... ?

thanks again,

j

On Saturday, September 6, 2014 2:47:42 PM UTC+1, Jeannie Fitzgerald wrote:

François-Michel De Rainville

unread,
Sep 7, 2014, 7:23:10 AM9/7/14
to deap-...@googlegroups.com
I think MA was first showing an example outside of an algorithm and then showed how to use it within the toolbox.

The only code you have to write is :

def selElitistAndTournament(individuals, k_elitist, k_tournament, tournsize):
    return tools.selBest(individuals, k_elitist) + tools.selTournament(individuals, k_tournament, tournsize=3)

    toolbox.register("select", selElitistAndTournament, k_elitist=int(0.1*POP_SIZE), k_tournament=POP_SIZE - int(0.1*POP_SIZE), tournsize=3)


which defines the new selection method and register iy as "select" in the toolbox

Cheers,
François-Michel

--
You received this message because you are subscribed to the Google Groups "deap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to deap-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jeannie Fitzgerald

unread,
Sep 9, 2014, 4:18:06 PM9/9/14
to deap-...@googlegroups.com
Hi again,

The only code you have to write is :

def selElitistAndTournament(individuals, k_elitist, k_tournament, tournsize):
    return tools.selBest(individuals, k_elitist) + tools.selTournament(individuals, k_tournament, tournsize=3)

    toolbox.register("select", selElitistAndTournament, k_elitist=int(0.1*POP_SIZE), k_tournament=POP_SIZE - int(0.1*POP_SIZE), tournsize=3)

I used the code as described above but I have received the following error:

 "offspring = toolbox.select(population, len(population))
TypeError: selElitistAndTournament() got multiple values for keyword argument 'k_elitist"

I am new to both Deap and Python - and am not totally familiar with the scoping rules etc - so forgive me if I am making newbie mistakes.

Many thanks for your help.

Jeannie

On Saturday, September 6, 2014 2:47:42 PM UTC+1, Jeannie Fitzgerald wrote:

Marc-André Gardner

unread,
Sep 9, 2014, 4:20:15 PM9/9/14
to deap-...@googlegroups.com
Hi,

You already register the value for the k_elitist parameter, so when you call it from the toolbox, you must only provide one argument (individuals). So your call should read :

offspring = toolbox.select(population)


Have a good day,

Marc-André

Marc-André Gardner

unread,
Sep 9, 2014, 4:23:40 PM9/9/14
to deap-...@googlegroups.com
Oh, and I think about it, maybe you want to let the call as it is in the algorithms. In this case, you just have to change your function signature :

def selElitistAndTournament(individuals, k, frac_elitist, tournsize):
    return tools.selBest(individuals, int(k*frac_elitist)) + tools.selTournament(individuals, int(k*(1-frac_elitist)), tournsize=tournsize)

Then you just provide frac_elitist in your register operation (since frac_tournament is automatically 1-frac_elitist) :
toolbox.register("select", selElitistAndTournament, frac_elitist=0.1 , tournsize=3)

And you will be able to call it using "offspring = toolbox.select(population, len(population))"

Hope it helps,

Marc-André

Jeannie Fitzgerald

unread,
Sep 9, 2014, 4:41:45 PM9/9/14
to deap-...@googlegroups.com
Thank you Marc-André that seems to do the trick nicely.


On Saturday, September 6, 2014 2:47:42 PM UTC+1, Jeannie Fitzgerald wrote:
Reply all
Reply to author
Forward
0 new messages