itertool和多处理,如何在Parallel中生成所有可能的组合 [英] itertool and multiprocessing, How can I generate all possible combinations in Parallel
本文介绍了itertool和多处理,如何在Parallel中生成所有可能的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码生成所有可能的组合,这些组合产生给定的总和(n).但是,此代码对于大数(n)会花费很长时间.有没有办法使我的代码在多个处理器之间并行化?
I have the following code which generates all possible combination that produces a given sum (n). This code, however, takes very long for large numbers (n). Is there a way I can parallelize my code across multiple processors?
from itertools import combinations_with_replacement
def all_combination(numbers, n):
result = [seq for i in range(n, 0, -1) for seq in combinations_with_replacement(numbers,i) if sum(seq) == n]
return result
numbers = [1, 2, 3, 4, 5, 6]
n=700
print len(all_combination(numbers,n))
推荐答案
from itertools import product
import math
import multiprocessing
def parallel_combination(i, limit):
numbers = [1, 2, 3, 4, 5, 6]
result=0
for seq in combinations_with_replacement(numbers, i):
if sum(seq) == limit:
result+=1
return result
def chunks(min_value, max_value):
for i in range(max_value, min_value, -1):
yield i
if __name__ == "__main__":
max_value=610
limit=610
min_value=int(math.floor(float(limit/6)))
pool = multiprocessing.Pool()
n_processesor=32
chunk_size=int((math.ceil(float((max_value-min_value))/n_processesor)))
processes = pool.map(func=parallel_combination, limit, iterable=chunks(min_value,max_value), chunksize=chunk_size)
final_result=0
for process in processes:
if process:
final_result+=process
print final_result
这篇关于itertool和多处理,如何在Parallel中生成所有可能的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文