如何防止 mutGaussian 将超出范围的值放入 DEAP 中的染色体基因组 Python

How to prevent mutGaussian from putting out of range values into genomes of chromosome in DEAP Python

我用过

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

突变代码。该函数将超出范围的值放入染色体的基因组中。有什么办法可以预防吗?换句话说,有什么方法可以让每个基因组的值都保持在特定的范围内吗?

谢谢

本例取自deap documentation:

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))