我在 python 中使用 DEAP 库来解决多目标优化问题。我想为这个任务使用多个处理器;但是,我遇到了一些麻烦。
为了给出一些上下文,我将 networkx 与 DEAP 结合使用,我还定义了适应度函数、交叉和变异函数(由于某些原因,我不会在这里展示)。
它在这里说我需要做的就是安装 Scoop 并添加行
from scoop import futures
toolbox.register("map", futures.map)
但是我似乎得到了一个错误:
scoop._comm.scoopexceptions.ReferenceBroken: 'module' object has no attribute 'Chromosome'
在进行了一些挖掘之后,我发现我需要将调用移动到主模块中的 creator.create ,如这里指定的那样。
这样做之后,我得到另一个错误:
scoop._comm.scoopexceptions.ReferenceBroken: This element could not be pickled: FutureId(worker='127.0.0.1:49663', rank=1):partial(<Chromosome representation of a solution here>)=None
我对并行计算并不完全熟悉,我不太确定“不能腌制”是什么意思。完整的代码可以在这里看到一些编辑:
def genetic(network, creator, no_sensors, sfpd, lambda1, lambda2, lambda3, k):
locations = network.graph.nodes()
#move creator.create calls to the main module
########################################
creator.create("FitnessMax", base.Fitness, weights=(lambda1, -lambda2, lambda3))
creator.create("Chromosome", list, fitness=creator.FitnessMax)
########################################
toolbox = base.Toolbox()
toolbox.register("attr_item", random.sample, locations, no_sensors)
toolbox.register("chromosome", tools.initRepeat, creator.Chromosome, toolbox.attr_item, n=1)
toolbox.register("population", tools.initRepeat, list, toolbox.chromosome)
toolbox.register("map", futures.map) #######<-- this line ##############
def evaluate(chromosome):
#fitness function defined here
# Crossover
def crossover(chromosome1, chromosome2): # Uniform Crossover
#crossover is defined here
# Mutation
def mutation(chromosome):
#mutation is defined here
toolbox.register("evaluate", evaluate)
toolbox.register("mate", crossover)
toolbox.register("mutate", mutation)
toolbox.register("select", tools.selNSGA2)
random.seed(64)
pop = toolbox.population(n=MU)
hof = tools.ParetoFront()
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", numpy.mean, axis=0)
stats.register("min", numpy.min, axis=0)
stats.register("max", numpy.max, axis=0)
algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats, halloffame=hof)
return list(hof)
谢谢,任何见解都将非常有价值。