Deap python 包:创建具有不同范围和整数和浮点数混合的个体

azu*_*ric 5 python deap

我正在尝试使用 DEAP 来最大化函数。

我了解如何使用基本示例进行操作:

toolbox.register("attr_bool", random.randint, 0, 1)
toolbox.register("individual", tools.initRepeat, creator.Individual, 
    toolbox.attr_bool, 100)
Run Code Online (Sandbox Code Playgroud)

它创建了 100 个随机值或 0 或 1。然后你继续创建一个种群并变异......

例如,当您有两个参数时,如何构建总体:

parameter 1 integer with range [0,1] 
parameter 2 float with range [0,2]
Run Code Online (Sandbox Code Playgroud)

然后创建一个结合两个随机采样参数的个体?或对于具有任意步长值的参数 2 样本,例如 0.25。

Sae*_*ide 4

您可以简单地执行以下操作来创建多种类型的染色体:

toolbox.register("attr_int", random.randint, 0, 1)
toolbox.register("attr_flt", random.uniform, 0, 2)
toolbox.register("individual", tools.initCycle, creator.Individual,
             (toolbox.attr_int, toolbox.attr_flt),
             n=1)
Run Code Online (Sandbox Code Playgroud)

然后创建一个大小为 100 的总体:

toolbox.register("population", tools.initRepeat, list, toolbox.individual)
population = toolbox.population(n=100)
Run Code Online (Sandbox Code Playgroud)