固定长度整数分区的唯一排列,其中每个元素具有最大值

时间:2016-09-13 09:36:48

标签: python performance numpy permutation integer-partition

这个问题类似于我几个月前的一个问题:Generating a numpy array with all combinations of numbers that sum to less than a given number。 在那个问题中,我希望生成所有总和最多为常数的数字,因为每个元素都有一定的最大值。

这次我想计算总结到那个常数的所有排列。这可以被视为计算整数分区的唯一排列,其中每个元素具有特定最大值。最终结果应该存储在一个numpy数组中。

使用发电机,一个内衬实现了我们想要的目标:

import numpy as np
from itertools import product
K = 3 
maxRange = np.array([1,3,2]) 

states = np.array([i for i in product(*(range(i+1) for i in maxRange)) if sum(i)==K]) 

array([[0, 1, 2],
       [0, 2, 1],
       [0, 3, 0],
       [1, 0, 2],
       [1, 1, 1],
       [1, 2, 0]])

K=20maxRange = [20]*6时,我的表现相当慢。排列数量限制为53130,但已经需要20秒。我的直觉告诉我,这应该花费不到一秒钟。

有没有人有更快的解决方案?我无法修改我之前问题的解决方案以解决这个问题,因为我不知道如何切断不再可能完全加起来K的排列。

我不介意使用来自numba的@jit运算符的解决方案......只要它们比我现在的速度快!

提前致谢。

1 个答案:

答案 0 :(得分:0)

我不得不考虑这个问题,但是我已经成功地将解决方案修改为Generating a numpy array with all combinations of numbers that sum to less than a given number来解决这个问题:

对于分区数量,我们的想法是计算数组feasible_range,该数组指定在某个阶段至少总共需要多少才能达到max_sum。例如,如果我们想要达到总共3和max_range[0] == 1,那么在开始使用最终元素之前我们需要至少有2个。该数组来自累积和:

feasible_range = np.maximum(max_sum - np.append(np.array([0]),np.cumsum(max_range)[:-1]),0)

现在我们可以像以前一样计算分区数,方法是将永远不会导致可行分区的元素设置为0。

def number_of_partitions(max_range, max_sum):
    M = max_sum + 1
    N = len(max_range) 
    arr = np.zeros(shape=(M,N), dtype = int)    
    feasible_range = max_sum - np.append(np.array([0]),np.cumsum(max_range)[:-1])
    feasible_range = np.maximum(feasible_range,0)     

    arr[:,-1] = np.where(np.arange(M) <= min(max_range[-1], max_sum), 1, 0) 
    arr[:feasible_range[-1],-1] = 0
    for i in range(N-2,-1,-1):
        for j in range(max_range[i]+1):
            arr[j:,i] += arr[:M-j,i+1] 
        arr[:feasible_range[i],i]=0  #Set options that will never add up to max_sum at 0.
    return arr.sum(axis = 0),feasible_range

分区功能也有与以前类似的解释。

def partition(max_range, max_sum, out = None, n_part = None,feasible_range=None):
    #Gives all possible partitions of the sets 0,...,max_range[i] that sum up to  max_sum. 
    if out is None:
        max_range = np.asarray(max_range, dtype = int).ravel()
        n_part,feasible_range = number_of_partitions(max_range, max_sum)
        out = np.zeros(shape = (n_part[0], max_range.size), dtype = int)

    if(max_range.size == 1):
        out[:] = np.arange(feasible_range[0],min(max_range[0],max_sum) + 1, dtype = int).reshape(-1,1)
        return out

    #Copy is needed since otherwise we overwrite some values of P.
    P = partition(max_range[1:], max_sum, out=out[:n_part[1],1:], n_part = n_part[1:],feasible_range=feasible_range[1:]).copy()      


    S = max_sum - P.sum(axis = 1) #The remaining space in the partition 
    offset, sz  = 0, 0
    for i in range(max_range[0]+1):
        #select indices for which there is remaining space
        #do this only if adding i brings us within the feasible_range.
        ind, = np.where(np.logical_and(S-i>=0,S-i <= max_sum-feasible_range[0])) 
        offset, sz = offset + sz, ind.size
        out[offset:offset+sz, 0] = i
        out[offset:offset+sz, 1:] = P[ind]
    return out

对于K=20maxRange = [20]*6partition(maxRange,K)需要13毫秒,而首先是18.5秒。

我真的不喜欢我要复制的部分;通过颠倒顺序可以避免这种情况。不过现在的速度已经够好了。

相关问题