Deleting individuals that violate a constraint

683 views
Skip to first unread message

Christopher Ryan

unread,
Feb 25, 2016, 12:27:45 PM2/25/16
to deap-users
Hello,

I'm new to DEAP and Python but have some experience coding genetic algorithms. I'm having a small problem which I hope someone can give me advice on: I need to make sure all individuals meet a certain criteria, this is calculated from the variables in the individual, so the variable bounds in the individual, and the bounded crossover, mutation operators do not help (i think?). I do not want to apply a penalty function if the individuals are out of the bounds, just delete them, and the decorator example in the documentation only makes it clear how to change the individual rather than delete it. What I'd like to do is to delete any individuals that violate this constraint, and keep producing new individuals until the population is filled with individuals that do not violate my constraint. Is this currently possible using DEAP? 

Any help would be appreciated. Regards, Chris

François-Michel De Rainville

unread,
Feb 25, 2016, 12:33:24 PM2/25/16
to deap-users
Yes, but you have to implement your own algorithm that adds a filter after the mutation/crossover step. Just create a function that returns if the individual is valid or not and apply it inside the standard filter function.

pop = filter(valid, pop)

Best 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.

Christopher Ryan

unread,
Mar 2, 2016, 10:00:30 AM3/2/16
to deap-users
Thanks for the quick reply. I've managed to implement exactly what you said and it works, however the population size changes on each generation. What I did was use the code below to filter generation zero, then used the same code in the generational loop, after the crossover and mutation, just replacing 'pop' with 'offspring'. Popsize is the population size parameter in toolbox.population. Do you have any ideas why the population size is changing and how to fix this?

Regards,

Chris

pop = list(filter(valid,pop))

while len(pop)<originalPopsize:
    popsize = originalPopsize-len(pop)
    newpop = toolbox.population()
    [pop.append(item) for item in newpop]
    pop = list(filter(valid,pop))

François-Michel De Rainville

unread,
Mar 2, 2016, 11:12:02 AM3/2/16
to deap-users
You are probably generating invalid individuals with toolbox.population.

Could you just use the selection to keep your population size constant?

orig_pop_size = len(pop)
pop = list(filter(valid,pop))
pop = toolbox.select(pop, orig_pop_size)

The cons of this method is that you might end up with no valid individual after filtering and you might lose diversity very fast. I don't know what is your algorithm but you could also try to use a (mu , lambda) algorithm, with variable lambda. The algorithm would generate new individuals until all slots are filled.

Regards,
François-Michel


Christopher Ryan

unread,
Mar 2, 2016, 12:09:49 PM3/2/16
to deap-users
Thanks for getting back to me, the issue was that I needed to register the population constructor in the toolbox again with the updated population size on each loop. Thanks for your help and the final code snippet is posted below for reference.

Regards,

Chris 

    offspring = list(filter(valid,offspring))
    
    while len(offspring)<originalPopsize:
        popsize = originalPopsize-len(offspring)
        toolbox.register("population", tools.initRepeat, list, toolbox.individual, n=popsize)
        newpop = toolbox.population()
        [offspring.append(item) for item in newpop]
        offspring = list(filter(valid,offspring))
Reply all
Reply to author
Forward
0 new messages