我似乎不知道如何在DEAP中拥有可变长度的基因组。
我浏览了DEAP文档,没有发现与可变长度基因组有关的任何信息。
答案 0 :(得分:0)
如果在不指定n
参数的情况下定义个人,则可以创建许多具有不同长度的个人。例如
from deap import base, tools, creator
import random
toolbox = base.Toolbox()
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)
toolbox.register("individual", tools.initRepeat, creator.Individual, random.random)
print(toolbox.individual(n=2))
print(toolbox.individual(n=5))
如果要创建每个个体的长度都不同的种群,可以查看Knapsack problem from the DEAP documentation。主要思想归结为以这种方式定义人口:
from deap import creator, base, tools
import random
creator.create("Fitness", base.Fitness, weights=(-1.0, 1.0))
creator.create("Individual", set, fitness=creator.Fitness)
toolbox = base.Toolbox()
toolbox.register("attr_item", random.randrange, 5)
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.attr_item, 3)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.population(n=5)