【发布时间】:2016-11-14 03:28:54
【问题描述】:
我目前在 Python 中将 DEAP 用于遗传算法。我想创建长度为no_sensors 的初始个体群体。我的问题是由于random.choice(nodes) 函数,一些节点最终是相同的,并且初始长度最终小于no_sensors。我想知道是否有办法解决这个问题:
creator.create("FitnessMax", base.Fitness, weights=(2.0, -1.0))
creator.create("Individual", set, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("attr_item", random.choice, nodes)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_item, n=no_sensors)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
基本上,我需要列表nodes 中固定长度的唯一项。我正在考虑使用random.sample(nodes, no_sensors),但我似乎无法将其合并到代码中而不会产生错误
您可以查看其他示例 here。
【问题讨论】:
-
no_sensors是提前设置的还是每个人的值可能不同?
标签: python algorithm genetic deap