1

I have used

toolbox.register("mutate", tools.mutGaussian, mu=1, sigma=1, indpb=10)

code for mutation. The function puts values out of the range into the chromosome's genomes. Is there any way to prevent it? In other words, is there any way to keep the value of each genome in its specific range?

Thanks

4

1 回答 1

1

此示例取自deap 文档

def checkBounds(min, max):
    def decorator(func):
        def wrapper(*args, **kargs):
            offspring = func(*args, **kargs)
            for child in offspring:
                for i in xrange(len(child)):
                    if child[i] > max:
                        child[i] = max
                    elif child[i] < min:
                        child[i] = min
            return offspring
        return wrapper
    return decorator

toolbox.register("mate", tools.cxBlend, alpha=0.2)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=2)

toolbox.decorate("mate", checkBounds(MIN, MAX))
toolbox.decorate("mutate", checkBounds(MIN, MAX))
于 2017-03-19T19:20:31.110 回答