make evaluate function which generation it is in

128 views
Skip to first unread message

Pierre-Yves Mathieu

unread,
May 7, 2014, 3:11:49 PM5/7/14
to deap-...@googlegroups.com
Hi,

I have been playing with the canned eaSimple algorithm.

My goal is to tell the evaluate function the generation number, to fine tune some parameter depending on the generation number.

here is my take at it, first I set:
toolbox.register("evaluate", evalSymbReg, gen=1)

and change evalSymbReg ( don't mind the incorrect name, it is not a symbolic regression, just started with copy and paste of the example provided in the doc)

def evalSymbReg(individual, gen):



then

def eaSimple(population, toolbox, cxpb, mutpb, ngen, stats=None, halloffame=None, verbose=__debug__):

    logbook = tools.Logbook()
    logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in population if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind, 0)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    if halloffame is not None:
        halloffame.update(population)

    record = stats.compile(population) if stats else {}
    logbook.record(gen=0, nevals=len(invalid_ind), **record)
    if verbose:
        print logbook.stream

    # Begin the generational process
    for gen in range(1, ngen+1):
        # Select the next generation individuals
        offspring = toolbox.select(population, len(population))
        
        # Vary the pool of individuals
        offspring = varAnd(offspring, toolbox, cxpb, mutpb)
        
        # Evaluate the individuals with an invalid fitness
        invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
        fitnesses = toolbox.map(toolbox.evaluate, invalid_ind, gen)
        for ind, fit in zip(invalid_ind, fitnesses):
            ind.fitness.values = fit
...

then I get this error:

    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind, 0)
TypeError: argument 3 to map() must support iteration

so I change gen (and 0) to

[gen]*len(invalid_ind)

then I get:
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind, 0)
TypeError: argument 3 to map() must support iteration

What would be the best course of action to pass the gen number to the evaluate function?

thank you very much


François-Michel De Rainville

unread,
May 8, 2014, 9:06:00 AM5/8/14
to deap-...@googlegroups.com
You shouldn't register a gen argument in your toolbox. The registered arguments are those that won't change during the evolution. You can still provide your gen argument latter in the process even if it is not registered. Think of the register operation as to create alias for function and freeze some arguments.

The toolbox essentially wraps the functools.partial functionality. You can play with that if you are not sure of what argument goes where.

Regards,
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.

François-Michel De Rainville

unread,
May 8, 2014, 9:13:24 AM5/8/14
to deap-...@googlegroups.com
Based on your error, I would check if you haven't made the change to lists everywhere. Make sure you use [0]*len(invalid) and [gen]*len(invalid) and remove the gen=1 in the registration.
Reply all
Reply to author
Forward
0 new messages