我不知道为什么我的python软件出现异常(我在windows下运行):
File "C:\Users\lumy\PycharmProjects\EternityPuzzle\puzzle.py", line 94, in log_stats
self.record = self.stats.compile(self.population)
File "C:\Python27\lib\site-packages\deap\tools\support.py", line 237, in compile
record[name] = stats.compile(data)
File "C:\Python27\lib\site-packages\deap\tools\support.py", line 204, in compile
values = tuple(self.key(elem) for elem in data)
File "C:\Python27\lib\site-packages\deap\tools\support.py", line 204, in <genexpr>
values = tuple(self.key(elem) for elem in data)
File "C:\Users\lumy\PycharmProjects\EternityPuzzle\puzzle.py", line 73, in <lambda>
stats2 = tools.Statistics(key=lambda ind: ind.fitness_group.values)
File "C:\Python27\lib\site-packages\deap\base.py", line 181, in getValues
return tuple(map(truediv, self.wvalues, self.weights))
TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'
我试图记录两个不同的健身。
所以getValues函数在Fitness对象中:
def getValues(self):
print self.wvalues, self.weights
return tuple(map(truediv, self.wvalues, self.weights))
输出:
() (1.0,)
如果我理解,wvalues应该包含所有旧值,但它第一次似乎是空的。 我的代码示例如下:
from deap import base
from deap import creator
from deap import tools
creator.create("FitnessInd", base.Fitness, weights=(1.0,))
creator.create("FitnessGroup", base.Fitness, weights=(1.0,))
creator.create("Individual", ind.Ind, fitness_ind=creator.FitnessInd, fitness_group=creator.FitnessGroup)
toolbox.register("new_individual", creator.Individual)
toolbox.register("desk", tools.initRepeat, list, toolbox.new_individual)
population = toolbox.desk(n=len(arr))
stats2 = tools.Statistics(key=lambda ind: ind.fitness_group.values)
stats1 = tools.Statistics(key=lambda ind: ind.fitness_ind.values)
stats = tools.MultiStatistics(fitness_ind=stats1, fitness_group=stats2)
stats.register("min", min)
logbook = tools.Logbook()
values = eval_solution(population)
for ind, v in zip(population, values):
ind.fitness_ind.value = v,
ind.fitness_group.value = v,
record = stats.compile(population)
不确定我做错了什么。