生成Deap群体时使用np.arange

  • 本文关键字:np arange Deap 生成 python 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在他们的例子中使用的类类型相同。我修改了他们的";主";算法生成我需要的种群作为列表列表,但后来它遇到了更多错误,因为每个基因只是一个列表,而不是deap类对象。希望在我开始重新编码一个新的主方法之前,使用上面的一个尝试来解决这个问题。

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

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)

最新更新