如何在 DEAP 中将参数传递给 "toolbox.population"

How to pass argument to "toolbox.population" in DEAP

我尝试在 DEAP 中创建自定义“个人”。原因是个体是由几个解释变量组成的。每个解释变量都有下限和上限。此外,它可能有步长或精度。

这是我正在处理的代码:

import random
from deap import base, creator, tools
from typing import Optional, List, Any


creator.create("FitnessMin", base.Fitness, weights=(-1.0,))   
creator.create("Individual", list, fitness=creator.FitnessMin)   # individual data-type : List
toolbox = base.Toolbox()


'''
Functions
'''

def random_pick(lower: float, upper: float, step: Optional[int] = None) -> float:
    # Pick random numbers within specified bounds 
    if step is None:
        return random.uniform(lower, upper)
    else:
        candidates_num = round((upper - lower) / step)
        return lower + random.randint(0, candidates_num) * step


def make_individual(explanatories) -> List[Any]:
     individual = []
     for explanatory in explanatories:
         individual.append(
             random_pick(
                 lower=explanatories[explanatory][0],
                 upper=explanatories[explanatory][1],
                 step=explanatories[explanatory][2] if len(explanatories[explanatory]) == 3 else None,
             )
         )
     return creator.Individual(individual)


'''
Main
'''
pop_size = 10

explanatories={
    "a": (30, 100, 1),   # (low, high, step)
    "b": (80, 200, 1),
    "c": (15, 80, 1),
    "d": (1.5, 5, 0.1),
    "e": (15, 200, 1),
    "f": (5, 1699, 1),
    "g": (0.5, 0.5),     # (low, high)
    }


toolbox.register("individual", make_individual)

# Check value of individual
ind = toolbox.individual(explanatories)   # list


# Population (following is the Bag type of population which is common)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

# Check population
pop = toolbox.population(pop_size)   # ERROR  

函数random_pick通过考虑步骤随机选取下限和上限内所有解释的值。而函数make_individual负责制作个体。

上面代码的问题就是我尝试做population的地方。当我 运行 以下行时:

pop = toolbox.population(pop_size)    

我收到错误:

TypeError: make_individual() missing 1 required positional argument: 'explanatories'

我的问题是如何在生成人口时将解释作为输入参数传递给“toolbox.population”或“make_individual()”。

感谢您的帮助。

您的问题似乎是由于 tools.initRepeat 只接受三个参数:containerfuncn (docs here)。

因此,它不能将 explanatories 参数“传递”给 func(在本例中为 toolbox.individual)。

您可以通过在注册 toolbox.individual.

时指定 explanatories 作为 make_individual 的参数来解决这个问题

也就是说,这个有效:

toolbox.register(
  "individual", 
  make_individual,
  explanatories=explanatories  # now toolbox.individual uses default explanatories
)
ind = toolbox.individual()  # no longer provide explanatories arg
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
pop = toolbox.population(pop_size)
print(int)
print(pop)

它会打印类似

的内容
<class 'int'>
[[71, 122, 50, 1.9, 62, 679, 0.5], [88, 192, 24, 3.4000000000000004, 40, 1377, 0.5], [78, 182, 59, 4.1, 65, 1297, 0.5], [39, 174, 49, 3.0, 104, 883, 0.5], [89, 153, 36, 5.0, 102, 718, 0.5], [57, 114, 23, 2.7, 54, 1628, 0.5], [78, 146, 32, 2.1, 145, 961, 0.5], [96, 174, 40, 5.0, 19, 974, 0.5], [39, 150, 80, 4.7, 140, 801, 0.5], [31, 150, 37, 2.8, 109, 1230, 0.5]]