无法通过多处理加快Python DEAP [英] Unable to speed up Python DEAP with Multiprocessing

查看:126
本文介绍了无法通过多处理加快Python DEAP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的示例代码来解决使用DEAP包和多处理的OneMax问题(最大限度地增加一个位串的数目).

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.

谢谢.

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

推荐答案

如果要在Windows的交互式解释器中运行代码,请尝试在cmd中运行它.就我而言,它奏效了. 在DEAP中使用多重处理进行基因编程

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

这篇关于无法通过多处理加快Python DEAP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆