使用 Deap 生成种群时使用 np.arange?

Use np.arange when generating a population with Deap?

给定这本词典:

gene_space = [
    {"name": "length",'high':110,'low':10,'step':10},
    {"name": "emaLength",'high':34,'low':1,'step':2},
    {"name": "averageLength",'high':110,'low':10,'step':10},
    {"name": "factor",'high':0.5,'low':0.02,'step':0.02},
    {"name": "criticalValue",'high':60,'low':-50,'step':10},
]

...我怎样才能创建一个从这些范围中随机选择的基因群,以便它们可以与 Deap 一起使用?

您可以在下面看到我的尝试:

# Define a maximizing fitness function
creator.create("FitnessMax", base.Fitness, weights=(1.0,))

# We'll create a list of the current population
creator.create("Individual", list, fitness=creator.FitnessMax)

# Create toolbox instance
toolbox = base.Toolbox()

list_of_attrs = []

# Iterate through the gene space
for i in range(len(gene_space)):
    
    # Register the current gene attribute (by name), which is just a np.arange between low and high, steping each way
    # Attempt 1 - Doesn't work as the "population" later is just the actual ranges, not selections from the range
    # toolbox.register(gene_space[i]["name"], np.arange, gene_space[i]['low'], gene_space[i]['high'], gene_space[i]['step'])
    
    # Attempt 2 - Getting better, however this now only selects 1 random choice for the population, and I want more.
    # Defined n=10 below, but doesn't work?
    # toolbox.register(gene_space[i]["name"], random.choice, np.arange(gene_space[i]['low'], gene_space[i]['high'], gene_space[i]['step']))
    
    # Attempt 3 - Tried to make several random selections which is more in line with what I want, but get the error:
    # TypeError: Population must be a sequence or set.  For dicts, use list(d).
    toolbox.register(gene_space[i]["name"], random.sample, np.arange(gene_space[i]['low'], gene_space[i]['high'], gene_space[i]['step']), 10)

    list_of_attrs.append(gene_space[i]["name"])

# Register the individual (full of the attributes)
toolbox.register("individual", tools.initCycle, creator.Individual, (getattr(toolbox, key) for key in list_of_attrs), n=10) # n=10 here does nothing?

# Now register the population
toolbox.register("population", tools.initRepeat, list, toolbox.individual, n=10)

toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.1)
toolbox.register("select", tools.selTournament, tournsize=3)
toolbox.register("evaluate", evaluate)

我需要人口与 Deap 从他们的示例中使用的 class 类型相同。我已经修改了他们的“主要”算法以生成我需要的人口作为列表列表,但后来它遇到了更多错误,因为每个基因只是一个列表,而不是 deap class 对象。希望在我开始重新编写新的主要方法之前使用上述尝试之一解决此问题。

看起来这样做的方法是这样的(灵感来自):

dataset = []
for _ in range(300):
    current_chromosome = []
    for j in range(len(gene_space)):
        current_chromosome.append(random.choice(np.arange(gene_space[j]['low'], gene_space[j]['high'], gene_space[j]['step'])))
    dataset.append(current_chromosome)

# toolbox.register("random_sampling", random.sample, dataset, 1)
toolbox.register("random_sampling", lambda x,y: random.sample(x,y)[0], dataset, 1)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.random_sampling)