【问题标题】:Unable to speed up Python DEAP with Multiprocessing无法通过多处理加速 Python DEAP
【发布时间】:2020-09-06 04:16:46
【问题描述】:

我使用 DEAP 包和多处理将以下示例代码用于 OneMax 问题(最大化位串的个数)。

我无法使用多处理来加快处理速度。在找出问题所在之前,我想将其用于更复杂的问题。

谢谢。

import array
import multiprocessing
from multiprocessing import Pool
import random
import time
import numpy as np

from deap import algorithms
from deap import base
from deap import creator
from deap import tools

creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", array.array, typecode='b', fitness=creator.FitnessMax)

toolbox = base.Toolbox()

toolbox.register("attr_bool", random.randint, 0, 1)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 10000)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)


#OneMax problem is a simple problem consisting in maximizing the number of ones of a bitstring.
def evalOneMax(individual):
    return sum(individual),

toolbox.register("evaluate", evalOneMax)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)

if __name__ == "__main__":

    t1 = time.time()
    CPU_count = multiprocessing.cpu_count()-1

    p = Pool(CPU_count)
    toolbox.register("map", p.map)

    pop = toolbox.population(n=1000)
    hof = tools.HallOfFame(1)
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", np.mean)
    stats.register("std", np.std)
    stats.register("min", np.min)
    stats.register("max", np.max)

    algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=50, 
                        stats=stats, halloffame=hof)

    p.close()
    p.join()
    t2 = time.time()
    print("Multiprocessing with",CPU_count,"core(s) took",round((t2-t1),2),"s")

【问题讨论】:

    标签: python multiprocessing genetic-algorithm deap


    【解决方案1】:

    如果您在 windows 的交互式解释器中运行代码,请尝试在 cmd 中运行它。它在我的情况下有效。 Using multiprocessing in DEAP for genetic programming

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-18
      • 2017-09-17
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      • 2019-12-01
      • 1970-01-01
      相关资源
      最近更新 更多