Unable to speed up Python DEAP with Multiprocessing

别等时光非礼了梦想. 提交于 2020-06-16 16:59:11

问题


I am using the below sample code for OneMax problem (maximizing the number of ones of a bitstring) using DEAP package and multiprocessing.

I am unable to speed up the process using multiprocessing. I want to use this for a more complex problem before finding out what is the issue here.

Thank you.

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")

回答1:


If you are running your code inside an interactive interpreter in windows, try running it in cmd. It worked in my case. Using multiprocessing in DEAP for genetic programming



来源:https://stackoverflow.com/questions/61902235/unable-to-speed-up-python-deap-with-multiprocessing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!