Using decorator to fix the bounds of mutGaussian function in a chromossome composed by real and integer numbers

40 views
Skip to first unread message

Rachel

unread,
Dec 13, 2019, 8:13:05 AM12/13/19
to deap-users
Hello all,

I coded a customized chromossome composed by real and integer numbers. Due to that, my mutation function is also customized according to each part of the chromossome. For the real value coded parts, I am using mutGaussian, but some values are going out of the bounds. 
I've seen that to fix it I can use a decorator, such as presented in this example. This example shows a case in which only mutGaussian is used, in the case that we have more than one type of mutation applied, how to fix the bounds of decorator according to each part of chromossome? 

Thanks in advance. 


Derek Tishler

unread,
Dec 13, 2019, 10:05:03 AM12/13/19
to deap-users
Are you using a mutator like this recent questions describes?
Which gives the example:

def my_mutation(ind):
    ind
[:4], = mutGaussian(ind[:4])
    ind
[4:], = mutFlipBit(ind[4:])
   
return ind,



Then to work toward a fix we could try something messy like this perhaps to get it all hooked up:
toolbox.register("mutate", my_mutation, gauss_mu=0, gauss_sigma=3, gauss_indpb=0.3, flipb_indpb=0.05)

And we match the above mutation function's inputs:
def my_mutation(ind, gauss_mu, gauss_sigma, gauss_indpb, flipb_indpb):
    ind
[:4], = mutGaussian(ind[:4], mu=gauss_mu, sigma=gauss_sigma, indpb=gauss_indpb)
    ind[4:], = mutFlipBit(ind[4:], indpb=flipb_indpb)
    return ind,


Ref to mutation source in case you want to drop it in as local funct with overridden values or something also works:
https://github.com/DEAP/deap/blob/master/deap/tools/mutation.py#L17

Derek Tishler

unread,
Dec 13, 2019, 10:13:52 AM12/13/19
to deap-users
You may also be able to register the separate partials themselves to the toolbox for a cleaner fix, I realize, and then call from the toolbox?

Something like:
toolbox.register("mutGaussian", tools.mutGaussian, mu=0, sigma=3, indpb=0.3)
toolbox
.register("mutFlipBit", tools.mutFlipBit, indpb=0.3)

def my_mutation(ind):
    ind
[:4], = toolbox.mutGaussian(ind[:4])
    ind
[4:], = toolbox.mutFlipBit(ind[4:])
   
return ind,

François-Michel De Rainville

unread,
Dec 14, 2019, 6:44:28 AM12/14/19
to deap-...@googlegroups.com
If you want there is also a bounded mutation and crossover. Look for the polynomial mutation and simulated binary crossover.

Le 13 déc. 2019 à 10:13, Derek Tishler <lstr...@gmail.com> a écrit :

You may also be able to register the separate partials themselves to the toolbox for a cleaner fix, I realize, and then call from the toolbox?

Something like:
toolbox.register("mutGaussian", tools.mutGaussian, mu=0, sigma=3,indpb=0.3)
toolbox
.register("mutFlipBit", tools.mutFlipBit, indpb=0.3)

def my_mutation(ind):
    ind
[:4], = toolbox.mutGaussian(ind[:4])
    ind
[4:], = toolbox.mutFlipBit(ind[4:])
    
return ind,



On Friday, December 13, 2019 at 10:05:03 AM UTC-5, Derek Tishler wrote:
Are you using a mutator like this recent questions describes?
Which gives the example:

def my_mutation(ind):
    ind
[:4], = mutGaussian(ind[:4])
    ind
[4:], = mutFlipBit(ind[4:])
    
return ind,



Then to work toward a fix we could try something messy like this perhaps to get it all hooked up:
toolbox.register("mutate", my_mutation, gauss_mu=0, gauss_sigma=3,gauss_indpb=0.3, flipb_indpb=0.05)

And we match the above mutation function's inputs:
def my_mutation(ind, gauss_mu, gauss_sigma, gauss_indpb, flipb_indpb):
    ind
[:4], = mutGaussian(ind[:4], mu=gauss_mu, sigma=gauss_sigma, indpb=gauss_indpb)
    ind[4:], = mutFlipBit(ind[4:], indpb=flipb_indpb)
    return ind,


Ref to mutation source in case you want to drop it in as local funct with overridden values or something also works:
https://github.com/DEAP/deap/blob/master/deap/tools/mutation.py#L17
https://github.com/DEAP/deap/blob/master/deap/tools/mutation.py#L124

On Friday, December 13, 2019 at 8:13:05 AM UTC-5, Rachel wrote:
Hello all,

I coded a customized chromossome composed by real and integer numbers. Due to that, my mutation function is also customized according to each part of the chromossome. For the real value coded parts, I am using mutGaussian, but some values are going out of the bounds. 
I've seen that to fix it I can use a decorator, such as presented in this example. This example shows a case in which only mutGaussian is used, in the case that we have more than one type of mutation applied, how to fix the bounds of decorator according to each part of chromossome? 

Thanks in advance. 



-- 
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/deap-users/98afe82c-b028-41f5-8d44-c1f683f44f1c%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages