Subset-AVG-查找与已知有理数匹配的列表的子集

时间:2019-04-25 21:27:49

标签: python python-3.x stack-overflow exponential coding-efficiency

我在数学溢出时问过这个问题,并使用注释来澄清/夸大我的问题。我希望它能达到预期的效果,并且不会因为震动而消失。

我正在尝试找出数字的哪些子集达到已知的平均值。

我有一个已知值,负数和可能的小数的列表。它们看起来像这样{-.32,-。64,-。12,.08,-。54,-。43,...}

在某些情况下大约为50,尽管在其他情况下也会测试此问题。

该集合大多数包含负十进制数字,而在极少数情况下,则具有一些正十进制-永远不会有整数。

我也有一个已知值,我知道它是上述列表中某些子集的平均值。

已知值类似于-.03。

我不确定所使用的分组机制,但是在不进行分组时试图解决此问题似乎会达到堆栈溢出。

我已经尝试了几种解决此问题的方法。 我正在使用Python 3.6,并将numpy导入为np。

我想知道我从“ subset-avg”代码是否已从此处的子集和的另一种解决方案改编而成(当我可以再次找到该问题时,我会给予应有的重视)不是最有效的方法/是否我什至尝试解决这个我从未见过的重大错误。

预先感谢您的任何想法。

def subset_avg(numbers, target, partial=[],depth=1):
    # create AVG function

    # set average of partial
    a = np.mean(partial)

    # check if the partial sum is equals to target


    if a != target:
        print("Currently Testing the Following Subset's " " " + "Average(%s)  =  %s\n\n" % (partial, round(a,2)))
    print(depth)

    if a == target or round(a,2) == target:

            print('\n\n')
            print("************")
            print("************")
            print('\n\n')
            print("Found Subset AVG " + "Average(%s)  =  %s" % (partial, target))
            print('\n\n')
            print("************")
            print("************")
            print('\n\n')
    print(depth)        
    # for each number in range of list
    for i in range(len(numbers)):
        # set n = current iteration in list
        n = numbers[i]
        # remaining values is current iteration + 1 through end of list
        remaining = numbers[i+1:]
        # calculate mean of partial, set partial = partial plus n 
        subset_avg(remaining, target, partial + [n],depth+1)

# Example of use
x = [-.32,-.64,-.12,.08,-.54,-.43]

subset_avg(x,-.03)

1 个答案:

答案 0 :(得分:0)

这是我针对另一个问题(here)发布的subSet sum算法改编而成的解决方案。由于该算法会遍历潜在的解决方案大小,因此很容易对其进行调整以搜索平均值。

iSubSum()函数具有3个参数:目标平均值,值列表和可选的舍入精度参数。它是一个生成器,因此在循环中使用时将产生所有可能的解决方案。您还可以使用next()函数快速获得第一个解决方案。这应该比蛮力方法更快地产生结果,尤其是对于大型列表。

该函数基于子集和算法的修改版本,该算法将解决方案作为索引列表返回。旨在区分具有重复值的组合,这些重复值来自原始列表中的不同索引。

from bisect import bisect_right
from itertools import accumulate
def iSubAverage(M,A,P=0):
    smallSize     = 20
    smallSums     = set()
    def subSumForSize(S,A,size,failedSums=None):
        nextSum = A[size-2][2] if size>1 else 0
        index   = bisect_right([a for a,_,_ in A],S-nextSum) # max element for target
        A       = A[:index]
        if len(A) < size:    return                  # not enough elements for size
        if A[size-1][2]  > S: return                 # minimum sum > target
        maxSum = A[-1][2]
        if len(A) > size: maxSum -= A[-size-1][2]
        if maxSum < S:  return                       # maximum sum < target
        if len(A) <= smallSize and S not in smallSums: return

        if failedSums is None: failedSums = set()

        while index >= size: 
            index -= 1
            a,i,ca = A[index]
            if size == 1:
                if a == S: yield [i]
                continue
            c0 = A[index-size][2] if index>size else 0
            if ca-c0 < S: break
            subS = S-a
            if subS in failedSums: continue # known unreachable sum
            failed = True
            for result in subSumForSize(subS,A[:index],size-1,failedSums):
                yield result+[i]
                failed = False
            if failed: failedSums.add(subS)

    if not A: return
    if M < 0: M,A = -M,[-a for a in A] # must have positive target
    offset = max(0,-min(A)) # circumvent negatives (requires loop on sizes)
    A      = sorted([(round(a+offset,P),i) for i,a in enumerate(A)])
    cumA   = accumulate(a for a,i in A)
    A      = [(a,i,ca) for (a,i),ca in zip(A,cumA)]

    for a,_,_ in A[:smallSize]:
        newSums = [a+s for s in smallSums] + [a]
        smallSums.update(newSums)

    for size in range(1,len(A)+1):
        subS = round(M*size,P*2) + round(offset*size,P)
        for result in subSumForSize(subS,A,size):
            yield result

要获取实际值,iSubAvg()函数将索引映射到列表中的相应值:

def iSubAvg(M,A,P=0):
    for iA in iSubAverage(M,A,P):
        yield sorted([A[i] for i in iA])

L       = [-.32,-.64,-.12,.08,-.54,-.43]
targetL = -0.02
for solution in iSubAvg(targetL,L,2):
    print(solution)

# [-0.12, 0.08]   (there isn't a solution for -0.03)

K = [0.72, 0.69, 0.81, -0.28, 0.6, 0.59, 0.77, 0.46, 0.36, 0.66, 0.88, 0.88, 0.9, -0.24, 0.5, -0.5, 0.46, 0.96, -0.22, -0.8, -0.13, 0.87, 0.78, 0.2]    
targetK = -0.02
for solution in iSubAvg(targetK,K,2):
    print(solution)

# [-0.5, 0.46]
# [-0.5, 0.46]
# [-0.8, -0.22, 0.96]
# [-0.5, -0.28, 0.72]
# [-0.28, -0.24, 0.46]
# [-0.28, -0.24, 0.46]
# [-0.5, -0.24, 0.2, 0.46]
# [-0.5, -0.24, 0.2, 0.46]
# [-0.8, -0.28, -0.24, -0.22, 0.46, 0.96]
# [-0.8, -0.28, -0.24, -0.22, 0.46, 0.96]


next(iSubAvg(0.165,K,2))

# [-0.8, -0.28, -0.24, 0.66, 0.69, 0.96]

请注意,该函数返回所有组合,包括源列表中重复值的重复。您可以过滤掉不需要的重复项